public_api_controller.go 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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. //var org_ids []int64
  145. //for _, apps := range org_app {
  146. // xt_org_app, _ := service.GetAppByType(int64(apps.OrgId), 3)
  147. // scrm_org_app, _ := service.GetAppByType(int64(apps.OrgId), 1)
  148. // cdm_org_app, _ := service.GetAppByType(int64(apps.OrgId), 4)
  149. // mall_org_app, _ := service.GetAppByType(int64(apps.OrgId), 5)
  150. // org_app_6, _ := service.GetAppByType(int64(apps.OrgId), 6)
  151. // org_app_7, _ := service.GetAppByType(int64(apps.OrgId), 7)
  152. // org_app_8, _ := service.GetAppByType(int64(apps.OrgId), 8)
  153. // org_app_9, _ := service.GetAppByType(int64(apps.OrgId), 9)
  154. // org_app_10, _ := service.GetAppByType(int64(apps.OrgId), 10)
  155. // org_app_11, _ := service.GetAppByType(int64(apps.OrgId), 11)
  156. // org_app_12, _ := service.GetAppByType(int64(apps.OrgId), 12)
  157. // org_app_13, _ := service.GetAppByType(int64(apps.OrgId), 13)
  158. // org_app_14, _ := service.GetAppByType(int64(apps.OrgId), 14)
  159. // org_app_15, _ := service.GetAppByType(int64(apps.OrgId), 15)
  160. //
  161. // var xt_temp_org_app *models.OrgApp
  162. // var mall_temp_org_app *models.OrgApp
  163. //
  164. // if xt_org_app == nil || xt_org_app.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  165. //
  166. // app, _ := service.GetOrgAppA(0, 3)
  167. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  168. // if org == nil || org.Id == 0 {
  169. // continue
  170. // }
  171. //
  172. // app.Id = 0
  173. // app.OrgId = apps.OrgId
  174. // app.Creator = int(org.Creator)
  175. // app.Number = 0
  176. // app.Status = 1
  177. // app.ModifyTime = time.Now().Unix()
  178. // app.CreateTime = time.Now().Unix()
  179. // service.CreateOrgApp(app)
  180. // xt_temp_org_app = app
  181. // } else {
  182. //
  183. // xt_temp_org_app = xt_org_app
  184. //
  185. // }
  186. //
  187. // if scrm_org_app == nil || scrm_org_app.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  188. //
  189. // app, _ := service.GetOrgAppA(0, 1)
  190. // //org, _ := service.GetOrgById(int64(12))
  191. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  192. // if org == nil || org.Id == 0 {
  193. // continue
  194. // }
  195. //
  196. //
  197. // app.OrgId = apps.OrgId
  198. // app.Id = 0
  199. //
  200. // app.Creator = int(org.Creator)
  201. // app.Number = 0
  202. // app.Status = 1
  203. // app.ModifyTime = time.Now().Unix()
  204. // app.CreateTime = time.Now().Unix()
  205. // service.CreateOrgApp(app)
  206. //
  207. // }
  208. //
  209. // if cdm_org_app == nil || cdm_org_app.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  210. // app, _ := service.GetOrgAppA(0, 4)
  211. // //org, _ := service.GetOrgById(int64(12))
  212. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  213. // if org == nil || org.Id == 0 {
  214. // continue
  215. // }
  216. //
  217. //
  218. // app.OrgId = apps.OrgId
  219. // app.Creator = int(org.Creator)
  220. // app.Number = 0
  221. // app.Id = 0
  222. //
  223. // app.Status = 1
  224. // app.ModifyTime = time.Now().Unix()
  225. // app.CreateTime = time.Now().Unix()
  226. // service.CreateOrgApp(app)
  227. // }
  228. //
  229. // if mall_org_app == nil || mall_org_app.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  230. // app, _ := service.GetOrgAppA(0, 5)
  231. // //org, _ := service.GetOrgById(int64(12))
  232. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  233. //
  234. // if org == nil || org.Id == 0 {
  235. // continue
  236. // }
  237. //
  238. // app.OrgId = apps.OrgId
  239. // app.Creator = int(org.Creator)
  240. // app.Number = 0
  241. // app.Id = 0
  242. //
  243. // app.Status = 1
  244. // app.ModifyTime = time.Now().Unix()
  245. // app.CreateTime = time.Now().Unix()
  246. // service.CreateOrgApp(app)
  247. // mall_temp_org_app = app
  248. // } else {
  249. // mall_temp_org_app = mall_org_app
  250. // }
  251. //
  252. // if org_app_6 == nil || org_app_6.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  253. // app, _ := service.GetOrgAppA(0, 6)
  254. // //org, _ := service.GetOrgById(int64(12))
  255. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  256. // if org == nil || org.Id == 0 {
  257. // continue
  258. // }
  259. //
  260. //
  261. // app.OrgId = apps.OrgId
  262. // app.Creator = int(org.Creator)
  263. // app.Number = 1
  264. // app.Id = 0
  265. //
  266. // app.Status = 1
  267. // app.ModifyTime = time.Now().Unix()
  268. // app.CreateTime = time.Now().Unix()
  269. // app.Pid = xt_temp_org_app.Id
  270. // service.CreateOrgApp(app)
  271. //
  272. // }
  273. // if org_app_7 == nil || org_app_7.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  274. // app, _ := service.GetOrgAppA(0, 7)
  275. // //org, _ := service.GetOrgById(int64(12))
  276. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  277. // if org == nil || org.Id == 0 {
  278. // continue
  279. // }
  280. //
  281. //
  282. // app.OrgId = apps.OrgId
  283. // app.Creator = int(org.Creator)
  284. // app.Number = 2
  285. // app.Status = 2
  286. // app.Id = 0
  287. //
  288. // app.ModifyTime = time.Now().Unix()
  289. // app.CreateTime = time.Now().Unix()
  290. // app.Pid = xt_temp_org_app.Id
  291. //
  292. // service.CreateOrgApp(app)
  293. // }
  294. // if org_app_8 == nil || org_app_8.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  295. // app, _ := service.GetOrgAppA(0, 8)
  296. // //org, _ := service.GetOrgById(int64(12))
  297. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  298. // if org == nil || org.Id == 0 {
  299. // continue
  300. // }
  301. //
  302. //
  303. // app.OrgId = apps.OrgId
  304. // app.Creator = int(org.Creator)
  305. // app.Number = 3
  306. // app.Id = 0
  307. //
  308. // app.Pid = mall_temp_org_app.Id
  309. //
  310. // app.Status = 1
  311. // app.ModifyTime = time.Now().Unix()
  312. // app.CreateTime = time.Now().Unix()
  313. // service.CreateOrgApp(app)
  314. // }
  315. // if org_app_9 == nil || org_app_9.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  316. // app, _ := service.GetOrgAppA(0, 9)
  317. // //org, _ := service.GetOrgById(int64(12))
  318. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  319. // if org == nil || org.Id == 0 {
  320. // continue
  321. // }
  322. //
  323. //
  324. // app.OrgId = apps.OrgId
  325. // app.Pid = xt_temp_org_app.Id
  326. //
  327. // app.Creator = int(org.Creator)
  328. // app.Number = 4
  329. // app.Id = 0
  330. //
  331. // app.Status = 1
  332. // app.ModifyTime = time.Now().Unix()
  333. // app.CreateTime = time.Now().Unix()
  334. // service.CreateOrgApp(app)
  335. //
  336. // }
  337. // if org_app_10 == nil || org_app_10.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  338. // app, _ := service.GetOrgAppA(0, 10)
  339. // //org, _ := service.GetOrgById(int64(12))
  340. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  341. // if org == nil || org.Id == 0 {
  342. // continue
  343. // }
  344. //
  345. //
  346. // app.OrgId = apps.OrgId
  347. // app.Creator = int(org.Creator)
  348. // app.Pid = xt_temp_org_app.Id
  349. //
  350. // app.Number = 5
  351. // app.Id = 0
  352. //
  353. // app.Status = 1
  354. // app.ModifyTime = time.Now().Unix()
  355. // app.CreateTime = time.Now().Unix()
  356. // service.CreateOrgApp(app)
  357. // }
  358. // if org_app_11 == nil || org_app_11.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  359. // app, _ := service.GetOrgAppA(0, 11)
  360. // //org, _ := service.GetOrgById(int64(12))
  361. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  362. // if org == nil || org.Id == 0 {
  363. // continue
  364. // }
  365. //
  366. //
  367. // app.OrgId = apps.OrgId
  368. // app.Creator = int(org.Creator)
  369. // app.Pid = xt_temp_org_app.Id
  370. // app.Id = 0
  371. //
  372. // app.Number = 6
  373. // app.Status = 1
  374. // app.ModifyTime = time.Now().Unix()
  375. // app.CreateTime = time.Now().Unix()
  376. // service.CreateOrgApp(app)
  377. // }
  378. // if org_app_12 == nil || org_app_12.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  379. // app, _ := service.GetOrgAppA(0, 12)
  380. // //org, _ := service.GetOrgById(int64(12))
  381. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  382. // if org == nil || org.Id == 0 {
  383. // continue
  384. // }
  385. //
  386. //
  387. // app.OrgId = apps.OrgId
  388. // app.Creator = int(org.Creator)
  389. // app.Pid = xt_temp_org_app.Id
  390. // app.Number = 7
  391. // app.Status = 1
  392. // app.Id = 0
  393. //
  394. // app.ModifyTime = time.Now().Unix()
  395. // app.CreateTime = time.Now().Unix()
  396. // service.CreateOrgApp(app)
  397. // }
  398. // if org_app_13 == nil || org_app_13.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  399. // app, _ := service.GetOrgAppA(0, 13)
  400. // //org, _ := service.GetOrgById(int64(12))
  401. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  402. // if org == nil || org.Id == 0 {
  403. // continue
  404. // }
  405. //
  406. //
  407. // app.OrgId = apps.OrgId
  408. // app.Creator = int(org.Creator)
  409. // app.Pid = xt_temp_org_app.Id
  410. // app.Number = 8
  411. // app.Id = 0
  412. // app.Status = 1
  413. // app.ModifyTime = time.Now().Unix()
  414. // app.CreateTime = time.Now().Unix()
  415. // service.CreateOrgApp(app)
  416. // }
  417. // if org_app_14 == nil || org_app_14.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  418. // app, _ := service.GetOrgAppA(0, 14)
  419. // //org, _ := service.GetOrgById(int64(12))
  420. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  421. // if org == nil || org.Id == 0 {
  422. // continue
  423. // }
  424. //
  425. //
  426. // app.OrgId = apps.OrgId
  427. // app.Creator = int(org.Creator)
  428. // app.Pid = xt_temp_org_app.Id
  429. // app.Id = 0
  430. // app.Number = 9
  431. // app.Status = 1
  432. // app.ModifyTime = time.Now().Unix()
  433. // app.CreateTime = time.Now().Unix()
  434. // service.CreateOrgApp(app)
  435. // }
  436. //
  437. // if org_app_15 == nil || org_app_15.Id == 0 { //判断之前是否有创建该app类型,如果没有则插入数据
  438. // app, _ := service.GetOrgAppA(0, 15)
  439. // //org, _ := service.GetOrgById(int64(12))
  440. // org, _ := service.GetOrgByIdB(int64(apps.OrgId))
  441. // if org == nil || org.Id == 0 {
  442. // continue
  443. // }
  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. if app == nil || app.Id == 0 {
  504. continue
  505. }
  506. for _, item := range roles {
  507. role := &models.Role{
  508. RoleName: item.RoleName,
  509. RoleIntro: item.RoleIntroduction,
  510. Creator: 0,
  511. OrgId: org_id,
  512. AppId: app.Id,
  513. IsSuperAdmin: false,
  514. Status: 1,
  515. CreateTime: time.Now().Unix(),
  516. ModifyTime: time.Now().Unix(),
  517. Number: item.Number,
  518. IsSystem: item.IsSystem,
  519. }
  520. err := service.CreateOrgRoleB(role)
  521. if err == nil {
  522. purview := &models.RolePurview{
  523. RoleId: role.Id,
  524. OrgId: org_id,
  525. AppId: role.AppId,
  526. PurviewIds: item.PurviewIds,
  527. Status: 1,
  528. CreateTime: time.Now().Unix(),
  529. ModifyTime: time.Now().Unix(),
  530. }
  531. func_purview := &models.SgjUserRoleFuncPurview{
  532. RoleId: role.Id,
  533. OrgId: org_id,
  534. AppId: role.AppId,
  535. PurviewIds: item.FuncIds,
  536. Status: 1,
  537. Ctime: time.Now().Unix(),
  538. Mtime: time.Now().Unix(),
  539. }
  540. service.CreateRolePurviewB(purview)
  541. service.CreateFuncRolePurviewB(func_purview)
  542. }
  543. }
  544. }
  545. }
  546. type RoleConfig struct {
  547. Roles []*models.VMUserRoleAndPurview "json:roles"
  548. }
  549. func LoadRoleConfig(dataFile string) *RoleConfig {
  550. var config RoleConfig
  551. _, filename, _, _ := runtime.Caller(1)
  552. datapath := path.Join(path.Dir(filename), dataFile)
  553. config_file, err := os.Open(datapath)
  554. if err != nil {
  555. emit("Failed to open config file '%s': %s\n", datapath, err)
  556. return &config
  557. }
  558. fi, _ := config_file.Stat()
  559. buffer := make([]byte, fi.Size())
  560. _, err = config_file.Read(buffer)
  561. buffer, err = StripComments(buffer) //去掉注释
  562. if err != nil {
  563. emit("Failed to strip comments from json: %s\n", err)
  564. return &config
  565. }
  566. buffer = []byte(os.ExpandEnv(string(buffer))) //特殊
  567. err = json.Unmarshal(buffer, &config) //解析json格式数据
  568. if err != nil {
  569. emit("Failed unmarshalling json: %s\n", err)
  570. return &config
  571. }
  572. return &config
  573. }