123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package admin_user
-
- import (
- base_ctl "SCRM/controllers"
- "SCRM/enums"
- base_service "SCRM/service"
- "SCRM/service/admin_service"
- "SCRM/service/role_service"
- "SCRM/service/sms_service"
- "strconv"
- "time"
-
- "fmt"
- "github.com/astaxie/beego"
- )
-
- func AdminCtlRegistRouters() {
- beego.Router("/api/password/code", &AdminController{}, "post:CodeOfModifyPwd")
- beego.Router("/api/password/modify", &AdminController{}, "post:ModifyPwd")
- beego.Router("/api/admin/edit_info", &AdminController{}, "post:EditAdminUserInfo")
- }
-
- type AdminController struct {
- base_ctl.BaseAuthAPIController
- }
-
- // /api/admin/edit_info [post]
- // @param avatar:string
- // @param name:string
- // @param opwd?:string 没有原始密码的时候,认为不修改密码
- // @param npwd?:string
- func (this *AdminController) EditAdminUserInfo() {
- adminUserInfo := this.GetAdminUserInfo()
-
- avatar := this.GetString("avatar")
- name := this.GetString("name")
- if len(name) == 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeMissingUserName)
- return
- }
- modifyErr := role_service.ModifyAdminUserInfo(adminUserInfo.AdminUser.Id, adminUserInfo.CurrentOrgId, adminUserInfo.CurrentAppId, name, avatar, "")
- if modifyErr != nil {
- this.ErrorLog("修改个人信息失败:%v", modifyErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDBUpdate)
-
- } else {
- appRole := adminUserInfo.AppRoles[adminUserInfo.CurrentAppId]
- appRole.UserName = name
- appRole.Avatar = avatar
- this.ServeSuccessJSON(nil)
- }
- }
-
- // /api/password/code [post]
- func (this *AdminController) CodeOfModifyPwd() {
- adminUserInfo := this.GetAdminUserInfo()
- mobile := adminUserInfo.AdminUser.Mobile
-
- redisClient := base_service.RedisClient()
- defer redisClient.Close()
-
- cur_date := time.Now().Format("2006-01-02")
- moblie_count, _ := redisClient.Get("scrm_verification_code_" + mobile + "_" + cur_date).Result()
- moblie_count_int, _ := strconv.Atoi(moblie_count)
- if moblie_max := 5; moblie_count_int >= moblie_max {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeVerificationCodeLimit)
- return
- }
-
- if code, err := sms_service.SMSSendVerificationCode(mobile); err != nil {
- this.ErrorLog("修改密码发送验证码失败:%v", err)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
-
- } else {
- cur_date := time.Now().Format("2006-01-02")
- redisClient.Set("scrm_verification_code_"+mobile, code, time.Minute*10)
- redisClient.Incr("scrm_verification_code_" + mobile + "_" + cur_date).Result()
-
- this.ServeSuccessJSON(map[string]interface{}{
- "msg": "短信发送成功,有效期为10分钟",
- })
- }
- }
-
- // /api/password/modify [post]
- // @param password:string
- // @param code:string
- func (this *AdminController) ModifyPwd() {
- new_pwd := this.GetString("password")
- code := this.GetString("code")
- if len(new_pwd) == 0 || len(code) == 0 {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
- return
- }
-
- adminUserInfo := this.GetAdminUserInfo()
- mobile := adminUserInfo.AdminUser.Mobile
-
- redisClient := base_service.RedisClient()
- defer redisClient.Close()
- cachedCode, err := redisClient.Get("scrm_verification_code_" + mobile).Result()
- fmt.Println("hhhhhhhhhhhhhh", cachedCode)
- if err != nil {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrVerCodeWrong)
- return
- }
- if code != cachedCode {
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeAccountOrVerCodeWrong)
- return
- }
-
- if modifyErr := admin_service.ModifyPassword(adminUserInfo.AdminUser.Id, new_pwd); modifyErr != nil {
- this.ErrorLog("修改密码失败:%v", modifyErr)
- this.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDataException)
- return
- }
-
- // 清除验证码
- redisClient.Del("scrm_verification_code_" + mobile)
- this.ServeSuccessJSON(map[string]interface{}{
- "msg": "密码已修改",
- })
- }
|