- BindMiniProgram
This commit is contained in:
@@ -19,7 +19,7 @@ import (
|
||||
const (
|
||||
LoginType = "weixinsns"
|
||||
LoginTypeMiniProgram = "weixinmini"
|
||||
DefTempPasswordDuration = 5 * time.Minute // 登录时间限制在5分钟内
|
||||
DefTempPasswordDuration = 20 * time.Minute // 登录时间限制在5分钟内
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -38,7 +38,7 @@ var (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrExceptionalLogin = errors.New("登录异常,无前置微信授权操作")
|
||||
ErrExceptionalLogin = errors.New("登录异常,超时,请重走绑定流程")
|
||||
)
|
||||
|
||||
type Auther struct {
|
||||
@@ -87,7 +87,6 @@ func GetWeiXinUserInfo(code string, state string) (userInfo *UserInfoExt, err er
|
||||
TempPassword: utils.GetUUID(),
|
||||
}
|
||||
globals.SugarLogger.Debugf("GetUserInfo code:%s, pwd:%s", code, userInfo.TempPassword)
|
||||
// api.Cacher.Set(wxUserinfo.OpenID, userInfo.TempPassword, DefTempPasswordDuration)
|
||||
cacheSNSInfo(wxUserinfo, userInfo.TempPassword, DefTempPasswordDuration)
|
||||
user, err2 := dao.GetWeiXinUserByIDs(dao.GetDB(), "", wxUserinfo.UnionID, wxUserinfo.OpenID, "")
|
||||
if err = err2; err == nil {
|
||||
@@ -189,24 +188,27 @@ func (a *AutherMiniProgram) BindWeiXin(ctx *jxcontext.Context, code, nickName st
|
||||
return auth.ConvertErr2NoUser(err, "")
|
||||
}
|
||||
|
||||
// 绑定手机加登录
|
||||
// func (a *AutherMiniProgram) BindMobile2(code, nickName, mobileNum, verifyCode string) (loginInfo *auth.LoginInfo, err error) {
|
||||
// globals.SugarLogger.Debugf("BindMobile2 code:%s, nickName:%s, mobileNum:%s, verifyCode:%s", code, nickName, mobileNum, verifyCode)
|
||||
// err = ErrExceptionalLogin
|
||||
// if mobile.VerifyCode(mobileNum, code) {
|
||||
// sessionInfo, err := api.WeixinMiniAPI.SNSCode2Session(code)
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
// if err = dao.UpdateWeiXinUser(dao.GetDB(), mobileNum, nickName, sessionInfo.UnionID, "", sessionInfo.OpenID); err == nil {
|
||||
// api.Cacher.Set(composeSessionKeyCacheKey(sessionInfo.OpenID), sessionInfo.SessionKey, auth.DefTokenDuration)
|
||||
// loginInfo = auth.CreateLoginInfo(mobileNum, mobile.LoginType)
|
||||
// }
|
||||
// } else {
|
||||
// err = ErrVerifyCodeIsWrong
|
||||
// }
|
||||
// return loginInfo, err
|
||||
// }
|
||||
// 绑定小程序
|
||||
func (a *AutherMiniProgram) BindMiniProgram(ctx *jxcontext.Context, code string) (err error) {
|
||||
globals.SugarLogger.Debugf("BindMiniProgram code:%s", code)
|
||||
if ctx.GetLoginType() != mobile.LoginType {
|
||||
return errors.New("登录方式应该为手机")
|
||||
}
|
||||
sessionInfo, err := api.WeixinMiniAPI.SNSCode2Session(code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db := dao.GetDB()
|
||||
user, err := dao.GetWeiXinUserByIDs(db, ctx.GetLoginID(), "", "", "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if user.OpenIDUnion != sessionInfo.UnionID {
|
||||
return errors.New("绑定用户不匹配")
|
||||
}
|
||||
err = auth.ConvertErr2NoUser(dao.UpdateWeiXinUser(db, user.Tel, "", "", "", sessionInfo.OpenID), user.Tel)
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *AutherMiniProgram) Login(mobileNum, code string) (userID, LoginType string, err error) {
|
||||
globals.SugarLogger.Debugf("AutherMiniProgram Login mobileNum:%s, code:%s", mobileNum, code)
|
||||
@@ -225,7 +227,6 @@ func (a *AutherMiniProgram) Login(mobileNum, code string) (userID, LoginType str
|
||||
|
||||
}
|
||||
api.Cacher.Set(composeSessionKeyCacheKey(user.Tel), sessionInfo.SessionKey, auth.DefTokenDuration)
|
||||
err = auth.ConvertErr2NoUser(dao.UpdateWeiXinUser(db, user.Tel, "", sessionInfo.UnionID, "", sessionInfo.OpenID), mobileNum)
|
||||
return user.Tel, mobile.LoginType, err
|
||||
}
|
||||
|
||||
|
||||
@@ -71,6 +71,14 @@ func (ctx *Context) GetUserName() string {
|
||||
return userName
|
||||
}
|
||||
|
||||
func (ctx *Context) GetLoginID() string {
|
||||
if ctx.userInfo != nil {
|
||||
return ctx.userInfo.ID
|
||||
}
|
||||
return ""
|
||||
|
||||
}
|
||||
|
||||
func (ctx *Context) GetLoginType() string {
|
||||
if ctx.userInfo != nil {
|
||||
return ctx.userInfo.LoginType
|
||||
@@ -78,7 +86,7 @@ func (ctx *Context) GetLoginType() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (ctx *Context) GetUserID() string {
|
||||
func (ctx *Context) GetToken() string {
|
||||
return ctx.token
|
||||
}
|
||||
|
||||
|
||||
@@ -122,7 +122,7 @@ func (c *AuthController) SendMobileVerifyCode() {
|
||||
}
|
||||
|
||||
// @Title 绑定手机
|
||||
// @Description 绑定手机
|
||||
// @Description 绑定手机,待删除
|
||||
// @Param token header string true "认证token"
|
||||
// @Param mobile formData string true "手机号"
|
||||
// @Param code formData string true "验证码"
|
||||
@@ -140,39 +140,6 @@ func (c *AuthController) BindMobile() {
|
||||
})
|
||||
}
|
||||
|
||||
// @Title 绑定手机
|
||||
// @Description 绑定手机(调用此方法前先需要以短信方式登录)
|
||||
// @Param token header string true "认证token"
|
||||
// @Param code formData string true "小程序用户code"
|
||||
// @Param nickname formData string false "用户名"
|
||||
// @Success 200 {object} controllers.CallResult
|
||||
// @Failure 200 {object} controllers.CallResult
|
||||
// @router /MiniBindWeiXin [post]
|
||||
func (c *AuthController) MiniBindWeiXin() {
|
||||
c.callMiniBindWeiXin(func(params *tAuthMiniBindWeiXinParams) (retVal interface{}, errCode string, err error) {
|
||||
err = weixin.AutherMini.BindWeiXin(params.Ctx, params.Code, params.Nickname)
|
||||
if err == auth.ErrUserNotExist {
|
||||
return retVal, model.ErrCodeUserNotExist, err
|
||||
}
|
||||
return retVal, "", err
|
||||
})
|
||||
}
|
||||
|
||||
// @Title 解密小程序数据
|
||||
// @Description 解密小程序数据
|
||||
// @Param token header string true "认证token"
|
||||
// @Param data formData string true "加密数据"
|
||||
// @Param iv formData string true "iv"
|
||||
// @Success 200 {object} controllers.CallResult
|
||||
// @Failure 200 {object} controllers.CallResult
|
||||
// @router /MiniDecryptData [post]
|
||||
func (c *AuthController) MiniDecryptData() {
|
||||
c.callMiniDecryptData(func(params *tAuthMiniDecryptDataParams) (retVal interface{}, errCode string, err error) {
|
||||
retVal, err = weixin.AutherMini.DecryptData(params.Ctx, params.Data, params.Iv)
|
||||
return retVal, "", err
|
||||
})
|
||||
}
|
||||
|
||||
// @Title 微信公众号绑定手机2
|
||||
// @Description 微信公众号绑定手机2
|
||||
// @Param openID formData string true "微信公众号ID"
|
||||
@@ -192,3 +159,53 @@ func (c *AuthController) BindMobile2() {
|
||||
return retVal, "", err
|
||||
})
|
||||
}
|
||||
|
||||
// @Title 绑定手机
|
||||
// @Description 绑定手机(调用此方法前先需要以短信方式登录),待删除
|
||||
// @Param token header string true "认证token"
|
||||
// @Param code formData string true "小程序用户code"
|
||||
// @Param nickname formData string false "用户名"
|
||||
// @Success 200 {object} controllers.CallResult
|
||||
// @Failure 200 {object} controllers.CallResult
|
||||
// @router /MiniBindWeiXin [post]
|
||||
func (c *AuthController) MiniBindWeiXin() {
|
||||
c.callMiniBindWeiXin(func(params *tAuthMiniBindWeiXinParams) (retVal interface{}, errCode string, err error) {
|
||||
err = weixin.AutherMini.BindWeiXin(params.Ctx, params.Code, params.Nickname)
|
||||
if err == auth.ErrUserNotExist {
|
||||
return retVal, model.ErrCodeUserNotExist, err
|
||||
}
|
||||
return retVal, "", err
|
||||
})
|
||||
}
|
||||
|
||||
// @Title 绑定小程序
|
||||
// @Description 绑定小程序
|
||||
// / @Param token header string true "认证token"
|
||||
// @Param code formData string true "小程序用户code"
|
||||
// @Success 200 {object} controllers.CallResult
|
||||
// @Failure 200 {object} controllers.CallResult
|
||||
// @router /BindMiniProgram [post]
|
||||
func (c *AuthController) BindMiniProgram() {
|
||||
c.callBindMiniProgram(func(params *tAuthBindMiniProgramParams) (retVal interface{}, errCode string, err error) {
|
||||
err = weixin.AutherMini.BindMiniProgram(params.Ctx, params.Code)
|
||||
if err == auth.ErrUserNotExist {
|
||||
return retVal, model.ErrCodeUserNotExist, err
|
||||
}
|
||||
return retVal, "", err
|
||||
})
|
||||
}
|
||||
|
||||
// @Title 解密小程序数据
|
||||
// @Description 解密小程序数据
|
||||
// @Param token header string true "认证token"
|
||||
// @Param data formData string true "加密数据"
|
||||
// @Param iv formData string true "iv"
|
||||
// @Success 200 {object} controllers.CallResult
|
||||
// @Failure 200 {object} controllers.CallResult
|
||||
// @router /MiniDecryptData [post]
|
||||
func (c *AuthController) MiniDecryptData() {
|
||||
c.callMiniDecryptData(func(params *tAuthMiniDecryptDataParams) (retVal interface{}, errCode string, err error) {
|
||||
retVal, err = weixin.AutherMini.DecryptData(params.Ctx, params.Data, params.Iv)
|
||||
return retVal, "", err
|
||||
})
|
||||
}
|
||||
|
||||
@@ -7,6 +7,14 @@ import (
|
||||
|
||||
func init() {
|
||||
|
||||
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:AuthController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:AuthController"],
|
||||
beego.ControllerComments{
|
||||
Method: "BindMiniProgram",
|
||||
Router: `/BindMiniProgram`,
|
||||
AllowHTTPMethods: []string{"post"},
|
||||
MethodParams: param.Make(),
|
||||
Params: nil})
|
||||
|
||||
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:AuthController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:AuthController"],
|
||||
beego.ControllerComments{
|
||||
Method: "BindMobile",
|
||||
|
||||
Reference in New Issue
Block a user