scrm-go

admin_controller.go 3.2KB

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