- mini program login
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package weixin
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxcallback/auth"
|
||||
"git.rosy.net.cn/jx-callback/business/jxcallback/auth/mobile"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
"git.rosy.net.cn/jx-callback/business/model/legacymodel"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
@@ -18,21 +20,32 @@ import (
|
||||
|
||||
const (
|
||||
LoginType = "weixinsns"
|
||||
LoginTypeMiniProgram = "weixinmini"
|
||||
DefTempPasswordDuration = 5 * time.Minute // 登录时间限制在5分钟内
|
||||
)
|
||||
|
||||
const (
|
||||
CacheKeySeparator = "/"
|
||||
MiniVerifyCodePrefix = "MiniVerifyCode"
|
||||
SessionKeyPrefix = "SessionKey"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrLoginFailed = errors.New("登录失败")
|
||||
StrStateIsWrong = "state:%s状态不对"
|
||||
)
|
||||
|
||||
var (
|
||||
auther *Auther
|
||||
auther *Auther
|
||||
AutherMini *AutherMiniProgram
|
||||
)
|
||||
|
||||
type Auther struct {
|
||||
}
|
||||
|
||||
type AutherMiniProgram struct {
|
||||
}
|
||||
|
||||
type UserInfoExt struct {
|
||||
weixinapi.SNSUserInfo
|
||||
TempPassword string `json:"tempPassword"` // 一段时间有效的登录密码
|
||||
@@ -41,6 +54,9 @@ type UserInfoExt struct {
|
||||
func init() {
|
||||
auther = new(Auther)
|
||||
auth.RegisterAuther(LoginType, auther)
|
||||
|
||||
AutherMini = new(AutherMiniProgram)
|
||||
auth.RegisterAuther(LoginTypeMiniProgram, AutherMini)
|
||||
}
|
||||
|
||||
func GetUserInfo(code string, state string) (token *UserInfoExt, err error) {
|
||||
@@ -66,8 +82,8 @@ func GetUserInfo(code string, state string) (token *UserInfoExt, err error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (a *Auther) Login(openid, password string) (err error) {
|
||||
globals.SugarLogger.Debugf("Login openid:%s, password:%s", openid, password)
|
||||
func (a *Auther) Login(openid, password string) (userID string, err error) {
|
||||
globals.SugarLogger.Debugf("weixinsns Login openid:%s, password:%s", openid, password)
|
||||
|
||||
if value := api.Cacher.Get(openid); value != nil {
|
||||
if password == value.(string) {
|
||||
@@ -76,17 +92,17 @@ func (a *Auther) Login(openid, password string) (err error) {
|
||||
// }
|
||||
// if err = dao.GetEntity(nil, wxUser, "OpenID"); err == nil {
|
||||
api.Cacher.Del(openid)
|
||||
return nil
|
||||
return "", nil
|
||||
// }
|
||||
}
|
||||
} else {
|
||||
err = ErrLoginFailed
|
||||
}
|
||||
return err
|
||||
return "", err
|
||||
}
|
||||
|
||||
func (a *Auther) Logout(openid string) error {
|
||||
return api.Cacher.Del(openid)
|
||||
func (a *Auther) Logout(loginInfo *auth.LoginInfo) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func BindMobile(token, mobileNum, code, nickname string) (err error) {
|
||||
@@ -122,3 +138,84 @@ func BindMobile(token, mobileNum, code, nickname string) (err error) {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// 对于小程序来说,
|
||||
// 1,用户必须先在后台创建(手机号标识)
|
||||
// 2,用户必须先绑定微信
|
||||
// 先以短信方式登录:
|
||||
// SendMobileVerifyCode
|
||||
// Login use type mobile
|
||||
// MiniBindWeiXin
|
||||
// 3,用户以CODE来登录(Login use type weixinmini)
|
||||
// Login
|
||||
|
||||
func (a *AutherMiniProgram) BindWeiXin(ctx *jxcontext.Context, code, nickName string) (err error) {
|
||||
globals.SugarLogger.Debugf("AutherMiniProgram BindWeiXin code:%s, nickName:%s", code, nickName)
|
||||
loginInfo := ctx.GetLoginInfo()
|
||||
if loginInfo.LoginType != mobile.LoginType {
|
||||
return fmt.Errorf("调用AutherMiniProgram BindWeiXin时,必须以手机验证方式登录")
|
||||
}
|
||||
user := &legacymodel.WeiXins{
|
||||
Tel: loginInfo.ID,
|
||||
}
|
||||
db := dao.GetDB()
|
||||
if err = dao.GetEntity(db, user, "Tel"); err != nil {
|
||||
return auth.ConvertErr2NoUser(err, loginInfo.ID)
|
||||
}
|
||||
sessionInfo, err := api.WeixinAPI.SNSCode2Session(code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
user.OpenIDMini = sessionInfo.OpenID
|
||||
if nickName != "" {
|
||||
user.NickName = nickName
|
||||
}
|
||||
_, err = dao.UpdateEntity(db, user)
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *AutherMiniProgram) Login(mobileNum, code string) (userID string, err error) {
|
||||
globals.SugarLogger.Debugf("AutherMiniProgram Login mobileNum:%s, code:%s", mobileNum, code)
|
||||
sessionInfo, err := api.WeixinAPI.SNSCode2Session(code)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
user := &legacymodel.WeiXins{
|
||||
OpenIDMini: sessionInfo.OpenID,
|
||||
}
|
||||
db := dao.GetDB()
|
||||
if err = dao.GetEntity(db, user, "OpenIDMini"); err != nil {
|
||||
return "", auth.ConvertErr2NoUser(err, mobileNum)
|
||||
}
|
||||
if mobileNum != user.Tel {
|
||||
|
||||
}
|
||||
api.Cacher.Set(composeSessionKeyCacheKey(sessionInfo.OpenID), sessionInfo.SessionKey, auth.DefTokenDuration)
|
||||
return sessionInfo.OpenID, err
|
||||
}
|
||||
|
||||
func (a *AutherMiniProgram) Logout(loginInfo *auth.LoginInfo) error {
|
||||
globals.SugarLogger.Debugf("AutherMiniProgram Logout openid:%s", utils.Format4Output(loginInfo, false))
|
||||
return api.Cacher.Del(composeSessionKeyCacheKey(loginInfo.ID))
|
||||
}
|
||||
|
||||
func (a *AutherMiniProgram) DecryptData(ctx *jxcontext.Context, encryptedData, iv string) (decryptedDataBase64 string, err error) {
|
||||
globals.SugarLogger.Debugf("AutherMiniProgram DecryptData encryptedData:%s, iv:%s", encryptedData, iv)
|
||||
var sessionKey string
|
||||
if err = api.Cacher.GetAs(composeSessionKeyCacheKey(ctx.GetLoginInfo().ID), &sessionKey); err != nil {
|
||||
return "", err
|
||||
}
|
||||
decryptedData, err := api.WeixinAPI.SNSDecodeMiniProgramData(encryptedData, sessionKey, iv)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.StdEncoding.EncodeToString(decryptedData), nil
|
||||
}
|
||||
|
||||
func composeMiniVerifiyCacheKey(key string) string {
|
||||
return MiniVerifyCodePrefix + CacheKeySeparator + key
|
||||
}
|
||||
|
||||
func composeSessionKeyCacheKey(key string) string {
|
||||
return SessionKeyPrefix + CacheKeySeparator + key
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user