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

830 lines
24 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/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. ShowTime: v.ShowTime,
  387. Username: v.Username,
  388. State: v.State,
  389. IsSuperAdministrator: v.IsSuperAdministrator,
  390. Memo: v.Memo,
  391. CreateAt: v.CreateAt,
  392. UpdateAt: v.UpdateAt,
  393. RoleList: roleList,
  394. })
  395. }
  396. e.OutSuc(c, map[string]interface{}{
  397. "list": result,
  398. "total": total,
  399. "state": []map[string]interface{}{
  400. {
  401. "name": enum.RoleState(enum.RoleStateForNormal).String(),
  402. "value": enum.RoleStateForNormal,
  403. },
  404. {
  405. "name": enum.RoleState(enum.RoleStateForFreeze).String(),
  406. "value": enum.RoleStateForFreeze,
  407. },
  408. },
  409. }, nil)
  410. return
  411. }
  412. // UpdateAdminState
  413. // @Summary 修改管理员状态
  414. // @Tags 权限管理
  415. // @Description 权限管理-修改管理员状态
  416. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  417. // @Accept json
  418. // @Produce json
  419. // @Param args body md.UpdateAdminStateReq true "请求参数"
  420. // @Success 200 {string} "success"
  421. // @Failure 400 {object} md.Response "具体错误"
  422. // @Router /api/role/updateAdminState [POST]
  423. func UpdateAdminState(c *gin.Context) {
  424. var req md.UpdateAdminStateReq
  425. err := c.ShouldBindJSON(&req)
  426. if err != nil {
  427. err = validate.HandleValidateErr(err)
  428. err1 := err.(e.E)
  429. e.OutErr(c, err1.Code, err1.Error())
  430. return
  431. }
  432. engine := db.DBs[svc.GetMasterId(c)]
  433. admDb := implement.NewAdminDb(engine)
  434. admin, err := admDb.GetAdmin(req.AdmId)
  435. if err != nil {
  436. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  437. return
  438. }
  439. if admin == nil {
  440. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  441. return
  442. }
  443. admin.State = req.State
  444. _, err = admDb.UpdateAdmin(admin, "state")
  445. if err != nil {
  446. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  447. return
  448. }
  449. e.OutSuc(c, "success", nil)
  450. return
  451. }
  452. // UpdateAdmin
  453. // @Summary 修改管理员信息
  454. // @Tags 权限管理
  455. // @Description 权限管理-修改管理员信息
  456. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  457. // @Accept json
  458. // @Produce json
  459. // @Param args body md.UpdateAdminReq true "请求参数"
  460. // @Success 200 {string} "success"
  461. // @Failure 400 {object} md.Response "具体错误"
  462. // @Router /api/role/updateAdmin [POST]
  463. func UpdateAdmin(c *gin.Context) {
  464. var req md.UpdateAdminReq
  465. err := c.ShouldBindJSON(&req)
  466. if err != nil {
  467. err = validate.HandleValidateErr(err)
  468. err1 := err.(e.E)
  469. e.OutErr(c, err1.Code, err1.Error())
  470. return
  471. }
  472. engine := db.DBs[svc.GetMasterId(c)]
  473. admDb := implement.NewAdminDb(engine)
  474. admin, err := admDb.GetAdmin(req.AdmId)
  475. if err != nil {
  476. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  477. return
  478. }
  479. if admin == nil {
  480. e.OutErr(c, e.ERR_NO_DATA, "未查询到相应记录")
  481. return
  482. }
  483. admin.Username = req.Username
  484. admin.Memo = req.Memo
  485. admin.ShowTime = req.ShowTime
  486. if req.Password != "" {
  487. admin.Password = utils.Md5(req.Password)
  488. }
  489. _, err = admDb.UpdateAdmin(admin, "username", "memo", "password", "show_time")
  490. if err != nil {
  491. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  492. return
  493. }
  494. e.OutSuc(c, "success", nil)
  495. return
  496. }
  497. // AddAdmin
  498. // @Summary 新增管理员
  499. // @Tags 权限管理
  500. // @Description 权限管理-新增管理员
  501. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  502. // @Accept json
  503. // @Produce json
  504. // @Param args body md.AddAdminReq true "请求参数"
  505. // @Success 200 {string} "success"
  506. // @Failure 400 {object} md.Response "具体错误"
  507. // @Router /api/role/addAdmin [POST]
  508. func AddAdmin(c *gin.Context) {
  509. var req md.AddAdminReq
  510. err := c.ShouldBindJSON(&req)
  511. if err != nil {
  512. err = validate.HandleValidateErr(err)
  513. err1 := err.(e.E)
  514. e.OutErr(c, err1.Code, err1.Error())
  515. return
  516. }
  517. engine := db.DBs[svc.GetMasterId(c)]
  518. admDb := implement.NewAdminDb(engine)
  519. isHasAdmin, err := admDb.GetAdminByUserName(req.Username)
  520. if err != nil {
  521. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  522. return
  523. }
  524. if isHasAdmin != nil {
  525. e.OutErr(c, e.ERR, "当前用户名已存在,请勿重复添加")
  526. return
  527. }
  528. admId, err := admDb.CreateAdminId()
  529. if err != nil {
  530. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  531. return
  532. }
  533. admin := model.Admin{
  534. ShowTime: req.ShowTime,
  535. AdmId: admId,
  536. Username: req.Username,
  537. Password: utils.Md5(req.Password),
  538. State: enum.AdminStateForNormal,
  539. IsSuperAdministrator: 0,
  540. Memo: req.Memo,
  541. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  542. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  543. }
  544. _, err = admDb.AdminInsert(&admin)
  545. if err != nil {
  546. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  547. return
  548. }
  549. e.OutSuc(c, "success", nil)
  550. return
  551. }
  552. // DeleteAdmin
  553. // @Summary 删除管理员
  554. // @Tags 权限管理
  555. // @Description 权限管理-删除管理员
  556. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  557. // @Accept json
  558. // @Produce json
  559. // @Success 200 {string} "success"
  560. // @Failure 400 {object} md.Response "具体错误"
  561. // @Router /api/role/deleteAdmin/{$adm_id} [DELETE]
  562. func DeleteAdmin(c *gin.Context) {
  563. admId := c.Param("adm_id")
  564. err := svc.AdminDelete(c, []int{utils.StrToInt(admId)})
  565. if err != nil {
  566. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  567. return
  568. }
  569. e.OutSuc(c, "success", nil)
  570. return
  571. }
  572. // BindAdminRole
  573. // @Summary 管理员绑定角色
  574. // @Tags 权限管理
  575. // @Description 权限管理-管理员绑定角色
  576. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  577. // @Accept json
  578. // @Produce json
  579. // @Param args body md.BindAdminRoleReq true "请求参数"
  580. // @Success 200 {string} "success"
  581. // @Failure 400 {object} md.Response "具体错误"
  582. // @Router /api/role/bindAdminRole/ [POST]
  583. func BindAdminRole(c *gin.Context) {
  584. var req md.BindAdminRoleReq
  585. err := c.ShouldBindJSON(&req)
  586. if err != nil {
  587. err = validate.HandleValidateErr(err)
  588. err1 := err.(e.E)
  589. e.OutErr(c, err1.Code, err1.Error())
  590. return
  591. }
  592. err = svc.BindAdminRole(c, req)
  593. if err != nil {
  594. e.OutErr(c, e.ERR, err.Error())
  595. return
  596. }
  597. e.OutSuc(c, "success", nil)
  598. return
  599. }
  600. // AdminInfo
  601. // @Summary 管理员信息
  602. // @Tags 权限管理
  603. // @Description 权限管理-管理员信息
  604. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  605. // @Accept json
  606. // @Produce json
  607. // @param adm_id query string true "管理员id"
  608. // @Success 200 {string} "具体看返回内容"
  609. // @Failure 400 {object} md.Response "具体错误"
  610. // @Router /api/role/adminInfo [GET]
  611. func AdminInfo(c *gin.Context) {
  612. admId := c.DefaultQuery("adm_id", "")
  613. engine := db.DBs[svc.GetMasterId(c)]
  614. admDb := implement.NewAdminDb(engine)
  615. admin, err := admDb.GetAdmin(utils.StrToInt(admId))
  616. if err != nil {
  617. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  618. return
  619. }
  620. admin.Password = ""
  621. e.OutSuc(c, map[string]interface{}{
  622. "info": admin,
  623. "state": []map[string]interface{}{
  624. {
  625. "name": enum.RoleState(enum.RoleStateForNormal).String(),
  626. "value": enum.RoleStateForNormal,
  627. },
  628. {
  629. "name": enum.RoleState(enum.RoleStateForFreeze).String(),
  630. "value": enum.RoleStateForFreeze,
  631. },
  632. },
  633. }, nil)
  634. return
  635. }
  636. func RoleMediumList(c *gin.Context) {
  637. var req md.AdminBindMediumListReq
  638. err := c.ShouldBindJSON(&req)
  639. if err != nil {
  640. err = validate.HandleValidateErr(err)
  641. err1 := err.(e.E)
  642. e.OutErr(c, err1.Code, err1.Error())
  643. return
  644. }
  645. engine := svc.MasterDb(c)
  646. NewMediumDb := implement.NewMediumDb(engine)
  647. appId := svc.GetMediumIdStr(c, 0, req.Name, req.Account)
  648. list, total, _ := NewMediumDb.FindSuperAdminByMediumId(appId, utils.StrToInt(req.Page), utils.StrToInt(req.Limit))
  649. data := make([]md.AdminBindMediumListData, 0)
  650. if len(list) > 0 {
  651. for _, v := range list {
  652. var tmp = md.AdminBindMediumListData{
  653. MediumId: utils.IntToStr(v.MediumId),
  654. Name: v.Memo,
  655. IsBind: "0",
  656. Account: v.Username,
  657. }
  658. NewMediumListDb := implement2.NewMediumListDb(db.Db)
  659. GetMediumList, _ := NewMediumListDb.GetMediumList(v.MediumId)
  660. if GetMediumList != nil {
  661. tmp.Name = GetMediumList.CompanyName
  662. if GetMediumList.CompanyAbbreviation != "" {
  663. tmp.Name = GetMediumList.CompanyAbbreviation
  664. }
  665. }
  666. count, _ := engine.Where("medium_id=? and admin_id=?", v.MediumId, req.AdminId).Count(&model.AdminBindMedium{})
  667. if count > 0 {
  668. tmp.IsBind = "1"
  669. }
  670. data = append(data, tmp)
  671. }
  672. }
  673. res := md.AdminBindMediumListRes{
  674. List: data,
  675. Total: total,
  676. }
  677. e.OutSuc(c, res, nil)
  678. return
  679. }
  680. func RoleBindMedium(c *gin.Context) {
  681. var req md.AdminBindMediumListSaveReq
  682. err := c.ShouldBindJSON(&req)
  683. if err != nil {
  684. err = validate.HandleValidateErr(err)
  685. err1 := err.(e.E)
  686. e.OutErr(c, err1.Code, err1.Error())
  687. return
  688. }
  689. NewAdminBindMediumDb := implement.NewAdminBindMediumDb(svc.MasterDb(c))
  690. data := NewAdminBindMediumDb.GetAdminByMediumId(utils.StrToInt(req.AdminId), utils.StrToInt(req.MediumId))
  691. if data != nil {
  692. e.OutErr(c, 400, e.NewErr(400, "已绑定该媒体"))
  693. return
  694. }
  695. tmp := model.AdminBindMedium{
  696. AdminId: utils.StrToInt(req.AdminId),
  697. MediumId: utils.StrToInt(req.MediumId),
  698. CreateAt: time.Now(),
  699. UpdateAt: time.Now(),
  700. }
  701. svc.MasterDb(c).Insert(&tmp)
  702. e.OutSuc(c, "success", nil)
  703. return
  704. }
  705. func RoleDelMedium(c *gin.Context) {
  706. var req md.AdminBindMediumListSaveReq
  707. err := c.ShouldBindJSON(&req)
  708. if err != nil {
  709. err = validate.HandleValidateErr(err)
  710. err1 := err.(e.E)
  711. e.OutErr(c, err1.Code, err1.Error())
  712. return
  713. }
  714. svc.MasterDb(c).Where("medium_id=? and admin_id=?", req.MediumId, req.AdminId).Delete(&model.AdminBindMedium{})
  715. e.OutSuc(c, "success", nil)
  716. return
  717. }
  718. func RoleAgentList(c *gin.Context) {
  719. var req md.AdminBindMediumListReq
  720. err := c.ShouldBindJSON(&req)
  721. if err != nil {
  722. err = validate.HandleValidateErr(err)
  723. err1 := err.(e.E)
  724. e.OutErr(c, err1.Code, err1.Error())
  725. return
  726. }
  727. engine := svc.MasterDb(c)
  728. NewAgentDb := implement.NewAgentDb(engine)
  729. appId := svc.GetAgentIdStr(c, 0, req.Name, req.Account)
  730. list, total, _ := NewAgentDb.FindSuperAdminByAgentId(appId, utils.StrToInt(req.Page), utils.StrToInt(req.Limit))
  731. data := make([]md.AdminBindMediumListData, 0)
  732. if len(list) > 0 {
  733. for _, v := range list {
  734. var tmp = md.AdminBindMediumListData{
  735. MediumId: utils.IntToStr(v.AgentId),
  736. Name: v.Memo,
  737. IsBind: "0",
  738. Account: v.Username,
  739. }
  740. NewAgentListDb := implement2.NewAgentListDb(db.Db)
  741. GetAgentList, _ := NewAgentListDb.GetAgentList(v.AgentId)
  742. if GetAgentList != nil {
  743. tmp.Name = GetAgentList.CompanyName
  744. if GetAgentList.CompanyAbbreviation != "" {
  745. tmp.Name = GetAgentList.CompanyAbbreviation
  746. }
  747. }
  748. count, _ := engine.Where("agent_id=? and admin_id=?", v.AgentId, req.AdminId).Count(&model.AdminBindAgent{})
  749. if count > 0 {
  750. tmp.IsBind = "1"
  751. }
  752. data = append(data, tmp)
  753. }
  754. }
  755. res := md.AdminBindMediumListRes{
  756. List: data,
  757. Total: total,
  758. }
  759. e.OutSuc(c, res, nil)
  760. return
  761. }
  762. func RoleBindAgent(c *gin.Context) {
  763. var req md.AdminBindMediumListSaveReq
  764. err := c.ShouldBindJSON(&req)
  765. if err != nil {
  766. err = validate.HandleValidateErr(err)
  767. err1 := err.(e.E)
  768. e.OutErr(c, err1.Code, err1.Error())
  769. return
  770. }
  771. NewAdminBindAgentDb := implement.NewAdminBindAgentDb(svc.MasterDb(c))
  772. data := NewAdminBindAgentDb.GetAdminByAgentId(utils.StrToInt(req.AdminId), utils.StrToInt(req.MediumId))
  773. if data != nil {
  774. e.OutErr(c, 400, e.NewErr(400, "已绑定该媒体"))
  775. return
  776. }
  777. tmp := model.AdminBindAgent{
  778. AdminId: utils.StrToInt(req.AdminId),
  779. AgentId: utils.StrToInt(req.MediumId),
  780. CreateAt: time.Now(),
  781. UpdateAt: time.Now(),
  782. }
  783. svc.MasterDb(c).Insert(&tmp)
  784. e.OutSuc(c, "success", nil)
  785. return
  786. }
  787. func RoleDelAgent(c *gin.Context) {
  788. var req md.AdminBindMediumListSaveReq
  789. err := c.ShouldBindJSON(&req)
  790. if err != nil {
  791. err = validate.HandleValidateErr(err)
  792. err1 := err.(e.E)
  793. e.OutErr(c, err1.Code, err1.Error())
  794. return
  795. }
  796. svc.MasterDb(c).Where("agent_id=? and admin_id=?", req.MediumId, req.AdminId).Delete(&model.AdminBindAgent{})
  797. e.OutSuc(c, "success", nil)
  798. return
  799. }