sso

mobile_regist_controller.go 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. package controllers
  2. import (
  3. "SSO/enums"
  4. "SSO/models"
  5. "SSO/service"
  6. "SSO/utils"
  7. "encoding/json"
  8. "strings"
  9. "time"
  10. )
  11. type MobileRegistController struct {
  12. BaseController
  13. }
  14. // /mobile/regist [get]
  15. func (this *MobileRegistController) Register() {
  16. mobile := this.GetString("mobile")
  17. redisClient := service.RedisClient()
  18. defer redisClient.Close()
  19. req := this.Ctx.Request
  20. addr := utils.GetIP(req)
  21. cur_time := time.Now().Format("2006-01-02")
  22. _, err := redisClient.Get("ip:host_" + cur_time + "_" + addr).Result()
  23. if err != nil {
  24. redisClient.Set("ip:host_"+cur_time+"_"+addr, 0, time.Second*24*60*60)
  25. }
  26. //将客户端的ip加密传给前端,作为短信验证的密钥,来验证短信发送的IP地址
  27. aespass := utils.AESEncrypt(addr)
  28. this.Data["aespass"] = aespass
  29. this.Data["mobile"] = mobile
  30. this.SetTpl("mobile_site/regist.html")
  31. }
  32. // /mobile/regist/submit [post]
  33. // @param mobile:string
  34. // @param password:string
  35. // @param code:string
  36. func (this *MobileRegistController) RegistSubmit() {
  37. mobile := this.GetString("mobile")
  38. pwd := this.GetString("password")
  39. code := this.GetString("code")
  40. // 判断手机号是否存在
  41. if utils.CellPhoneRegexp().MatchString(mobile) == false {
  42. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeMobileFormat)
  43. this.ServeJSON()
  44. return
  45. }
  46. if len(pwd) == 0 {
  47. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodePasswordEmpty)
  48. this.ServeJSON()
  49. return
  50. }
  51. if len(code) == 0 {
  52. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  53. this.ServeJSON()
  54. return
  55. }
  56. if service.IsMobileRegister(mobile) == true {
  57. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeMobileRegistered)
  58. this.ServeJSON()
  59. return
  60. }
  61. redisClient := service.RedisClient()
  62. defer redisClient.Close()
  63. if strings.HasPrefix(mobile, "12") {
  64. if code != "123456" {
  65. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  66. this.ServeJSON()
  67. return
  68. }
  69. } else {
  70. cache_code, _ := redisClient.Get("code_msg_" + mobile).Result()
  71. if cache_code != code {
  72. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  73. this.ServeJSON()
  74. return
  75. }
  76. }
  77. admin, err := service.RegisterSuperAdmin(mobile, pwd)
  78. if err != nil {
  79. this.Data["json"] = enums.MakeFailResponseJSONWithSGJError(err)
  80. this.ServeJSON()
  81. } else {
  82. this.Ctx.SetCookie("mobile", mobile)
  83. this.SetSession("mobile_admin_user", admin)
  84. // 注册成功后验证码就要使其失效
  85. redisClient.Del("code_msg_" + mobile)
  86. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  87. "result": true,
  88. "id": admin.Id,
  89. })
  90. this.ServeJSON()
  91. }
  92. }
  93. // /mobile/org/create [get]
  94. func (this *MobileRegistController) CreateOrg() {
  95. adminUserObj := this.GetSession("mobile_admin_user")
  96. if adminUserObj == nil {
  97. this.Redirect302("/mobile/regist")
  98. return
  99. }
  100. adminUser := adminUserObj.(*models.AdminUser)
  101. if didCreateOrg, checkCreateOrgErr := service.DidAdminUserCreateOrg(adminUser.Id); checkCreateOrgErr != nil {
  102. utils.ErrorLog("检查id = %v的用户是否创建了机构时出错:%v", adminUser.Id, checkCreateOrgErr)
  103. this.Abort("404")
  104. return
  105. } else {
  106. if didCreateOrg {
  107. this.Redirect302("/mobile/finish")
  108. return
  109. }
  110. }
  111. cats, getCatErr := service.GetOrgCategoriesByPid(0)
  112. if getCatErr != nil {
  113. utils.ErrorLog("获取机构类型失败:%v", getCatErr)
  114. this.Abort("404")
  115. return
  116. }
  117. catsJSON, _ := json.Marshal(cats)
  118. this.Data["categories"] = string(catsJSON)
  119. this.SetTpl("mobile_site/create_org.html")
  120. }
  121. // /mobile/org/create/submit [post]
  122. // @param name:string
  123. // @param province:string 省名
  124. // @param city:string 市名
  125. // @param district:string 区县
  126. // @param address:string
  127. // @param category:int
  128. // @param contact_name:string
  129. // @param org_phone?:string
  130. // @param open_xt?:bool 是否开启血透系统
  131. // @param open_cdm?:bool 是否开启慢病系统
  132. // @param open_scrm?:bool 是否开启SCRM
  133. // @param open_mall?:bool 是否开启Mall
  134. func (this *MobileRegistController) CreateOrgSubmit() {
  135. adminUserObj := this.GetSession("mobile_admin_user")
  136. if adminUserObj == nil {
  137. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeLoginTimeout)
  138. this.ServeJSON()
  139. return
  140. }
  141. adminUser := adminUserObj.(*models.AdminUser)
  142. if didCreateOrg, checkCreateOrgErr := service.DidAdminUserCreateOrg(adminUser.Id); checkCreateOrgErr != nil {
  143. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  144. this.ServeJSON()
  145. return
  146. } else if didCreateOrg {
  147. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateOrg)
  148. this.ServeJSON()
  149. return
  150. }
  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.PhoneRegexp().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: 2,
  220. CreateTime: time.Now().Unix(),
  221. ModifyTime: time.Now().Unix(),
  222. }
  223. createErr := service.CreateOrg(&org, adminUser.Mobile, 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. }