admin.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  1. package ctrl_v1
  2. import (
  3. "gd_management_gateway/consts"
  4. "gd_management_gateway/errors"
  5. "gd_management_gateway/params/param_base"
  6. "gd_management_gateway/params/param_v1_0"
  7. "gd_management_gateway/rpc_apis"
  8. "gd_management_gateway/rpc_apis/gd_admin"
  9. "fmt"
  10. "strconv"
  11. "time"
  12. "gd_management_gateway/common.in/cache"
  13. "gd_management_gateway/common.in/config"
  14. "gd_management_gateway/common.in/httper"
  15. "gd_management_gateway/common.in/jsonrpc2"
  16. "gd_management_gateway/common.in/task"
  17. "gd_management_gateway/common.in/utils"
  18. "github.com/astaxie/beego"
  19. "github.com/go-redis/redis"
  20. "go.uber.org/zap"
  21. )
  22. // Operations about admin api
  23. type BaseAdminController struct {
  24. metadata interface{} // 中继元数据
  25. beego.Controller
  26. LogID string
  27. }
  28. // @Title 管理用户添加
  29. // @Description 管理用户添加
  30. // @Param token header string true "token"
  31. // @Param uid header int64 true "admin id"
  32. // @Param body body params.param_v1_0.AddAdminBody true ""
  33. // @Success 200 {object} params.param_v1_0.AdminRegisterResp "响应信息"
  34. // @Failure 500 服务器错误
  35. // @router /admin [post]
  36. func (u *BaseAdminController) AdminRegister() {
  37. u.LogID = fmt.Sprintf("AdminRegister[%d]", time.Now().UnixNano())
  38. req := &param_v1_0.AdminRegisterReq{}
  39. getParamsTask := func() error {
  40. httper.FillRequireParams(u.Ctx, req, u.LogID)
  41. return nil
  42. }
  43. handleTokenCheck := func() error {
  44. uid, _, err := utils.ParseToken(req.Token)
  45. if err != nil {
  46. return err
  47. }
  48. if uid != req.Uid {
  49. return errors.UserTokenillegality
  50. }
  51. return nil
  52. }
  53. handleDataTask := func() error {
  54. mreq := gd_admin.AdminRegisterReq{
  55. AdminName: req.Body.AdminName,
  56. AdminPasswd: req.Body.AdminPasswd,
  57. AdminPhone: req.Body.AdminPhone,
  58. AdminEmail: req.Body.AdminEmail,
  59. GroupId: req.Body.GroupId,
  60. NodeId: req.Body.NodeId,
  61. }
  62. reply, err := rpc_apis.Admin.AdminRegister(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
  63. if err != nil {
  64. l.Error("rpc",
  65. zap.String("call", "AdminRegister"),
  66. zap.String("args", utils.MarshalJsonString(req)),
  67. zap.String("error", err.Error()))
  68. return errors.ErrorTransform(err)
  69. }
  70. resp := param_v1_0.AdminRegisterResp{
  71. SimpleResp: param_base.SimpleResp{
  72. Code: 0,
  73. Msg: "SUCCESS",
  74. },
  75. Data: reply,
  76. }
  77. httper.JSON(u.Ctx, resp, u.LogID)
  78. return nil
  79. }
  80. // 执行以上定义的任务
  81. task.Do(&task.PanicRecover{
  82. Ctx: u.Ctx,
  83. LogID: u.LogID}, u.Ctx,
  84. getParamsTask, handleTokenCheck, handleDataTask,
  85. )
  86. }
  87. // @Title 管理用户删除
  88. // @Description 管理用户删除
  89. // @Param token header string true "token"
  90. // @Param uid header int64 true "admin id"
  91. // @Param admin_id query int64 true "要删除的admin id"
  92. // @Success 200 {object} params.param_v1_0.AdminDeleteResp "响应信息"
  93. // @Failure 500 服务器错误
  94. // @router /admin [delete]
  95. func (u *BaseAdminController) AdminDelete() {
  96. u.LogID = fmt.Sprintf("AdminDelete[%d]", time.Now().UnixNano())
  97. req := &param_v1_0.AdminDeleteReq{}
  98. getParamsTask := func() error {
  99. httper.FillRequireParams(u.Ctx, req, u.LogID)
  100. return nil
  101. }
  102. handleTokenCheck := func() error {
  103. uid, _, err := utils.ParseToken(req.Token)
  104. if err != nil {
  105. return err
  106. }
  107. if uid != req.Uid {
  108. return errors.UserTokenillegality
  109. }
  110. return nil
  111. }
  112. handleDataTask := func() error {
  113. mreq := gd_admin.AdminDeleteReq{}
  114. mreq.AdminId = req.AdminId
  115. _, err := rpc_apis.Admin.AdminDelete(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
  116. if err != nil {
  117. l.Error("rpc",
  118. zap.String("call", "AdminDelete"),
  119. zap.String("args", utils.MarshalJsonString(req)),
  120. zap.String("error", err.Error()))
  121. return errors.ErrorTransform(err)
  122. }
  123. resp := param_v1_0.AdminDeleteResp{
  124. SimpleResp: param_base.SimpleResp{
  125. Code: 0,
  126. Msg: "SUCCESS",
  127. },
  128. }
  129. httper.JSON(u.Ctx, resp, u.LogID)
  130. return nil
  131. }
  132. // 执行以上定义的任务
  133. task.Do(&task.PanicRecover{
  134. Ctx: u.Ctx,
  135. LogID: u.LogID}, u.Ctx,
  136. getParamsTask, handleTokenCheck, handleDataTask,
  137. )
  138. }
  139. // @Title 管理用户信息修改
  140. // @Description 管理用户信息修改
  141. // @Param token header string true "token"
  142. // @Param uid header int64 true "admin id"
  143. // @Param body body params.param_v1_0.AdminModifyBody true
  144. // @Success 200 {object} params.param_v1_0.AdminModifyResp "响应信息"
  145. // @Failure 500 服务器错误
  146. // @router /admin [put]
  147. func (u *BaseAdminController) AdminModify() {
  148. u.LogID = fmt.Sprintf("AdminModify[%d]", time.Now().UnixNano())
  149. req := &param_v1_0.AdminModifyReq{}
  150. getParamsTask := func() error {
  151. httper.FillRequireParams(u.Ctx, req, u.LogID)
  152. return nil
  153. }
  154. checkPasswordTask := func() error {
  155. return utils.VerifyPassword(req.Body.AdminPasswd)
  156. }
  157. handleTokenCheck := func() error {
  158. uid, _, err := utils.ParseToken(req.Token)
  159. if err != nil {
  160. return err
  161. }
  162. if uid != req.Uid {
  163. return errors.UserTokenillegality
  164. }
  165. return nil
  166. }
  167. handleDataTask := func() error {
  168. mreq := gd_admin.AdminModifyReq{}
  169. mreq.AdminId = req.Body.AdminId
  170. mreq.AdminPasswd = req.Body.AdminPasswd
  171. mreq.AdminPhone = req.Body.AdminPhone
  172. mreq.AdminEmail = req.Body.AdminEmail
  173. mreq.GroupId = req.Body.GroupId
  174. mreq.NodeId = req.Body.NodeId
  175. _, err := rpc_apis.Admin.AdminModify(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
  176. if err != nil {
  177. l.Error("rpc",
  178. zap.String("call", "AdminModify"),
  179. zap.String("args", utils.MarshalJsonString(req)),
  180. zap.String("error", err.Error()))
  181. return errors.ErrorTransform(err)
  182. }
  183. resp := param_v1_0.AdminModifyResp{
  184. SimpleResp: param_base.SimpleResp{
  185. Code: 0,
  186. Msg: "SUCCESS",
  187. },
  188. }
  189. httper.JSON(u.Ctx, resp, u.LogID)
  190. return nil
  191. }
  192. // 执行以上定义的任务
  193. task.Do(&task.PanicRecover{
  194. Ctx: u.Ctx,
  195. LogID: u.LogID}, u.Ctx,
  196. getParamsTask, checkPasswordTask, handleTokenCheck, handleDataTask,
  197. )
  198. }
  199. // @Title 管理用户列表
  200. // @Description 管理用户列表
  201. // @Param token header string true "token"
  202. // @Param uid header int64 true "admin id"
  203. // @Param page_size query string false "页大小"
  204. // @Param page_number query int64 false "页号"
  205. // @Param search query string false "搜索"
  206. // @Success 200 {object} params.param_v1_0.AdminGetAllResp "响应信息"
  207. // @Failure 500 服务器错误
  208. // @router /list [get]
  209. func (u *BaseAdminController) GetAdminList() {
  210. u.LogID = fmt.Sprintf("GetAdminList[%d]", time.Now().UnixNano())
  211. req := &param_v1_0.AdminGetAllReq{}
  212. getParamsTask := func() error {
  213. httper.FillRequireParams(u.Ctx, req, u.LogID)
  214. return nil
  215. }
  216. handleTokenCheck := func() error {
  217. uid, _, err := utils.ParseToken(req.Token)
  218. if err != nil {
  219. return err
  220. }
  221. if uid != req.Uid {
  222. return errors.UserTokenillegality
  223. }
  224. return nil
  225. }
  226. handleDataTask := func() error {
  227. mreq := gd_admin.AdminGetAllReq{}
  228. mreq.PageSize = req.PageSize
  229. mreq.PageNumber = req.PageNumber
  230. mreq.Search = req.Search
  231. reply, err := rpc_apis.Admin.AdminGetAll(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
  232. mreq.PageSize = req.PageSize
  233. mreq.PageNumber = req.PageNumber
  234. if err != nil {
  235. l.Error("rpc",
  236. zap.String("call", "AdminGetAll"),
  237. zap.String("args", utils.MarshalJsonString(req)),
  238. zap.String("error", err.Error()))
  239. return errors.ErrorTransform(err)
  240. }
  241. resp := param_v1_0.AdminGetAllResp{
  242. SimpleResp: param_base.SimpleResp{
  243. Code: 0,
  244. Msg: "SUCCESS",
  245. },
  246. }
  247. resp.Data = reply
  248. httper.JSON(u.Ctx, resp, u.LogID)
  249. return nil
  250. }
  251. // 执行以上定义的任务
  252. task.Do(&task.PanicRecover{
  253. Ctx: u.Ctx,
  254. LogID: u.LogID}, u.Ctx,
  255. getParamsTask, handleTokenCheck, handleDataTask,
  256. )
  257. }
  258. // @Title 管理用户登录
  259. // @Description 管理用户登录
  260. // @Param body body params.param_v1_0.AdminLoginBody true ""
  261. // @Success 200 {string} json "{}" "响应信息"
  262. // @Failure 500 服务器错误
  263. // @router /login [put]
  264. func (u *BaseAdminController) AdminLogin() {
  265. u.LogID = fmt.Sprintf("AdminLogin[%d]", time.Now().UnixNano())
  266. req := &param_v1_0.AdminLoginReq{}
  267. getParamsTask := func() error {
  268. httper.FillRequireParams(u.Ctx, req, u.LogID)
  269. return nil
  270. }
  271. checkoutTask := func() error {
  272. // 检查账户是否被锁定
  273. ts, _ := config.Conf.Cgi.Gateway.AccountLockTimestamp.Int64()
  274. errorTimes, _ := config.Conf.Cgi.Gateway.AccountLockErrors.Int64()
  275. if ts == 0 || errorTimes == 0 {
  276. return nil
  277. }
  278. times, err := cache.Redis.Get(fmt.Sprintf(consts.AccountLock, req.Body.AdminName))
  279. if err == nil {
  280. t, _ := strconv.Atoi(times)
  281. if t >= int(errorTimes) {
  282. return jsonrpc2.NewJsonError(201111, "账号登录频繁,请"+config.Conf.Cgi.Gateway.AccountLockTimestamp.String()+"秒后再试")
  283. }
  284. }
  285. return nil
  286. }
  287. checkPasswordTask := func() error {
  288. return utils.VerifyPassword(req.Body.AdminPasswd)
  289. }
  290. handleDataTask := func() error {
  291. mreq := gd_admin.AdminLoginReq{}
  292. mreq.AdminName = req.Body.AdminName
  293. mreq.AdminPasswd = req.Body.AdminPasswd
  294. reply, err := rpc_apis.Admin.AdminLogin(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
  295. if err != nil {
  296. l.Error("rpc",
  297. zap.String("call", "AdminLogin"),
  298. zap.String("args", utils.MarshalJsonString(req)),
  299. zap.String("error", err.Error()))
  300. // 发生错误锁定账户
  301. ts, _ := config.Conf.Cgi.Gateway.AccountLockTimestamp.Int64()
  302. errorTimes, _ := config.Conf.Cgi.Gateway.AccountLockErrors.Int64()
  303. if ts > 0 && errorTimes > 0 {
  304. key := fmt.Sprintf(consts.AccountLock, req.Body.AdminName)
  305. _, err := cache.Redis.Get(key)
  306. if err == redis.Nil {
  307. // 不存在
  308. cache.Redis.SetEx(key, ts, 1)
  309. } else {
  310. // 存在
  311. cache.Redis.IncrBy(key, 1)
  312. }
  313. }
  314. return errors.ErrorTransform(err)
  315. }
  316. resp := param_v1_0.AdminLoginResp{
  317. SimpleResp: param_base.SimpleResp{
  318. Code: 0,
  319. Msg: "SUCCESS",
  320. },
  321. }
  322. resp.Data = reply
  323. resp.Data.Token, err = utils.ProductToken(reply.AdminId, reply.Level,mreq.AdminName)
  324. if err != nil {
  325. return err
  326. }
  327. httper.JSON(u.Ctx, resp, u.LogID)
  328. return nil
  329. }
  330. // 执行以上定义的任务
  331. task.Do(&task.PanicRecover{
  332. Ctx: u.Ctx,
  333. LogID: u.LogID}, u.Ctx,
  334. getParamsTask, checkPasswordTask, checkoutTask, handleDataTask,
  335. )
  336. }
  337. // @Title 获取用户权限
  338. // @Description 获取用户权限
  339. // @Param token header string true "token"
  340. // @Param uid header int64 true "admin id"
  341. // @Success 200 {object} params.param_v1_0.GetAccessResp "响应信息"
  342. // @Failure 500 服务器错误
  343. // @router /access [get]
  344. func (u *BaseAdminController) GetAccess() {
  345. u.LogID = fmt.Sprintf("GetAccess[%d]", time.Now().UnixNano())
  346. req := &param_v1_0.GetAccessReq{}
  347. getParamsTask := func() error {
  348. httper.FillRequireParams(u.Ctx, req, u.LogID)
  349. return nil
  350. }
  351. handleTokenCheck := func() error {
  352. uid, _, err := utils.ParseToken(req.Token)
  353. if err != nil {
  354. return err
  355. }
  356. if uid != req.Uid {
  357. return errors.UserTokenillegality
  358. }
  359. return nil
  360. }
  361. handleDataTask := func() error {
  362. mreq := gd_admin.GetAccessReq{
  363. Uid: req.Uid,
  364. }
  365. reply, err := rpc_apis.Admin.GetRbacAccess(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
  366. if err != nil {
  367. l.Error("rpc",
  368. zap.String("call", "GetRbacAccess"),
  369. zap.String("args", utils.MarshalJsonString(req)),
  370. zap.String("error", err.Error()))
  371. return errors.ErrorTransform(err)
  372. }
  373. resp := param_v1_0.GetAccessResp{
  374. SimpleResp: param_base.SimpleResp{
  375. Code: 0,
  376. Msg: "SUCCESS",
  377. },
  378. }
  379. resp.Data = reply
  380. httper.JSON(u.Ctx, resp, u.LogID)
  381. return nil
  382. }
  383. // 执行以上定义的任务
  384. task.Do(&task.PanicRecover{
  385. Ctx: u.Ctx,
  386. LogID: u.LogID}, u.Ctx,
  387. getParamsTask, handleTokenCheck, handleDataTask,
  388. )
  389. }
  390. // @Title 获取权限分组信息
  391. // @Description 获取权限分组信息
  392. // @Param token header string true "token"
  393. // @Param uid header int64 true "admin id"
  394. // @Success 200 {object} params.param_v1_0.GetRbacGroupListResp "响应信息"
  395. // @Failure 500 服务器错误
  396. // @router /rbac_group_list [get]
  397. func (u *BaseAdminController) GetRbacGroupList() {
  398. u.LogID = fmt.Sprintf("GetRbacGroupList[%d]", time.Now().UnixNano())
  399. req := &param_v1_0.GetRbacGroupListReq{}
  400. getParamsTask := func() error {
  401. httper.FillRequireParams(u.Ctx, req, u.LogID)
  402. return nil
  403. }
  404. handleTokenCheck := func() error {
  405. uid, _, err := utils.ParseToken(req.Token)
  406. if err != nil {
  407. return err
  408. }
  409. if uid != req.Uid {
  410. return errors.UserTokenillegality
  411. }
  412. return nil
  413. }
  414. handleDataTask := func() error {
  415. mreq := gd_admin.GetRbacGroupListReq{}
  416. reply, err := rpc_apis.Admin.GetRbacGroupList(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
  417. if err != nil {
  418. l.Error("rpc",
  419. zap.String("call", "GetRbacGroupList"),
  420. zap.String("args", utils.MarshalJsonString(req)),
  421. zap.String("error", err.Error()))
  422. return errors.ErrorTransform(err)
  423. }
  424. resp := param_v1_0.GetRbacGroupListResp{
  425. SimpleResp: param_base.SimpleResp{
  426. Code: 0,
  427. Msg: "SUCCESS",
  428. },
  429. }
  430. resp.Data = reply
  431. httper.JSON(u.Ctx, resp, u.LogID)
  432. return nil
  433. }
  434. // 执行以上定义的任务
  435. task.Do(&task.PanicRecover{
  436. Ctx: u.Ctx,
  437. LogID: u.LogID}, u.Ctx,
  438. getParamsTask, handleTokenCheck, handleDataTask,
  439. )
  440. }
  441. // @Title 更新权限分组
  442. // @Description 更新权限分组
  443. // @Param token header string true "token"
  444. // @Param uid header int64 true "admin id"
  445. // @Param body body params.param_v1_0.UpdateRbacGroup true "body"
  446. // @Success 200 {object} params.param_v1_0.UpdateRbacGroupResp "响应信息"
  447. // @Failure 500 服务器错误
  448. // @router /rbac_group [put]
  449. func (u *BaseAdminController) UpdateRbacGroup() {
  450. u.LogID = fmt.Sprintf("UpdateRbacGroup[%d]", time.Now().UnixNano())
  451. req := &param_v1_0.UpdateRbacGroupReq{}
  452. getParamsTask := func() error {
  453. httper.FillRequireParams(u.Ctx, req, u.LogID)
  454. return nil
  455. }
  456. handleTokenCheck := func() error {
  457. uid, _, err := utils.ParseToken(req.Token)
  458. if err != nil {
  459. return err
  460. }
  461. if uid != req.Uid {
  462. return errors.UserTokenillegality
  463. }
  464. return nil
  465. }
  466. handleDataTask := func() error {
  467. mreq := gd_admin.UpdateRbacGroupReq{
  468. Id: req.Body.Id,
  469. Name: req.Body.Name,
  470. NodeId: req.Body.NodeId,
  471. }
  472. _, err := rpc_apis.Admin.UpdateRbacGroup(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
  473. if err != nil {
  474. l.Error("rpc",
  475. zap.String("call", "UpdateRbacGroup"),
  476. zap.String("args", utils.MarshalJsonString(req)),
  477. zap.String("error", err.Error()))
  478. return errors.ErrorTransform(err)
  479. }
  480. resp := param_v1_0.UpdateRbacGroupResp{
  481. SimpleResp: param_base.SimpleResp{
  482. Code: 0,
  483. Msg: "SUCCESS",
  484. },
  485. }
  486. httper.JSON(u.Ctx, resp, u.LogID)
  487. return nil
  488. }
  489. // 执行以上定义的任务
  490. task.Do(&task.PanicRecover{
  491. Ctx: u.Ctx,
  492. LogID: u.LogID}, u.Ctx,
  493. getParamsTask, handleTokenCheck, handleDataTask,
  494. )
  495. }
  496. // @Title 删除权限分组
  497. // @Description 删除权限分组
  498. // @Param token header string true "token"
  499. // @Param uid header int64 true "admin id"
  500. // @Param id query int true "id"
  501. // @Success 200 {object} params.param_v1_0.DeleteRbacGroupResp "响应信息"
  502. // @Failure 500 服务器错误
  503. // @router /rbac_group [delete]
  504. func (u *BaseAdminController) DeleteRbacGroup() {
  505. u.LogID = fmt.Sprintf("DeleteRbacGroup[%d]", time.Now().UnixNano())
  506. req := &param_v1_0.DeleteRbacGroupReq{}
  507. getParamsTask := func() error {
  508. httper.FillRequireParams(u.Ctx, req, u.LogID)
  509. return nil
  510. }
  511. handleTokenCheck := func() error {
  512. uid, _, err := utils.ParseToken(req.Token)
  513. if err != nil {
  514. return err
  515. }
  516. if uid != req.Uid {
  517. return errors.UserTokenillegality
  518. }
  519. return nil
  520. }
  521. handleDataTask := func() error {
  522. mreq := gd_admin.DeleteRbacGroupReq{
  523. Id: req.Id,
  524. }
  525. _, err := rpc_apis.Admin.DeleteRbacGroup(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
  526. if err != nil {
  527. l.Error("rpc",
  528. zap.String("call", "DeleteRbacGroup"),
  529. zap.String("args", utils.MarshalJsonString(req)),
  530. zap.String("error", err.Error()))
  531. return errors.ErrorTransform(err)
  532. }
  533. resp := param_v1_0.DeleteRbacGroupResp{
  534. SimpleResp: param_base.SimpleResp{
  535. Code: 0,
  536. Msg: "SUCCESS",
  537. },
  538. }
  539. httper.JSON(u.Ctx, resp, u.LogID)
  540. return nil
  541. }
  542. // 执行以上定义的任务
  543. task.Do(&task.PanicRecover{
  544. Ctx: u.Ctx,
  545. LogID: u.LogID}, u.Ctx,
  546. getParamsTask, handleTokenCheck, handleDataTask,
  547. )
  548. }
  549. // @Title 添加权限分组
  550. // @Description 添加权限分组
  551. // @Param token header string true "token"
  552. // @Param uid header int64 true "admin id"
  553. // @Param body body params.param_v1_0.UpdateRbacGroup true "body"
  554. // @Success 200 {object} params.param_v1_0.AddRbacGroupResp "响应信息"
  555. // @Failure 500 服务器错误
  556. // @router /rbac_group [post]
  557. func (u *BaseAdminController) AddRbacGroup() {
  558. u.LogID = fmt.Sprintf("AddRbacGroup[%d]", time.Now().UnixNano())
  559. req := &param_v1_0.AddRbacGroupReq{}
  560. getParamsTask := func() error {
  561. httper.FillRequireParams(u.Ctx, req, u.LogID)
  562. return nil
  563. }
  564. handleTokenCheck := func() error {
  565. uid, _, err := utils.ParseToken(req.Token)
  566. if err != nil {
  567. return err
  568. }
  569. if uid != req.Uid {
  570. return errors.UserTokenillegality
  571. }
  572. return nil
  573. }
  574. handleDataTask := func() error {
  575. mreq := gd_admin.AddRbacGroupReq{
  576. Name: req.Body.Name,
  577. NodeId: req.Body.NodeId,
  578. }
  579. _, err := rpc_apis.Admin.AddRbacGroup(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
  580. if err != nil {
  581. l.Error("rpc",
  582. zap.String("call", "AddRbacGroup"),
  583. zap.String("args", utils.MarshalJsonString(req)),
  584. zap.String("error", err.Error()))
  585. return errors.ErrorTransform(err)
  586. }
  587. resp := param_v1_0.AddRbacGroupResp{
  588. SimpleResp: param_base.SimpleResp{
  589. Code: 0,
  590. Msg: "SUCCESS",
  591. },
  592. }
  593. httper.JSON(u.Ctx, resp, u.LogID)
  594. return nil
  595. }
  596. // 执行以上定义的任务
  597. task.Do(&task.PanicRecover{
  598. Ctx: u.Ctx,
  599. LogID: u.LogID}, u.Ctx,
  600. getParamsTask, handleTokenCheck, handleDataTask,
  601. )
  602. }
  603. // @Title 获取所有的节点列表
  604. // @Description 获取所有的节点列表
  605. // @Param token header string true "token"
  606. // @Param uid header int64 true "admin id"
  607. // @Success 200 {object} params.param_v1_0.GetNodeListResp "响应信息"
  608. // @Failure 500 服务器错误
  609. // @router /node_list [get]
  610. func (u *BaseAdminController) GetNodeList() {
  611. u.LogID = fmt.Sprintf("GetNodeList[%d]", time.Now().UnixNano())
  612. req := &param_v1_0.GetNodeListReq{}
  613. getParamsTask := func() error {
  614. httper.FillRequireParams(u.Ctx, req, u.LogID)
  615. return nil
  616. }
  617. handleTokenCheck := func() error {
  618. uid, _, err := utils.ParseToken(req.Token)
  619. if err != nil {
  620. return err
  621. }
  622. if uid != req.Uid {
  623. return errors.UserTokenillegality
  624. }
  625. return nil
  626. }
  627. handleDataTask := func() error {
  628. mreq := gd_admin.GetNodeListReq{}
  629. reply, err := rpc_apis.Admin.GetNodeList(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
  630. if err != nil {
  631. l.Error("rpc",
  632. zap.String("call", "GetNodeList"),
  633. zap.String("args", utils.MarshalJsonString(req)),
  634. zap.String("error", err.Error()))
  635. return errors.ErrorTransform(err)
  636. }
  637. resp := param_v1_0.GetNodeListResp{
  638. SimpleResp: param_base.SimpleResp{
  639. Code: 0,
  640. Msg: "SUCCESS",
  641. },
  642. }
  643. resp.List = reply
  644. httper.JSON(u.Ctx, resp, u.LogID)
  645. return nil
  646. }
  647. // 执行以上定义的任务
  648. task.Do(&task.PanicRecover{
  649. Ctx: u.Ctx,
  650. LogID: u.LogID}, u.Ctx,
  651. getParamsTask, handleTokenCheck, handleDataTask,
  652. )
  653. }
  654. // @Title 修改管理用户状态
  655. // @Description 修改管理用户状态
  656. // @Param token header string true "token"
  657. // @Param uid header int64 true "admin id"
  658. // @Param body body param_base.param_v1_0.UpdateStatusBody true ""
  659. // @Success 200 {object} params.param_v1_0.UpdateStatusResp "响应信息"
  660. // @Failure 500 服务器错误
  661. // @router /status [put]
  662. func (u *BaseAdminController) UpdateStatus() {
  663. u.LogID = fmt.Sprintf("AdminModify[%d]", time.Now().UnixNano())
  664. req := &param_v1_0.UpdateStatusReq{}
  665. getParamsTask := func() error {
  666. httper.FillRequireParams(u.Ctx, req, u.LogID)
  667. return nil
  668. }
  669. handleTokenCheck := func() error {
  670. uid, _, err := utils.ParseToken(req.Token)
  671. if err != nil {
  672. return err
  673. }
  674. if uid != req.Uid {
  675. return errors.UserTokenillegality
  676. }
  677. return nil
  678. }
  679. handleDataTask := func() error {
  680. mreq := gd_admin.UpdateStatusReq{}
  681. mreq.AdminId = req.Body.AdminId
  682. reply, err := rpc_apis.Admin.UpdateAdminStatus(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
  683. if err != nil {
  684. l.Error("rpc",
  685. zap.String("call", "UpdateAdminStatus"),
  686. zap.String("args", utils.MarshalJsonString(req)),
  687. zap.String("error", err.Error()))
  688. return errors.ErrorTransform(err)
  689. }
  690. resp := param_v1_0.UpdateStatusResp{
  691. SimpleResp: param_base.SimpleResp{
  692. Code: 0,
  693. Msg: "SUCCESS",
  694. },
  695. Data: reply,
  696. }
  697. httper.JSON(u.Ctx, resp, u.LogID)
  698. return nil
  699. }
  700. // 执行以上定义的任务
  701. task.Do(&task.PanicRecover{
  702. Ctx: u.Ctx,
  703. LogID: u.LogID}, u.Ctx,
  704. getParamsTask, handleTokenCheck, handleDataTask,
  705. )
  706. }
  707. // @Title 管理用户状态
  708. // @Description 管理用户状态
  709. // @Param token header string true "token"
  710. // @Param uid header int64 true "admin id"
  711. // @param admin_id query int64 true "user_id"
  712. // @Success 200 {object} params.param_v1_0.GetInfoResp "响应信息"
  713. // @Failure 500 服务器错误
  714. // @router /info [get]
  715. func (u *BaseAdminController) GetInfo() {
  716. u.LogID = fmt.Sprintf("GetInfo[%d]", time.Now().UnixNano())
  717. req := &param_v1_0.GetInfoReq{}
  718. getParamsTask := func() error {
  719. httper.FillRequireParams(u.Ctx, req, u.LogID)
  720. return nil
  721. }
  722. handleTokenCheck := func() error {
  723. uid, _, err := utils.ParseToken(req.Token)
  724. if err != nil {
  725. return err
  726. }
  727. if uid != req.Uid {
  728. return errors.UserTokenillegality
  729. }
  730. return nil
  731. }
  732. handleDataTask := func() error {
  733. mreq := gd_admin.GetInfoReq{}
  734. mreq.Uid = req.AdminId
  735. reply, err := rpc_apis.Admin.GetInfo(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
  736. if err != nil {
  737. l.Error("rpc",
  738. zap.String("call", "GetInfo"),
  739. zap.String("args", utils.MarshalJsonString(req)),
  740. zap.String("error", err.Error()))
  741. return errors.ErrorTransform(err)
  742. }
  743. resp := param_v1_0.GetInfoResp{
  744. SimpleResp: param_base.SimpleResp{
  745. Code: 0,
  746. Msg: "SUCCESS",
  747. },
  748. Data: reply,
  749. }
  750. httper.JSON(u.Ctx, resp, u.LogID)
  751. return nil
  752. }
  753. // 执行以上定义的任务
  754. task.Do(&task.PanicRecover{
  755. Ctx: u.Ctx,
  756. LogID: u.LogID}, u.Ctx,
  757. getParamsTask, handleTokenCheck, handleDataTask,
  758. )
  759. }
  760. // @Title token check
  761. // @Description token验证
  762. // @Param token header string true "token"
  763. // @Param uid header int64 true "admin id"
  764. // @Success 200 {object} params.param_v1_0.AdminTokenVerifyResp "响应信息"
  765. // @Failure 500 服务器错误
  766. // @router /token_verify [get]
  767. func (u *BaseAdminController) AdminTokenVerify() {
  768. u.LogID = fmt.Sprintf("AdminRegister[%d]", time.Now().UnixNano())
  769. req := &param_v1_0.AdminTokenVerifyReq{}
  770. getParamsTask := func() error {
  771. httper.FillRequireParams(u.Ctx, req, u.LogID)
  772. return nil
  773. }
  774. adminId := int64(0)
  775. handleTokenCheck := func() error {
  776. uid, _, err := utils.ParseToken(req.Token)
  777. if err != nil {
  778. return err
  779. }
  780. if uid != req.Uid {
  781. return errors.UserTokenillegality
  782. }
  783. adminId = uid
  784. return nil
  785. }
  786. handleDataTask := func() error {
  787. mreq := gd_admin.AdminLoginReq{}
  788. mreq.Uid = adminId
  789. fmt.Println(req)
  790. reply, err := rpc_apis.Admin.AdminLogin(utils.NewContextFromBeegoCtx(u.Ctx), &mreq)
  791. if err != nil {
  792. l.Error("rpc",
  793. zap.String("call", "AdminLogin"),
  794. zap.String("args", utils.MarshalJsonString(req)),
  795. zap.String("error", err.Error()))
  796. return errors.ErrorTransform(err)
  797. }
  798. resp := param_v1_0.AdminTokenVerifyResp{
  799. SimpleResp: param_base.SimpleResp{
  800. Code: 0,
  801. Msg: "SUCCESS",
  802. },
  803. }
  804. resp.Data = reply
  805. resp.Data.Token = req.Token
  806. httper.JSON(u.Ctx, resp, u.LogID)
  807. return nil
  808. }
  809. // 执行以上定义的任务
  810. task.Do(&task.PanicRecover{
  811. Ctx: u.Ctx,
  812. LogID: u.LogID}, u.Ctx,
  813. getParamsTask, handleTokenCheck, handleDataTask,
  814. )
  815. }