login_api_controller.go 6.5KB

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