home_api_controller.go 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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. //删除session和cookie
  142. this.DelSession("mobile_admin_user_info")
  143. this.Ctx.SetCookie("token_cookie", "")
  144. //设置new seesion
  145. this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
  146. //设置new cookie
  147. mobile := adminUser.Mobile + "-" + strconv.FormatInt(org.Id, 10) + "-" + strconv.FormatInt(appRole.Id, 10)
  148. token := utils.GenerateLoginToken(mobile)
  149. expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
  150. this.Ctx.SetCookie("token_cookie", token, expiration, "/")
  151. var configList interface{}
  152. var FiledList []*models.FiledConfig
  153. if org.Id > 0 {
  154. configList, _ = service.GetConfigList(org.Id)
  155. FiledList, _ = service.FindFiledByOrgId(org.Id)
  156. }
  157. if len(FiledList) == 0 {
  158. var err error
  159. if org.Id > 0 {
  160. err = service.BatchInsertFiledConfig(org.Id)
  161. if err == nil {
  162. FiledList, _ = service.FindFiledByOrgId(org.Id)
  163. } else {
  164. utils.ErrorLog("字段批量插入失败:%v", err)
  165. }
  166. } else {
  167. FiledList = make([]*models.FiledConfig, 0)
  168. }
  169. }
  170. this.ServeSuccessJSON(map[string]interface{}{
  171. "admin": adminUser,
  172. "user": appRole,
  173. "org": org,
  174. "template_info": map[string]interface{}{
  175. "id": templateInfo.ID,
  176. "org_id": templateInfo.OrgId,
  177. "template_id": templateInfo.TemplateId,
  178. },
  179. "config_list": configList,
  180. "filed_list": FiledList,
  181. })
  182. }
  183. }
  184. func (this *HomeController) CreateOrg() {
  185. adminUserInfo := this.GetMobileAdminUserInfo()
  186. adminUser := adminUserInfo.AdminUser
  187. //if didCreateOrg, checkCreateOrgErr := service.DidAdminUserCreateOrg(adminUser.Id); checkCreateOrgErr != nil {
  188. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  189. // this.ServeJSON()
  190. // return
  191. //} else if didCreateOrg {
  192. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateOrg)
  193. // this.ServeJSON()
  194. // return
  195. //}
  196. name := this.GetString("org_name")
  197. shortName := name
  198. provinceName := this.GetString("provinces_name")
  199. cityName := this.GetString("city_name")
  200. districtName := this.GetString("district_name")
  201. address := this.GetString("address")
  202. org_type := this.GetString("org_type")
  203. contactName := this.GetString("contact_name")
  204. openXT := true
  205. openCDM := false
  206. openSCRM := false
  207. openMall := false
  208. 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 {
  209. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  210. this.ServeJSON()
  211. return
  212. }
  213. orgPhone := this.GetString("telephone")
  214. if len(orgPhone) > 0 {
  215. if utils.PhoneRegexp().MatchString(orgPhone) == false {
  216. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  217. this.ServeJSON()
  218. return
  219. }
  220. }
  221. provinceID := 0
  222. cityID := 0
  223. districtID := 0
  224. province, getProvinceErr := service.GetProvinceWithName(provinceName)
  225. if getProvinceErr != nil {
  226. utils.ErrorLog("查询省名失败:%v", getProvinceErr)
  227. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  228. this.ServeJSON()
  229. return
  230. } else if province != nil {
  231. provinceID = int(province.ID)
  232. city, getCityErr := service.GetCityWithName(province.ID, cityName)
  233. if getCityErr != nil {
  234. utils.ErrorLog("查询城市名失败:%v", getCityErr)
  235. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  236. this.ServeJSON()
  237. return
  238. } else if city != nil {
  239. cityID = int(city.ID)
  240. district, getDistrictErr := service.GetDistrictWithName(city.ID, districtName)
  241. if getDistrictErr != nil {
  242. utils.ErrorLog("查询区县名失败:%v", getDistrictErr)
  243. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  244. this.ServeJSON()
  245. return
  246. } else if district != nil {
  247. districtID = int(district.ID)
  248. }
  249. }
  250. }
  251. var orgs []*models.Org
  252. vmAdminUser, err := service.GetHomeData(adminUser.Id)
  253. if err != nil {
  254. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
  255. return
  256. }
  257. for _, item := range vmAdminUser.Org {
  258. orgs = append(orgs, item)
  259. }
  260. for _, item := range vmAdminUser.VMApp_Role {
  261. for _, subItem := range item.Org {
  262. orgs = append(orgs, subItem)
  263. }
  264. }
  265. orgs = RemoveRepeatedOrgElement(orgs)
  266. orgType := service.GetOrgTypeByName(org_type)
  267. org := &models.Org{
  268. Creator: adminUser.Id,
  269. OrgName: name,
  270. OrgShortName: shortName,
  271. Province: int64(provinceID),
  272. City: int64(cityID),
  273. District: int64(districtID),
  274. Address: address,
  275. OrgType: orgType.ID,
  276. Telephone: orgPhone,
  277. ContactName: contactName,
  278. Claim: 1,
  279. Evaluate: 5,
  280. Status: 1,
  281. CreateTime: time.Now().Unix(),
  282. ModifyTime: time.Now().Unix(),
  283. }
  284. createErr := service.CreateOrg(org, adminUser.Mobile, openXT, openCDM, openSCRM, openMall) // 创建机构以及所有类型的 app,如果有新类型的平台,则需要在这个方法里面把创建这一新类型的 app 的代码加上
  285. if createErr != nil {
  286. utils.ErrorLog("mobile=%v的超级管理员创建机构失败:%v", adminUser.Mobile, createErr)
  287. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  288. this.ServeJSON()
  289. } else {
  290. //初始化病人和排班相关数据
  291. InitPatientAndSchedule(org)
  292. //初始化透析方案
  293. InitSystemPrescrption(org)
  294. //初始化医嘱模版
  295. InitAdviceTemplate(org)
  296. //初始化角色和权限
  297. //初始化设备管理
  298. //初始化显示配置
  299. if len(orgs) == 0 {
  300. ip := utils.GetIP(this.Ctx.Request)
  301. ssoDomain := beego.AppConfig.String("sso_domain")
  302. api := ssoDomain + "/m/login/pwd"
  303. values := make(url.Values)
  304. values.Set("mobile", adminUser.Mobile)
  305. values.Set("password", adminUser.Password)
  306. values.Set("app_type", "3")
  307. values.Set("ip", ip)
  308. resp, requestErr := http.PostForm(api, values)
  309. if requestErr != nil {
  310. utils.ErrorLog("请求SSO登录接口失败: %v", requestErr)
  311. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  312. return
  313. }
  314. defer resp.Body.Close()
  315. body, ioErr := ioutil.ReadAll(resp.Body)
  316. if ioErr != nil {
  317. utils.ErrorLog("SSO登录接口返回数据读取失败: %v", ioErr)
  318. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  319. return
  320. }
  321. var respJSON map[string]interface{}
  322. utils.InfoLog(string(body))
  323. if err := json.Unmarshal([]byte(string(body)), &respJSON); err != nil {
  324. utils.ErrorLog("SSO登录接口返回数据解析JSON失败: %v", err)
  325. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  326. return
  327. }
  328. if respJSON["state"].(float64) != 1 {
  329. msg := respJSON["msg"].(string)
  330. utils.ErrorLog("SSO登录接口请求失败: %v", msg)
  331. if int(respJSON["code"].(float64)) == 609 {
  332. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrPasswordWrong)
  333. return
  334. }
  335. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  336. return
  337. } else {
  338. utils.SuccessLog("SSO登录成功")
  339. // 下面这几段 Map=>JSON=>Struct 的流程可能会造成速度很慢
  340. userJSON := respJSON["data"].(map[string]interface{})["admin"].(map[string]interface{})
  341. userJSONBytes, _ := json.Marshal(userJSON)
  342. var adminUser models.AdminUser
  343. if err := json.Unmarshal(userJSONBytes, &adminUser); err != nil {
  344. utils.ErrorLog("解析管理员失败:%v", err)
  345. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  346. return
  347. }
  348. var org models.Org
  349. if respJSON["data"].(map[string]interface{})["org"] != nil {
  350. orgJSON := respJSON["data"].(map[string]interface{})["org"].(map[string]interface{})
  351. orgJSONBytes, _ := json.Marshal(orgJSON)
  352. if err := json.Unmarshal(orgJSONBytes, &org); err != nil {
  353. utils.ErrorLog("解析机构失败:%v", err)
  354. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  355. return
  356. }
  357. }
  358. var app models.OrgApp
  359. if respJSON["data"].(map[string]interface{})["app"] != nil {
  360. appJSON := respJSON["data"].(map[string]interface{})["app"].(map[string]interface{})
  361. appJSONBytes, _ := json.Marshal(appJSON)
  362. if err := json.Unmarshal(appJSONBytes, &app); err != nil {
  363. utils.ErrorLog("解析应用失败:%v", err)
  364. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  365. return
  366. }
  367. }
  368. var appRole models.App_Role
  369. if respJSON["data"].(map[string]interface{})["app_role"] != nil {
  370. appRoleJSON := respJSON["data"].(map[string]interface{})["app_role"].(map[string]interface{})
  371. appRoleJSONBytes, _ := json.Marshal(appRoleJSON)
  372. if err := json.Unmarshal(appRoleJSONBytes, &appRole); err != nil {
  373. utils.ErrorLog("解析AppRole失败:%v", err)
  374. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  375. return
  376. }
  377. }
  378. var subscibe models.ServeSubscibe
  379. if respJSON["data"].(map[string]interface{})["subscibe"] != nil {
  380. subscibeJSON := respJSON["data"].(map[string]interface{})["subscibe"].(map[string]interface{})
  381. subscibeJSONBytes, _ := json.Marshal(subscibeJSON)
  382. if err := json.Unmarshal(subscibeJSONBytes, &subscibe); err != nil {
  383. utils.ErrorLog("解析Subscibe失败:%v", err)
  384. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  385. return
  386. }
  387. }
  388. //service.GetOrgSubscibeState(&subscibe)
  389. templateInfo, _ := service.GetOrgInfoTemplate(org.Id)
  390. mobileAdminUserInfo := &mobile_api_controllers.MobileAdminUserInfo{
  391. AdminUser: &adminUser,
  392. Org: &org,
  393. App: &app,
  394. AppRole: &appRole,
  395. Subscibe: &subscibe,
  396. TemplateInfo: &templateInfo,
  397. }
  398. this.Ctx.SetCookie("token_cookie", "")
  399. //设置seesion
  400. this.SetSession("mobile_admin_user_info", mobileAdminUserInfo)
  401. //设置cookie
  402. mobile := adminUser.Mobile + "-" + strconv.FormatInt(org.Id, 10) + "-" + strconv.FormatInt(appRole.Id, 10)
  403. token := utils.GenerateLoginToken(mobile)
  404. expiration, _ := beego.AppConfig.Int64("mobile_token_expiration_second")
  405. this.Ctx.SetCookie("token_cookie", token, expiration, "/")
  406. var configList interface{}
  407. var FiledList []*models.FiledConfig
  408. if org.Id > 0 {
  409. configList, _ = service.GetConfigList(org.Id)
  410. FiledList, _ = service.FindFiledByOrgId(org.Id)
  411. }
  412. if len(FiledList) == 0 {
  413. var err error
  414. if org.Id > 0 {
  415. err = service.BatchInsertFiledConfig(org.Id)
  416. if err == nil {
  417. FiledList, _ = service.FindFiledByOrgId(org.Id)
  418. } else {
  419. utils.ErrorLog("字段批量插入失败:%v", err)
  420. }
  421. } else {
  422. FiledList = make([]*models.FiledConfig, 0)
  423. }
  424. }
  425. this.ServeSuccessJSON(map[string]interface{}{
  426. "admin": adminUser,
  427. "user": appRole,
  428. "org": org,
  429. "template_info": map[string]interface{}{
  430. "id": templateInfo.ID,
  431. "org_id": templateInfo.OrgId,
  432. "template_id": templateInfo.TemplateId,
  433. },
  434. "config_list": configList,
  435. "filed_list": FiledList,
  436. "status": 1,
  437. })
  438. }
  439. } else {
  440. this.ServeSuccessJSON(map[string]interface{}{
  441. "org": org,
  442. "status": 2,
  443. })
  444. }
  445. }
  446. }