forget_password_controller.go 8.3KB

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