scrm-go

admin_controller.go 3.6KB

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