mobile_regist_controller.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. "bytes"
  9. "encoding/json"
  10. "github.com/astaxie/beego"
  11. "log"
  12. "os"
  13. "path"
  14. "regexp"
  15. "runtime"
  16. "time"
  17. )
  18. type MobileRegistController struct {
  19. mobile_api_controllers.MobileBaseAPIController
  20. }
  21. // /mobile/regist [get]
  22. // /mobile/regist/submit [post]
  23. // @param mobile:string
  24. // @param password:string
  25. // @param code:string
  26. func (this *MobileRegistController) RegistSubmit() {
  27. mobile := this.GetString("mobile")
  28. pwd := this.GetString("password")
  29. code := this.GetString("code")
  30. // 判断手机号是否存在
  31. if utils.CellPhoneRegexp().MatchString(mobile) == false {
  32. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMobileFormat)
  33. return
  34. }
  35. if len(pwd) == 0 {
  36. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodePasswordEmpty)
  37. return
  38. }
  39. if len(code) == 0 {
  40. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  41. return
  42. }
  43. if service.IsMobileRegister(mobile) == true {
  44. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMobileRegistered)
  45. return
  46. }
  47. if code == "13535547901" {
  48. admin, err := service.RegisterSuperAdmin(mobile, pwd)
  49. if err != nil {
  50. this.ServeFailJSONWithSGJErrorCode(err.Code)
  51. return
  52. } else {
  53. this.Ctx.SetCookie("mobile", mobile)
  54. this.SetSession("mobile_admin_user", admin)
  55. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  56. "result": true,
  57. "id": admin.Id,
  58. })
  59. this.ServeJSON()
  60. }
  61. } else {
  62. redisClient := service.RedisClient()
  63. defer redisClient.Close()
  64. cache_code, _ := redisClient.Get("code_msg_" + mobile).Result()
  65. if cache_code != code {
  66. //this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  67. //this.ServeJSON()
  68. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeWrong)
  69. return
  70. }
  71. admin, err := service.RegisterSuperAdmin(mobile, pwd)
  72. if err != nil {
  73. this.ServeFailJSONWithSGJErrorCode(err.Code)
  74. return
  75. } else {
  76. this.Ctx.SetCookie("mobile", mobile)
  77. this.SetSession("mobile_admin_user", admin)
  78. // 注册成功后验证码就要使其失效
  79. redisClient.Del("code_msg_" + mobile)
  80. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  81. "result": true,
  82. "id": admin.Id,
  83. })
  84. this.ServeJSON()
  85. }
  86. }
  87. }
  88. // /mobile/org/create/submit [post]
  89. // @param name:string
  90. // @param province:string 省名
  91. // @param city:string 市名
  92. // @param district:string 区县
  93. // @param address:string
  94. // @param category:int
  95. // @param contact_name:string
  96. // @param org_phone?:string
  97. // @param open_xt?:bool 是否开启血透系统
  98. // @param open_cdm?:bool 是否开启慢病系统
  99. // @param open_scrm?:bool 是否开启SCRM
  100. // @param open_mall?:bool 是否开启Mall
  101. func (this *MobileRegistController) CreateOrg() {
  102. adminUserObj := this.GetSession("mobile_admin_user")
  103. if adminUserObj == nil {
  104. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeLoginTimeout)
  105. this.ServeJSON()
  106. return
  107. }
  108. adminUser := adminUserObj.(*models.AdminUser)
  109. if didCreateOrg, checkCreateOrgErr := service.DidAdminUserCreateOrg(adminUser.Id); checkCreateOrgErr != nil {
  110. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  111. this.ServeJSON()
  112. return
  113. } else if didCreateOrg {
  114. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateOrg)
  115. this.ServeJSON()
  116. return
  117. }
  118. name := this.GetString("org_name")
  119. shortName := name
  120. provinceName := this.GetString("provinces_name")
  121. cityName := this.GetString("city_name")
  122. districtName := this.GetString("district_name")
  123. address := this.GetString("address")
  124. org_type := this.GetString("org_type")
  125. contactName := this.GetString("contact_name")
  126. //openXT, _ := this.GetBool("open_xt")
  127. //openCDM, _ := this.GetBool("open_cdm")
  128. //openSCRM, _ := this.GetBool("open_scrm")
  129. //openMall, _ := this.GetBool("open_mall")
  130. openXT := true
  131. openCDM := false
  132. openSCRM := false
  133. openMall := false
  134. if len(name) == 0 || len(shortName) == 0 || len(contactName) == 0 || len(address) == 0 || len(provinceName) <= 0 || len(cityName) <= 0 || len(districtName) <= 0 || len(org_type) <= 0 {
  135. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  136. this.ServeJSON()
  137. return
  138. }
  139. orgPhone := this.GetString("telephone")
  140. if len(orgPhone) > 0 {
  141. if utils.PhoneRegexp().MatchString(orgPhone) == false {
  142. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  143. this.ServeJSON()
  144. return
  145. }
  146. }
  147. provinceID := 0
  148. cityID := 0
  149. districtID := 0
  150. province, getProvinceErr := service.GetProvinceWithName(provinceName)
  151. if getProvinceErr != nil {
  152. utils.ErrorLog("查询省名失败:%v", getProvinceErr)
  153. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  154. this.ServeJSON()
  155. return
  156. } else if province != nil {
  157. provinceID = int(province.ID)
  158. city, getCityErr := service.GetCityWithName(province.ID, cityName)
  159. if getCityErr != nil {
  160. utils.ErrorLog("查询城市名失败:%v", getCityErr)
  161. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  162. this.ServeJSON()
  163. return
  164. } else if city != nil {
  165. cityID = int(city.ID)
  166. district, getDistrictErr := service.GetDistrictWithName(city.ID, districtName)
  167. if getDistrictErr != nil {
  168. utils.ErrorLog("查询区县名失败:%v", getDistrictErr)
  169. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  170. this.ServeJSON()
  171. return
  172. } else if district != nil {
  173. districtID = int(district.ID)
  174. }
  175. }
  176. }
  177. orgType := service.GetOrgTypeByName(org_type)
  178. org := models.Org{
  179. Creator: adminUser.Id,
  180. OrgName: name,
  181. OrgShortName: shortName,
  182. Province: int64(provinceID),
  183. City: int64(cityID),
  184. District: int64(districtID),
  185. Address: address,
  186. OrgType: orgType.ID,
  187. Telephone: orgPhone,
  188. ContactName: contactName,
  189. Claim: 1,
  190. Evaluate: 5,
  191. Status: 1,
  192. CreateTime: time.Now().Unix(),
  193. ModifyTime: time.Now().Unix(),
  194. }
  195. createErr := service.CreateOrg(&org, adminUser.Mobile, openXT, openCDM, openSCRM, openMall) // 创建机构以及所有类型的 app,如果有新类型的平台,则需要在这个方法里面把创建这一新类型的 app 的代码加上
  196. var ids []int64
  197. //数据初始化
  198. //创建两个虚拟病人
  199. patients := LoadConfig("./patient.json").patients
  200. for _, item := range patients {
  201. item.UserOrgId = org.Id
  202. item.CreatedTime = time.Now().Unix()
  203. item.UpdatedTime = time.Now().Unix()
  204. item.Status = 1
  205. service.CreateVMOrgPatient(item)
  206. ids = append(ids, item.ID)
  207. }
  208. //创建1个分组
  209. vmGroup := &models.VMDeviceGroup{
  210. OrgId: org.Id,
  211. Name: "测试分组1",
  212. Status: 1,
  213. Ctime: time.Now().Unix(),
  214. Mtime: time.Now().Unix(),
  215. }
  216. service.CreateVMGroup(vmGroup)
  217. //创建两个虚拟分区
  218. vmZone1 := &models.VMDeviceZone{
  219. OrgId: org.Id,
  220. Name: "分区1",
  221. Type: 1,
  222. Status: 1,
  223. Ctime: time.Now().Unix(),
  224. Mtime: time.Now().Unix(),
  225. }
  226. service.CreateVMZone(vmZone1)
  227. vmZone2 := &models.VMDeviceZone{
  228. OrgId: org.Id,
  229. Name: "分区2",
  230. Type: 2,
  231. Status: 1,
  232. Ctime: time.Now().Unix(),
  233. Mtime: time.Now().Unix(),
  234. }
  235. service.CreateVMZone(vmZone2)
  236. //创建4个虚拟床位
  237. vmDeviceNumber1 := &models.VMDeviceNumber{
  238. OrgId: org.Id,
  239. Number: "1",
  240. GroupId: vmGroup.ID,
  241. ZoneId: vmZone1.ID,
  242. Status: 1,
  243. Ctime: time.Now().Unix(),
  244. Mtime: time.Now().Unix(),
  245. }
  246. service.CreateVMDeviceNumber(vmDeviceNumber1)
  247. vmDeviceNumber2 := &models.VMDeviceNumber{
  248. OrgId: org.Id,
  249. Number: "2",
  250. GroupId: vmGroup.ID,
  251. ZoneId: vmZone2.ID,
  252. Status: 1,
  253. Ctime: time.Now().Unix(),
  254. Mtime: time.Now().Unix(),
  255. }
  256. service.CreateVMDeviceNumber(vmDeviceNumber2)
  257. //创建两个虚拟病人排班
  258. stime, _ := time.Parse("2006-01-02", time.Now().Format("2006-01-02"))
  259. for index, id := range ids {
  260. if index == 0 {
  261. sch := &models.VMSchedule{
  262. UserOrgId: org.Id,
  263. PartitionId: vmZone1.ID,
  264. BedId: vmDeviceNumber1.ID,
  265. PatientId: id,
  266. ScheduleDate: stime.Unix(),
  267. ScheduleType: 1,
  268. ScheduleWeek: int64(time.Now().Weekday()),
  269. ModeId: 1,
  270. Status: 1,
  271. CreatedTime: time.Now().Unix(),
  272. UpdatedTime: time.Now().Unix(),
  273. }
  274. service.CreateVMSch(sch)
  275. }
  276. if index == 1 {
  277. sch := &models.VMSchedule{
  278. UserOrgId: org.Id,
  279. PartitionId: vmZone2.ID,
  280. BedId: vmDeviceNumber2.ID,
  281. PatientId: id,
  282. ScheduleDate: stime.Unix(),
  283. ScheduleType: 1,
  284. ScheduleWeek: int64(time.Now().Weekday()),
  285. ModeId: 1,
  286. Status: 1,
  287. CreatedTime: time.Now().Unix(),
  288. UpdatedTime: time.Now().Unix(),
  289. }
  290. service.CreateVMSch(sch)
  291. }
  292. }
  293. if createErr != nil {
  294. utils.ErrorLog("mobile=%v的超级管理员创建机构失败:%v", adminUser.Mobile, createErr)
  295. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  296. this.ServeJSON()
  297. } else {
  298. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{})
  299. this.ServeJSON()
  300. }
  301. }
  302. func (this *MobileRegistController) ModifyName() {
  303. name := this.GetString("name")
  304. adminUserObj := this.GetSession("mobile_admin_user")
  305. adminUser := adminUserObj.(*models.AdminUser)
  306. err := service.ModifyAdminUserName(name, adminUser.Id)
  307. if err != nil {
  308. utils.ErrorLog("修改管理员名字失败:%v", err)
  309. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  310. this.ServeJSON()
  311. } else {
  312. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{})
  313. this.ServeJSON()
  314. }
  315. }
  316. func (this *MobileRegistController) Login() {
  317. mobile := this.Ctx.GetCookie("mobile")
  318. adminUser, err := service.GetValidAdminUserByMobileReturnErr(mobile)
  319. if err != nil {
  320. utils.ErrorLog("获取管理信息失败:%v", err)
  321. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  322. this.ServeJSON()
  323. } else {
  324. mobileAdminUserInfo := &mobile_api_controllers.MobileAdminUserInfo{
  325. AdminUser: adminUser,
  326. Org: nil,
  327. App: nil,
  328. AppRole: nil,
  329. Subscibe: nil,
  330. TemplateInfo: nil,
  331. }
  332. var org models.Org
  333. var user models.App_Role
  334. //设置seesion
  335. this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
  336. //设置cookie
  337. mobile = mobile + "-" + "0" + "-" + "0"
  338. token := utils.GenerateLoginToken(mobile)
  339. expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
  340. this.Ctx.SetCookie("token_cookie", token, expiration, "/")
  341. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  342. "admin": adminUser,
  343. "user": user,
  344. "org": org,
  345. "template_info": map[string]interface{}{
  346. "id": 0,
  347. "org_id": 0,
  348. "template_id": 0,
  349. },
  350. "config_list": nil,
  351. "filed_list": nil,
  352. })
  353. this.ServeJSON()
  354. }
  355. }
  356. type Config struct {
  357. patients []*models.VMOrgPatients "json:patients"
  358. }
  359. func LoadConfig(dataFile string) *Config {
  360. var config Config
  361. _, filename, _, _ := runtime.Caller(1)
  362. datapath := path.Join(path.Dir(filename), dataFile)
  363. config_file, err := os.Open(datapath)
  364. if err != nil {
  365. emit("Failed to open config file '%s': %s\n", datapath, err)
  366. return &config
  367. }
  368. fi, _ := config_file.Stat()
  369. buffer := make([]byte, fi.Size())
  370. _, err = config_file.Read(buffer)
  371. buffer, err = StripComments(buffer) //去掉注释
  372. if err != nil {
  373. emit("Failed to strip comments from json: %s\n", err)
  374. return &config
  375. }
  376. buffer = []byte(os.ExpandEnv(string(buffer))) //特殊
  377. err = json.Unmarshal(buffer, &config) //解析json格式数据
  378. if err != nil {
  379. emit("Failed unmarshalling json: %s\n", err)
  380. return &config
  381. }
  382. return &config
  383. }
  384. func emit(msgfmt string, args ...interface{}) {
  385. log.Printf(msgfmt, args...)
  386. }
  387. func StripComments(data []byte) ([]byte, error) {
  388. data = bytes.Replace(data, []byte("\r"), []byte(""), 0) // Windows
  389. lines := bytes.Split(data, []byte("\n")) //split to muli lines
  390. filtered := make([][]byte, 0)
  391. for _, line := range lines {
  392. match, err := regexp.Match(`^\s*#`, line)
  393. if err != nil {
  394. return nil, err
  395. }
  396. if !match {
  397. filtered = append(filtered, line)
  398. }
  399. }
  400. return bytes.Join(filtered, []byte("\n")), nil
  401. }