作者 唐旭辉

添加功能 重置个人密码

@@ -135,6 +135,32 @@ func (c *AuthController) SmsCode() { @@ -135,6 +135,32 @@ func (c *AuthController) SmsCode() {
135 return 135 return
136 } 136 }
137 137
  138 +//SmsCode 验证码短信校验
  139 +//@router /auth/smscode/check
  140 +func (c *AuthController) SmsCodeCheck() {
  141 + var msg *protocol.ResponseMessage
  142 + defer func() {
  143 + c.ResposeJson(msg)
  144 + }()
  145 + type Parameter struct {
  146 + Phone string `json:"phone"`
  147 + Code string `json:"code"`
  148 + }
  149 + var param Parameter
  150 + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
  151 + log.Error("json 解析失败", err)
  152 + msg = protocol.BadRequestParam("1")
  153 + return
  154 + }
  155 + if len(param.Phone) == 0 {
  156 + msg = protocol.BadRequestParam("1")
  157 + return
  158 + }
  159 + err := serveauth.SmsCodeCheck(param.Phone, param.Code)
  160 + msg = protocol.NewReturnResponse(nil, err)
  161 + return
  162 +}
  163 +
138 //ChangeCompany 切换公司 164 //ChangeCompany 切换公司
139 //@Router /change_company [post] 165 //@Router /change_company [post]
140 func (c *AuthController) ChangeCompany() { 166 func (c *AuthController) ChangeCompany() {
  1 +package controllers
  2 +
  3 +import (
  4 + "encoding/json"
  5 + "oppmg/common/log"
  6 + "oppmg/protocol"
  7 + "oppmg/services/usermy"
  8 +)
  9 +
  10 +// MyController 个人中心
  11 +type MyController struct {
  12 + BaseController
  13 +}
  14 +
  15 +//ResetPassword 重置密码
  16 +//@router /my/reset_password
  17 +func (c *MyController) ResetPassword() {
  18 + var msg *protocol.ResponseMessage
  19 + defer func() {
  20 + c.ResposeJson(msg)
  21 + }()
  22 + type Parameter struct {
  23 + Phone string `json:"phone"`
  24 + NewPwd string `json:"newpwd"`
  25 + ConfirmPwd string `json:"confirmpwd"`
  26 + }
  27 + var param Parameter
  28 + if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
  29 + log.Error("json 解析失败 err:%s", err)
  30 + msg = protocol.BadRequestParam("1")
  31 + return
  32 + }
  33 + companyid := c.GetCompanyId()
  34 + userId := c.GetUserId()
  35 + if companyid <= 0 {
  36 + msg = protocol.BadRequestParam("1")
  37 + return
  38 + }
  39 + if userId <= 0 {
  40 + msg = protocol.BadRequestParam("1")
  41 + return
  42 + }
  43 + err := usermy.ResetPasswordBySms(param.Phone, param.NewPwd, param.ConfirmPwd)
  44 + msg = protocol.NewReturnResponse(nil, err)
  45 + return
  46 +}
@@ -19,15 +19,17 @@ var errmessge ErrorMap = map[string]string{ @@ -19,15 +19,17 @@ var errmessge ErrorMap = map[string]string{
19 "10013": "职位已存在", 19 "10013": "职位已存在",
20 "10014": "职位名称最多10个字符", 20 "10014": "职位名称最多10个字符",
21 //安全认证相关 21 //安全认证相关
  22 + "10020": "验证码过期",
22 "10021": "账号或密码不正确", 23 "10021": "账号或密码不正确",
23 "10022": "账号已被禁用", 24 "10022": "账号已被禁用",
24 "10023": "用户无使用权限", 25 "10023": "用户无使用权限",
25 "10024": "登录凭证失效", 26 "10024": "登录凭证失效",
26 "10025": "该账号已在其他地方登录", 27 "10025": "该账号已在其他地方登录",
27 - // "10026": "登录凭证过期", 28 + "10026": "验证码校验失败",
28 "10027": "无操作权限", 29 "10027": "无操作权限",
29 "10028": "验证码错误", 30 "10028": "验证码错误",
30 "10029": "获取验证码失败", 31 "10029": "获取验证码失败",
  32 +
31 //用户相关 33 //用户相关
32 "10031": "无效角色", 34 "10031": "无效角色",
33 "10032": "无效部门", 35 "10032": "无效部门",
@@ -38,7 +40,7 @@ var errmessge ErrorMap = map[string]string{ @@ -38,7 +40,7 @@ var errmessge ErrorMap = map[string]string{
38 "10037": "用户的部门必填", 40 "10037": "用户的部门必填",
39 "10038": "用户的角色必填", 41 "10038": "用户的角色必填",
40 "10039": "用户已存在", 42 "10039": "用户已存在",
41 - "10040": "注册用户失败", 43 + // "10040": "注册用户失败",
42 //部门相关 44 //部门相关
43 "10041": "无效的主管设置", 45 "10041": "无效的主管设置",
44 "10042": "无效的上级部门", 46 "10042": "无效的上级部门",
@@ -49,6 +51,7 @@ var errmessge ErrorMap = map[string]string{ @@ -49,6 +51,7 @@ var errmessge ErrorMap = map[string]string{
49 "10047": "只能删除没有成员的部门,需要先删除部门下的员工,再删除该部门", 51 "10047": "只能删除没有成员的部门,需要先删除部门下的员工,再删除该部门",
50 //用户中心相关 52 //用户中心相关
51 "10051": "无法从远端接口获取公司数据", 53 "10051": "无法从远端接口获取公司数据",
  54 + "10052": "服务调用失败",
52 //模板相关 55 //模板相关
53 "10061": "请先删除该分类下的二级分类", 56 "10061": "请先删除该分类下的二级分类",
54 "10062": "该分类已被使用无法删除", 57 "10062": "该分类已被使用无法删除",
@@ -57,6 +57,7 @@ func init() { @@ -57,6 +57,7 @@ func init() {
57 beego.NSRouter("/change_company", &controllers.AuthController{}, "post:ChangeCompany"), 57 beego.NSRouter("/change_company", &controllers.AuthController{}, "post:ChangeCompany"),
58 //beego.NSRouter("/refresh_token", &controllers.AuthController{}, "post:RefreshToken"), 58 //beego.NSRouter("/refresh_token", &controllers.AuthController{}, "post:RefreshToken"),
59 beego.NSRouter("/me", &controllers.AuthController{}, "post:Me"), 59 beego.NSRouter("/me", &controllers.AuthController{}, "post:Me"),
  60 + beego.NSRouter("/smscode/check", &controllers.AuthController{}, "post:SmsCodeCheck"),
60 ), 61 ),
61 beego.NSNamespace("/bulletin", 62 beego.NSNamespace("/bulletin",
62 beego.NSRouter("/add", &controllers.BulletinController{}, "post:BulletinRelease"), 63 beego.NSRouter("/add", &controllers.BulletinController{}, "post:BulletinRelease"),
@@ -90,6 +91,9 @@ func init() { @@ -90,6 +91,9 @@ func init() {
90 beego.NSNamespace("/upload", 91 beego.NSNamespace("/upload",
91 beego.NSRouter("/image", &controllers.UploadController{}, "post:UploadImage"), 92 beego.NSRouter("/image", &controllers.UploadController{}, "post:UploadImage"),
92 ), 93 ),
  94 + beego.NSNamespace("/my",
  95 + beego.NSRouter("/reset_password", &controllers.MyController{}, "post:ResetPassword"),
  96 + ),
93 ) 97 )
94 98
95 nsAuth := beego.NewNamespace("/auth", 99 nsAuth := beego.NewNamespace("/auth",
@@ -100,6 +104,7 @@ func init() { @@ -100,6 +104,7 @@ func init() {
100 beego.NSRouter("/verifyCaptcha", &controllers.AuthController{}, "post:ValidateGeetest"), 104 beego.NSRouter("/verifyCaptcha", &controllers.AuthController{}, "post:ValidateGeetest"),
101 beego.NSRouter("/login_sms", &controllers.AuthController{}, "post:LoginSms"), 105 beego.NSRouter("/login_sms", &controllers.AuthController{}, "post:LoginSms"),
102 beego.NSRouter("/sms_code", &controllers.AuthController{}, "post:SmsCode"), 106 beego.NSRouter("/sms_code", &controllers.AuthController{}, "post:SmsCode"),
  107 + beego.NSRouter("/sms_code/check", &controllers.AuthController{}, "post:SmsCodeCheck"),
103 ) 108 )
104 nsUcenter := beego.NewNamespace("/ucenter", 109 nsUcenter := beego.NewNamespace("/ucenter",
105 beego.NSBefore(middleware.LogRequestData), 110 beego.NSBefore(middleware.LogRequestData),
@@ -400,5 +400,11 @@ func SmsCodeSend(phone string) error { @@ -400,5 +400,11 @@ func SmsCodeSend(phone string) error {
400 } 400 }
401 401
402 func SmsCodeCheck(phone string, code string) error { 402 func SmsCodeCheck(phone string, code string) error {
  403 + resp, err := ucenter.RequestUCenterSmsCodeCheck(phone, code)
  404 + if err != nil {
  405 + return protocol.NewErrWithMessage("10026")
  406 + }
  407 + //redis 缓存设置
  408 + redisdata.SetCaptchAuth(phone, resp.Data.CaptchaCertificate)
403 return nil 409 return nil
404 } 410 }
@@ -28,7 +28,7 @@ func RequestUCenterLogin(account, password string) (*ResponseLogin, error) { @@ -28,7 +28,7 @@ func RequestUCenterLogin(account, password string) (*ResponseLogin, error) {
28 } 28 }
29 if !(uclientReturn.Code == ResponseCode0 && 29 if !(uclientReturn.Code == ResponseCode0 &&
30 uclientReturn.Msg == ResponseMsgOk) { 30 uclientReturn.Msg == ResponseMsgOk) {
31 - return nil, protocol.NewErrWithMessage("10021") 31 + return nil, protocol.NewErrWithMessage("10052")
32 } 32 }
33 return uclientReturn, nil 33 return uclientReturn, nil
34 } 34 }
@@ -60,7 +60,7 @@ func RequestUCenterAddUser(phone string, nickname string, avatar string) (*Respo @@ -60,7 +60,7 @@ func RequestUCenterAddUser(phone string, nickname string, avatar string) (*Respo
60 } 60 }
61 if !(ucenterReturn.Code == ResponseCode0 && 61 if !(ucenterReturn.Code == ResponseCode0 &&
62 ucenterReturn.Msg == ResponseMsgOk) { 62 ucenterReturn.Msg == ResponseMsgOk) {
63 - return nil, protocol.NewErrWithMessage("10040") 63 + return nil, protocol.NewErrWithMessage("10052")
64 } 64 }
65 return ucenterReturn, nil 65 return ucenterReturn, nil
66 } 66 }
@@ -87,7 +87,7 @@ func RequestUCenterSmsCode(phone string) error { @@ -87,7 +87,7 @@ func RequestUCenterSmsCode(phone string) error {
87 } 87 }
88 if !(ucenterReturn.Code == ResponseCode0 && 88 if !(ucenterReturn.Code == ResponseCode0 &&
89 ucenterReturn.Msg == ResponseMsgOk) { 89 ucenterReturn.Msg == ResponseMsgOk) {
90 - return protocol.NewErrWithMessage("10040") 90 + return protocol.NewErrWithMessage("10052")
91 } 91 }
92 return nil 92 return nil
93 } 93 }
@@ -113,7 +113,58 @@ func RequestUCenterLoginSms(phone string, captcha string) (*ResponseLoginSms, er @@ -113,7 +113,58 @@ func RequestUCenterLoginSms(phone string, captcha string) (*ResponseLoginSms, er
113 } 113 }
114 if !(ucenterReturn.Code == ResponseCode0 && 114 if !(ucenterReturn.Code == ResponseCode0 &&
115 ucenterReturn.Msg == ResponseMsgOk) { 115 ucenterReturn.Msg == ResponseMsgOk) {
116 - return nil, protocol.NewErrWithMessage("10040") 116 + return nil, protocol.NewErrWithMessage("10052")
117 } 117 }
118 return ucenterReturn, nil 118 return ucenterReturn, nil
119 } 119 }
  120 +
  121 +func RequestUCenterSmsCodeCheck(phone string, captcha string) (*ResponseSmsCodeCheck, error) {
  122 + param := RequestSmsCodeCheck{
  123 + Phone: phone,
  124 + Captcha: captcha,
  125 + }
  126 + uclient := NewUCenterClient()
  127 + btBody, err := uclient.Call(param)
  128 + if err != nil {
  129 + log.Error("统一用户中心请求失败 err:%s", err)
  130 + return nil, protocol.NewErrWithMessage("1")
  131 + }
  132 + var ucenterReturn *ResponseSmsCodeCheck
  133 + err = json.Unmarshal(btBody, ucenterReturn)
  134 + if err != nil {
  135 + log.Error("解析统一用户中心响应失败 err:%s", err)
  136 + return nil, protocol.NewErrWithMessage("1")
  137 + }
  138 + if !(ucenterReturn.Code == ResponseCode0 &&
  139 + ucenterReturn.Msg == ResponseMsgOk) {
  140 + return nil, protocol.NewErrWithMessage("10026")
  141 + }
  142 + return ucenterReturn, nil
  143 +}
  144 +
  145 +func RequestUCenterRestPassword(phone, newPwd, confirmPwd, certificate string) error {
  146 + param := RequestResetPassword{
  147 + Phone: phone,
  148 + CaptchaCertificate: certificate,
  149 + NewPwd: newPwd,
  150 + ConfirmPwd: confirmPwd,
  151 + }
  152 + uclient := NewUCenterClient()
  153 + btBody, err := uclient.Call(param)
  154 + if err != nil {
  155 + log.Error("统一用户中心请求失败 err:%s", err)
  156 + return protocol.NewErrWithMessage("1")
  157 + }
  158 + var ucenterReturn *CommResponse
  159 + err = json.Unmarshal(btBody, ucenterReturn)
  160 + if err != nil {
  161 + log.Error("解析统一用户中心响应失败 err:%s", err)
  162 + return protocol.NewErrWithMessage("1")
  163 + }
  164 + if !(ucenterReturn.Code == ResponseCode0 &&
  165 + ucenterReturn.Msg == ResponseMsgOk) {
  166 + return protocol.NewErrWithMessage("10052")
  167 + }
  168 +
  169 + return nil
  170 +}
@@ -164,3 +164,55 @@ type LoginSmsData struct { @@ -164,3 +164,55 @@ type LoginSmsData struct {
164 Imtoken string `json:"imToken"` 164 Imtoken string `json:"imToken"`
165 CsAccountID int64 `json:"csAccountID"` 165 CsAccountID int64 `json:"csAccountID"`
166 } 166 }
  167 +
  168 +//验证码短信校验
  169 +type RequestSmsCodeCheck struct {
  170 + Phone string `json:"phone"`
  171 + Captcha string `json:"captcha"`
  172 +}
  173 +
  174 +var (
  175 + _ IUCenterParam = RequestSmsCodeCheck{}
  176 +)
  177 +
  178 +func (r RequestSmsCodeCheck) Format() []byte {
  179 + var bt []byte
  180 + bt, _ = json.Marshal(r)
  181 + return bt
  182 +}
  183 +
  184 +//GetPath 实现IUCenterParam接口
  185 +func (r RequestSmsCodeCheck) GetPath() (string, string) {
  186 + return "/user/checkSmsCode", "POST"
  187 +}
  188 +
  189 +//验证码校验结果
  190 +type ResponseSmsCodeCheck struct {
  191 + CommResponse
  192 + Data struct {
  193 + CaptchaCertificate string `json:"captchaCertificate"`
  194 + }
  195 +}
  196 +
  197 +//验证码短信修改密码
  198 +type RequestResetPassword struct {
  199 + Phone string `json:"phone"`
  200 + CaptchaCertificate string `json:"captchaCertificate"`
  201 + NewPwd string `json:"newPwd"`
  202 + ConfirmPwd string `json:"confirmPwd"`
  203 +}
  204 +
  205 +var (
  206 + _ IUCenterParam = RequestResetPassword{}
  207 +)
  208 +
  209 +func (r RequestResetPassword) Format() []byte {
  210 + var bt []byte
  211 + bt, _ = json.Marshal(r)
  212 + return bt
  213 +}
  214 +
  215 +//GetPath 实现IUCenterParam接口
  216 +func (r RequestResetPassword) GetPath() (string, string) {
  217 + return "/user/resetPassword", "POST"
  218 +}
  1 +package usermy
  2 +
  3 +import (
  4 + "oppmg/common/log"
  5 + "oppmg/protocol"
  6 + "oppmg/services/ucenter"
  7 + "oppmg/storage/redisdata"
  8 +)
  9 +
  10 +//个人数据
  11 +//个人重置密码
  12 +func ResetPasswordBySms(phone string, newPwd string, confirmPwd string) error {
  13 + var (
  14 + certificate string
  15 + err error
  16 + )
  17 + certificate, err = redisdata.GetCaptchAuth(phone)
  18 + if err != nil {
  19 + log.Error("从redis获取凭证失败:%s", err)
  20 + return protocol.NewErrWithMessage("10020")
  21 + }
  22 + err = ucenter.RequestUCenterRestPassword(phone, newPwd, confirmPwd, certificate)
  23 + if err != nil {
  24 + log.Error("密码重置失败:%s", err)
  25 + return protocol.NewErrWithMessage("1")
  26 + }
  27 + return nil
  28 +}
@@ -12,7 +12,8 @@ const ( @@ -12,7 +12,8 @@ const (
12 ) 12 )
13 13
14 const ( 14 const (
15 - LOGIN_TOKEN_EXP = 1800 * time.Second //token 过期时间30分钟 15 + LOGIN_TOKEN_EXP = 1800 * time.Second //token 过期时间30分钟
  16 + CAPTCHA_AUTH_EXP = 300 * time.Second // 验证码凭证 过期时间5分钟
16 ) 17 )
17 18
18 type RedisLoginToken struct { 19 type RedisLoginToken struct {
@@ -25,3 +26,8 @@ func GetKeyLoginToken(userid int64, companyid int64) string { @@ -25,3 +26,8 @@ func GetKeyLoginToken(userid int64, companyid int64) string {
25 key := fmt.Sprintf("%s%s:%d_%d", KEY_PREFIX, KEY_USER_TOKEN, userid, companyid) 26 key := fmt.Sprintf("%s%s:%d_%d", KEY_PREFIX, KEY_USER_TOKEN, userid, companyid)
26 return key 27 return key
27 } 28 }
  29 +
  30 +func GetKeyCaptchAuth(phone string) string {
  31 + key := fmt.Sprintf("%s%s:%s", KEY_PREFIX, KEY_CAPTCHA_AUTH, phone)
  32 + return key
  33 +}
@@ -83,4 +83,16 @@ func RefreshLoginTokenExpires(userid int64, companyid int64) error { @@ -83,4 +83,16 @@ func RefreshLoginTokenExpires(userid int64, companyid int64) error {
83 return nil 83 return nil
84 } 84 }
85 85
86 -// func Set 86 +func SetCaptchAuth(phone string, captchAuth string) error {
  87 + key := GetKeyCaptchAuth(phone)
  88 + client := redis.GetRedis()
  89 + err := client.Set(key, captchAuth, CAPTCHA_AUTH_EXP).Err()
  90 + return err
  91 +}
  92 +
  93 +func GetCaptchAuth(phone string) (string, error) {
  94 + key := GetKeyCaptchAuth(phone)
  95 + client := redis.GetRedis()
  96 + r, err := client.Get(key).Result()
  97 + return r, err
  98 +}