sso

mobile_regist_controller.go 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package controllers
  2. import (
  3. "SSO/enums"
  4. "SSO/models"
  5. "SSO/service"
  6. "SSO/utils"
  7. "encoding/json"
  8. "time"
  9. )
  10. type MobileRegistController struct {
  11. BaseController
  12. }
  13. // /mobile/regist [get]
  14. func (this *MobileRegistController) Register() {
  15. mobile := this.GetString("mobile")
  16. redisClient := service.RedisClient()
  17. defer redisClient.Close()
  18. req := this.Ctx.Request
  19. addr := utils.GetIP(req)
  20. cur_time := time.Now().Format("2006-01-02")
  21. _, err := redisClient.Get("ip:host_" + cur_time + "_" + addr).Result()
  22. if err != nil {
  23. redisClient.Set("ip:host_"+cur_time+"_"+addr, 0, time.Second*24*60*60)
  24. }
  25. //将客户端的ip加密传给前端,作为短信验证的密钥,来验证短信发送的IP地址
  26. aespass := utils.AESEncrypt(addr)
  27. this.Data["aespass"] = aespass
  28. this.Data["mobile"] = mobile
  29. this.SetTpl("mobile_site/regist.html")
  30. }
  31. // /mobile/regist/submit [post]
  32. // @param mobile:string
  33. // @param password:string
  34. // @param code:string
  35. func (this *MobileRegistController) RegistSubmit() {
  36. mobile := this.GetString("mobile")
  37. pwd := this.GetString("password")
  38. code := this.GetString("code")
  39. // 判断手机号是否存在
  40. if utils.CellPhoneRegexp().MatchString(mobile) == false {
  41. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeMobileFormat)
  42. this.ServeJSON()
  43. return
  44. }
  45. if len(pwd) == 0 {
  46. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodePasswordEmpty)
  47. this.ServeJSON()
  48. return
  49. }
  50. if len(code) == 0 {
  51. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  52. this.ServeJSON()
  53. return
  54. }
  55. if service.IsMobileRegister(mobile) == true {
  56. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeMobileRegistered)
  57. this.ServeJSON()
  58. return
  59. }
  60. redisClient := service.RedisClient()
  61. defer redisClient.Close()
  62. // if strings.HasPrefix(mobile, "12") {
  63. // if code != "123456" {
  64. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  65. // this.ServeJSON()
  66. // return
  67. // }
  68. // } else {
  69. cache_code, _ := redisClient.Get("code_msg_" + mobile).Result()
  70. if cache_code != code {
  71. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  72. this.ServeJSON()
  73. return
  74. }
  75. // }
  76. admin, err := service.RegisterSuperAdmin(mobile, pwd)
  77. if err != nil {
  78. this.Data["json"] = enums.MakeFailResponseJSONWithSGJError(err)
  79. this.ServeJSON()
  80. } else {
  81. this.Ctx.SetCookie("mobile", mobile)
  82. this.SetSession("mobile_admin_user", admin)
  83. // 注册成功后验证码就要使其失效
  84. redisClient.Del("code_msg_" + mobile)
  85. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  86. "result": true,
  87. "id": admin.Id,
  88. })
  89. this.ServeJSON()
  90. }
  91. }
  92. // /mobile/org/create [get]
  93. func (this *MobileRegistController) CreateOrg() {
  94. adminUserObj := this.GetSession("mobile_admin_user")
  95. if adminUserObj == nil {
  96. this.Redirect302("/mobile/regist")
  97. return
  98. }
  99. adminUser := adminUserObj.(*models.AdminUser)
  100. if didCreateOrg, checkCreateOrgErr := service.DidAdminUserCreateOrg(adminUser.Id); checkCreateOrgErr != nil {
  101. utils.ErrorLog("检查id = %v的用户是否创建了机构时出错:%v", adminUser.Id, checkCreateOrgErr)
  102. this.Abort("404")
  103. return
  104. } else {
  105. if didCreateOrg {
  106. this.Redirect302("/mobile/finish")
  107. return
  108. }
  109. }
  110. cats, getCatErr := service.GetOrgCategoriesByPid(0)
  111. if getCatErr != nil {
  112. utils.ErrorLog("获取机构类型失败:%v", getCatErr)
  113. this.Abort("404")
  114. return
  115. }
  116. catsJSON, _ := json.Marshal(cats)
  117. this.Data["categories"] = string(catsJSON)
  118. this.SetTpl("mobile_site/create_org.html")
  119. }
  120. // /mobile/org/create/submit [post]
  121. // @param name:string
  122. // @param province:string 省名
  123. // @param city:string 市名
  124. // @param district:string 区县
  125. // @param address:string
  126. // @param category:int
  127. // @param contact_name:string
  128. // @param org_phone?:string
  129. // @param open_xt?:bool 是否开启血透系统
  130. // @param open_cdm?:bool 是否开启慢病系统
  131. // @param open_scrm?:bool 是否开启SCRM
  132. // @param open_mall?:bool 是否开启Mall
  133. func (this *MobileRegistController) CreateOrgSubmit() {
  134. adminUserObj := this.GetSession("mobile_admin_user")
  135. if adminUserObj == nil {
  136. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeLoginTimeout)
  137. this.ServeJSON()
  138. return
  139. }
  140. adminUser := adminUserObj.(*models.AdminUser)
  141. if didCreateOrg, checkCreateOrgErr := service.DidAdminUserCreateOrg(adminUser.Id); checkCreateOrgErr != nil {
  142. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  143. this.ServeJSON()
  144. return
  145. } else if didCreateOrg {
  146. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateOrg)
  147. this.ServeJSON()
  148. return
  149. }
  150. name := this.GetString("name")
  151. shortName := name
  152. provinceName := this.GetString("province")
  153. cityName := this.GetString("city")
  154. districtName := this.GetString("district")
  155. address := this.GetString("address")
  156. category, _ := this.GetInt64("category")
  157. contactName := this.GetString("contact_name")
  158. openXT, _ := this.GetBool("open_xt")
  159. openCDM, _ := this.GetBool("open_cdm")
  160. openSCRM, _ := this.GetBool("open_scrm")
  161. openMall, _ := this.GetBool("open_mall")
  162. if len(name) == 0 || len(shortName) == 0 || len(contactName) == 0 || len(address) == 0 || len(provinceName) <= 0 || len(cityName) <= 0 || len(districtName) <= 0 || category <= 0 || (!openXT && !openCDM && !openSCRM && !openMall) {
  163. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  164. this.ServeJSON()
  165. return
  166. }
  167. orgPhone := this.GetString("org_phone")
  168. if len(orgPhone) > 0 {
  169. if utils.PhoneRegexp().MatchString(orgPhone) == false {
  170. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  171. this.ServeJSON()
  172. return
  173. }
  174. }
  175. provinceID := 0
  176. cityID := 0
  177. districtID := 0
  178. province, getProvinceErr := service.GetProvinceWithName(provinceName)
  179. if getProvinceErr != nil {
  180. utils.ErrorLog("查询省名失败:%v", getProvinceErr)
  181. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  182. this.ServeJSON()
  183. return
  184. } else if province != nil {
  185. provinceID = int(province.Id)
  186. city, getCityErr := service.GetCityWithName(province.Id, cityName)
  187. if getCityErr != nil {
  188. utils.ErrorLog("查询城市名失败:%v", getCityErr)
  189. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  190. this.ServeJSON()
  191. return
  192. } else if city != nil {
  193. cityID = int(city.Id)
  194. district, getDistrictErr := service.GetDistrictWithName(city.Id, districtName)
  195. if getDistrictErr != nil {
  196. utils.ErrorLog("查询区县名失败:%v", getDistrictErr)
  197. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  198. this.ServeJSON()
  199. return
  200. } else if district != nil {
  201. districtID = int(district.Id)
  202. }
  203. }
  204. }
  205. org := models.Org{
  206. Creator: adminUser.Id,
  207. OrgName: name,
  208. OrgShortName: shortName,
  209. Province: provinceID,
  210. City: cityID,
  211. District: districtID,
  212. Address: address,
  213. OrgType: category,
  214. Telephone: orgPhone,
  215. ContactName: contactName,
  216. Claim: 1,
  217. Evaluate: 5,
  218. Status: 1,
  219. CreateTime: time.Now().Unix(),
  220. ModifyTime: time.Now().Unix(),
  221. }
  222. createErr := service.CreateOrg(&org, adminUser.Mobile, openXT, openCDM, openSCRM, openMall) // 创建机构以及所有类型的 app,如果有新类型的平台,则需要在这个方法里面把创建这一新类型的 app 的代码加上
  223. if createErr != nil {
  224. utils.ErrorLog("mobile=%v的超级管理员创建机构失败:%v", adminUser.Mobile, createErr)
  225. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  226. this.ServeJSON()
  227. } else {
  228. this.Ctx.SetCookie("org_name", name)
  229. this.SetSession("mobile_org", &org)
  230. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{})
  231. this.ServeJSON()
  232. }
  233. }
  234. // /mobile/finish [get]
  235. func (this *MobileRegistController) Finish() {
  236. adminUserObj := this.GetSession("mobile_admin_user")
  237. orgObj := this.GetSession("mobile_org")
  238. if adminUserObj == nil || orgObj == nil {
  239. this.Redirect302("/")
  240. return
  241. }
  242. adminUser := adminUserObj.(*models.AdminUser)
  243. org := orgObj.(*models.Org)
  244. this.Data["mobile"] = adminUser.Mobile
  245. this.Data["org_name"] = org.OrgName
  246. this.SetTpl("mobile_site/close.html")
  247. }