login_api_controller.go 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package mobile_api_controllers
  2. import (
  3. "Xcx_New/enums"
  4. "Xcx_New/models"
  5. "Xcx_New/service"
  6. "Xcx_New/utils"
  7. "encoding/json"
  8. "fmt"
  9. "io/ioutil"
  10. "net/http"
  11. "net/url"
  12. "strconv"
  13. "github.com/astaxie/beego"
  14. )
  15. type LoginAPIController struct {
  16. MobileBaseAPIController
  17. }
  18. // /m/api/login/pwd [post] LoginByPwd
  19. // @param mobile:string
  20. // @param password:string
  21. func (this *LoginAPIController) LoginByPwd() {
  22. mobile := this.GetString("mobile")
  23. pwd := this.GetString("password")
  24. //var mobile = "15089497668"
  25. //var pwd = "123456"
  26. if len(mobile) == 0 || len(pwd) == 0 || utils.CellPhoneRegexp().MatchString(mobile) == false {
  27. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  28. return
  29. }
  30. ip := utils.GetIP(this.Ctx.Request)
  31. ssoDomain := beego.AppConfig.String("sso_domain")
  32. //ssoDomain := "http://localhost:8091"
  33. api := ssoDomain + "/m/login/pwd"
  34. values := make(url.Values)
  35. values.Set("mobile", mobile)
  36. values.Set("password", pwd)
  37. values.Set("app_type", "3")
  38. values.Set("ip", ip)
  39. resp, requestErr := http.PostForm(api, values)
  40. if requestErr != nil {
  41. utils.ErrorLog("请求SSO登录接口失败: %v", requestErr)
  42. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  43. return
  44. }
  45. defer resp.Body.Close()
  46. body, ioErr := ioutil.ReadAll(resp.Body)
  47. if ioErr != nil {
  48. utils.ErrorLog("SSO登录接口返回数据读取失败: %v", ioErr)
  49. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  50. return
  51. }
  52. var respJSON map[string]interface{}
  53. utils.InfoLog(string(body))
  54. if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
  55. utils.ErrorLog("SSO登录接口返回数据解析JSON失败: %v", err)
  56. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  57. return
  58. }
  59. if respJSON["state"].(float64) != 1 {
  60. msg := respJSON["msg"].(string)
  61. utils.ErrorLog("SSO登录接口请求失败: %v", msg)
  62. if int(respJSON["code"].(float64)) == 609 {
  63. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  64. return
  65. }
  66. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  67. return
  68. } else {
  69. utils.SuccessLog("SSO登录成功")
  70. // 下面这几段 Map=>JSON=>Struct 的流程可能会造成速度很慢
  71. userJSON := respJSON["data"].(map[string]interface{})["admin"].(map[string]interface{})
  72. userJSONBytes, _ := json.Marshal(userJSON)
  73. var adminUser models.AdminUser
  74. if err := json.Unmarshal(userJSONBytes, &adminUser); err != nil {
  75. utils.ErrorLog("解析管理员失败:%v", err)
  76. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  77. return
  78. }
  79. var org models.Org
  80. if respJSON["data"].(map[string]interface{})["org"] != nil {
  81. orgJSON := respJSON["data"].(map[string]interface{})["org"].(map[string]interface{})
  82. orgJSONBytes, _ := json.Marshal(orgJSON)
  83. if err := json.Unmarshal(orgJSONBytes, &org); err != nil {
  84. utils.ErrorLog("解析机构失败:%v", err)
  85. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  86. return
  87. }
  88. }
  89. var app models.OrgApp
  90. if respJSON["data"].(map[string]interface{})["app"] != nil {
  91. appJSON := respJSON["data"].(map[string]interface{})["app"].(map[string]interface{})
  92. appJSONBytes, _ := json.Marshal(appJSON)
  93. if err := json.Unmarshal(appJSONBytes, &app); err != nil {
  94. utils.ErrorLog("解析应用失败:%v", err)
  95. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  96. return
  97. }
  98. }
  99. var appRole models.App_Role
  100. if respJSON["data"].(map[string]interface{})["app_role"] != nil {
  101. appRoleJSON := respJSON["data"].(map[string]interface{})["app_role"].(map[string]interface{})
  102. appRoleJSONBytes, _ := json.Marshal(appRoleJSON)
  103. if err := json.Unmarshal(appRoleJSONBytes, &appRole); err != nil {
  104. utils.ErrorLog("解析AppRole失败:%v", err)
  105. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  106. return
  107. }
  108. }
  109. var subscibe models.ServeSubscibe
  110. if respJSON["data"].(map[string]interface{})["subscibe"] != nil {
  111. subscibeJSON := respJSON["data"].(map[string]interface{})["subscibe"].(map[string]interface{})
  112. subscibeJSONBytes, _ := json.Marshal(subscibeJSON)
  113. if err := json.Unmarshal(subscibeJSONBytes, &subscibe); err != nil {
  114. utils.ErrorLog("解析Subscibe失败:%v", err)
  115. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  116. return
  117. }
  118. }
  119. //service.GetOrgSubscibeState(&subscibe)
  120. templateInfo, _ := service.GetOrgInfoTemplate(org.Id)
  121. mobileAdminUserInfo := &MobileAdminUserInfo{
  122. AdminUser: &adminUser,
  123. Org: &org,
  124. App: &app,
  125. AppRole: &appRole,
  126. Subscibe: &subscibe,
  127. TemplateInfo: &templateInfo,
  128. }
  129. //设置seesion
  130. this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
  131. //设置cookie
  132. mobile = mobile + "-" + strconv.FormatInt(org.Id, 10) + "-" + strconv.FormatInt(appRole.Id, 10)
  133. token := utils.GenerateLoginToken(mobile)
  134. expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
  135. this.Ctx.SetCookie("token_cookie", token, expiration, "/")
  136. var configList interface{}
  137. var dict_config_list interface{}
  138. var FiledList []*models.FiledConfig
  139. if org.Id > 0 {
  140. dict_config_list, _ = service.GetDictConfigList(org.Id)
  141. configList, _ = service.GetConfigList(org.Id)
  142. FiledList, _ = service.FindFiledByOrgId(org.Id)
  143. }
  144. if len(FiledList) == 0 {
  145. var err error
  146. if org.Id > 0 {
  147. err = service.BatchInsertFiledConfig(org.Id)
  148. if err == nil {
  149. FiledList, _ = service.FindFiledByOrgId(org.Id)
  150. } else {
  151. utils.ErrorLog("字段批量插入失败:%v", err)
  152. }
  153. } else {
  154. FiledList = make([]*models.FiledConfig, 0)
  155. }
  156. }
  157. //产寻该机构是否有收缩压和舒张压
  158. pressure, err := service.GetDefaultSystolicPressure(org.Id)
  159. fmt.Println(err)
  160. if len(pressure) == 0 {
  161. err = service.BathInsertQualityControlTwo(org.Id)
  162. } else {
  163. utils.ErrorLog("字段批量插入失败:%v", err)
  164. }
  165. //批量插入质控达标统计配置
  166. major, requestErr := service.GetInspectionMajor(org.Id)
  167. if len(major) == 0 {
  168. QualityeList, err := service.FindQualityByOrgId(org.Id)
  169. if len(QualityeList) == 0 {
  170. err = service.BatchInsertQualityControl(org.Id)
  171. } else {
  172. utils.ErrorLog("字段批量插入失败:%v", err)
  173. }
  174. //批量插入检验检查统计配置
  175. InspectionList, err := service.FindeInspectionByOrgId(org.Id)
  176. if len(InspectionList) == 0 {
  177. err = service.BatchInspectionConfiguration(org.Id)
  178. } else {
  179. utils.ErrorLog("字段批量插入失败:%v", err)
  180. }
  181. } else {
  182. utils.ErrorLog("字段批量插入失败:%v", requestErr)
  183. }
  184. this.ServeSuccessJSON(map[string]interface{}{
  185. "admin": adminUser,
  186. "user": appRole,
  187. "org": org,
  188. "template_info": map[string]interface{}{
  189. "id": templateInfo.ID,
  190. "org_id": templateInfo.OrgId,
  191. "template_id": templateInfo.TemplateId,
  192. },
  193. "config_list": configList,
  194. "dict_config_list": dict_config_list,
  195. "filed_list": FiledList,
  196. })
  197. }
  198. }
  199. // /m/api/login/code [post] LoginByCode
  200. func (this *LoginAPIController) LoginByCode() {
  201. }