public_api_controller.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. package controllers
  2. import (
  3. "XT_New/enums"
  4. "XT_New/models"
  5. "XT_New/service"
  6. "encoding/json"
  7. "fmt"
  8. "os"
  9. "path"
  10. "runtime"
  11. "strings"
  12. "time"
  13. "github.com/astaxie/beego"
  14. )
  15. type PublicApiController struct {
  16. BaseAPIController
  17. }
  18. func PublicApiRegistRouters() {
  19. beego.Router("/api/public/getadviceconfig", &PublicApiController{}, "Get:GetDoctorAdviceConfig")
  20. beego.Router("/api/public/servertime", &PublicApiController{}, "Get:GetServerTime")
  21. beego.Router("/api/app/release", &PublicApiController{}, "Get:AppRelease")
  22. beego.Router("/api/get", &PublicApiController{}, "Get:GetJson")
  23. beego.Router("/api/public/handledata", &PublicApiController{}, "get:HandleData")
  24. }
  25. func (c *PublicApiController) GetDoctorAdviceConfig() {
  26. drugs, _, _ := service.GetPublicDrugDics()
  27. drugways, _, _ := service.GetPublicDrugWayDics()
  28. efs, _, _ := service.GetPublicExecutionFrequencyDics()
  29. c.ServeSuccessJSON(map[string]interface{}{
  30. "drugs": drugs,
  31. "drugways": drugways,
  32. "efs": efs,
  33. })
  34. }
  35. func (c *PublicApiController) GetServerTime() {
  36. timeNow := time.Now()
  37. timeNowStamp := timeNow.Unix()
  38. timeNowStr := timeNow.Format("2006-01-02 15:04:05")
  39. c.ServeSuccessJSON(map[string]interface{}{
  40. "time": timeNowStr,
  41. "timestamp": timeNowStamp,
  42. })
  43. }
  44. func (c *PublicApiController) AppRelease() {
  45. // appId := c.GetString("appid")
  46. version := c.GetString("version")
  47. appType, _ := c.GetInt64("app_type", 0)
  48. appVersion, err := service.GetAppVersionByAppType(appType)
  49. if err != nil {
  50. c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  51. return
  52. }
  53. if appVersion == nil {
  54. c.ServeSuccessJSON(map[string]interface{}{
  55. "version": nil,
  56. "state": 2,
  57. })
  58. return
  59. }
  60. state := strings.Compare(version, appVersion.Version)
  61. // state := 1
  62. if state < 0 {
  63. c.ServeSuccessJSON(map[string]interface{}{
  64. "version": appVersion,
  65. "state": appVersion.Status,
  66. })
  67. return
  68. }
  69. c.ServeSuccessJSON(map[string]interface{}{
  70. "version": nil,
  71. "state": 2,
  72. })
  73. return
  74. }
  75. func (this *PublicApiController) GetJson() {
  76. type Global struct {
  77. DeviceSN string
  78. Version string
  79. Timestamp string
  80. InterfaceSource string
  81. }
  82. type RBPResultModel struct {
  83. Sys string
  84. Dia string
  85. HR string
  86. MeasureTime string
  87. }
  88. type ScannerResultModel struct {
  89. Code string
  90. }
  91. type IDCardResultModel struct {
  92. IDCardNo string
  93. UserName string
  94. Age string
  95. Female string
  96. Male string
  97. SocialSecurityNo string
  98. }
  99. type JsonTemp struct {
  100. Global Global
  101. RBPResultModel RBPResultModel
  102. ScannerResultModel ScannerResultModel
  103. IDCardResultModel IDCardResultModel
  104. }
  105. var person JsonTemp
  106. err3 := json.Unmarshal([]byte(this.GetString("params")), &person)
  107. fmt.Println(err3)
  108. fmt.Println(person.Global)
  109. //temp := JsonTemp{
  110. // Global:Global{
  111. // DeviceSN:"1122",
  112. // Version:"1122",
  113. // Timestamp:"1222",
  114. // InterfaceSource:"1233",
  115. // },
  116. // RBPResultModel: RBPResultModel{
  117. // Sys:"12",
  118. // Dia:"22",
  119. // HR:"23",
  120. // MeasureTime:"123",
  121. // },
  122. // ScannerResultModel:ScannerResultModel{
  123. // Code:"1233",
  124. // },
  125. // IDCardResultModel:IDCardResultModel{
  126. // IDCardNo:"123",
  127. // UserName:"123",
  128. // Age:"123",
  129. // Female:"123",
  130. // Male:"123",
  131. // SocialSecurityNo:"123",
  132. // },
  133. //}
  134. //
  135. //jsonBytes, err := json.Marshal(temp)
  136. //if err != nil {
  137. // fmt.Println(err)
  138. //}
  139. //fmt.Println(string(jsonBytes))
  140. }
  141. func (c *PublicApiController) HandleData() {
  142. //org_app, _ := service.GetAllAppOrg()
  143. //org_role, _ := service.GetAllRole()
  144. //
  145. //var org_ids []int64
  146. //for _, apps := range org_app {
  147. // xt_org_app, _ := service.GetAppByType(int64(apps.OrgId), 3)
  148. // scrm_org_app, _ := service.GetAppByType(int64(apps.OrgId), 1)
  149. // cdm_org_app, _ := service.GetAppByType(int64(apps.OrgId), 4)
  150. // mall_org_app, _ := service.GetAppByType(int64(apps.OrgId), 5)
  151. // org_app_6, _ := service.GetAppByType(int64(apps.OrgId), 6)
  152. // org_app_7, _ := service.GetAppByType(int64(apps.OrgId), 7)
  153. // org_app_8, _ := service.GetAppByType(int64(apps.OrgId), 8)
  154. // org_app_9, _ := service.GetAppByType(int64(apps.OrgId), 9)
  155. // org_app_10, _ := service.GetAppByType(int64(apps.OrgId), 10)
  156. // org_app_11, _ := service.GetAppByType(int64(apps.OrgId), 11)
  157. // org_app_12, _ := service.GetAppByType(int64(apps.OrgId), 12)
  158. // org_app_13, _ := service.GetAppByType(int64(apps.OrgId), 13)
  159. // org_app_14, _ := service.GetAppByType(int64(apps.OrgId), 14)
  160. // org_app_15, _ := service.GetAppByType(int64(apps.OrgId), 15)
  161. //
  162. // var xt_temp_org_app *models.OrgApp
  163. // var mall_temp_org_app *models.OrgApp
  164. //
  165. // if xt_org_app == nil || xt_org_app.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  166. //
  167. // app, _ := service.GetOrgAppA(0, 3)
  168. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  169. // if org == nil || org.Id == 0 {
  170. // continue
  171. // }
  172. //
  173. // app.Id = 0
  174. // app.OrgId = apps.OrgId
  175. // app.Creator = int(org.Creator)
  176. // app.Number = 0
  177. // app.Status = 1
  178. // app.ModifyTime = time.Now().Unix()
  179. // app.CreateTime = time.Now().Unix()
  180. // service.CreateOrgApp(app)
  181. // xt_temp_org_app = app
  182. // } else {
  183. //
  184. // xt_temp_org_app = xt_org_app
  185. //
  186. // }
  187. //
  188. // if scrm_org_app == nil || scrm_org_app.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  189. //
  190. // app, _ := service.GetOrgAppA(0, 1)
  191. // //org, _ := service.GetOrgById(int64(12))
  192. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  193. // if org == nil || org.Id == 0 {
  194. // continue
  195. // }
  196. //
  197. //
  198. // app.OrgId = apps.OrgId
  199. // app.Id = 0
  200. //
  201. // app.Creator = int(org.Creator)
  202. // app.Number = 0
  203. // app.Status = 1
  204. // app.ModifyTime = time.Now().Unix()
  205. // app.CreateTime = time.Now().Unix()
  206. // service.CreateOrgApp(app)
  207. //
  208. // }
  209. //
  210. // if cdm_org_app == nil || cdm_org_app.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  211. // app, _ := service.GetOrgAppA(0, 4)
  212. // //org, _ := service.GetOrgById(int64(12))
  213. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  214. // if org == nil || org.Id == 0 {
  215. // continue
  216. // }
  217. //
  218. //
  219. // app.OrgId = apps.OrgId
  220. // app.Creator = int(org.Creator)
  221. // app.Number = 0
  222. // app.Id = 0
  223. //
  224. // app.Status = 1
  225. // app.ModifyTime = time.Now().Unix()
  226. // app.CreateTime = time.Now().Unix()
  227. // service.CreateOrgApp(app)
  228. // }
  229. //
  230. // if mall_org_app == nil || mall_org_app.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  231. // app, _ := service.GetOrgAppA(0, 5)
  232. // //org, _ := service.GetOrgById(int64(12))
  233. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  234. //
  235. // if org == nil || org.Id == 0 {
  236. // continue
  237. // }
  238. //
  239. // app.OrgId = apps.OrgId
  240. // app.Creator = int(org.Creator)
  241. // app.Number = 0
  242. // app.Id = 0
  243. //
  244. // app.Status = 1
  245. // app.ModifyTime = time.Now().Unix()
  246. // app.CreateTime = time.Now().Unix()
  247. // service.CreateOrgApp(app)
  248. // mall_temp_org_app = app
  249. // } else {
  250. // mall_temp_org_app = mall_org_app
  251. // }
  252. //
  253. // if org_app_6 == nil || org_app_6.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  254. // app, _ := service.GetOrgAppA(0, 6)
  255. // //org, _ := service.GetOrgById(int64(12))
  256. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  257. // if org == nil || org.Id == 0 {
  258. // continue
  259. // }
  260. //
  261. //
  262. // app.OrgId = apps.OrgId
  263. // app.Creator = int(org.Creator)
  264. // app.Number = 1
  265. // app.Id = 0
  266. //
  267. // app.Status = 1
  268. // app.ModifyTime = time.Now().Unix()
  269. // app.CreateTime = time.Now().Unix()
  270. // app.Pid = xt_temp_org_app.Id
  271. // service.CreateOrgApp(app)
  272. //
  273. // }
  274. // if org_app_7 == nil || org_app_7.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  275. // app, _ := service.GetOrgAppA(0, 7)
  276. // //org, _ := service.GetOrgById(int64(12))
  277. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  278. // if org == nil || org.Id == 0 {
  279. // continue
  280. // }
  281. //
  282. //
  283. // app.OrgId = apps.OrgId
  284. // app.Creator = int(org.Creator)
  285. // app.Number = 2
  286. // app.Status = 2
  287. // app.Id = 0
  288. //
  289. // app.ModifyTime = time.Now().Unix()
  290. // app.CreateTime = time.Now().Unix()
  291. // app.Pid = xt_temp_org_app.Id
  292. //
  293. // service.CreateOrgApp(app)
  294. // }
  295. // if org_app_8 == nil || org_app_8.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  296. // app, _ := service.GetOrgAppA(0, 8)
  297. // //org, _ := service.GetOrgById(int64(12))
  298. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  299. // if org == nil || org.Id == 0 {
  300. // continue
  301. // }
  302. //
  303. //
  304. // app.OrgId = apps.OrgId
  305. // app.Creator = int(org.Creator)
  306. // app.Number = 3
  307. // app.Id = 0
  308. //
  309. // app.Pid = mall_temp_org_app.Id
  310. //
  311. // app.Status = 1
  312. // app.ModifyTime = time.Now().Unix()
  313. // app.CreateTime = time.Now().Unix()
  314. // service.CreateOrgApp(app)
  315. // }
  316. // if org_app_9 == nil || org_app_9.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  317. // app, _ := service.GetOrgAppA(0, 9)
  318. // //org, _ := service.GetOrgById(int64(12))
  319. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  320. // if org == nil || org.Id == 0 {
  321. // continue
  322. // }
  323. //
  324. //
  325. // app.OrgId = apps.OrgId
  326. // app.Pid = xt_temp_org_app.Id
  327. //
  328. // app.Creator = int(org.Creator)
  329. // app.Number = 4
  330. // app.Id = 0
  331. //
  332. // app.Status = 1
  333. // app.ModifyTime = time.Now().Unix()
  334. // app.CreateTime = time.Now().Unix()
  335. // service.CreateOrgApp(app)
  336. //
  337. // }
  338. // if org_app_10 == nil || org_app_10.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  339. // app, _ := service.GetOrgAppA(0, 10)
  340. // //org, _ := service.GetOrgById(int64(12))
  341. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  342. // if org == nil || org.Id == 0 {
  343. // continue
  344. // }
  345. //
  346. //
  347. // app.OrgId = apps.OrgId
  348. // app.Creator = int(org.Creator)
  349. // app.Pid = xt_temp_org_app.Id
  350. //
  351. // app.Number = 5
  352. // app.Id = 0
  353. //
  354. // app.Status = 1
  355. // app.ModifyTime = time.Now().Unix()
  356. // app.CreateTime = time.Now().Unix()
  357. // service.CreateOrgApp(app)
  358. // }
  359. // if org_app_11 == nil || org_app_11.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  360. // app, _ := service.GetOrgAppA(0, 11)
  361. // //org, _ := service.GetOrgById(int64(12))
  362. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  363. // if org == nil || org.Id == 0 {
  364. // continue
  365. // }
  366. //
  367. //
  368. // app.OrgId = apps.OrgId
  369. // app.Creator = int(org.Creator)
  370. // app.Pid = xt_temp_org_app.Id
  371. // app.Id = 0
  372. //
  373. // app.Number = 6
  374. // app.Status = 1
  375. // app.ModifyTime = time.Now().Unix()
  376. // app.CreateTime = time.Now().Unix()
  377. // service.CreateOrgApp(app)
  378. // }
  379. // if org_app_12 == nil || org_app_12.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  380. // app, _ := service.GetOrgAppA(0, 12)
  381. // //org, _ := service.GetOrgById(int64(12))
  382. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  383. // if org == nil || org.Id == 0 {
  384. // continue
  385. // }
  386. //
  387. //
  388. // app.OrgId = apps.OrgId
  389. // app.Creator = int(org.Creator)
  390. // app.Pid = xt_temp_org_app.Id
  391. // app.Number = 7
  392. // app.Status = 1
  393. // app.Id = 0
  394. //
  395. // app.ModifyTime = time.Now().Unix()
  396. // app.CreateTime = time.Now().Unix()
  397. // service.CreateOrgApp(app)
  398. // }
  399. // if org_app_13 == nil || org_app_13.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  400. // app, _ := service.GetOrgAppA(0, 13)
  401. // //org, _ := service.GetOrgById(int64(12))
  402. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  403. // if org == nil || org.Id == 0 {
  404. // continue
  405. // }
  406. //
  407. //
  408. // app.OrgId = apps.OrgId
  409. // app.Creator = int(org.Creator)
  410. // app.Pid = xt_temp_org_app.Id
  411. // app.Number = 8
  412. // app.Id = 0
  413. // app.Status = 1
  414. // app.ModifyTime = time.Now().Unix()
  415. // app.CreateTime = time.Now().Unix()
  416. // service.CreateOrgApp(app)
  417. // }
  418. // if org_app_14 == nil || org_app_14.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  419. // app, _ := service.GetOrgAppA(0, 14)
  420. // //org, _ := service.GetOrgById(int64(12))
  421. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  422. // if org == nil || org.Id == 0 {
  423. // continue
  424. // }
  425. //
  426. //
  427. // app.OrgId = apps.OrgId
  428. // app.Creator = int(org.Creator)
  429. // app.Pid = xt_temp_org_app.Id
  430. // app.Id = 0
  431. // app.Number = 9
  432. // app.Status = 1
  433. // app.ModifyTime = time.Now().Unix()
  434. // app.CreateTime = time.Now().Unix()
  435. // service.CreateOrgApp(app)
  436. // }
  437. //
  438. // if org_app_15 == nil || org_app_15.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  439. // app, _ := service.GetOrgAppA(0, 15)
  440. // //org, _ := service.GetOrgById(int64(12))
  441. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  442. // if org == nil || org.Id == 0 {
  443. // continue
  444. // }
  445. //
  446. // app.OrgId = apps.OrgId
  447. // app.Creator = int(org.Creator)
  448. // app.Pid = xt_temp_org_app.Id
  449. // app.Number = 10
  450. // app.Status = 1
  451. // app.Id = 0
  452. // app.ModifyTime = time.Now().Unix()
  453. // app.CreateTime = time.Now().Unix()
  454. // service.CreateOrgApp(app)
  455. // }
  456. //}
  457. //for _, role := range org_role {
  458. // org_ids = append(org_ids, role.OrgId)
  459. //}
  460. //
  461. //
  462. ////创建内置角色
  463. InitRoleAndPurviews(org_ids)
  464. //var org_id int = 0
  465. //管理员
  466. //app_roles := service.GetAllUserRoleByUserTypeOne(org_id)
  467. //for _, item := range app_roles{
  468. // var ids string
  469. // if len(item.RoleIds) == 0{
  470. // ids = strconv.FormatInt( item.RoleId,10)
  471. // }else{
  472. // ids = item.RoleIds + ","+strconv.FormatInt( item.RoleId,10)
  473. // }
  474. // service.UpdateRoleIds(item.Id, ids)
  475. //}
  476. //护士医生
  477. //app_roles2 := service.GetAllUserRoleByUserTypeOther()
  478. // for _, item := range app_roles2{
  479. // var ids string
  480. // if len(item.RoleIds) == 0{
  481. // ids = strconv.FormatInt( item.RoleId,10)
  482. // }else{
  483. // ids = item.RoleIds + ","+strconv.FormatInt( item.RoleId,10)
  484. // }
  485. // if item.UserType == 2{
  486. // role := service.FindRoleByUserTypeOne(item.OrgId)
  487. // ids = ids +"," + strconv.FormatInt(role.Id,10)
  488. //
  489. // } else if item.UserType == 3{
  490. // role := service.FindRoleByUserTypeTwo(item.OrgId)
  491. // ids = ids +"," + strconv.FormatInt(role.Id,10)
  492. // }
  493. // service.UpdateRoleIds(item.Id, ids)
  494. // }
  495. //c.ServeSuccessJSON(map[string]interface{}{
  496. // "msg": "ok",
  497. //})
  498. }
  499. func InitRoleAndPurviews(org_ids []int64) {
  500. roles := LoadRoleConfig("./role.json").Roles
  501. for _, org_id := range org_ids {
  502. app, _ := service.GetOrgAppB(org_id, 3)
  503. role, _ := service.GetSystemRole(org_id)
  504. if app == nil || app.Id == 0 {
  505. continue
  506. }
  507. if len(role) == 0 {
  508. for _, item := range roles {
  509. role := &models.Role{
  510. RoleName: item.RoleName,
  511. RoleIntro: item.RoleIntroduction,
  512. Creator: 0,
  513. OrgId: org_id,
  514. AppId: app.Id,
  515. IsSuperAdmin: false,
  516. Status: 1,
  517. CreateTime: time.Now().Unix(),
  518. ModifyTime: time.Now().Unix(),
  519. Number: item.Number,
  520. IsSystem: item.IsSystem,
  521. }
  522. err := service.CreateOrgRoleB(role)
  523. if err == nil {
  524. purview := &models.RolePurview{
  525. RoleId: role.Id,
  526. OrgId: org_id,
  527. AppId: role.AppId,
  528. PurviewIds: item.PurviewIds,
  529. Status: 1,
  530. CreateTime: time.Now().Unix(),
  531. ModifyTime: time.Now().Unix(),
  532. }
  533. func_purview := &models.SgjUserRoleFuncPurview{
  534. RoleId: role.Id,
  535. OrgId: org_id,
  536. AppId: role.AppId,
  537. PurviewIds: item.FuncIds,
  538. Status: 1,
  539. Ctime: time.Now().Unix(),
  540. Mtime: time.Now().Unix(),
  541. }
  542. service.CreateRolePurviewB(purview)
  543. service.CreateFuncRolePurviewB(func_purview)
  544. }
  545. }
  546. }
  547. }
  548. }
  549. type RoleConfig struct {
  550. Roles []*models.VMUserRoleAndPurview "json:roles"
  551. }
  552. func LoadRoleConfig(dataFile string) *RoleConfig {
  553. var config RoleConfig
  554. _, filename, _, _ := runtime.Caller(1)
  555. datapath := path.Join(path.Dir(filename), dataFile)
  556. config_file, err := os.Open(datapath)
  557. if err != nil {
  558. emit("Failed to open config file '%s': %s\n", datapath, err)
  559. return &config
  560. }
  561. fi, _ := config_file.Stat()
  562. buffer := make([]byte, fi.Size())
  563. _, err = config_file.Read(buffer)
  564. buffer, err = StripComments(buffer) //去掉注释
  565. if err != nil {
  566. emit("Failed to strip comments from json: %s\n", err)
  567. return &config
  568. }
  569. buffer = []byte(os.ExpandEnv(string(buffer))) //特殊
  570. err = json.Unmarshal(buffer, &config) //解析json格式数据
  571. if err != nil {
  572. emit("Failed unmarshalling json: %s\n", err)
  573. return &config
  574. }
  575. return &config
  576. }