login_api_controller.go 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package mobile_api_controllers
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/service"
  6. "XT_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. fmt.Println("mobile-------------------------------------------------------", mobile)
  134. token := utils.GenerateLoginToken(mobile)
  135. fmt.Println("token-------------------------------------------------------", token)
  136. expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
  137. this.Ctx.SetCookie("token_cookie", token, expiration, "/")
  138. fmt.Println("cookie获取--------------------------------------------------", this.Ctx.GetCookie("token_cookie"))
  139. var configList interface{}
  140. var dict_config_list interface{}
  141. var FiledList []*models.FiledConfig
  142. if org.Id > 0 {
  143. dict_config_list, _ = service.GetDictConfigList(org.Id)
  144. configList, _ = service.GetConfigList(org.Id)
  145. FiledList, _ = service.FindFiledByOrgId(org.Id)
  146. }
  147. if len(FiledList) == 0 {
  148. var err error
  149. if org.Id > 0 {
  150. err = service.BatchInsertFiledConfig(org.Id)
  151. if err == nil {
  152. FiledList, _ = service.FindFiledByOrgId(org.Id)
  153. } else {
  154. utils.ErrorLog("字段批量插入失败:%v", err)
  155. }
  156. } else {
  157. FiledList = make([]*models.FiledConfig, 0)
  158. }
  159. }
  160. //产寻该机构是否有收缩压和舒张压
  161. pressure, _ := service.GetDefaultSystolicPressure(org.Id)
  162. //fmt.Println(err)
  163. var err error
  164. if len(pressure) == 0 {
  165. err = service.BathInsertQualityControlTwo(org.Id)
  166. } else {
  167. utils.ErrorLog("字段批量插入失败:%v", err)
  168. }
  169. //批量插入质控达标统计配置
  170. major, requestErr := service.GetInspectionMajor(org.Id)
  171. if len(major) == 0 {
  172. QualityeList, err := service.FindQualityByOrgId(org.Id)
  173. if len(QualityeList) == 0 {
  174. err = service.BatchInsertQualityControl(org.Id)
  175. } else {
  176. utils.ErrorLog("字段批量插入失败:%v", err)
  177. }
  178. //批量插入检验检查统计配置
  179. InspectionList, err := service.FindeInspectionByOrgId(org.Id)
  180. if len(InspectionList) == 0 {
  181. err = service.BatchInspectionConfiguration(org.Id)
  182. } else {
  183. utils.ErrorLog("字段批量插入失败:%v", err)
  184. }
  185. } else {
  186. utils.ErrorLog("字段批量插入失败:%v", requestErr)
  187. }
  188. this.ServeSuccessJSON(map[string]interface{}{
  189. "admin": adminUser,
  190. "user": appRole,
  191. "org": org,
  192. "template_info": map[string]interface{}{
  193. "id": templateInfo.ID,
  194. "org_id": templateInfo.OrgId,
  195. "template_id": templateInfo.TemplateId,
  196. },
  197. "config_list": configList,
  198. "dict_config_list": dict_config_list,
  199. "filed_list": FiledList,
  200. })
  201. }
  202. }
  203. // /m/api/login/code [post] LoginByCode
  204. func (this *LoginAPIController) LoginByCode() {
  205. }