作者 唐旭辉

添加功能 重置个人密码

... ... @@ -135,6 +135,32 @@ func (c *AuthController) SmsCode() {
return
}
//SmsCode 验证码短信校验
//@router /auth/smscode/check
func (c *AuthController) SmsCodeCheck() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
Phone string `json:"phone"`
Code string `json:"code"`
}
var param Parameter
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败", err)
msg = protocol.BadRequestParam("1")
return
}
if len(param.Phone) == 0 {
msg = protocol.BadRequestParam("1")
return
}
err := serveauth.SmsCodeCheck(param.Phone, param.Code)
msg = protocol.NewReturnResponse(nil, err)
return
}
//ChangeCompany 切换公司
//@Router /change_company [post]
func (c *AuthController) ChangeCompany() {
... ...
package controllers
import (
"encoding/json"
"oppmg/common/log"
"oppmg/protocol"
"oppmg/services/usermy"
)
// MyController 个人中心
type MyController struct {
BaseController
}
//ResetPassword 重置密码
//@router /my/reset_password
func (c *MyController) ResetPassword() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
type Parameter struct {
Phone string `json:"phone"`
NewPwd string `json:"newpwd"`
ConfirmPwd string `json:"confirmpwd"`
}
var param Parameter
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
companyid := c.GetCompanyId()
userId := c.GetUserId()
if companyid <= 0 {
msg = protocol.BadRequestParam("1")
return
}
if userId <= 0 {
msg = protocol.BadRequestParam("1")
return
}
err := usermy.ResetPasswordBySms(param.Phone, param.NewPwd, param.ConfirmPwd)
msg = protocol.NewReturnResponse(nil, err)
return
}
... ...
... ... @@ -19,15 +19,17 @@ var errmessge ErrorMap = map[string]string{
"10013": "职位已存在",
"10014": "职位名称最多10个字符",
//安全认证相关
"10020": "验证码过期",
"10021": "账号或密码不正确",
"10022": "账号已被禁用",
"10023": "用户无使用权限",
"10024": "登录凭证失效",
"10025": "该账号已在其他地方登录",
// "10026": "登录凭证过期",
"10026": "验证码校验失败",
"10027": "无操作权限",
"10028": "验证码错误",
"10029": "获取验证码失败",
//用户相关
"10031": "无效角色",
"10032": "无效部门",
... ... @@ -38,7 +40,7 @@ var errmessge ErrorMap = map[string]string{
"10037": "用户的部门必填",
"10038": "用户的角色必填",
"10039": "用户已存在",
"10040": "注册用户失败",
// "10040": "注册用户失败",
//部门相关
"10041": "无效的主管设置",
"10042": "无效的上级部门",
... ... @@ -49,6 +51,7 @@ var errmessge ErrorMap = map[string]string{
"10047": "只能删除没有成员的部门,需要先删除部门下的员工,再删除该部门",
//用户中心相关
"10051": "无法从远端接口获取公司数据",
"10052": "服务调用失败",
//模板相关
"10061": "请先删除该分类下的二级分类",
"10062": "该分类已被使用无法删除",
... ...
... ... @@ -57,6 +57,7 @@ func init() {
beego.NSRouter("/change_company", &controllers.AuthController{}, "post:ChangeCompany"),
//beego.NSRouter("/refresh_token", &controllers.AuthController{}, "post:RefreshToken"),
beego.NSRouter("/me", &controllers.AuthController{}, "post:Me"),
beego.NSRouter("/smscode/check", &controllers.AuthController{}, "post:SmsCodeCheck"),
),
beego.NSNamespace("/bulletin",
beego.NSRouter("/add", &controllers.BulletinController{}, "post:BulletinRelease"),
... ... @@ -90,6 +91,9 @@ func init() {
beego.NSNamespace("/upload",
beego.NSRouter("/image", &controllers.UploadController{}, "post:UploadImage"),
),
beego.NSNamespace("/my",
beego.NSRouter("/reset_password", &controllers.MyController{}, "post:ResetPassword"),
),
)
nsAuth := beego.NewNamespace("/auth",
... ... @@ -100,6 +104,7 @@ func init() {
beego.NSRouter("/verifyCaptcha", &controllers.AuthController{}, "post:ValidateGeetest"),
beego.NSRouter("/login_sms", &controllers.AuthController{}, "post:LoginSms"),
beego.NSRouter("/sms_code", &controllers.AuthController{}, "post:SmsCode"),
beego.NSRouter("/sms_code/check", &controllers.AuthController{}, "post:SmsCodeCheck"),
)
nsUcenter := beego.NewNamespace("/ucenter",
beego.NSBefore(middleware.LogRequestData),
... ...
... ... @@ -400,5 +400,11 @@ func SmsCodeSend(phone string) error {
}
func SmsCodeCheck(phone string, code string) error {
resp, err := ucenter.RequestUCenterSmsCodeCheck(phone, code)
if err != nil {
return protocol.NewErrWithMessage("10026")
}
//redis 缓存设置
redisdata.SetCaptchAuth(phone, resp.Data.CaptchaCertificate)
return nil
}
... ...
... ... @@ -28,7 +28,7 @@ func RequestUCenterLogin(account, password string) (*ResponseLogin, error) {
}
if !(uclientReturn.Code == ResponseCode0 &&
uclientReturn.Msg == ResponseMsgOk) {
return nil, protocol.NewErrWithMessage("10021")
return nil, protocol.NewErrWithMessage("10052")
}
return uclientReturn, nil
}
... ... @@ -60,7 +60,7 @@ func RequestUCenterAddUser(phone string, nickname string, avatar string) (*Respo
}
if !(ucenterReturn.Code == ResponseCode0 &&
ucenterReturn.Msg == ResponseMsgOk) {
return nil, protocol.NewErrWithMessage("10040")
return nil, protocol.NewErrWithMessage("10052")
}
return ucenterReturn, nil
}
... ... @@ -87,7 +87,7 @@ func RequestUCenterSmsCode(phone string) error {
}
if !(ucenterReturn.Code == ResponseCode0 &&
ucenterReturn.Msg == ResponseMsgOk) {
return protocol.NewErrWithMessage("10040")
return protocol.NewErrWithMessage("10052")
}
return nil
}
... ... @@ -113,7 +113,58 @@ func RequestUCenterLoginSms(phone string, captcha string) (*ResponseLoginSms, er
}
if !(ucenterReturn.Code == ResponseCode0 &&
ucenterReturn.Msg == ResponseMsgOk) {
return nil, protocol.NewErrWithMessage("10040")
return nil, protocol.NewErrWithMessage("10052")
}
return ucenterReturn, nil
}
func RequestUCenterSmsCodeCheck(phone string, captcha string) (*ResponseSmsCodeCheck, error) {
param := RequestSmsCodeCheck{
Phone: phone,
Captcha: captcha,
}
uclient := NewUCenterClient()
btBody, err := uclient.Call(param)
if err != nil {
log.Error("统一用户中心请求失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
var ucenterReturn *ResponseSmsCodeCheck
err = json.Unmarshal(btBody, ucenterReturn)
if err != nil {
log.Error("解析统一用户中心响应失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
if !(ucenterReturn.Code == ResponseCode0 &&
ucenterReturn.Msg == ResponseMsgOk) {
return nil, protocol.NewErrWithMessage("10026")
}
return ucenterReturn, nil
}
func RequestUCenterRestPassword(phone, newPwd, confirmPwd, certificate string) error {
param := RequestResetPassword{
Phone: phone,
CaptchaCertificate: certificate,
NewPwd: newPwd,
ConfirmPwd: confirmPwd,
}
uclient := NewUCenterClient()
btBody, err := uclient.Call(param)
if err != nil {
log.Error("统一用户中心请求失败 err:%s", err)
return protocol.NewErrWithMessage("1")
}
var ucenterReturn *CommResponse
err = json.Unmarshal(btBody, ucenterReturn)
if err != nil {
log.Error("解析统一用户中心响应失败 err:%s", err)
return protocol.NewErrWithMessage("1")
}
if !(ucenterReturn.Code == ResponseCode0 &&
ucenterReturn.Msg == ResponseMsgOk) {
return protocol.NewErrWithMessage("10052")
}
return nil
}
... ...
... ... @@ -164,3 +164,55 @@ type LoginSmsData struct {
Imtoken string `json:"imToken"`
CsAccountID int64 `json:"csAccountID"`
}
//验证码短信校验
type RequestSmsCodeCheck struct {
Phone string `json:"phone"`
Captcha string `json:"captcha"`
}
var (
_ IUCenterParam = RequestSmsCodeCheck{}
)
func (r RequestSmsCodeCheck) Format() []byte {
var bt []byte
bt, _ = json.Marshal(r)
return bt
}
//GetPath 实现IUCenterParam接口
func (r RequestSmsCodeCheck) GetPath() (string, string) {
return "/user/checkSmsCode", "POST"
}
//验证码校验结果
type ResponseSmsCodeCheck struct {
CommResponse
Data struct {
CaptchaCertificate string `json:"captchaCertificate"`
}
}
//验证码短信修改密码
type RequestResetPassword struct {
Phone string `json:"phone"`
CaptchaCertificate string `json:"captchaCertificate"`
NewPwd string `json:"newPwd"`
ConfirmPwd string `json:"confirmPwd"`
}
var (
_ IUCenterParam = RequestResetPassword{}
)
func (r RequestResetPassword) Format() []byte {
var bt []byte
bt, _ = json.Marshal(r)
return bt
}
//GetPath 实现IUCenterParam接口
func (r RequestResetPassword) GetPath() (string, string) {
return "/user/resetPassword", "POST"
}
... ...
package usermy
import (
"oppmg/common/log"
"oppmg/protocol"
"oppmg/services/ucenter"
"oppmg/storage/redisdata"
)
//个人数据
//个人重置密码
func ResetPasswordBySms(phone string, newPwd string, confirmPwd string) error {
var (
certificate string
err error
)
certificate, err = redisdata.GetCaptchAuth(phone)
if err != nil {
log.Error("从redis获取凭证失败:%s", err)
return protocol.NewErrWithMessage("10020")
}
err = ucenter.RequestUCenterRestPassword(phone, newPwd, confirmPwd, certificate)
if err != nil {
log.Error("密码重置失败:%s", err)
return protocol.NewErrWithMessage("1")
}
return nil
}
... ...
... ... @@ -12,7 +12,8 @@ const (
)
const (
LOGIN_TOKEN_EXP = 1800 * time.Second //token 过期时间30分钟
LOGIN_TOKEN_EXP = 1800 * time.Second //token 过期时间30分钟
CAPTCHA_AUTH_EXP = 300 * time.Second // 验证码凭证 过期时间5分钟
)
type RedisLoginToken struct {
... ... @@ -25,3 +26,8 @@ func GetKeyLoginToken(userid int64, companyid int64) string {
key := fmt.Sprintf("%s%s:%d_%d", KEY_PREFIX, KEY_USER_TOKEN, userid, companyid)
return key
}
func GetKeyCaptchAuth(phone string) string {
key := fmt.Sprintf("%s%s:%s", KEY_PREFIX, KEY_CAPTCHA_AUTH, phone)
return key
}
... ...
... ... @@ -83,4 +83,16 @@ func RefreshLoginTokenExpires(userid int64, companyid int64) error {
return nil
}
// func Set
func SetCaptchAuth(phone string, captchAuth string) error {
key := GetKeyCaptchAuth(phone)
client := redis.GetRedis()
err := client.Set(key, captchAuth, CAPTCHA_AUTH_EXP).Err()
return err
}
func GetCaptchAuth(phone string) (string, error) {
key := GetKeyCaptchAuth(phone)
client := redis.GetRedis()
r, err := client.Get(key).Result()
return r, err
}
... ...