user.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_api/models"
  5. "eta/eta_mini_api/models/request"
  6. "eta/eta_mini_api/models/response"
  7. "eta/eta_mini_api/services"
  8. "eta/eta_mini_api/services/go_redis"
  9. "eta/eta_mini_api/utils"
  10. "fmt"
  11. "strings"
  12. "time"
  13. )
  14. type UserController struct {
  15. BaseCommonController
  16. }
  17. type UserAuthController struct {
  18. BaseAuthController
  19. }
  20. // @Title 用户登录接口
  21. // @Description 用户登录
  22. // @Param request body models.LoginReq true "type json string"
  23. // @Success 200 {object} models.LoginResp
  24. // @router /login [post]
  25. func (this *UserAuthController) Login() {
  26. br := new(models.BaseResponse).Init()
  27. defer func() {
  28. this.Data["json"] = br
  29. this.ServeJSON()
  30. }()
  31. session := this.Session
  32. var req request.LoginReq
  33. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  34. if err != nil {
  35. br.Msg = "参数解析失败"
  36. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  37. return
  38. }
  39. switch req.LoginType {
  40. case 1:
  41. if req.Phone == "" {
  42. br.Msg = "请输入手机号"
  43. br.ErrMsg = "请输入手机号"
  44. return
  45. }
  46. if req.SmsCode == "" {
  47. br.Msg = "请输入验证码"
  48. br.ErrMsg = "请输入验证码"
  49. return
  50. }
  51. phone := req.AreaCode + req.Phone
  52. item, err := models.GetMsgCode(phone, req.SmsCode)
  53. if err != nil {
  54. if err.Error() == utils.ErrNoRow() {
  55. br.Msg = "验证码错误,请重新输入"
  56. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  57. return
  58. } else {
  59. br.Msg = "验证码错误,请重新输入"
  60. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  61. return
  62. }
  63. }
  64. if item == nil {
  65. br.Msg = "验证码错误,请重新输入"
  66. return
  67. }
  68. case 2:
  69. if req.Email == "" {
  70. br.Msg = "请输入邮箱"
  71. br.ErrMsg = "请输入邮箱"
  72. return
  73. }
  74. if req.SmsCode == "" {
  75. br.Msg = "请输入验证码"
  76. br.ErrMsg = "请输入验证码"
  77. return
  78. }
  79. item, err := models.GetMsgCode(req.Email, req.SmsCode)
  80. if err != nil {
  81. if err.Error() == utils.ErrNoRow() {
  82. br.Msg = "验证码错误,请重新输入"
  83. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  84. return
  85. } else {
  86. br.Msg = "验证码错误,请重新输入"
  87. br.ErrMsg = "校验验证码失败,Err:" + err.Error()
  88. return
  89. }
  90. }
  91. if item == nil {
  92. br.Msg = "验证码错误,请重新输入"
  93. return
  94. }
  95. }
  96. _, errMsg, err := services.BindUser(session.UnionId, session.OpenId, req.Phone, req.Email, req.AreaCode)
  97. if err != nil && err.Error() != utils.ErrNoRow() {
  98. br.Msg = "登录失败,系统处理中,请稍后重试"
  99. if errMsg != "" {
  100. br.Msg = errMsg
  101. }
  102. br.ErrMsg = "登录失败:" + err.Error()
  103. return
  104. }
  105. br.Msg = "登录成功"
  106. br.Success = true
  107. br.Ret = 200
  108. }
  109. // @Title 获取短信/邮箱验证码
  110. // @Description 用户登录
  111. // @Param request body models.LoginReq true "type json string"
  112. // @Success 200 {object} models.LoginResp
  113. // @router /getVerifyCode [post]
  114. func (this *UserController) GetVerifyCode() {
  115. br := new(models.BaseResponse).Init()
  116. defer func() {
  117. this.Data["json"] = br
  118. this.ServeJSON()
  119. }()
  120. var req request.VerifyCodeReq
  121. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  122. if err != nil {
  123. br.Msg = "参数解析异常!"
  124. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  125. return
  126. }
  127. if req.VerifyType != 1 && req.VerifyType != 2 {
  128. br.Msg = "验证方式有误"
  129. br.ErrMsg = fmt.Sprintf("验证方式异常<%d>", req.VerifyType)
  130. }
  131. code := utils.GetRandDigit(6)
  132. fmt.Println(code)
  133. switch req.VerifyType {
  134. case 1:
  135. if req.AreaCode == "" {
  136. br.Msg = "请选择区号"
  137. return
  138. }
  139. if req.Phone == "" {
  140. br.Msg = "请输入手机号"
  141. return
  142. }
  143. if req.AreaCode == utils.TelAreaCodeHome && !utils.ValidateMobileFormatat(req.Phone) {
  144. br.Msg = "您的手机号输入有误, 请检查"
  145. return
  146. }
  147. phoneKey := utils.CACHE_ACCESS_PHONE_LOGIN_CODE + req.AreaCode + req.Phone
  148. res, _ := go_redis.RedisInt(phoneKey)
  149. if res > 5 {
  150. br.Msg = "验证码发送太频繁,请稍后重试"
  151. return
  152. }
  153. var ok bool
  154. if req.AreaCode == "86" {
  155. ok = services.SendSmsCode(req.Phone, code)
  156. }
  157. if !ok {
  158. br.Msg = "请检查手机号和区号"
  159. br.ErrMsg = "短信验证码发送失败"
  160. return
  161. } else {
  162. item := new(models.MsgCode)
  163. item.OpenId = ""
  164. item.Code = code
  165. item.Mobile = req.AreaCode + req.Phone
  166. item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
  167. item.CreateTime = time.Now()
  168. err = item.Insert()
  169. if err != nil {
  170. br.Msg = "发送失败"
  171. br.ErrMsg = "发送失败,Err:" + err.Error()
  172. return
  173. }
  174. br.Msg = "发送成功"
  175. isExist := go_redis.IsExist(phoneKey)
  176. if isExist {
  177. go_redis.Incr(phoneKey)
  178. } else {
  179. go_redis.SetNX(phoneKey, 1, time.Minute*15)
  180. }
  181. }
  182. case 2:
  183. if req.Email == "" {
  184. br.Msg = "请输入邮箱"
  185. }
  186. if !utils.ValidateEmailFormatat(req.Email) {
  187. br.Msg = "您的邮箱格式输入有误, 请检查"
  188. return
  189. }
  190. emailKey := utils.CACHE_ACCESS_EMAIL_LOGIN_CODE + req.Email
  191. res, _ := go_redis.RedisInt(emailKey)
  192. if res > 5 {
  193. br.Msg = "验证码发送太频繁,请稍后重试"
  194. return
  195. }
  196. date := time.Now()
  197. content := "尊敬的用户:</br>本次请求的验证码为:" + code + "(为了保障您账号的安全性,请在15分钟内完成验证。)</br>东吴期货研究团队 </br>" + fmt.Sprintf("%d年%02d月%02d日", date.Year(), date.Month(), date.Day())
  198. title := "东吴期货登录验证"
  199. result, err := utils.SendEmailByDw(title, content, req.Email)
  200. if err != nil {
  201. br.Msg = "发送失败"
  202. br.ErrMsg = "发送失败,Err:" + err.Error()
  203. return
  204. }
  205. if result {
  206. item := new(models.MsgCode)
  207. item.OpenId = ""
  208. item.Code = code
  209. item.Mobile = req.Email
  210. item.ExpiredIn = time.Now().Add(15 * time.Minute).Unix()
  211. item.CreateTime = time.Now()
  212. err = item.Insert()
  213. if err != nil {
  214. br.Msg = "发送失败"
  215. br.ErrMsg = "发送失败,Err:" + err.Error()
  216. return
  217. }
  218. br.Msg = "发送成功"
  219. isExist := go_redis.IsExist(emailKey)
  220. if isExist {
  221. go_redis.Incr(emailKey)
  222. } else {
  223. go_redis.SetNX(emailKey, 1, time.Minute*15)
  224. }
  225. } else {
  226. br.Msg = "发送失败"
  227. }
  228. }
  229. br.Ret = 200
  230. br.Success = true
  231. br.Msg = "发送成功"
  232. }
  233. // @Title 新增报告浏览记录
  234. // @Description 新增报告浏览记录接口
  235. // @Param request body models.ReportRecordReq true "type json string"
  236. // @Success 200 新增成功
  237. // @router /addReportRecord [post]
  238. func (this *UserAuthController) AddReportRecord() {
  239. br := new(models.BaseResponse).Init()
  240. defer func() {
  241. this.Data["json"] = br
  242. this.ServeJSON()
  243. }()
  244. user := this.User
  245. if user == nil {
  246. br.Msg = "请登录"
  247. br.ErrMsg = "请登录,用户信息为空"
  248. br.Ret = 403
  249. return
  250. }
  251. var req request.ReportRecordReq
  252. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  253. br.Msg = "参数解析异常!"
  254. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  255. return
  256. }
  257. if req.ReportId <= 0 {
  258. br.Msg = "参数错误"
  259. br.ErrMsg = "参数错误,报告id小于等于0"
  260. return
  261. }
  262. reportResp, err := services.GetReportDetail(req.ReportId, user.UserId)
  263. if err != nil {
  264. br.Msg = "添加阅读记录失败"
  265. br.ErrMsg = "获取研报错误,Err:" + err.Error()
  266. return
  267. }
  268. if reportResp.Ret != 200 {
  269. br.Msg = reportResp.Msg
  270. br.ErrMsg = reportResp.ErrMsg
  271. return
  272. }
  273. reportDetail := reportResp.Data.Report
  274. reportPermissionResp, err := services.GetReportChartPermissionList(req.ReportId)
  275. if err != nil {
  276. br.Msg = "添加阅读记录失败"
  277. br.ErrMsg = "获取研报品种错误,Err:" + err.Error()
  278. return
  279. }
  280. if reportPermissionResp.Ret != 200 {
  281. br.Msg = reportPermissionResp.Msg
  282. br.ErrMsg = reportPermissionResp.ErrMsg
  283. return
  284. }
  285. reportPermissionList := reportPermissionResp.Data
  286. chartPermissionList, err := services.GetChartPermissionAllList()
  287. if err != nil {
  288. br.Msg = "添加阅读记录失败"
  289. br.ErrMsg = "获取研报品种列表失败,Err:" + err.Error()
  290. return
  291. }
  292. chartMap := make(map[int]*models.ChartPermission)
  293. for _, permission := range chartPermissionList.Data {
  294. chartMap[permission.ChartPermissionId] = permission
  295. }
  296. curTime := time.Now()
  297. var insertId int64
  298. if req.RecordId == 0 {
  299. // 如果不存在就新增一条记录
  300. permission1Ids := make([]int, 0)
  301. permission2Ids := make([]int, 0)
  302. permissionNames := make([]string, 0)
  303. for _, item := range reportPermissionList {
  304. curPermission := chartMap[item.ChartPermissionId]
  305. permission1Ids = append(permission1Ids, curPermission.ParentId)
  306. permission2Ids = append(permission2Ids, curPermission.ChartPermissionId)
  307. permissionNames = append(permissionNames, curPermission.PermissionName)
  308. }
  309. permission1Ids = utils.Unique(permission1Ids)
  310. permission2Ids = utils.Unique(permission2Ids)
  311. permissionNames = utils.Unique(permissionNames)
  312. userReadRecord := &models.UserReadRecord{
  313. UserId: user.UserId,
  314. ReportId: req.ReportId,
  315. ReportTitle: reportDetail.Title,
  316. ChartPermissionName: strings.Join(permissionNames, ","),
  317. ClassifyId1: reportDetail.ClassifyIdFirst,
  318. ClassifyName1: reportDetail.ClassifyNameFirst,
  319. ClassifyId2: reportDetail.ClassifyIdSecond,
  320. ClassifyName2: reportDetail.ClassifyNameSecond,
  321. Timestamp: int(curTime.Unix()),
  322. CreateTime: curTime,
  323. CreateDate: curTime.Format(utils.FormatDate),
  324. }
  325. insertId, err = userReadRecord.Insert()
  326. if err != nil {
  327. br.Msg = "添加阅读记录失败"
  328. br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
  329. return
  330. }
  331. userReadPermission1 := make([]*models.UserReadPermission1, 0)
  332. for _, id := range permission1Ids {
  333. userReadPermission1 = append(userReadPermission1, &models.UserReadPermission1{
  334. UserReadRecordId: int(insertId),
  335. ChartPermissionId: id,
  336. PermissionName: chartMap[id].PermissionName,
  337. })
  338. }
  339. err = models.UserReadPermission1MultiInsert(userReadPermission1)
  340. if err != nil {
  341. br.Msg = "添加阅读记录失败"
  342. br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
  343. return
  344. }
  345. userReadPermission2 := make([]*models.UserReadPermission2, 0)
  346. for _, id := range permission2Ids {
  347. userReadPermission2 = append(userReadPermission2, &models.UserReadPermission2{
  348. UserReadRecordId: int(insertId),
  349. ChartPermissionId: id,
  350. })
  351. }
  352. err = models.UserReadPermission2MultiInsert(userReadPermission2)
  353. if err != nil {
  354. br.Msg = "添加阅读记录失败"
  355. br.ErrMsg = "添加阅读记录失败,Err:" + err.Error()
  356. return
  357. }
  358. } else {
  359. // 如果存在就计算停留时间
  360. userRecord, err := models.GetUserReadRecordListById(req.RecordId)
  361. if err != nil {
  362. if err.Error() == utils.ErrNoRow() {
  363. br.Msg = "更新阅读记录不存在"
  364. return
  365. }
  366. br.Msg = "更新阅读记录失败"
  367. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  368. return
  369. }
  370. stayTime := curTime.Unix() - int64(userRecord.Timestamp)
  371. stayTimeStr := utils.SecondsToHMS(stayTime)
  372. err = models.UpdateUserReadRecordById(req.RecordId, int(curTime.Unix()), int(stayTime), stayTimeStr)
  373. if err != nil {
  374. br.Msg = "更新阅读记录失败"
  375. br.ErrMsg = "更新阅读记录失败,Err:" + err.Error()
  376. return
  377. }
  378. }
  379. resp := new(response.UserReadRecordResp)
  380. resp.RecordId = insertId
  381. br.Msg = "添加阅读记录成功"
  382. br.Ret = 200
  383. br.Success = true
  384. br.Data = resp
  385. }
  386. // AreaCodeList
  387. // @Title 手机号区号列表
  388. // @Description 手机号区号列表
  389. // @Success 200 Ret=200 获取成功
  390. // @router /area_code/list [get]
  391. func (this *UserController) AreaCodeList() {
  392. br := new(models.BaseResponse).Init()
  393. defer func() {
  394. if br.ErrMsg == "" {
  395. br.IsSendEmail = false
  396. }
  397. this.Data["json"] = br
  398. this.ServeJSON()
  399. }()
  400. resp := make([]response.AreaCodeListResp, 0)
  401. confValue, e := models.GetConfigDetailByCode(models.ConfAreaCodeListKey)
  402. if e != nil {
  403. br.Msg = "获取失败"
  404. br.ErrMsg = "获取手机号区号配置失败, Err: " + e.Error()
  405. return
  406. }
  407. if confValue == "" {
  408. br.Msg = "获取失败"
  409. br.ErrMsg = "手机号区号配置为空"
  410. return
  411. }
  412. if e := json.Unmarshal([]byte(confValue), &resp); e != nil {
  413. br.Msg = "获取失败"
  414. br.ErrMsg = "手机号区号配置有误"
  415. return
  416. }
  417. br.Data = resp
  418. br.Ret = 200
  419. br.Success = true
  420. br.Msg = "获取成功"
  421. }
  422. // 用户品种信息
  423. // @Title 用户信息
  424. // @Description 用户信息
  425. // @Success 200 Ret=200 获取成功
  426. // @router /info [get]
  427. func (this *UserAuthController) Info() {
  428. br := new(models.BaseResponse).Init()
  429. defer func() {
  430. this.Data["json"] = br
  431. this.ServeJSON()
  432. }()
  433. user := this.User
  434. if user == nil {
  435. user = &models.User{}
  436. }
  437. seller, err := models.GetSellerById(user.SellerId)
  438. if err != nil && err.Error() != utils.ErrNoRow() {
  439. br.Msg = "查询失败"
  440. br.ErrMsg = "查询失败,系统异常,Err:" + err.Error()
  441. return
  442. }
  443. private, err := services.GetPrivateChartPermissionList()
  444. if err != nil {
  445. br.Msg = "查询失败"
  446. br.ErrMsg = "查询失败,系统异常,Err:" + err.Error()
  447. return
  448. }
  449. if private.Ret != 200 {
  450. br.Msg = private.Msg
  451. br.ErrMsg = private.ErrMsg
  452. return
  453. }
  454. userPermissionIds, err := models.GetChartPermissionIdByUserId(user.UserId)
  455. if err != nil {
  456. br.Msg = "查看权限失败"
  457. br.ErrMsg = "查看权限失败,系统异常,Err:" + err.Error()
  458. return
  459. }
  460. userPermissionMap := make(map[int]struct{})
  461. for _, v := range userPermissionIds {
  462. userPermissionMap[v] = struct{}{}
  463. }
  464. if seller == nil {
  465. // 添加默认的销售电话
  466. seller = &models.SysUser{
  467. Phone: utils.DefaultPhone,
  468. AreaCode: utils.DefaultAreaCode,
  469. }
  470. }
  471. var hasPermission string
  472. for _, v := range private.Data {
  473. for _, vv := range v.Child {
  474. if _, ok := userPermissionMap[vv.ChartPermissionId]; ok {
  475. hasPermission = "私有权限"
  476. break
  477. }
  478. }
  479. }
  480. if hasPermission == "" || user.Status == utils.UserStatusNo {
  481. hasPermission = "公有权限"
  482. }
  483. miniconf, err := models.GetMiniConf()
  484. if err != nil {
  485. br.Msg = "获取配置失败"
  486. br.ErrMsg = "获取配置失败,系统异常,Err:" + err.Error()
  487. return
  488. }
  489. userView := &models.UserView{
  490. Headimgurl: miniconf["Logo"],
  491. RealName: user.RealName,
  492. Phone: user.Phone,
  493. AreaCode: user.AreaCode,
  494. SellerName: seller.SysRealName,
  495. SellerPhone: seller.Phone,
  496. SellerAreaCode: seller.AreaCode,
  497. HasPermission: hasPermission,
  498. Email: user.Email,
  499. Componey: user.Company,
  500. IsRegistered: user.IsRegistered,
  501. Status: user.Status,
  502. ValidEndTime: user.ValidEndTime.Format(utils.FormatDate),
  503. }
  504. if user.ValidEndTime.IsZero() {
  505. userView.ValidEndTime = ""
  506. }
  507. br.Data = userView
  508. br.Msg = "查询成功"
  509. br.Success = true
  510. br.Ret = 200
  511. }
  512. // PermissionList
  513. // @Title 用户个人权限列表
  514. // @Description 用户个人权限列表
  515. // @Param request body models.LoginReq true "type json string"
  516. // @Success 200 {object} []models.ChartPermissionTreeView
  517. // @router /permission/list [get]
  518. func (this *UserAuthController) PermissionList() {
  519. br := new(models.BaseResponse).Init()
  520. defer func() {
  521. this.Data["json"] = br
  522. this.ServeJSON()
  523. }()
  524. user := this.User
  525. permissionIds, err := models.GetChartPermissionIdByUserId(user.UserId)
  526. if err != nil {
  527. br.Msg = "查询用户权限失败"
  528. br.ErrMsg = "查询用户权限失败,系统异常,Err:" + err.Error()
  529. return
  530. }
  531. userPermissionMap := make(map[int]struct{})
  532. for _, v := range permissionIds {
  533. userPermissionMap[v] = struct{}{}
  534. }
  535. // 查询公有权限
  536. var publicView []*models.ChartPermissionTreeView
  537. publicResp, err := services.GetPublicChartPermissionList()
  538. if err != nil {
  539. br.Msg = "查询公有权限失败"
  540. br.ErrMsg = "查询私有权限失败,系统异常,Err:" + err.Error()
  541. }
  542. if publicResp.Ret != 200 {
  543. br.Msg = publicResp.Msg
  544. br.ErrMsg = publicResp.ErrMsg
  545. return
  546. }
  547. publicView = publicResp.Data
  548. // 如果是正式用户,则查询私有权限
  549. var privateView []*models.ChartPermissionTreeView
  550. if user.Status == utils.UserStatusFormal {
  551. privateResp, err := services.GetPrivateChartPermissionList()
  552. if err != nil {
  553. br.Msg = "查询私有权限失败"
  554. br.ErrMsg = "查询私有权限失败,系统异常,Err:" + err.Error()
  555. }
  556. if privateResp.Ret != 200 {
  557. br.Msg = privateResp.Msg
  558. br.ErrMsg = privateResp.ErrMsg
  559. return
  560. }
  561. for _, v := range privateResp.Data {
  562. IsAdd := false
  563. curPermissionView := &models.ChartPermissionTreeView{
  564. ChartPermissionId: v.ChartPermissionId,
  565. PermissionName: v.PermissionName,
  566. IsPublic: v.IsPublic,
  567. Sort: v.Sort,
  568. Child: make([]*models.ChartPermissionTreeView, 0),
  569. }
  570. for _, vv := range v.Child {
  571. if _, ok := userPermissionMap[vv.ChartPermissionId]; ok {
  572. curPermissionView.Child = append(curPermissionView.Child, vv)
  573. IsAdd = true
  574. }
  575. }
  576. if IsAdd {
  577. privateView = append(privateView, curPermissionView)
  578. }
  579. }
  580. }
  581. privateAloneView := make([]*models.ChartPermissionTreeView, 0)
  582. for _, vi := range privateView {
  583. isHas := false
  584. for _, vb := range publicView {
  585. if vi.ChartPermissionId == vb.ChartPermissionId {
  586. isHas = true
  587. vb.Child = append(vb.Child, vi.Child...)
  588. break
  589. }
  590. }
  591. if !isHas {
  592. privateAloneView = append(privateAloneView, vi)
  593. }
  594. }
  595. publicView = append(publicView, privateAloneView...)
  596. br.Data = publicView
  597. br.Msg = "查询成功"
  598. br.Success = true
  599. br.Ret = 200
  600. }
  601. // PermissionList
  602. // @Title 用户已购权限列表
  603. // @Description 用户已购权限列表
  604. // @Param request body models.LoginReq true "type json string"
  605. // @Success 200 {object} []models.ChartPermissionTreeView
  606. // @router /purchased/list [get]
  607. func (this *UserAuthController) PurchasedPermission() {
  608. br := new(models.BaseResponse).Init()
  609. defer func() {
  610. this.Data["json"] = br
  611. this.ServeJSON()
  612. }()
  613. user := this.User
  614. permissionIds, err := models.GetChartPermissionIdByUserId(user.UserId)
  615. if err != nil {
  616. br.Msg = "查询用户权限失败"
  617. br.ErrMsg = "查询用户权限失败,系统异常,Err:" + err.Error()
  618. return
  619. }
  620. permissionMap := make(map[int]struct{})
  621. for _, v := range permissionIds {
  622. permissionMap[v] = struct{}{}
  623. }
  624. // 查询私有权限
  625. privateResp, err := services.GetPrivateChartPermissionList()
  626. if err != nil {
  627. br.Msg = "查询私有权限失败"
  628. br.ErrMsg = "查询私有权限失败,系统异常,Err:" + err.Error()
  629. }
  630. if privateResp.Ret != 200 {
  631. br.Msg = privateResp.Msg
  632. br.ErrMsg = privateResp.ErrMsg
  633. return
  634. }
  635. var privateView []*models.ChartPermissionTreeView
  636. for _, v := range privateResp.Data {
  637. IsAdd := false
  638. curPermissionView := &models.ChartPermissionTreeView{
  639. ChartPermissionId: v.ChartPermissionId,
  640. PermissionName: v.PermissionName,
  641. IsPublic: v.IsPublic,
  642. Sort: v.Sort,
  643. Child: make([]*models.ChartPermissionTreeView, 0),
  644. }
  645. for _, vv := range v.Child {
  646. if _, ok := permissionMap[vv.ChartPermissionId]; ok && vv.ParentId != 0 {
  647. curPermissionView.Child = append(curPermissionView.Child, vv)
  648. IsAdd = true
  649. }
  650. }
  651. if IsAdd {
  652. privateView = append(privateView, curPermissionView)
  653. }
  654. }
  655. br.Data = privateView
  656. br.Msg = "查询成功"
  657. br.Success = true
  658. br.Ret = 200
  659. }