sso

org_controller.go 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. package controllers
  2. import (
  3. "SSO/enums"
  4. "SSO/models"
  5. "SSO/service"
  6. "SSO/utils"
  7. "time"
  8. "github.com/astaxie/beego"
  9. )
  10. type OrgController struct {
  11. BaseController
  12. }
  13. // /org/create [get]
  14. func (this *OrgController) Create() {
  15. adminUserObj := this.GetSession("admin_user")
  16. if adminUserObj == nil {
  17. this.Redirect302(beego.URLFor("LoginController.PwdLogin"))
  18. return
  19. }
  20. adminUser := adminUserObj.(*models.AdminUser)
  21. if !adminUser.IsSuperAdmin {
  22. utils.ErrorLog("用户%v不是超级管理员,没有权限创建机构,应跳转到错误页", adminUser.Mobile)
  23. this.Redirect302(beego.URLFor("OrgController.CreateAppHint"))
  24. return
  25. }
  26. if didCreateOrg, checkCreateOrgErr := service.DidAdminUserCreateOrg(adminUser.Id); checkCreateOrgErr != nil {
  27. utils.ErrorLog("检查id = %v的用户是否创建了机构时出错:%v", adminUser.Id, checkCreateOrgErr)
  28. // this.Redirect302(beego.URLFor("LoginController.PwdLogin"))
  29. this.Abort("404")
  30. return
  31. } else {
  32. if didCreateOrg {
  33. // if didCreateApp, checkCreateAppErr := service.DidAdminUserOrgCreateApp(adminUser.Id); checkCreateAppErr != nil {
  34. // utils.ErrorLog("检查id = %v的用户是否创建了应用时出错:%v", adminUser.Id, checkCreateAppErr)
  35. // this.Redirect302(beego.URLFor("LoginController.Login"))
  36. // return
  37. // } else {
  38. // if didCreateApp {
  39. // // 前往该去的地方 LoginController.getRedirectURL()
  40. // }
  41. // }
  42. utils.ErrorLog("id = %v 的用户已创建机构,接下来应当前往应用页或者前往错误页", adminUser.Id)
  43. this.Redirect302(beego.URLFor("LoginController.CreateApp"))
  44. return
  45. }
  46. }
  47. cats, getCatErr := service.GetOrgCategoriesByPid(0)
  48. if getCatErr != nil {
  49. utils.ErrorLog("获取机构类型失败:%v", getCatErr)
  50. this.Abort("404")
  51. return
  52. }
  53. this.Data["categories"] = cats
  54. this.Data["avatar"] = "/static/images/userData.png"
  55. this.Data["user_name"] = adminUser.Mobile
  56. this.Data["province"] = service.GetAllProvince()
  57. this.Data["illness"], _ = service.GetIllness()
  58. this.SetTpl("new_main/create_org.html")
  59. }
  60. // /org/create/submit [post]
  61. // @param name:string
  62. // @param short_name:string
  63. // @param intro?:string
  64. // @param logo?:string
  65. // @param province:int
  66. // @param city:int
  67. // @param district:int
  68. // @param address:string
  69. // @param ill?:string ("病种1,病种2")
  70. // @param category:int
  71. // @param org_phone?:string
  72. // @param business_week?:string
  73. // @param business_time?:string
  74. // @param business_state?:int
  75. // @param org_pics?:string (url1@@url2@@url3)
  76. func (this *OrgController) CreateSubmit() {
  77. adminUserObj := this.GetSession("admin_user")
  78. if adminUserObj == nil {
  79. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeLoginTimeout)
  80. this.ServeJSON()
  81. return
  82. }
  83. adminUser := adminUserObj.(*models.AdminUser)
  84. if !adminUser.IsSuperAdmin {
  85. utils.ErrorLog("用户%v不是超级管理员,没有权限创建机构", adminUser.Mobile)
  86. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  87. this.ServeJSON()
  88. return
  89. }
  90. if didCreateOrg, checkCreateOrgErr := service.DidAdminUserCreateOrg(adminUser.Id); checkCreateOrgErr != nil {
  91. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  92. this.ServeJSON()
  93. return
  94. } else if didCreateOrg {
  95. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateOrg)
  96. this.ServeJSON()
  97. return
  98. }
  99. name := this.GetString("name")
  100. shortName := this.GetString("short_name")
  101. intro := this.GetString("intro")
  102. logo := this.GetString("logo")
  103. province, _ := this.GetInt("province", 0)
  104. city, _ := this.GetInt("city", 0)
  105. district, _ := this.GetInt("district", 0)
  106. address := this.GetString("address")
  107. ill := this.GetString("ill")
  108. category, _ := this.GetInt64("category")
  109. if len(name) == 0 || len(shortName) == 0 || len(address) == 0 || province <= 0 || city <= 0 || district <= 0 || category <= 0 {
  110. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  111. this.ServeJSON()
  112. return
  113. }
  114. orgPhone := this.GetString("org_phone")
  115. businessWeek := this.GetString("business_week")
  116. businessTime := this.GetString("business_time")
  117. businessState, _ := this.GetInt64("business_state")
  118. orgPics := this.GetString("org_pics")
  119. if len(orgPhone) > 0 {
  120. if utils.PhoneRegexp().MatchString(orgPhone) == false {
  121. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  122. this.ServeJSON()
  123. return
  124. }
  125. }
  126. if businessState != 0 && businessState != 1 {
  127. businessState = 0
  128. }
  129. org := models.Org{
  130. Creator: adminUser.Id,
  131. OrgName: name,
  132. OrgShortName: shortName,
  133. OrgIntroduction: intro,
  134. OrgLogo: logo,
  135. Province: province,
  136. City: city,
  137. District: district,
  138. Address: address,
  139. Illness: ill,
  140. OrgType: category,
  141. OperatingState: businessState,
  142. Telephone: orgPhone,
  143. BusinessWeek: businessWeek,
  144. BusinessTime: businessTime,
  145. Gallery: orgPics,
  146. Claim: 1,
  147. Evaluate: 5,
  148. Status: 2,
  149. CreateTime: time.Now().Unix(),
  150. ModifyTime: time.Now().Unix(),
  151. }
  152. createErr := service.CreateOrg(&org, adminUser.Mobile) // 创建机构以及所有类型的 app,如果有新类型的平台,则需要在这个方法里面把创建这一新类型的 app 的代码加上
  153. if createErr != nil {
  154. utils.ErrorLog("mobile=%v的超级管理员创建机构失败:%v", adminUser.Mobile, createErr)
  155. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  156. this.ServeJSON()
  157. } else {
  158. redirectURL := beego.AppConfig.String("submodule_domain_dialysis_manage")
  159. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  160. "url": redirectURL,
  161. })
  162. this.ServeJSON()
  163. }
  164. }
  165. // /org/app/create [get] 19.06.04 之前的管理应用逻辑,已弃用
  166. func (this *OrgController) CreateApp() {
  167. // this.Abort("404")
  168. // return
  169. adminUserObj := this.GetSession("admin_user")
  170. if adminUserObj == nil {
  171. this.Redirect302(beego.URLFor("LoginController.PwdLogin"))
  172. return
  173. }
  174. adminUser := adminUserObj.(*models.AdminUser)
  175. if !adminUser.IsSuperAdmin {
  176. utils.ErrorLog("用户%v不是超级管理员,没有权限创建应用,应跳转到错误页", adminUser.Mobile)
  177. this.Redirect302(beego.URLFor("OrgController.CreateAppHint"))
  178. return
  179. }
  180. org, getOrgErr := service.GetOrgWithAdminUserID(adminUser.Id)
  181. if getOrgErr != nil {
  182. utils.ErrorLog("获取id = %v的用户创建的机构时出错:%v", adminUser.Id, getOrgErr)
  183. // this.Redirect302(beego.URLFor("LoginController.PwdLogin"))
  184. this.Abort("404")
  185. return
  186. } else {
  187. if org == nil {
  188. this.Redirect302(beego.URLFor("OrgController.Create"))
  189. return
  190. } else {
  191. apps, getAppsErr := service.GetOrgApps(adminUser.Id, org.Id)
  192. if getAppsErr != nil {
  193. utils.ErrorLog("获取id = %v的用户创建的机构下已创建的应用时出错:%v", adminUser.Id, getOrgErr)
  194. // this.Redirect302(beego.URLFor("LoginController.PwdLogin"))
  195. this.Abort("404")
  196. return
  197. } else {
  198. this.Data["avatar"] = org.OrgLogo //"/static/images/userData.png"
  199. this.Data["user_name"] = org.OrgShortName //adminUser.Mobile
  200. // 已创建的应用的信息
  201. did_patient_manage_create := false
  202. did_dialysis_manage_create := false
  203. did_cdm_manage_create := false
  204. did_mall_manage_create := false
  205. for _, app := range apps {
  206. if app.AppType == 1 {
  207. did_patient_manage_create = true
  208. } else if app.AppType == 3 {
  209. did_dialysis_manage_create = true
  210. } else if app.AppType == 4 {
  211. did_cdm_manage_create = true
  212. } else if app.AppType == 5 {
  213. did_mall_manage_create = true
  214. }
  215. }
  216. // 自动创建所有应用
  217. if did_patient_manage_create == false {
  218. err := service.CreateOrgApp(adminUser.Id, org.Id, 1)
  219. if err != nil {
  220. utils.ErrorLog("自动创建酷医聚客应用失败:%v", err)
  221. this.Abort("404")
  222. return
  223. }
  224. }
  225. if did_dialysis_manage_create == false {
  226. err := service.CreateOrgApp(adminUser.Id, org.Id, 3)
  227. if err != nil {
  228. utils.ErrorLog("自动创建透析管理应用失败:%v", err)
  229. this.Abort("404")
  230. return
  231. }
  232. }
  233. if did_cdm_manage_create == false {
  234. err := service.CreateOrgApp(adminUser.Id, org.Id, 4)
  235. if err != nil {
  236. utils.ErrorLog("自动创建慢病管理应用失败:%v", err)
  237. this.Abort("404")
  238. return
  239. }
  240. }
  241. if did_mall_manage_create == false {
  242. err := service.CreateOrgApp(adminUser.Id, org.Id, 5)
  243. if err != nil {
  244. utils.ErrorLog("自动创建微商城应用失败:%v", err)
  245. this.Abort("404")
  246. return
  247. }
  248. }
  249. this.Data["submodule_domain_patient_manage"] = beego.AppConfig.String("submodule_domain_patient_manage")
  250. this.Data["submodule_domain_dialysis_manage"] = beego.AppConfig.String("submodule_domain_dialysis_manage")
  251. this.Data["submodule_domain_cdm_manage"] = beego.AppConfig.String("submodule_domain_cdm_manage")
  252. this.Data["submodule_domain_mall_manage"] = beego.AppConfig.String("submodule_domain_mall_manage")
  253. this.SetTpl("new_main/manage_app.html")
  254. }
  255. }
  256. }
  257. }
  258. // /org/admin/apps [get]
  259. // @param org:int
  260. func (this *OrgController) ViewApps() {
  261. adminUserObj := this.GetSession("admin_user")
  262. if adminUserObj == nil {
  263. this.Redirect302(beego.URLFor("LoginController.PwdLogin"))
  264. return
  265. }
  266. adminUser := adminUserObj.(*models.AdminUser)
  267. orgID, _ := this.GetInt("org")
  268. if orgID <= 0 {
  269. this.Abort("404")
  270. return
  271. }
  272. org, getOrgErr := service.GetOrgById(orgID)
  273. if getOrgErr != nil {
  274. utils.ErrorLog("获取id = %v的用户创建的机构时出错:%v", adminUser.Id, getOrgErr)
  275. this.Abort("404")
  276. return
  277. } else {
  278. if org == nil {
  279. if adminUser.IsSuperAdmin == true {
  280. this.Redirect302(beego.URLFor("OrgController.Create"))
  281. } else {
  282. this.Abort("404")
  283. }
  284. return
  285. } else {
  286. this.Data["avatar"] = org.OrgLogo //"/static/images/userData.png"
  287. this.Data["user_name"] = org.OrgShortName //adminUser.Mobile
  288. if adminUser.IsSuperAdmin {
  289. this.Data["scrm_role_exist"] = true
  290. this.Data["xt_role_exist"] = true
  291. this.Data["cdm_role_exist"] = true
  292. this.Data["mall_role_exist"] = true
  293. } else {
  294. apps, getAppsErr := service.GetAdminUserAllOrgApp(adminUser.Id, org.Id)
  295. if getAppsErr != nil {
  296. utils.ErrorLog("获取 id = %v,org_id = %v 的用户有权限的应用时出错:%v", adminUser.Id, org.Id, getOrgErr)
  297. this.Abort("404")
  298. return
  299. }
  300. this.Data["scrm_role_exist"] = false
  301. this.Data["xt_role_exist"] = false
  302. this.Data["cdm_role_exist"] = false
  303. this.Data["mall_role_exist"] = false
  304. for _, app := range apps {
  305. if app.AppType == 1 {
  306. this.Data["scrm_role_exist"] = true
  307. }
  308. if app.AppType == 3 {
  309. this.Data["xt_role_exist"] = true
  310. }
  311. if app.AppType == 4 {
  312. this.Data["cdm_role_exist"] = true
  313. }
  314. if app.AppType == 5 {
  315. this.Data["mall_role_exist"] = true
  316. }
  317. }
  318. }
  319. this.Data["submodule_domain_patient_manage"] = beego.AppConfig.String("submodule_domain_patient_manage")
  320. this.Data["submodule_domain_dialysis_manage"] = beego.AppConfig.String("submodule_domain_dialysis_manage")
  321. this.Data["submodule_domain_cdm_manage"] = beego.AppConfig.String("submodule_domain_cdm_manage")
  322. this.Data["submodule_domain_mall_manage"] = beego.AppConfig.String("submodule_domain_mall_manage")
  323. this.SetTpl("new_main/manage_app.html")
  324. }
  325. }
  326. }
  327. // /org/app/create/submit [post]
  328. // @param app_type:int
  329. func (this *OrgController) CreateAppSubmit() {
  330. adminUserObj := this.GetSession("admin_user")
  331. if adminUserObj == nil {
  332. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeLoginTimeout)
  333. this.ServeJSON()
  334. return
  335. }
  336. adminUser := adminUserObj.(*models.AdminUser)
  337. if !adminUser.IsSuperAdmin {
  338. utils.ErrorLog("用户%v不是超级管理员,没有权限创建机构", adminUser.Mobile)
  339. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  340. this.ServeJSON()
  341. return
  342. }
  343. appType, _ := this.GetInt("app_type", 0)
  344. // if appType != 1 && appType != 3 && appType != 5 {
  345. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  346. // this.ServeJSON()
  347. // return
  348. // }
  349. if url := service.GetAppURLWithAppType(appType); len(url) == 0 {
  350. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  351. this.ServeJSON()
  352. return
  353. }
  354. org, getOrgErr := service.GetOrgWithAdminUserID(adminUser.Id)
  355. if getOrgErr != nil {
  356. utils.ErrorLog("获取id = %v的用户创建的机构时出错:%v", adminUser.Id, getOrgErr)
  357. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  358. this.ServeJSON()
  359. return
  360. } else {
  361. if org == nil {
  362. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeMissingOrg)
  363. this.ServeJSON()
  364. return
  365. } else {
  366. if didCreate, checkErr := service.DidOrgDidCreateAppWithType(org.Id, appType); checkErr != nil {
  367. utils.ErrorLog("检查id = %v的用户是否创建了类型为%v的应用时出错:%v", adminUser.Id, appType, checkErr)
  368. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  369. this.ServeJSON()
  370. return
  371. } else {
  372. if didCreate {
  373. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateApp)
  374. this.ServeJSON()
  375. return
  376. }
  377. }
  378. // 创建应用
  379. if createErr := service.CreateOrgApp(adminUser.Id, org.Id, appType); createErr != nil {
  380. utils.ErrorLog("id=%v的超级管理员创建类型为%v的应用时失败:%v", adminUser.Id, appType, createErr)
  381. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  382. this.ServeJSON()
  383. } else {
  384. this.Data["json"] = enums.MakeSuccessResponseJSON(nil)
  385. this.ServeJSON()
  386. }
  387. }
  388. }
  389. }
  390. type OrgCategoryController struct {
  391. BaseController
  392. }
  393. // /get_org_cat [get]
  394. // @param pid?:int
  395. func (this *OrgCategoryController) GetOrgCategories() {
  396. pid, _ := this.GetInt64("pid")
  397. if pid < 0 {
  398. pid = 0
  399. }
  400. cats, getCatErr := service.GetOrgCategoriesByPid(pid)
  401. if getCatErr != nil {
  402. utils.ErrorLog("获取机构类型失败:%v", getCatErr)
  403. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  404. this.ServeJSON()
  405. return
  406. } else {
  407. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  408. "list": cats,
  409. })
  410. this.ServeJSON()
  411. return
  412. }
  413. }
  414. // /create_app_hint [get]
  415. func (this *OrgController) CreateAppHint() {
  416. this.SetTpl("error/create_app_hint.html")
  417. }