login_api_controller.go 5.8KB

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