login_api_controller.go 6.5KB

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