广告平台(总站长使用)
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

650 lines
18 KiB

  1. package hdl
  2. import (
  3. "applet/app/e"
  4. "applet/app/enum"
  5. "applet/app/lib/validate"
  6. "applet/app/md"
  7. "applet/app/svc"
  8. "applet/app/utils"
  9. db "code.fnuoos.com/zhimeng/model.git/src"
  10. "code.fnuoos.com/zhimeng/model.git/src/super/implement"
  11. "code.fnuoos.com/zhimeng/model.git/src/super/model"
  12. "github.com/gin-gonic/gin"
  13. "time"
  14. )
  15. // PermissionGroupList
  16. // @Summary 权限组列表
  17. // @Tags 权限管理
  18. // @Description 权限管理-权限组列表
  19. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  20. // @Accept json
  21. // @Produce json
  22. // @param adm_id query string true "管理员id"
  23. // @Success 200 {string} "具体看返回内容"
  24. // @Failure 400 {object} md.Response "具体错误"
  25. // @Router /role/permissionGroupList [GET]
  26. func PermissionGroupList(c *gin.Context) {
  27. roleId := c.DefaultQuery("role_id", "")
  28. engine := db.Db
  29. qrcodeWithBatchRecordsDb := implement.NewPermissionGroupDb(engine)
  30. groupList, err := qrcodeWithBatchRecordsDb.FindPermissionGroup()
  31. if err != nil {
  32. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  33. return
  34. }
  35. roleDb := implement.NewRoleDb(engine, utils.StrToInt(roleId))
  36. list, _, err := roleDb.FindPermissionGroupByRole(utils.StrToInt(roleId))
  37. if err != nil {
  38. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  39. return
  40. }
  41. var isHasPermissionGroupId []string
  42. for _, v := range list {
  43. isHasPermissionGroupId = append(isHasPermissionGroupId, utils.IntToStr(v.PermissionGroup.Id))
  44. }
  45. var tempRespMap = map[string]*md.PermissionGroupListResp{}
  46. var tempRespMapKeys []string
  47. for _, v := range *groupList {
  48. isCheck := false
  49. if utils.InArr(utils.IntToStr(v.Id), isHasPermissionGroupId) {
  50. isCheck = true
  51. }
  52. tempRespMap[utils.IntToStr(v.Id)] = &md.PermissionGroupListResp{
  53. Id: v.Id,
  54. Name: v.Name,
  55. Key: v.Key,
  56. State: v.State,
  57. ParentId: v.ParentId,
  58. CreateAt: v.CreateAt,
  59. UpdateAt: v.UpdateAt,
  60. IsCheck: isCheck,
  61. }
  62. tempRespMapKeys = append(tempRespMapKeys, utils.IntToStr(v.Id))
  63. }
  64. for _, v := range tempRespMap {
  65. if v.ParentId != 0 && tempRespMap[utils.IntToStr(v.ParentId)].ParentId != 0 {
  66. tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList = append(tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList, *v)
  67. }
  68. }
  69. for _, v := range tempRespMap {
  70. if v.ParentId != 0 && tempRespMap[utils.IntToStr(v.ParentId)].ParentId == 0 {
  71. tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList = append(tempRespMap[utils.IntToStr(v.ParentId)].SubPermissionGroupList, *v)
  72. }
  73. }
  74. var resp []*md.PermissionGroupListResp
  75. for _, v := range tempRespMapKeys {
  76. if tempRespMap[v].ParentId == 0 {
  77. resp = append(resp, tempRespMap[v])
  78. }
  79. }
  80. e.OutSuc(c, map[string]interface{}{
  81. "list": resp,
  82. "state": []map[string]interface{}{
  83. {
  84. "name": enum.PermissionGroupState(enum.PermissionGroupStateForNormal).String(),
  85. "value": enum.PermissionGroupStateForNormal,
  86. },
  87. {
  88. "name": enum.PermissionGroupState(enum.PermissionGroupStateForDiscard).String(),
  89. "value": enum.PermissionGroupStateForDiscard,
  90. },
  91. },
  92. }, nil)
  93. return
  94. }
  95. // RoleList
  96. // @Summary 角色列表
  97. // @Tags 权限管理
  98. // @Description 权限管理-角色列表
  99. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  100. // @Accept json
  101. // @Produce json
  102. // @Success 200 {string} "具体看返回内容"
  103. // @Failure 400 {object} md.Response "具体错误"
  104. // @Router /role/roleList [GET]
  105. func RoleList(c *gin.Context) {
  106. engine := db.Db
  107. roleDb := implement.NewRoleDb(engine, 0)
  108. roleList, err := roleDb.FindRole()
  109. if err != nil {
  110. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  111. return
  112. }
  113. adminRoleDb := implement.NewAdminRoleDb(engine)
  114. adminDb := implement.NewAdminDb(engine)
  115. var result []*md.RoleListResp
  116. for _, v := range *roleList {
  117. var temp md.RoleListResp
  118. temp.Data = v
  119. adminRoles, err1 := adminRoleDb.FindAdminRoleByRoleId(v.Id)
  120. if err1 != nil {
  121. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  122. return
  123. }
  124. for _, adminRole := range *adminRoles {
  125. admin, err2 := adminDb.GetAdmin(adminRole.AdmId)
  126. if err2 != nil {
  127. e.OutErr(c, e.ERR_DB_ORM, err2.Error())
  128. return
  129. }
  130. temp.AdminList = append(temp.AdminList, struct {
  131. Name string `json:"name"`
  132. }{
  133. Name: admin.Username,
  134. })
  135. }
  136. result = append(result, &temp)
  137. }
  138. e.OutSuc(c, map[string]interface{}{
  139. "list": result,
  140. "state": []map[string]interface{}{
  141. {
  142. "name": enum.RoleState(enum.RoleStateForNormal).String(),
  143. "value": enum.RoleStateForNormal,
  144. },
  145. {
  146. "name": enum.RoleState(enum.RoleStateForFreeze).String(),
  147. "value": enum.RoleStateForFreeze,
  148. },
  149. },
  150. }, nil)
  151. return
  152. }
  153. // AddRole
  154. // @Summary 添加角色
  155. // @Tags 权限管理
  156. // @Description 权限管理-添加角色
  157. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  158. // @Accept json
  159. // @Produce json
  160. // @Param args body md.AddRoleReq true "请求参数"
  161. // @Success 200 {string} "success"
  162. // @Failure 400 {object} md.Response "具体错误"
  163. // @Router /role/addRole [POST]
  164. func AddRole(c *gin.Context) {
  165. var req md.AddRoleReq
  166. err := c.ShouldBindJSON(&req)
  167. if err != nil {
  168. err = validate.HandleValidateErr(err)
  169. err1 := err.(e.E)
  170. e.OutErr(c, err1.Code, err1.Error())
  171. return
  172. }
  173. engine := db.Db
  174. roleDb := implement.NewRoleDb(engine, 0)
  175. now := time.Now()
  176. _, err = roleDb.RoleInsert(&model.Role{
  177. Name: req.Name,
  178. State: enum.RoleStateForNormal,
  179. Memo: req.Memo,
  180. CreateAt: now.Format("2006-01-02 15:04:05"),
  181. UpdateAt: now.Format("2006-01-02 15:04:05"),
  182. })
  183. if err != nil {
  184. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  185. return
  186. }
  187. e.OutSuc(c, "success", nil)
  188. return
  189. }
  190. // UpdateRole
  191. // @Summary 修改角色
  192. // @Tags 权限管理
  193. // @Description 权限管理-修改角色
  194. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  195. // @Accept json
  196. // @Produce json
  197. // @Param args body md.UpdateRoleReq true "请求参数"
  198. // @Success 200 {string} "success"
  199. // @Failure 400 {object} md.Response "具体错误"
  200. // @Router /role/updateRole [POST]
  201. func UpdateRole(c *gin.Context) {
  202. var req md.UpdateRoleReq
  203. err := c.ShouldBindJSON(&req)
  204. if err != nil {
  205. err = validate.HandleValidateErr(err)
  206. err1 := err.(e.E)
  207. e.OutErr(c, err1.Code, err1.Error())
  208. return
  209. }
  210. engine := db.Db
  211. roleDb := implement.NewRoleDb(engine, req.RoleId)
  212. role, err := roleDb.GetRole()
  213. if err != nil {
  214. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  215. return
  216. }
  217. if role == nil {
  218. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  219. return
  220. }
  221. role.Name = req.Name
  222. role.Memo = req.Memo
  223. _, err = roleDb.UpdateRole(role, "name", "memo")
  224. if err != nil {
  225. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  226. return
  227. }
  228. e.OutSuc(c, "success", nil)
  229. return
  230. }
  231. // RoleBindPermissionGroup
  232. // @Summary 角色绑定权限组
  233. // @Tags 权限管理
  234. // @Description 权限管理-角色绑定权限组
  235. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  236. // @Accept json
  237. // @Produce json
  238. // @Param args body md.RoleBindPermissionGroupReq true "请求参数"
  239. // @Success 200 {string} "success"
  240. // @Failure 400 {object} md.Response "具体错误"
  241. // @Router /role/roleBindPermissionGroup [POST]
  242. func RoleBindPermissionGroup(c *gin.Context) {
  243. var req md.RoleBindPermissionGroupReq
  244. err := c.ShouldBindJSON(&req)
  245. if err != nil {
  246. err = validate.HandleValidateErr(err)
  247. err1 := err.(e.E)
  248. e.OutErr(c, err1.Code, err1.Error())
  249. return
  250. }
  251. err = svc.RoleBindPermissionGroup(c, req)
  252. if err != nil {
  253. e.OutErr(c, e.ERR, err.Error())
  254. return
  255. }
  256. e.OutSuc(c, "success", nil)
  257. return
  258. }
  259. // UpdateRoleState
  260. // @Summary 修改角色状态
  261. // @Tags 权限管理
  262. // @Description 权限管理-修改角色状态
  263. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  264. // @Accept json
  265. // @Produce json
  266. // @Param args body md.UpdateRoleStateReq true "请求参数"
  267. // @Success 200 {string} "success"
  268. // @Failure 400 {object} md.Response "具体错误"
  269. // @Router /role/updateRole [POST]
  270. func UpdateRoleState(c *gin.Context) {
  271. var req md.UpdateRoleStateReq
  272. err := c.ShouldBindJSON(&req)
  273. if err != nil {
  274. err = validate.HandleValidateErr(err)
  275. err1 := err.(e.E)
  276. e.OutErr(c, err1.Code, err1.Error())
  277. return
  278. }
  279. engine := db.Db
  280. roleDb := implement.NewRoleDb(engine, req.RoleId)
  281. role, err := roleDb.GetRole()
  282. if err != nil {
  283. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  284. return
  285. }
  286. if role == nil {
  287. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  288. return
  289. }
  290. role.State = req.State
  291. _, err = roleDb.UpdateRole(role, "state")
  292. if err != nil {
  293. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  294. return
  295. }
  296. e.OutSuc(c, "success", nil)
  297. return
  298. }
  299. // DeleteRole
  300. // @Summary 删除角色
  301. // @Tags 权限管理
  302. // @Description 权限管理-删除角色
  303. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  304. // @Accept json
  305. // @Produce json
  306. // @Param args body md.UpdateRoleStateReq true "请求参数"
  307. // @Success 200 {string} "success"
  308. // @Failure 400 {object} md.Response "具体错误"
  309. // @Router /role/deleteRole/{$id} [DELETE]
  310. func DeleteRole(c *gin.Context) {
  311. id := c.Param("id")
  312. engine := db.Db
  313. roleDb := implement.NewRoleDb(engine, utils.StrToInt(id))
  314. role, err := roleDb.GetRole()
  315. if err != nil {
  316. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  317. return
  318. }
  319. if role == nil {
  320. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  321. return
  322. }
  323. err = svc.DeleteRole(c, utils.StrToInt(id))
  324. if err != nil {
  325. e.OutErr(c, e.ERR, err.Error())
  326. return
  327. }
  328. e.OutSuc(c, "success", nil)
  329. return
  330. }
  331. // AdminList
  332. // @Summary 管理员列表
  333. // @Tags 权限管理
  334. // @Description 权限管理-管理员列表
  335. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  336. // @Accept json
  337. // @Produce json
  338. // @Param args body md.AdminListReq true "请求参数"
  339. // @Success 200 {string} "具体看返回内容"
  340. // @Failure 400 {object} md.Response "具体错误"
  341. // @Router /role/adminList [POST]
  342. func AdminList(c *gin.Context) {
  343. var req md.AdminListReq
  344. err := c.ShouldBindJSON(&req)
  345. if err != nil {
  346. err = validate.HandleValidateErr(err)
  347. err1 := err.(e.E)
  348. e.OutErr(c, err1.Code, err1.Error())
  349. return
  350. }
  351. if req.Limit == 0 {
  352. req.Limit = 10
  353. }
  354. if req.Page == 0 {
  355. req.Page = 10
  356. }
  357. engine := db.Db
  358. adminDb := implement.NewAdminDb(engine)
  359. adminList, total, err := adminDb.FindAdmin(req.UserName, req.State, req.Page, req.Limit)
  360. if err != nil {
  361. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  362. return
  363. }
  364. var result []md.AdminListResp
  365. for _, v := range adminList {
  366. permissionGroupList, _, err1 := adminDb.FindAdminRolePermissionGroup(v.AdmId)
  367. if err1 != nil {
  368. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  369. return
  370. }
  371. var roleList []string
  372. for _, v1 := range permissionGroupList {
  373. roleList = append(roleList, v1.Role.Name)
  374. }
  375. result = append(result, md.AdminListResp{
  376. AdmId: v.AdmId,
  377. Username: v.Username,
  378. State: v.State,
  379. IsSuperAdministrator: v.IsSuperAdministrator,
  380. Memo: v.Memo,
  381. CreateAt: v.CreateAt,
  382. UpdateAt: v.UpdateAt,
  383. RoleList: roleList,
  384. })
  385. }
  386. e.OutSuc(c, map[string]interface{}{
  387. "list": result,
  388. "total": total,
  389. "state": []map[string]interface{}{
  390. {
  391. "name": enum.RoleState(enum.RoleStateForNormal).String(),
  392. "value": enum.RoleStateForNormal,
  393. },
  394. {
  395. "name": enum.RoleState(enum.RoleStateForFreeze).String(),
  396. "value": enum.RoleStateForFreeze,
  397. },
  398. },
  399. }, nil)
  400. return
  401. }
  402. // UpdateAdminState
  403. // @Summary 修改管理员状态
  404. // @Tags 权限管理
  405. // @Description 权限管理-修改管理员状态
  406. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  407. // @Accept json
  408. // @Produce json
  409. // @Param args body md.UpdateAdminStateReq true "请求参数"
  410. // @Success 200 {string} "success"
  411. // @Failure 400 {object} md.Response "具体错误"
  412. // @Router /role/updateAdminState [POST]
  413. func UpdateAdminState(c *gin.Context) {
  414. var req md.UpdateAdminStateReq
  415. err := c.ShouldBindJSON(&req)
  416. if err != nil {
  417. err = validate.HandleValidateErr(err)
  418. err1 := err.(e.E)
  419. e.OutErr(c, err1.Code, err1.Error())
  420. return
  421. }
  422. engine := db.Db
  423. admDb := implement.NewAdminDb(engine)
  424. admin, err := admDb.GetAdmin(req.AdmId)
  425. if err != nil {
  426. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  427. return
  428. }
  429. if admin == nil {
  430. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  431. return
  432. }
  433. admin.State = req.State
  434. _, err = admDb.UpdateAdmin(admin, "state")
  435. if err != nil {
  436. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  437. return
  438. }
  439. e.OutSuc(c, "success", nil)
  440. return
  441. }
  442. // UpdateAdmin
  443. // @Summary 修改管理员信息
  444. // @Tags 权限管理
  445. // @Description 权限管理-修改管理员信息
  446. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  447. // @Accept json
  448. // @Produce json
  449. // @Param args body md.UpdateAdminReq true "请求参数"
  450. // @Success 200 {string} "success"
  451. // @Failure 400 {object} md.Response "具体错误"
  452. // @Router /role/updateAdmin [POST]
  453. func UpdateAdmin(c *gin.Context) {
  454. var req md.UpdateAdminReq
  455. err := c.ShouldBindJSON(&req)
  456. if err != nil {
  457. err = validate.HandleValidateErr(err)
  458. err1 := err.(e.E)
  459. e.OutErr(c, err1.Code, err1.Error())
  460. return
  461. }
  462. engine := db.Db
  463. admDb := implement.NewAdminDb(engine)
  464. admin, err := admDb.GetAdmin(req.AdmId)
  465. if err != nil {
  466. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  467. return
  468. }
  469. if admin == nil {
  470. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  471. return
  472. }
  473. admin.Username = req.Username
  474. admin.Memo = req.Memo
  475. admin.Password = utils.Md5(req.Password)
  476. _, err = admDb.UpdateAdmin(admin, "username", "memo", "password")
  477. if err != nil {
  478. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  479. return
  480. }
  481. e.OutSuc(c, "success", nil)
  482. return
  483. }
  484. // AddAdmin
  485. // @Summary 新增管理员
  486. // @Tags 权限管理
  487. // @Description 权限管理-新增管理员
  488. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  489. // @Accept json
  490. // @Produce json
  491. // @Param args body md.AddAdminReq true "请求参数"
  492. // @Success 200 {string} "success"
  493. // @Failure 400 {object} md.Response "具体错误"
  494. // @Router /role/addAdmin [POST]
  495. func AddAdmin(c *gin.Context) {
  496. var req md.AddAdminReq
  497. err := c.ShouldBindJSON(&req)
  498. if err != nil {
  499. err = validate.HandleValidateErr(err)
  500. err1 := err.(e.E)
  501. e.OutErr(c, err1.Code, err1.Error())
  502. return
  503. }
  504. engine := db.Db
  505. admDb := implement.NewAdminDb(engine)
  506. isHasAdmin, err := admDb.GetAdminByUserName(req.Username)
  507. if err != nil {
  508. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  509. return
  510. }
  511. if isHasAdmin != nil {
  512. e.OutErr(c, e.ERR, "当前用户名已存在,请勿重复添加")
  513. return
  514. }
  515. admId, err := admDb.CreateAdminId()
  516. if err != nil {
  517. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  518. return
  519. }
  520. admin := model.Admin{
  521. AdmId: admId,
  522. Username: req.Username,
  523. Password: utils.Md5(req.Password),
  524. State: enum.AdminStateForNormal,
  525. IsSuperAdministrator: 0,
  526. Memo: req.Memo,
  527. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  528. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  529. }
  530. _, err = admDb.AdminInsert(&admin)
  531. if err != nil {
  532. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  533. return
  534. }
  535. e.OutSuc(c, "success", nil)
  536. return
  537. }
  538. // DeleteAdmin
  539. // @Summary 删除管理员
  540. // @Tags 权限管理
  541. // @Description 权限管理-删除管理员
  542. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  543. // @Accept json
  544. // @Produce json
  545. // @Success 200 {string} "success"
  546. // @Failure 400 {object} md.Response "具体错误"
  547. // @Router /role/deleteAdmin/{$adm_id} [DELETE]
  548. func DeleteAdmin(c *gin.Context) {
  549. admId := c.Param("adm_id")
  550. err := svc.AdminDelete(c, []int{utils.StrToInt(admId)})
  551. if err != nil {
  552. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  553. return
  554. }
  555. e.OutSuc(c, "success", nil)
  556. return
  557. }
  558. // BindAdminRole
  559. // @Summary 管理员绑定角色
  560. // @Tags 权限管理
  561. // @Description 权限管理-管理员绑定角色
  562. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  563. // @Accept json
  564. // @Produce json
  565. // @Param args body md.BindAdminRoleReq true "请求参数"
  566. // @Success 200 {string} "success"
  567. // @Failure 400 {object} md.Response "具体错误"
  568. // @Router /role/bindAdminRole/ [POST]
  569. func BindAdminRole(c *gin.Context) {
  570. var req md.BindAdminRoleReq
  571. err := c.ShouldBindJSON(&req)
  572. if err != nil {
  573. err = validate.HandleValidateErr(err)
  574. err1 := err.(e.E)
  575. e.OutErr(c, err1.Code, err1.Error())
  576. return
  577. }
  578. err = svc.BindAdminRole(c, req)
  579. if err != nil {
  580. e.OutErr(c, e.ERR, err.Error())
  581. return
  582. }
  583. e.OutSuc(c, "success", nil)
  584. return
  585. }
  586. // AdminInfo
  587. // @Summary 管理员信息
  588. // @Tags 权限管理
  589. // @Description 权限管理-管理员信息
  590. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  591. // @Accept json
  592. // @Produce json
  593. // @param adm_id query string true "管理员id"
  594. // @Success 200 {string} "具体看返回内容"
  595. // @Failure 400 {object} md.Response "具体错误"
  596. // @Router /role/adminInfo [GET]
  597. func AdminInfo(c *gin.Context) {
  598. admId := c.DefaultQuery("adm_id", "")
  599. engine := db.Db
  600. admDb := implement.NewAdminDb(engine)
  601. admin, err := admDb.GetAdmin(utils.StrToInt(admId))
  602. if err != nil {
  603. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  604. return
  605. }
  606. admin.Password = ""
  607. e.OutSuc(c, map[string]interface{}{
  608. "info": admin,
  609. "state": []map[string]interface{}{
  610. {
  611. "name": enum.RoleState(enum.RoleStateForNormal).String(),
  612. "value": enum.RoleStateForNormal,
  613. },
  614. {
  615. "name": enum.RoleState(enum.RoleStateForFreeze).String(),
  616. "value": enum.RoleStateForFreeze,
  617. },
  618. },
  619. }, nil)
  620. return
  621. }