sso

mobile_regist_controller.go 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. user_name := this.GetString("user_name")
  151. name := this.GetString("name")
  152. shortName := name
  153. provinceName := this.GetString("province")
  154. cityName := this.GetString("city")
  155. districtName := this.GetString("district")
  156. address := this.GetString("address")
  157. category, _ := this.GetInt64("category")
  158. contactName := this.GetString("contact_name")
  159. openXT, _ := this.GetBool("open_xt")
  160. openCDM, _ := this.GetBool("open_cdm")
  161. openSCRM, _ := this.GetBool("open_scrm")
  162. openMall, _ := this.GetBool("open_mall")
  163. 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) {
  164. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  165. this.ServeJSON()
  166. return
  167. }
  168. orgPhone := this.GetString("org_phone")
  169. if len(orgPhone) > 0 {
  170. if utils.CellPhoneRegexp().MatchString(orgPhone) == false {
  171. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  172. this.ServeJSON()
  173. return
  174. }
  175. }
  176. provinceID := 0
  177. cityID := 0
  178. districtID := 0
  179. province, getProvinceErr := service.GetProvinceWithName(provinceName)
  180. if getProvinceErr != nil {
  181. utils.ErrorLog("查询省名失败:%v", getProvinceErr)
  182. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  183. this.ServeJSON()
  184. return
  185. } else if province != nil {
  186. provinceID = int(province.Id)
  187. city, getCityErr := service.GetCityWithName(province.Id, cityName)
  188. if getCityErr != nil {
  189. utils.ErrorLog("查询城市名失败:%v", getCityErr)
  190. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  191. this.ServeJSON()
  192. return
  193. } else if city != nil {
  194. cityID = int(city.Id)
  195. district, getDistrictErr := service.GetDistrictWithName(city.Id, districtName)
  196. if getDistrictErr != nil {
  197. utils.ErrorLog("查询区县名失败:%v", getDistrictErr)
  198. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  199. this.ServeJSON()
  200. return
  201. } else if district != nil {
  202. districtID = int(district.Id)
  203. }
  204. }
  205. }
  206. org := models.Org{
  207. Creator: adminUser.Id,
  208. OrgName: name,
  209. OrgShortName: shortName,
  210. Province: provinceID,
  211. City: cityID,
  212. District: districtID,
  213. Address: address,
  214. OrgType: category,
  215. Telephone: orgPhone,
  216. ContactName: contactName,
  217. Claim: 1,
  218. Evaluate: 5,
  219. Status: 1,
  220. CreateTime: time.Now().Unix(),
  221. ModifyTime: time.Now().Unix(),
  222. }
  223. createErr := service.CreateOrg(&org, user_name, openXT, openCDM, openSCRM, openMall) // 创建机构以及所有类型的 app,如果有新类型的平台,则需要在这个方法里面把创建这一新类型的 app 的代码加上
  224. if createErr != nil {
  225. utils.ErrorLog("mobile=%v的超级管理员创建机构失败:%v", adminUser.Mobile, createErr)
  226. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  227. this.ServeJSON()
  228. } else {
  229. this.Ctx.SetCookie("org_name", name)
  230. this.SetSession("mobile_org", &org)
  231. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{})
  232. this.ServeJSON()
  233. }
  234. }
  235. // /mobile/finish [get]
  236. func (this *MobileRegistController) Finish() {
  237. adminUserObj := this.GetSession("mobile_admin_user")
  238. orgObj := this.GetSession("mobile_org")
  239. if adminUserObj == nil || orgObj == nil {
  240. this.Redirect302("/")
  241. return
  242. }
  243. adminUser := adminUserObj.(*models.AdminUser)
  244. org := orgObj.(*models.Org)
  245. this.Data["mobile"] = adminUser.Mobile
  246. this.Data["org_name"] = org.OrgName
  247. this.SetTpl("mobile_site/close.html")
  248. }