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" "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() 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": "密码已修改", }) }