home_api_controller.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. "encoding/json"
  9. "github.com/astaxie/beego"
  10. "io/ioutil"
  11. "net/http"
  12. "net/url"
  13. "strconv"
  14. "time"
  15. )
  16. type HomeController struct {
  17. NewMobileBaseAPIAuthController
  18. }
  19. func (this *HomeController) GetHomeData() {
  20. adminUserInfo := this.GetMobileAdminUserInfo()
  21. if adminUserInfo.Org != nil && adminUserInfo.Org.Id != 0 {
  22. //获取该管理员所有机构列表
  23. var orgs []*models.Org
  24. adminUser, err := service.GetHomeData(adminUserInfo.AdminUser.Id)
  25. if err != nil {
  26. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  27. return
  28. }
  29. for _, item := range adminUser.Org {
  30. orgs = append(orgs, item)
  31. }
  32. for _, item := range adminUser.VMApp_Role {
  33. for _, subItem := range item.Org {
  34. orgs = append(orgs, subItem)
  35. }
  36. }
  37. orgs = RemoveRepeatedOrgElement(orgs)
  38. apps, err := service.GetAllApp(adminUserInfo.Org.Id)
  39. if err != nil {
  40. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  41. return
  42. }
  43. banners, err := service.GetSystemBanner()
  44. if err != nil {
  45. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  46. return
  47. }
  48. this.ServeSuccessJSON(map[string]interface{}{
  49. "orgs": orgs,
  50. "apps": apps,
  51. "banners": banners,
  52. "isCreateOrg": true,
  53. })
  54. } else {
  55. apps, err := service.GetAllApp(0)
  56. if err != nil {
  57. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  58. return
  59. }
  60. banners, err := service.GetSystemBanner()
  61. if err != nil {
  62. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  63. return
  64. }
  65. this.ServeSuccessJSON(map[string]interface{}{
  66. "isCreateOrg": false,
  67. "apps": apps,
  68. "banners": banners,
  69. })
  70. }
  71. }
  72. func RemoveRepeatedOrgElement(orgs []*models.Org) (newOrgs []*models.Org) {
  73. newOrgs = make([]*models.Org, 0)
  74. for i := 0; i < len(orgs); i++ {
  75. repeat := false
  76. for j := i + 1; j < len(orgs); j++ {
  77. if orgs[i].Id == orgs[j].Id {
  78. repeat = true
  79. break
  80. }
  81. }
  82. if !repeat {
  83. newOrgs = append(newOrgs, orgs[i])
  84. }
  85. }
  86. return
  87. }
  88. func (this *HomeController) ChangeOrg() {
  89. org_id, _ := this.GetInt64("org_id")
  90. adminUserInfo := this.GetMobileAdminUserInfo()
  91. tempOrg, err := service.GetOrgById(org_id)
  92. if err != nil {
  93. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  94. return
  95. }
  96. if tempOrg == nil {
  97. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeOrgNoExist)
  98. return
  99. }
  100. mobile := adminUserInfo.AdminUser.Mobile
  101. // 只取最近被创建的 admin_role
  102. adminUser, getAdminErr := service.GetValidAdminUserByMobileReturnErr(mobile) //账号信息唯一值
  103. if getAdminErr != nil {
  104. utils.ErrorLog("获取管理员失败:%v", getAdminErr)
  105. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  106. this.ServeJSON()
  107. return
  108. } else if adminUser == nil {
  109. utils.ErrorLog("查找不到 mobile = %v 的用户", mobile)
  110. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  111. this.ServeJSON()
  112. return
  113. } else {
  114. var appRole *models.App_Role
  115. var org *models.Org
  116. var subscibe *models.ServeSubscibe
  117. var app *models.OrgApp
  118. //根据登录信息的机构和用户id,去获取对应用户信息和机构信息
  119. tempApp, _ := service.GetOrgApp(tempOrg.Id, 3)
  120. tempRole, _ := service.GetAppRole(tempOrg.Id, tempApp.Id, adminUser.Id)
  121. tempSubscibe, getSubscibeErr := service.GetOrgServeSubscibe(tempOrg.Id)
  122. if getSubscibeErr != nil {
  123. utils.ErrorLog("获取机构订阅信息失败:%v", getSubscibeErr)
  124. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  125. this.ServeJSON()
  126. return
  127. }
  128. subscibe = tempSubscibe
  129. org = tempOrg
  130. appRole = tempRole
  131. app = tempApp
  132. templateInfo, _ := service.GetOrgInfoTemplate(org.Id)
  133. mobileAdminUserInfo := &mobile_api_controllers.MobileAdminUserInfo{
  134. AdminUser: adminUser,
  135. Org: org,
  136. App: app,
  137. AppRole: appRole,
  138. Subscibe: subscibe,
  139. TemplateInfo: &templateInfo,
  140. }
  141. if org != nil && appRole != nil {
  142. // 插入一条登录记录
  143. ip := this.GetString("ip")
  144. loginLog := &models.AdminUserLoginLog{
  145. AdminUserId: adminUser.Id,
  146. OrgId: org.Id,
  147. AppId: appRole.AppId,
  148. IP: ip,
  149. OperateType: 3,
  150. AppType: 3,
  151. CreateTime: time.Now().Unix(),
  152. }
  153. if insertErr := service.InsertLoginLog(loginLog); insertErr != nil {
  154. utils.ErrorLog("为手机号为%v的用户插入一条登录记录失败:%v", mobile, insertErr)
  155. }
  156. }
  157. //删除session和cookie
  158. this.DelSession("mobile_admin_user_info")
  159. this.Ctx.SetCookie("token_cookie", "")
  160. //设置new seesion
  161. this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
  162. //设置new cookie
  163. mobile := adminUser.Mobile + "-" + strconv.FormatInt(org.Id, 10) + "-" + strconv.FormatInt(appRole.Id, 10)
  164. token := utils.GenerateLoginToken(mobile)
  165. expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
  166. this.Ctx.SetCookie("token_cookie", token, expiration, "/")
  167. var configList interface{}
  168. var FiledList []*models.FiledConfig
  169. if org.Id > 0 {
  170. configList, _ = service.GetConfigList(org.Id)
  171. FiledList, _ = service.FindFiledByOrgId(org.Id)
  172. }
  173. if len(FiledList) == 0 {
  174. var err error
  175. if org.Id > 0 {
  176. err = service.BatchInsertFiledConfig(org.Id)
  177. if err == nil {
  178. FiledList, _ = service.FindFiledByOrgId(org.Id)
  179. } else {
  180. utils.ErrorLog("字段批量插入失败:%v", err)
  181. }
  182. } else {
  183. FiledList = make([]*models.FiledConfig, 0)
  184. }
  185. }
  186. this.ServeSuccessJSON(map[string]interface{}{
  187. "admin": adminUser,
  188. "user": appRole,
  189. "org": org,
  190. "template_info": map[string]interface{}{
  191. "id": templateInfo.ID,
  192. "org_id": templateInfo.OrgId,
  193. "template_id": templateInfo.TemplateId,
  194. },
  195. "config_list": configList,
  196. "filed_list": FiledList,
  197. })
  198. }
  199. }
  200. func (this *HomeController) CreateOrg() {
  201. adminUserInfo := this.GetMobileAdminUserInfo()
  202. adminUser := adminUserInfo.AdminUser
  203. //if didCreateOrg, checkCreateOrgErr := service.DidAdminUserCreateOrg(adminUser.Id); checkCreateOrgErr != nil {
  204. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  205. // this.ServeJSON()
  206. // return
  207. //} else if didCreateOrg {
  208. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateOrg)
  209. // this.ServeJSON()
  210. // return
  211. //}
  212. name := this.GetString("org_name")
  213. shortName := name
  214. provinceName := this.GetString("provinces_name")
  215. cityName := this.GetString("city_name")
  216. districtName := this.GetString("district_name")
  217. address := this.GetString("address")
  218. org_type := this.GetString("org_type")
  219. contactName := this.GetString("contact_name")
  220. openXT := true
  221. openCDM := false
  222. openSCRM := false
  223. openMall := false
  224. 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 {
  225. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  226. this.ServeJSON()
  227. return
  228. }
  229. orgPhone := this.GetString("telephone")
  230. if len(orgPhone) > 0 {
  231. if utils.PhoneRegexp().MatchString(orgPhone) == false {
  232. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  233. this.ServeJSON()
  234. return
  235. }
  236. }
  237. provinceID := 0
  238. cityID := 0
  239. districtID := 0
  240. province, getProvinceErr := service.GetProvinceWithName(provinceName)
  241. if getProvinceErr != nil {
  242. utils.ErrorLog("查询省名失败:%v", getProvinceErr)
  243. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  244. this.ServeJSON()
  245. return
  246. } else if province != nil {
  247. provinceID = int(province.ID)
  248. city, getCityErr := service.GetCityWithName(province.ID, cityName)
  249. if getCityErr != nil {
  250. utils.ErrorLog("查询城市名失败:%v", getCityErr)
  251. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  252. this.ServeJSON()
  253. return
  254. } else if city != nil {
  255. cityID = int(city.ID)
  256. district, getDistrictErr := service.GetDistrictWithName(city.ID, districtName)
  257. if getDistrictErr != nil {
  258. utils.ErrorLog("查询区县名失败:%v", getDistrictErr)
  259. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  260. this.ServeJSON()
  261. return
  262. } else if district != nil {
  263. districtID = int(district.ID)
  264. }
  265. }
  266. }
  267. var orgs []*models.Org
  268. vmAdminUser, err := service.GetHomeData(adminUser.Id)
  269. if err != nil {
  270. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  271. return
  272. }
  273. for _, item := range vmAdminUser.Org {
  274. orgs = append(orgs, item)
  275. }
  276. for _, item := range vmAdminUser.VMApp_Role {
  277. for _, subItem := range item.Org {
  278. orgs = append(orgs, subItem)
  279. }
  280. }
  281. orgs = RemoveRepeatedOrgElement(orgs)
  282. orgType := service.GetOrgTypeByName(org_type)
  283. org := &models.Org{
  284. Creator: adminUser.Id,
  285. OrgName: name,
  286. OrgShortName: shortName,
  287. Province: int64(provinceID),
  288. City: int64(cityID),
  289. District: int64(districtID),
  290. Address: address,
  291. OrgType: orgType.ID,
  292. Telephone: orgPhone,
  293. ContactName: contactName,
  294. Claim: 1,
  295. Evaluate: 5,
  296. Status: 1,
  297. CreateTime: time.Now().Unix(),
  298. ModifyTime: time.Now().Unix(),
  299. }
  300. createErr := service.CreateOrg(org, adminUser.Mobile, openXT, openCDM, openSCRM, openMall) // 创建机构以及所有类型的 app,如果有新类型的平台,则需要在这个方法里面把创建这一新类型的 app 的代码加上
  301. if createErr != nil {
  302. utils.ErrorLog("mobile=%v的超级管理员创建机构失败:%v", adminUser.Mobile, createErr)
  303. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  304. this.ServeJSON()
  305. } else {
  306. //初始化病人和排班相关数据
  307. InitPatientAndSchedule(org)
  308. //初始化透析方案
  309. InitSystemPrescrption(org)
  310. //初始化医嘱模版
  311. InitAdviceTemplate(org)
  312. //初始化角色和权限
  313. //初始化设备管理
  314. //初始化显示配置
  315. if len(orgs) == 0 {
  316. ip := utils.GetIP(this.Ctx.Request)
  317. ssoDomain := beego.AppConfig.String("sso_domain")
  318. api := ssoDomain + "/m/login/pwd"
  319. values := make(url.Values)
  320. values.Set("mobile", adminUser.Mobile)
  321. values.Set("password", adminUser.Password)
  322. values.Set("app_type", "3")
  323. values.Set("ip", ip)
  324. resp, requestErr := http.PostForm(api, values)
  325. if requestErr != nil {
  326. utils.ErrorLog("请求SSO登录接口失败: %v", requestErr)
  327. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  328. return
  329. }
  330. defer resp.Body.Close()
  331. body, ioErr := ioutil.ReadAll(resp.Body)
  332. if ioErr != nil {
  333. utils.ErrorLog("SSO登录接口返回数据读取失败: %v", ioErr)
  334. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  335. return
  336. }
  337. var respJSON map[string]interface{}
  338. utils.InfoLog(string(body))
  339. if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
  340. utils.ErrorLog("SSO登录接口返回数据解析JSON失败: %v", err)
  341. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  342. return
  343. }
  344. if respJSON["state"].(float64) != 1 {
  345. msg := respJSON["msg"].(string)
  346. utils.ErrorLog("SSO登录接口请求失败: %v", msg)
  347. if int(respJSON["code"].(float64)) == 609 {
  348. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  349. return
  350. }
  351. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  352. return
  353. } else {
  354. utils.SuccessLog("SSO登录成功")
  355. // 下面这几段 Map=>JSON=>Struct 的流程可能会造成速度很慢
  356. userJSON := respJSON["data"].(map[string]interface{})["admin"].(map[string]interface{})
  357. userJSONBytes, _ := json.Marshal(userJSON)
  358. var adminUser models.AdminUser
  359. if err := json.Unmarshal(userJSONBytes, &adminUser); err != nil {
  360. utils.ErrorLog("解析管理员失败:%v", err)
  361. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  362. return
  363. }
  364. var org models.Org
  365. if respJSON["data"].(map[string]interface{})["org"] != nil {
  366. orgJSON := respJSON["data"].(map[string]interface{})["org"].(map[string]interface{})
  367. orgJSONBytes, _ := json.Marshal(orgJSON)
  368. if err := json.Unmarshal(orgJSONBytes, &org); err != nil {
  369. utils.ErrorLog("解析机构失败:%v", err)
  370. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  371. return
  372. }
  373. }
  374. var app models.OrgApp
  375. if respJSON["data"].(map[string]interface{})["app"] != nil {
  376. appJSON := respJSON["data"].(map[string]interface{})["app"].(map[string]interface{})
  377. appJSONBytes, _ := json.Marshal(appJSON)
  378. if err := json.Unmarshal(appJSONBytes, &app); err != nil {
  379. utils.ErrorLog("解析应用失败:%v", err)
  380. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  381. return
  382. }
  383. }
  384. var appRole models.App_Role
  385. if respJSON["data"].(map[string]interface{})["app_role"] != nil {
  386. appRoleJSON := respJSON["data"].(map[string]interface{})["app_role"].(map[string]interface{})
  387. appRoleJSONBytes, _ := json.Marshal(appRoleJSON)
  388. if err := json.Unmarshal(appRoleJSONBytes, &appRole); err != nil {
  389. utils.ErrorLog("解析AppRole失败:%v", err)
  390. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  391. return
  392. }
  393. }
  394. var subscibe models.ServeSubscibe
  395. if respJSON["data"].(map[string]interface{})["subscibe"] != nil {
  396. subscibeJSON := respJSON["data"].(map[string]interface{})["subscibe"].(map[string]interface{})
  397. subscibeJSONBytes, _ := json.Marshal(subscibeJSON)
  398. if err := json.Unmarshal(subscibeJSONBytes, &subscibe); err != nil {
  399. utils.ErrorLog("解析Subscibe失败:%v", err)
  400. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  401. return
  402. }
  403. }
  404. //service.GetOrgSubscibeState(&subscibe)
  405. templateInfo, _ := service.GetOrgInfoTemplate(org.Id)
  406. mobileAdminUserInfo := &mobile_api_controllers.MobileAdminUserInfo{
  407. AdminUser: &adminUser,
  408. Org: &org,
  409. App: &app,
  410. AppRole: &appRole,
  411. Subscibe: &subscibe,
  412. TemplateInfo: &templateInfo,
  413. }
  414. this.Ctx.SetCookie("token_cookie", "")
  415. //设置seesion
  416. this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
  417. //设置cookie
  418. mobile := adminUser.Mobile + "-" + strconv.FormatInt(org.Id, 10) + "-" + strconv.FormatInt(appRole.Id, 10)
  419. token := utils.GenerateLoginToken(mobile)
  420. expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
  421. this.Ctx.SetCookie("token_cookie", token, expiration, "/")
  422. var configList interface{}
  423. var FiledList []*models.FiledConfig
  424. if org.Id > 0 {
  425. configList, _ = service.GetConfigList(org.Id)
  426. FiledList, _ = service.FindFiledByOrgId(org.Id)
  427. }
  428. if len(FiledList) == 0 {
  429. var err error
  430. if org.Id > 0 {
  431. err = service.BatchInsertFiledConfig(org.Id)
  432. if err == nil {
  433. FiledList, _ = service.FindFiledByOrgId(org.Id)
  434. } else {
  435. utils.ErrorLog("字段批量插入失败:%v", err)
  436. }
  437. } else {
  438. FiledList = make([]*models.FiledConfig, 0)
  439. }
  440. }
  441. this.ServeSuccessJSON(map[string]interface{}{
  442. "admin": adminUser,
  443. "user": appRole,
  444. "org": org,
  445. "template_info": map[string]interface{}{
  446. "id": templateInfo.ID,
  447. "org_id": templateInfo.OrgId,
  448. "template_id": templateInfo.TemplateId,
  449. },
  450. "config_list": configList,
  451. "filed_list": FiledList,
  452. "status": 1,
  453. })
  454. }
  455. } else {
  456. this.ServeSuccessJSON(map[string]interface{}{
  457. "org": org,
  458. "status": 2,
  459. })
  460. }
  461. }
  462. }