public_api_controller.go 17KB

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