admin.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "hongze/hongze_open_api/models/request/admin"
  5. adminTable "hongze/hongze_open_api/models/tables/admin"
  6. "hongze/hongze_open_api/utils"
  7. "time"
  8. )
  9. // Admin
  10. // 系统用户模块
  11. type Admin struct {
  12. BaseAuth
  13. }
  14. // Create
  15. // @Title 创建系统用户
  16. // @Description 创建系统用户
  17. // @Param request body admin.CreateUserReq true "type json string"
  18. // @Success 200 创建成功
  19. // @router /create [post]
  20. func (c *Admin) Create() {
  21. //appid权限校验
  22. appid := c.GetString("appid", "")
  23. if utils.RunMode == "release" && appid != "CQWx3EqDLNk7bVHo" {
  24. c.FailWithMessage("无权限")
  25. return
  26. }
  27. var req admin.CreateUserReq
  28. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  29. if err != nil {
  30. c.FailWithMessage("参数解析异常")
  31. return
  32. }
  33. roleType, ok := utils.RoleTypeMap[req.RoleType]
  34. if !ok {
  35. c.FailWithMessage("角色类型异常")
  36. return
  37. }
  38. mobile := utils.TrimStr(req.Mobile)
  39. if mobile == `` {
  40. c.FailWithMessage("手机号必传")
  41. return
  42. }
  43. realName := utils.TrimStr(req.RealName)
  44. if realName == `` {
  45. c.FailWithMessage("真实姓名必传")
  46. return
  47. }
  48. //校验该用户是否存在系统中
  49. count, err := adminTable.GetSysAdminCountByMobile(mobile)
  50. if count > 0 {
  51. c.FailWithMessage("该手机号已存在系统中")
  52. return
  53. }
  54. passWord := `123456a`
  55. //pwdByte, err := base64.StdEncoding.DecodeString(passWord)
  56. //if err != nil {
  57. // fmt.Println("err:",err)
  58. // c.FailWithMessage("解析数据失败")
  59. // return
  60. //}
  61. //pwdStr := string(pwdByte)
  62. //pwdStr = strings.ToLower(pwdStr)
  63. adminInfo := &adminTable.Admin{
  64. //AdminId: 0,
  65. AdminName: mobile,
  66. RealName: realName,
  67. Password: utils.MD5(passWord),
  68. //LastUpdatedPasswordTime: time.Now().Format(utils.FormatDateTime),
  69. Enabled: 0,
  70. Email: "",
  71. //LastLoginTime: time.Now().Format(utils.FormatDateTime),
  72. CreatedTime: time.Now(),
  73. //LastUpdatedTime: time.Now().Format(utils.FormatDateTime),
  74. Role: roleType.Role,
  75. Mobile: mobile,
  76. RoleType: 0,
  77. RoleId: roleType.RoleId,
  78. RoleName: roleType.RoleName,
  79. RoleTypeCode: roleType.RoleTypeCode,
  80. DepartmentId: roleType.DepartmentId,
  81. DepartmentName: roleType.DepartmentName,
  82. GroupId: roleType.GroupId,
  83. GroupName: roleType.GroupName,
  84. Authority: 0,
  85. Position: "",
  86. }
  87. err = adminTable.AddAdmin(adminInfo)
  88. if err != nil {
  89. c.OkWithMessage("创建失败")
  90. return
  91. }
  92. // 清除用户列表缓存key
  93. _ = utils.Rc.Delete(utils.CACHE_KEY_ADMIN)
  94. c.OkWithMessage("创建成功")
  95. }