admin_controller.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package admin_user
  2. import (
  3. base_ctl "SCRM/controllers"
  4. "SCRM/enums"
  5. base_service "SCRM/service"
  6. "SCRM/service/admin_service"
  7. "SCRM/service/role_service"
  8. "SCRM/service/sms_service"
  9. "strconv"
  10. "time"
  11. "fmt"
  12. "github.com/astaxie/beego"
  13. )
  14. func AdminCtlRegistRouters() {
  15. beego.Router("/api/password/code", &AdminController{}, "post:CodeOfModifyPwd")
  16. beego.Router("/api/password/modify", &AdminController{}, "post:ModifyPwd")
  17. beego.Router("/api/admin/edit_info", &AdminController{}, "post:EditAdminUserInfo")
  18. }
  19. type AdminController struct {
  20. base_ctl.BaseAuthAPIController
  21. }
  22. // /api/admin/edit_info [post]
  23. // @param avatar:string
  24. // @param name:string
  25. // @param opwd?:string 没有原始密码的时候,认为不修改密码
  26. // @param npwd?:string
  27. func (this *AdminController) EditAdminUserInfo() {
  28. adminUserInfo := this.GetAdminUserInfo()
  29. avatar := this.GetString("avatar")
  30. name := this.GetString("name")
  31. if len(name) == 0 {
  32. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMissingUserName)
  33. return
  34. }
  35. modifyErr := role_service.ModifyAdminUserInfo(adminUserInfo.AdminUser.Id, adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, name, avatar, "")
  36. if modifyErr != nil {
  37. this.ErrorLog("修改个人信息失败:%v", modifyErr)
  38. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
  39. } else {
  40. appRole := adminUserInfo.AppRoles[adminUserInfo.CurrentAppId]
  41. appRole.UserName = name
  42. appRole.Avatar = avatar
  43. this.ServeSuccessJSON(nil)
  44. }
  45. }
  46. // /api/password/code [post]
  47. func (this *AdminController) CodeOfModifyPwd() {
  48. adminUserInfo := this.GetAdminUserInfo()
  49. mobile := adminUserInfo.AdminUser.Mobile
  50. redisClient := base_service.RedisClient()
  51. defer redisClient.Close()
  52. cur_date := time.Now().Format("2006-01-02")
  53. moblie_count, _ := redisClient.Get("scrm_verification_code_" + mobile + "_" + cur_date).Result()
  54. moblie_count_int, _ := strconv.Atoi(moblie_count)
  55. if moblie_max := 5; moblie_count_int >= moblie_max {
  56. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeLimit)
  57. return
  58. }
  59. if code, err := sms_service.SMSSendVerificationCode(mobile); err != nil {
  60. this.ErrorLog("修改密码发送验证码失败:%v", err)
  61. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  62. return
  63. } else {
  64. cur_date := time.Now().Format("2006-01-02")
  65. redisClient.Set("scrm_verification_code_"+mobile, code, time.Minute*10)
  66. redisClient.Incr("scrm_verification_code_" + mobile + "_" + cur_date).Result()
  67. this.ServeSuccessJSON(map[string]interface{}{
  68. "msg": "短信发送成功,有效期为10分钟",
  69. })
  70. }
  71. }
  72. // /api/password/modify [post]
  73. // @param password:string
  74. // @param code:string
  75. func (this *AdminController) ModifyPwd() {
  76. new_pwd := this.GetString("password")
  77. code := this.GetString("code")
  78. if len(new_pwd) == 0 || len(code) == 0 {
  79. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
  80. return
  81. }
  82. adminUserInfo := this.GetAdminUserInfo()
  83. mobile := adminUserInfo.AdminUser.Mobile
  84. redisClient := base_service.RedisClient()
  85. defer redisClient.Close()
  86. cachedCode, err := redisClient.Get("scrm_verification_code_" + mobile).Result()
  87. fmt.Println("hhhhhhhhhhhhhh", cachedCode)
  88. if err != nil {
  89. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrVerCodeWrong)
  90. return
  91. }
  92. if code != cachedCode {
  93. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrVerCodeWrong)
  94. return
  95. }
  96. if modifyErr := admin_service.ModifyPassword(adminUserInfo.AdminUser.Id, new_pwd); modifyErr != nil {
  97. this.ErrorLog("修改密码失败:%v", modifyErr)
  98. this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
  99. return
  100. }
  101. // 清除验证码
  102. redisClient.Del("scrm_verification_code_" + mobile)
  103. this.ServeSuccessJSON(map[string]interface{}{
  104. "msg": "密码已修改",
  105. })
  106. }