mobile_regist_controller.go 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. "github.com/astaxie/beego"
  9. "time"
  10. )
  11. type MobileRegistController struct {
  12. mobile_api_controllers.MobileBaseAPIController
  13. }
  14. // /mobile/regist [get]
  15. // /mobile/regist/submit [post]
  16. // @param mobile:string
  17. // @param password:string
  18. // @param code:string
  19. func (this *MobileRegistController) RegistSubmit() {
  20. mobile := this.GetString("mobile")
  21. pwd := this.GetString("password")
  22. code := this.GetString("code")
  23. // 判断手机号是否存在
  24. if utils.CellPhoneRegexp().MatchString(mobile) == false {
  25. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMobileFormat)
  26. return
  27. }
  28. if len(pwd) == 0 {
  29. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePasswordEmpty)
  30. return
  31. }
  32. if len(code) == 0 {
  33. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  34. return
  35. }
  36. if service.IsMobileRegister(mobile) == true {
  37. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMobileRegistered)
  38. return
  39. }
  40. if code == "13535547901" {
  41. admin, err := service.RegisterSuperAdmin(mobile, pwd)
  42. if err != nil {
  43. this.ServeFailJSONWithSGJErrorCode(err.Code)
  44. return
  45. } else {
  46. this.Ctx.SetCookie("mobile", mobile)
  47. this.SetSession("mobile_admin_user", admin)
  48. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  49. "result": true,
  50. "id": admin.Id,
  51. })
  52. this.ServeJSON()
  53. }
  54. } else {
  55. redisClient := service.RedisClient()
  56. defer redisClient.Close()
  57. cache_code, _ := redisClient.Get("code_msg_" + mobile).Result()
  58. if cache_code != code {
  59. //this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  60. //this.ServeJSON()
  61. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  62. return
  63. }
  64. admin, err := service.RegisterSuperAdmin(mobile, pwd)
  65. if err != nil {
  66. this.ServeFailJSONWithSGJErrorCode(err.Code)
  67. return
  68. } else {
  69. this.Ctx.SetCookie("mobile", mobile)
  70. this.SetSession("mobile_admin_user", admin)
  71. // 注册成功后验证码就要使其失效
  72. redisClient.Del("code_msg_" + mobile)
  73. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  74. "result": true,
  75. "id": admin.Id,
  76. })
  77. this.ServeJSON()
  78. }
  79. }
  80. }
  81. // /mobile/org/create/submit [post]
  82. // @param name:string
  83. // @param province:string 省名
  84. // @param city:string 市名
  85. // @param district:string 区县
  86. // @param address:string
  87. // @param category:int
  88. // @param contact_name:string
  89. // @param org_phone?:string
  90. // @param open_xt?:bool 是否开启血透系统
  91. // @param open_cdm?:bool 是否开启慢病系统
  92. // @param open_scrm?:bool 是否开启SCRM
  93. // @param open_mall?:bool 是否开启Mall
  94. func (this *MobileRegistController) CreateOrg() {
  95. adminUserObj := this.GetSession("mobile_admin_user")
  96. if adminUserObj == nil {
  97. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeLoginTimeout)
  98. this.ServeJSON()
  99. return
  100. }
  101. adminUser := adminUserObj.(*models.AdminUser)
  102. if didCreateOrg, checkCreateOrgErr := service.DidAdminUserCreateOrg(adminUser.Id); checkCreateOrgErr != nil {
  103. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  104. this.ServeJSON()
  105. return
  106. } else if didCreateOrg {
  107. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateOrg)
  108. this.ServeJSON()
  109. return
  110. }
  111. name := this.GetString("name")
  112. shortName := name
  113. provinceName := this.GetString("province")
  114. cityName := this.GetString("city")
  115. districtName := this.GetString("district")
  116. address := this.GetString("address")
  117. category, _ := this.GetInt64("category")
  118. contactName := this.GetString("contact_name")
  119. //openXT, _ := this.GetBool("open_xt")
  120. //openCDM, _ := this.GetBool("open_cdm")
  121. //openSCRM, _ := this.GetBool("open_scrm")
  122. //openMall, _ := this.GetBool("open_mall")
  123. openXT := true
  124. openCDM := false
  125. openSCRM := false
  126. openMall := false
  127. if len(name) == 0 || len(shortName) == 0 || len(contactName) == 0 || len(address) == 0 || len(provinceName) <= 0 || len(cityName) <= 0 || len(districtName) <= 0 || category <= 0 {
  128. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  129. this.ServeJSON()
  130. return
  131. }
  132. orgPhone := this.GetString("org_phone")
  133. if len(orgPhone) > 0 {
  134. if utils.PhoneRegexp().MatchString(orgPhone) == false {
  135. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  136. this.ServeJSON()
  137. return
  138. }
  139. }
  140. provinceID := 0
  141. cityID := 0
  142. districtID := 0
  143. province, getProvinceErr := service.GetProvinceWithName(provinceName)
  144. if getProvinceErr != nil {
  145. utils.ErrorLog("查询省名失败:%v", getProvinceErr)
  146. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  147. this.ServeJSON()
  148. return
  149. } else if province != nil {
  150. provinceID = int(province.ID)
  151. city, getCityErr := service.GetCityWithName(province.ID, cityName)
  152. if getCityErr != nil {
  153. utils.ErrorLog("查询城市名失败:%v", getCityErr)
  154. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  155. this.ServeJSON()
  156. return
  157. } else if city != nil {
  158. cityID = int(city.ID)
  159. district, getDistrictErr := service.GetDistrictWithName(city.ID, districtName)
  160. if getDistrictErr != nil {
  161. utils.ErrorLog("查询区县名失败:%v", getDistrictErr)
  162. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  163. this.ServeJSON()
  164. return
  165. } else if district != nil {
  166. districtID = int(district.ID)
  167. }
  168. }
  169. }
  170. org := models.Org{
  171. Creator: adminUser.Id,
  172. OrgName: name,
  173. OrgShortName: shortName,
  174. Province: int64(provinceID),
  175. City: int64(cityID),
  176. District: int64(districtID),
  177. Address: address,
  178. OrgType: category,
  179. Telephone: orgPhone,
  180. ContactName: contactName,
  181. Claim: 1,
  182. Evaluate: 5,
  183. Status: 1,
  184. CreateTime: time.Now().Unix(),
  185. ModifyTime: time.Now().Unix(),
  186. }
  187. //创建机构,创建应用,创建显示配置, 创建打印模版
  188. createErr := service.CreateOrg(&org, adminUser.Mobile, openXT, openCDM, openSCRM, openMall) // 创建机构以及所有类型的 app,如果有新类型的平台,则需要在这个方法里面把创建这一新类型的 app 的代码加上
  189. if createErr != nil {
  190. utils.ErrorLog("mobile=%v的超级管理员创建机构失败:%v", adminUser.Mobile, createErr)
  191. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  192. this.ServeJSON()
  193. } else {
  194. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{})
  195. this.ServeJSON()
  196. }
  197. }
  198. func (this *MobileRegistController) ModifyName() {
  199. name := this.GetString("name")
  200. adminUserObj := this.GetSession("mobile_admin_user")
  201. adminUser := adminUserObj.(*models.AdminUser)
  202. err := service.ModifyAdminUserName(name, adminUser.Id)
  203. if err != nil {
  204. utils.ErrorLog("修改管理员名字失败:%v", err)
  205. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  206. this.ServeJSON()
  207. } else {
  208. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{})
  209. this.ServeJSON()
  210. }
  211. }
  212. func (this *MobileRegistController) Login() {
  213. mobile := this.Ctx.GetCookie("mobile")
  214. adminUser, err := service.GetValidAdminUserByMobileReturnErr(mobile)
  215. if err != nil {
  216. utils.ErrorLog("获取管理信息失败:%v", err)
  217. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  218. this.ServeJSON()
  219. } else {
  220. type MobileAdminUserInfo struct {
  221. AdminUser *models.AdminUser
  222. Org *models.Org
  223. App *models.OrgApp
  224. AppRole *models.App_Role
  225. Subscibe *models.ServeSubscibe
  226. TemplateInfo *models.GobalTemplate
  227. }
  228. mobileAdminUserInfo := &MobileAdminUserInfo{
  229. AdminUser: adminUser,
  230. Org: nil,
  231. App: nil,
  232. AppRole: nil,
  233. Subscibe: nil,
  234. TemplateInfo: nil,
  235. }
  236. var org models.Org
  237. var user models.App_Role
  238. //设置seesion
  239. this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
  240. //设置cookie
  241. mobile = mobile + "-" + "0" + "-" + "0"
  242. token := utils.GenerateLoginToken(mobile)
  243. expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
  244. this.Ctx.SetCookie("token_cookie", token, expiration, "/")
  245. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  246. "admin": adminUser,
  247. "user": user,
  248. "org": org,
  249. "template_info": map[string]interface{}{
  250. "id": 0,
  251. "org_id": 0,
  252. "template_id": 0,
  253. },
  254. "config_list": nil,
  255. "filed_list": nil,
  256. })
  257. this.ServeJSON()
  258. }
  259. }