forget_password_controller.go 7.4KB

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