sso

org_controller.go 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 已和 name 同步,不需要传了
  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 contact_name:string
  72. // @param org_phone?:string
  73. // @param business_week?:string
  74. // @param business_time?:string
  75. // @param business_state?:int
  76. // @param org_pics?:string (url1@@url2@@url3)
  77. // @param open_xt?:bool 是否开启血透系统
  78. // @param open_cdm?:bool 是否开启慢病系统
  79. // @param open_scrm?:bool 是否开启SCRM
  80. // @param open_mall?:bool 是否开启Mall
  81. func (this *OrgController) CreateSubmit() {
  82. adminUserObj := this.GetSession("admin_user")
  83. if adminUserObj == nil {
  84. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeLoginTimeout)
  85. this.ServeJSON()
  86. return
  87. }
  88. adminUser := adminUserObj.(*models.AdminUser)
  89. if !adminUser.IsSuperAdmin {
  90. utils.ErrorLog("用户%v不是超级管理员,没有权限创建机构", adminUser.Mobile)
  91. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  92. this.ServeJSON()
  93. return
  94. }
  95. if didCreateOrg, checkCreateOrgErr := service.DidAdminUserCreateOrg(adminUser.Id); checkCreateOrgErr != nil {
  96. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  97. this.ServeJSON()
  98. return
  99. } else if didCreateOrg {
  100. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateOrg)
  101. this.ServeJSON()
  102. return
  103. }
  104. name := this.GetString("name")
  105. shortName := name // this.GetString("short_name")
  106. intro := this.GetString("intro")
  107. logo := this.GetString("logo")
  108. province, _ := this.GetInt("province", 0)
  109. city, _ := this.GetInt("city", 0)
  110. district, _ := this.GetInt("district", 0)
  111. address := this.GetString("address")
  112. ill := this.GetString("ill")
  113. category, _ := this.GetInt64("category")
  114. contactName := this.GetString("contact_name")
  115. openXT, _ := this.GetBool("open_xt")
  116. openCDM, _ := this.GetBool("open_cdm")
  117. openSCRM, _ := this.GetBool("open_scrm")
  118. openMall, _ := this.GetBool("open_mall")
  119. if len(name) == 0 || len(shortName) == 0 || len(contactName) == 0 || len(address) == 0 || province <= 0 || city <= 0 || district <= 0 || category <= 0 || (!openXT && !openCDM && !openSCRM && !openMall) {
  120. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  121. this.ServeJSON()
  122. return
  123. }
  124. orgPhone := this.GetString("org_phone")
  125. businessWeek := this.GetString("business_week")
  126. businessTime := this.GetString("business_time")
  127. businessState, _ := this.GetInt64("business_state")
  128. orgPics := this.GetString("org_pics")
  129. if len(orgPhone) > 0 {
  130. if utils.PhoneRegexp().MatchString(orgPhone) == false {
  131. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  132. this.ServeJSON()
  133. return
  134. }
  135. }
  136. if businessState != 0 && businessState != 1 {
  137. businessState = 0
  138. }
  139. org := models.Org{
  140. Creator: adminUser.Id,
  141. OrgName: name,
  142. OrgShortName: shortName,
  143. OrgIntroduction: intro,
  144. OrgLogo: logo,
  145. Province: province,
  146. City: city,
  147. District: district,
  148. Address: address,
  149. Illness: ill,
  150. OrgType: category,
  151. OperatingState: businessState,
  152. Telephone: orgPhone,
  153. ContactName: contactName,
  154. BusinessWeek: businessWeek,
  155. BusinessTime: businessTime,
  156. Gallery: orgPics,
  157. Claim: 1,
  158. Evaluate: 5,
  159. Status: 2,
  160. CreateTime: time.Now().Unix(),
  161. ModifyTime: time.Now().Unix(),
  162. }
  163. createErr := service.CreateOrg(&org, adminUser.Mobile, openXT, openCDM, openSCRM, openMall) // 创建机构以及所有类型的 app,如果有新类型的平台,则需要在这个方法里面把创建这一新类型的 app 的代码加上
  164. if createErr != nil {
  165. utils.ErrorLog("mobile=%v的超级管理员创建机构失败:%v", adminUser.Mobile, createErr)
  166. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  167. this.ServeJSON()
  168. } else {
  169. redirectURL := "" // beego.AppConfig.String("submodule_domain_dialysis_manage")
  170. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  171. "url": redirectURL,
  172. })
  173. this.ServeJSON()
  174. }
  175. }
  176. // /org/app/create [get] 19.06.04 之前的管理应用逻辑,已弃用
  177. func (this *OrgController) CreateApp() {
  178. // this.Abort("404")
  179. // return
  180. adminUserObj := this.GetSession("admin_user")
  181. if adminUserObj == nil {
  182. this.Redirect302(beego.URLFor("LoginController.PwdLogin"))
  183. return
  184. }
  185. adminUser := adminUserObj.(*models.AdminUser)
  186. if !adminUser.IsSuperAdmin {
  187. utils.ErrorLog("用户%v不是超级管理员,没有权限创建应用,应跳转到错误页", adminUser.Mobile)
  188. this.Redirect302(beego.URLFor("OrgController.CreateAppHint"))
  189. return
  190. }
  191. org, getOrgErr := service.GetOrgWithAdminUserID(adminUser.Id)
  192. if getOrgErr != 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. if org == nil {
  199. this.Redirect302(beego.URLFor("OrgController.Create"))
  200. return
  201. } else {
  202. apps, getAppsErr := service.GetOrgApps(adminUser.Id, org.Id)
  203. if getAppsErr != nil {
  204. utils.ErrorLog("获取id = %v的用户创建的机构下已创建的应用时出错:%v", adminUser.Id, getOrgErr)
  205. // this.Redirect302(beego.URLFor("LoginController.PwdLogin"))
  206. this.Abort("404")
  207. return
  208. } else {
  209. this.Data["avatar"] = org.OrgLogo //"/static/images/userData.png"
  210. this.Data["user_name"] = org.OrgShortName //adminUser.Mobile
  211. // 已创建的应用的信息
  212. did_patient_manage_create := false
  213. did_dialysis_manage_create := false
  214. did_cdm_manage_create := false
  215. did_mall_manage_create := false
  216. for _, app := range apps {
  217. if app.AppType == 1 {
  218. did_patient_manage_create = true
  219. } else if app.AppType == 3 {
  220. did_dialysis_manage_create = true
  221. } else if app.AppType == 4 {
  222. did_cdm_manage_create = true
  223. } else if app.AppType == 5 {
  224. did_mall_manage_create = true
  225. }
  226. }
  227. // 自动创建所有应用
  228. if did_patient_manage_create == false {
  229. err := service.CreateOrgApp(adminUser.Id, org.Id, 1, true)
  230. if err != nil {
  231. utils.ErrorLog("自动创建酷医聚客应用失败:%v", err)
  232. this.Abort("404")
  233. return
  234. }
  235. }
  236. if did_dialysis_manage_create == false {
  237. err := service.CreateOrgApp(adminUser.Id, org.Id, 3, true)
  238. if err != nil {
  239. utils.ErrorLog("自动创建透析管理应用失败:%v", err)
  240. this.Abort("404")
  241. return
  242. }
  243. }
  244. if did_cdm_manage_create == false {
  245. err := service.CreateOrgApp(adminUser.Id, org.Id, 4, true)
  246. if err != nil {
  247. utils.ErrorLog("自动创建慢病管理应用失败:%v", err)
  248. this.Abort("404")
  249. return
  250. }
  251. }
  252. if did_mall_manage_create == false {
  253. err := service.CreateOrgApp(adminUser.Id, org.Id, 5, true)
  254. if err != nil {
  255. utils.ErrorLog("自动创建微商城应用失败:%v", err)
  256. this.Abort("404")
  257. return
  258. }
  259. }
  260. this.Data["submodule_domain_patient_manage"] = beego.AppConfig.String("submodule_domain_patient_manage")
  261. this.Data["submodule_domain_dialysis_manage"] = beego.AppConfig.String("submodule_domain_dialysis_manage")
  262. this.Data["submodule_domain_cdm_manage"] = beego.AppConfig.String("submodule_domain_cdm_manage")
  263. this.Data["submodule_domain_mall_manage"] = beego.AppConfig.String("submodule_domain_mall_manage")
  264. this.SetTpl("new_main/manage_app.html")
  265. }
  266. }
  267. }
  268. }
  269. // /org/admin/apps [get]
  270. // @param org:int
  271. func (this *OrgController) ViewApps() {
  272. adminUserObj := this.GetSession("admin_user")
  273. if adminUserObj == nil {
  274. this.Redirect302(beego.URLFor("LoginController.PwdLogin"))
  275. return
  276. }
  277. adminUser := adminUserObj.(*models.AdminUser)
  278. orgID, _ := this.GetInt("org")
  279. if orgID <= 0 {
  280. this.Abort("404")
  281. return
  282. }
  283. org, getOrgErr := service.GetOrgById(orgID)
  284. if getOrgErr != nil {
  285. utils.ErrorLog("获取id = %v的用户创建的机构时出错:%v", adminUser.Id, getOrgErr)
  286. this.Abort("404")
  287. return
  288. } else {
  289. if org == nil {
  290. if adminUser.IsSuperAdmin == true {
  291. this.Redirect302(beego.URLFor("OrgController.Create"))
  292. } else {
  293. this.Abort("404")
  294. }
  295. return
  296. } else {
  297. this.Data["avatar"] = org.OrgLogo
  298. this.Data["user_name"] = org.OrgShortName
  299. this.Data["is_super_admin"] = adminUser.IsSuperAdmin
  300. this.Data["org_id"] = org.Id
  301. apps, getAppsErr := service.GetAdminUserAllOrgApp(adminUser.Id, org.Id)
  302. if getAppsErr != nil {
  303. utils.ErrorLog("获取 id = %v,org_id = %v 的用户有权限的应用时出错:%v", adminUser.Id, org.Id, getOrgErr)
  304. this.Abort("404")
  305. return
  306. }
  307. this.Data["scrm_role_exist"] = false
  308. this.Data["xt_role_exist"] = false
  309. this.Data["cdm_role_exist"] = false
  310. this.Data["mall_role_exist"] = false
  311. for _, app := range apps {
  312. if app.AppType == 1 && app.OpenStatus == 1 {
  313. this.Data["scrm_role_exist"] = true
  314. }
  315. if app.AppType == 3 && app.OpenStatus == 1 {
  316. this.Data["xt_role_exist"] = true
  317. }
  318. if app.AppType == 4 && app.OpenStatus == 1 {
  319. this.Data["cdm_role_exist"] = true
  320. }
  321. if app.AppType == 5 && app.OpenStatus == 1 {
  322. this.Data["mall_role_exist"] = true
  323. }
  324. }
  325. this.Data["submodule_domain_patient_manage"] = beego.AppConfig.String("submodule_domain_patient_manage")
  326. this.Data["submodule_domain_dialysis_manage"] = beego.AppConfig.String("submodule_domain_dialysis_manage")
  327. this.Data["submodule_domain_cdm_manage"] = beego.AppConfig.String("submodule_domain_cdm_manage")
  328. this.Data["submodule_domain_mall_manage"] = beego.AppConfig.String("submodule_domain_mall_manage")
  329. this.SetTpl("new_main/manage_app.html")
  330. }
  331. }
  332. }
  333. // /app/open [post]
  334. // @param type:int
  335. func (this *OrgController) OpenAppSubmit() {
  336. adminUserObj := this.GetSession("admin_user")
  337. if adminUserObj == nil {
  338. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeLoginTimeout)
  339. this.ServeJSON()
  340. return
  341. }
  342. adminUser := adminUserObj.(*models.AdminUser)
  343. if !adminUser.IsSuperAdmin {
  344. utils.ErrorLog("用户%v不是超级管理员,没有权限启用应用", adminUser.Mobile)
  345. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  346. this.ServeJSON()
  347. return
  348. }
  349. appType, _ := this.GetInt("type")
  350. if url := service.GetAppURLWithAppType(appType); len(url) == 0 {
  351. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  352. this.ServeJSON()
  353. return
  354. }
  355. org, getOrgErr := service.GetOrgWithAdminUserID(adminUser.Id)
  356. if getOrgErr != nil {
  357. utils.ErrorLog("获取id = %v的用户创建的机构时出错:%v", adminUser.Id, getOrgErr)
  358. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  359. this.ServeJSON()
  360. return
  361. } else {
  362. if org == nil {
  363. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeMissingOrg)
  364. this.ServeJSON()
  365. return
  366. } else {
  367. app, getAppErr := service.GetOrgAppWithType(adminUser.Id, org.Id, appType)
  368. if getAppErr != nil {
  369. utils.ErrorLog("获取 id=%v 的用户的类型为%v的应用时失败:%v", adminUser.Id, appType)
  370. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  371. this.ServeJSON()
  372. return
  373. }
  374. if app == nil {
  375. createAppErr := service.CreateOrgApp(adminUser.Id, org.Id, appType, true)
  376. if createAppErr != nil {
  377. utils.ErrorLog("id=%v的超级管理员创建类型为%v的应用时失败:%v", adminUser.Id, appType, createAppErr)
  378. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  379. this.ServeJSON()
  380. } else {
  381. this.Data["json"] = enums.MakeSuccessResponseJSON(nil)
  382. this.ServeJSON()
  383. }
  384. } else {
  385. if app.OpenStatus != 1 {
  386. app.OpenStatus = 1
  387. app.ModifyTime = time.Now().Unix()
  388. updateErr := service.SaveOrgApp(app)
  389. if updateErr != nil {
  390. utils.ErrorLog("id=%v的超级管理员开启类型为%v的应用时失败:%v", adminUser.Id, appType, updateErr)
  391. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  392. this.ServeJSON()
  393. return
  394. }
  395. }
  396. this.Data["json"] = enums.MakeSuccessResponseJSON(nil)
  397. this.ServeJSON()
  398. }
  399. }
  400. }
  401. }
  402. // /org/app/create/submit [post] 已废弃
  403. // @param app_type:int
  404. func (this *OrgController) CreateAppSubmit() {
  405. adminUserObj := this.GetSession("admin_user")
  406. if adminUserObj == nil {
  407. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeLoginTimeout)
  408. this.ServeJSON()
  409. return
  410. }
  411. adminUser := adminUserObj.(*models.AdminUser)
  412. if !adminUser.IsSuperAdmin {
  413. utils.ErrorLog("用户%v不是超级管理员,没有权限创建机构", adminUser.Mobile)
  414. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodePermissionDenied)
  415. this.ServeJSON()
  416. return
  417. }
  418. appType, _ := this.GetInt("app_type", 0)
  419. // if appType != 1 && appType != 3 && appType != 5 {
  420. // this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  421. // this.ServeJSON()
  422. // return
  423. // }
  424. if url := service.GetAppURLWithAppType(appType); len(url) == 0 {
  425. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  426. this.ServeJSON()
  427. return
  428. }
  429. org, getOrgErr := service.GetOrgWithAdminUserID(adminUser.Id)
  430. if getOrgErr != nil {
  431. utils.ErrorLog("获取id = %v的用户创建的机构时出错:%v", adminUser.Id, getOrgErr)
  432. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  433. this.ServeJSON()
  434. return
  435. } else {
  436. if org == nil {
  437. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeMissingOrg)
  438. this.ServeJSON()
  439. return
  440. } else {
  441. if didCreate, checkErr := service.DidOrgDidCreateAppWithType(org.Id, appType); checkErr != nil {
  442. utils.ErrorLog("检查id = %v的用户是否创建了类型为%v的应用时出错:%v", adminUser.Id, appType, checkErr)
  443. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  444. this.ServeJSON()
  445. return
  446. } else {
  447. if didCreate {
  448. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeRepeatCreateApp)
  449. this.ServeJSON()
  450. return
  451. }
  452. }
  453. // 创建应用
  454. if createErr := service.CreateOrgApp(adminUser.Id, org.Id, appType, false); createErr != nil {
  455. utils.ErrorLog("id=%v的超级管理员创建类型为%v的应用时失败:%v", adminUser.Id, appType, createErr)
  456. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDBCreate)
  457. this.ServeJSON()
  458. } else {
  459. this.Data["json"] = enums.MakeSuccessResponseJSON(nil)
  460. this.ServeJSON()
  461. }
  462. }
  463. }
  464. }
  465. type OrgCategoryController struct {
  466. BaseController
  467. }
  468. // /get_org_cat [get]
  469. // @param pid?:int
  470. func (this *OrgCategoryController) GetOrgCategories() {
  471. pid, _ := this.GetInt64("pid")
  472. if pid < 0 {
  473. pid = 0
  474. }
  475. cats, getCatErr := service.GetOrgCategoriesByPid(pid)
  476. if getCatErr != nil {
  477. utils.ErrorLog("获取机构类型失败:%v", getCatErr)
  478. this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  479. this.ServeJSON()
  480. return
  481. } else {
  482. this.Data["json"] = enums.MakeSuccessResponseJSON(map[string]interface{}{
  483. "list": cats,
  484. })
  485. this.ServeJSON()
  486. return
  487. }
  488. }
  489. // /create_app_hint [get]
  490. func (this *OrgController) CreateAppHint() {
  491. this.SetTpl("error/create_app_hint.html")
  492. }