public_api_controller.go 16KB

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