- BindMiniProgram
This commit is contained in:
@@ -19,7 +19,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
LoginType = "weixinsns"
|
LoginType = "weixinsns"
|
||||||
LoginTypeMiniProgram = "weixinmini"
|
LoginTypeMiniProgram = "weixinmini"
|
||||||
DefTempPasswordDuration = 5 * time.Minute // 登录时间限制在5分钟内
|
DefTempPasswordDuration = 20 * time.Minute // 登录时间限制在5分钟内
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -38,7 +38,7 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrExceptionalLogin = errors.New("登录异常,无前置微信授权操作")
|
ErrExceptionalLogin = errors.New("登录异常,超时,请重走绑定流程")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Auther struct {
|
type Auther struct {
|
||||||
@@ -87,7 +87,6 @@ func GetWeiXinUserInfo(code string, state string) (userInfo *UserInfoExt, err er
|
|||||||
TempPassword: utils.GetUUID(),
|
TempPassword: utils.GetUUID(),
|
||||||
}
|
}
|
||||||
globals.SugarLogger.Debugf("GetUserInfo code:%s, pwd:%s", code, userInfo.TempPassword)
|
globals.SugarLogger.Debugf("GetUserInfo code:%s, pwd:%s", code, userInfo.TempPassword)
|
||||||
// api.Cacher.Set(wxUserinfo.OpenID, userInfo.TempPassword, DefTempPasswordDuration)
|
|
||||||
cacheSNSInfo(wxUserinfo, userInfo.TempPassword, DefTempPasswordDuration)
|
cacheSNSInfo(wxUserinfo, userInfo.TempPassword, DefTempPasswordDuration)
|
||||||
user, err2 := dao.GetWeiXinUserByIDs(dao.GetDB(), "", wxUserinfo.UnionID, wxUserinfo.OpenID, "")
|
user, err2 := dao.GetWeiXinUserByIDs(dao.GetDB(), "", wxUserinfo.UnionID, wxUserinfo.OpenID, "")
|
||||||
if err = err2; err == nil {
|
if err = err2; err == nil {
|
||||||
@@ -189,24 +188,27 @@ func (a *AutherMiniProgram) BindWeiXin(ctx *jxcontext.Context, code, nickName st
|
|||||||
return auth.ConvertErr2NoUser(err, "")
|
return auth.ConvertErr2NoUser(err, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 绑定手机加登录
|
// 绑定小程序
|
||||||
// func (a *AutherMiniProgram) BindMobile2(code, nickName, mobileNum, verifyCode string) (loginInfo *auth.LoginInfo, err error) {
|
func (a *AutherMiniProgram) BindMiniProgram(ctx *jxcontext.Context, code string) (err error) {
|
||||||
// globals.SugarLogger.Debugf("BindMobile2 code:%s, nickName:%s, mobileNum:%s, verifyCode:%s", code, nickName, mobileNum, verifyCode)
|
globals.SugarLogger.Debugf("BindMiniProgram code:%s", code)
|
||||||
// err = ErrExceptionalLogin
|
if ctx.GetLoginType() != mobile.LoginType {
|
||||||
// if mobile.VerifyCode(mobileNum, code) {
|
return errors.New("登录方式应该为手机")
|
||||||
// sessionInfo, err := api.WeixinMiniAPI.SNSCode2Session(code)
|
}
|
||||||
// if err != nil {
|
sessionInfo, err := api.WeixinMiniAPI.SNSCode2Session(code)
|
||||||
// return nil, err
|
if err != nil {
|
||||||
// }
|
return 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)
|
db := dao.GetDB()
|
||||||
// loginInfo = auth.CreateLoginInfo(mobileNum, mobile.LoginType)
|
user, err := dao.GetWeiXinUserByIDs(db, ctx.GetLoginID(), "", "", "")
|
||||||
// }
|
if err != nil {
|
||||||
// } else {
|
return err
|
||||||
// err = ErrVerifyCodeIsWrong
|
}
|
||||||
// }
|
if user.OpenIDUnion != sessionInfo.UnionID {
|
||||||
// return loginInfo, err
|
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) {
|
func (a *AutherMiniProgram) Login(mobileNum, code string) (userID, LoginType string, err error) {
|
||||||
globals.SugarLogger.Debugf("AutherMiniProgram Login mobileNum:%s, code:%s", mobileNum, code)
|
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)
|
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
|
return user.Tel, mobile.LoginType, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -71,6 +71,14 @@ func (ctx *Context) GetUserName() string {
|
|||||||
return userName
|
return userName
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ctx *Context) GetLoginID() string {
|
||||||
|
if ctx.userInfo != nil {
|
||||||
|
return ctx.userInfo.ID
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func (ctx *Context) GetLoginType() string {
|
func (ctx *Context) GetLoginType() string {
|
||||||
if ctx.userInfo != nil {
|
if ctx.userInfo != nil {
|
||||||
return ctx.userInfo.LoginType
|
return ctx.userInfo.LoginType
|
||||||
@@ -78,7 +86,7 @@ func (ctx *Context) GetLoginType() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ctx *Context) GetUserID() string {
|
func (ctx *Context) GetToken() string {
|
||||||
return ctx.token
|
return ctx.token
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -122,7 +122,7 @@ func (c *AuthController) SendMobileVerifyCode() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// @Title 绑定手机
|
// @Title 绑定手机
|
||||||
// @Description 绑定手机
|
// @Description 绑定手机,待删除
|
||||||
// @Param token header string true "认证token"
|
// @Param token header string true "认证token"
|
||||||
// @Param mobile formData string true "手机号"
|
// @Param mobile formData string true "手机号"
|
||||||
// @Param code 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
|
// @Title 微信公众号绑定手机2
|
||||||
// @Description 微信公众号绑定手机2
|
// @Description 微信公众号绑定手机2
|
||||||
// @Param openID formData string true "微信公众号ID"
|
// @Param openID formData string true "微信公众号ID"
|
||||||
@@ -192,3 +159,53 @@ func (c *AuthController) BindMobile2() {
|
|||||||
return retVal, "", err
|
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() {
|
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.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:AuthController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:AuthController"],
|
||||||
beego.ControllerComments{
|
beego.ControllerComments{
|
||||||
Method: "BindMobile",
|
Method: "BindMobile",
|
||||||
|
|||||||
Reference in New Issue
Block a user