广告平台(站长使用)
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

hdl_role.go 21 KiB

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