- 注释掉老user与auth相关的代码
This commit is contained in:
@@ -1,121 +1,110 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
// const (
|
||||
// DefTokenDuration = 7 * 24 * time.Hour // 7天
|
||||
// )
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
)
|
||||
// type IAuther interface {
|
||||
// Login(id, secret string) (userID, loginType string, err error)
|
||||
// Logout(loginInfo *LoginInfo) error
|
||||
// }
|
||||
|
||||
const (
|
||||
DefTokenDuration = 7 * 24 * time.Hour // 7天
|
||||
)
|
||||
// var (
|
||||
// authers map[string]IAuther
|
||||
// )
|
||||
|
||||
type IAuther interface {
|
||||
Login(id, secret string) (userID, loginType string, err error)
|
||||
Logout(loginInfo *LoginInfo) error
|
||||
}
|
||||
// var (
|
||||
// ErrUserNotExist = errors.New("用户不存在,请联系运营创建")
|
||||
// ErrLoginTypeNotSupported = errors.New("不支持指定的登录类型")
|
||||
// ErrUIDAndPassNotMatch = errors.New("用户与密码不匹配")
|
||||
// ErrAPINeedRealLogin = errors.New("此API要求真正登录")
|
||||
// ErrIllegalLoginType = errors.New("不支持的登录类型")
|
||||
// )
|
||||
|
||||
var (
|
||||
authers map[string]IAuther
|
||||
)
|
||||
// type LoginInfo struct {
|
||||
// ID string
|
||||
// LoginType string
|
||||
// ExpiresIn int64
|
||||
// Token string
|
||||
// }
|
||||
|
||||
var (
|
||||
ErrUserNotExist = errors.New("用户不存在,请联系运营创建")
|
||||
ErrLoginTypeNotSupported = errors.New("不支持指定的登录类型")
|
||||
ErrUIDAndPassNotMatch = errors.New("用户与密码不匹配")
|
||||
ErrAPINeedRealLogin = errors.New("此API要求真正登录")
|
||||
ErrIllegalLoginType = errors.New("不支持的登录类型")
|
||||
)
|
||||
// func init() {
|
||||
// authers = make(map[string]IAuther)
|
||||
// }
|
||||
|
||||
type LoginInfo struct {
|
||||
ID string
|
||||
LoginType string
|
||||
ExpiresIn int64
|
||||
Token string
|
||||
}
|
||||
// func RegisterAuther(loginType string, handler IAuther) {
|
||||
// authers[loginType] = handler
|
||||
// }
|
||||
|
||||
func init() {
|
||||
authers = make(map[string]IAuther)
|
||||
}
|
||||
// func CreateLoginInfo(id, loginType string) (loginInfo *LoginInfo) {
|
||||
// token := utils.GetUUID()
|
||||
// loginInfo = &LoginInfo{
|
||||
// ID: id,
|
||||
// LoginType: loginType,
|
||||
// ExpiresIn: time.Now().Add(DefTokenDuration).Unix(),
|
||||
// Token: token,
|
||||
// }
|
||||
// globals.SugarLogger.Debugf("CreateLoginInfo id:%s, loginType:%s, loginInfo:%s", id, loginType, utils.Format4Output(loginInfo, true))
|
||||
// api.Cacher.Set(token, loginInfo, DefTokenDuration)
|
||||
// return loginInfo
|
||||
// }
|
||||
|
||||
func RegisterAuther(loginType string, handler IAuther) {
|
||||
authers[loginType] = handler
|
||||
}
|
||||
// func Login(id, loginType, secret string) (loginInfo *LoginInfo, err error) {
|
||||
// globals.SugarLogger.Debugf("Login id:%s, loginType:%s, secret:%s", id, loginType, secret)
|
||||
// if handler := authers[loginType]; handler != nil {
|
||||
// userID, loginType2, err2 := handler.Login(id, secret)
|
||||
// if err = err2; err == nil {
|
||||
// if userID != "" {
|
||||
// globals.SugarLogger.Debugf("Login id:%s, loginType:%s, id changed to:%s", id, loginType, userID)
|
||||
// id = userID
|
||||
// }
|
||||
// if loginType2 != "" {
|
||||
// loginType = loginType2
|
||||
// }
|
||||
// return CreateLoginInfo(id, loginType), nil
|
||||
// }
|
||||
// err = ConvertErr2NoUser(err, "")
|
||||
// } else {
|
||||
// err = ErrLoginTypeNotSupported
|
||||
// }
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
func CreateLoginInfo(id, loginType string) (loginInfo *LoginInfo) {
|
||||
token := utils.GetUUID()
|
||||
loginInfo = &LoginInfo{
|
||||
ID: id,
|
||||
LoginType: loginType,
|
||||
ExpiresIn: time.Now().Add(DefTokenDuration).Unix(),
|
||||
Token: token,
|
||||
}
|
||||
globals.SugarLogger.Debugf("CreateLoginInfo id:%s, loginType:%s, loginInfo:%s", id, loginType, utils.Format4Output(loginInfo, true))
|
||||
api.Cacher.Set(token, loginInfo, DefTokenDuration)
|
||||
return loginInfo
|
||||
}
|
||||
// func Logout(token string) (err error) {
|
||||
// globals.SugarLogger.Debugf("Logout token:%s", token)
|
||||
// loginInfo := new(LoginInfo)
|
||||
// if err = api.Cacher.GetAs(token, loginInfo); err == nil {
|
||||
// if handler := authers[loginInfo.LoginType]; handler != nil {
|
||||
// err = handler.Logout(loginInfo)
|
||||
// }
|
||||
// api.Cacher.Del(token)
|
||||
// }
|
||||
// return err
|
||||
// }
|
||||
|
||||
func Login(id, loginType, secret string) (loginInfo *LoginInfo, err error) {
|
||||
globals.SugarLogger.Debugf("Login id:%s, loginType:%s, secret:%s", id, loginType, secret)
|
||||
if handler := authers[loginType]; handler != nil {
|
||||
userID, loginType2, err2 := handler.Login(id, secret)
|
||||
if err = err2; err == nil {
|
||||
if userID != "" {
|
||||
globals.SugarLogger.Debugf("Login id:%s, loginType:%s, id changed to:%s", id, loginType, userID)
|
||||
id = userID
|
||||
}
|
||||
if loginType2 != "" {
|
||||
loginType = loginType2
|
||||
}
|
||||
return CreateLoginInfo(id, loginType), nil
|
||||
}
|
||||
err = ConvertErr2NoUser(err, "")
|
||||
} else {
|
||||
err = ErrLoginTypeNotSupported
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
// func GetUserInfo(token string) (loginInfo *LoginInfo, err error) {
|
||||
// loginInfo = new(LoginInfo)
|
||||
// if err = api.Cacher.GetAs(token, loginInfo); err == nil {
|
||||
// return loginInfo, nil
|
||||
// }
|
||||
// return nil, model.ErrTokenIsInvalid
|
||||
// }
|
||||
|
||||
func Logout(token string) (err error) {
|
||||
globals.SugarLogger.Debugf("Logout token:%s", token)
|
||||
loginInfo := new(LoginInfo)
|
||||
if err = api.Cacher.GetAs(token, loginInfo); err == nil {
|
||||
if handler := authers[loginInfo.LoginType]; handler != nil {
|
||||
err = handler.Logout(loginInfo)
|
||||
}
|
||||
api.Cacher.Del(token)
|
||||
}
|
||||
return err
|
||||
}
|
||||
// func ConvertErr2NoUser(err error, mobileNum string) error {
|
||||
// if dao.IsNoRowsError(err) {
|
||||
// err = ErrUserNotExist
|
||||
// }
|
||||
// return err
|
||||
// }
|
||||
|
||||
func GetUserInfo(token string) (loginInfo *LoginInfo, err error) {
|
||||
loginInfo = new(LoginInfo)
|
||||
if err = api.Cacher.GetAs(token, loginInfo); err == nil {
|
||||
return loginInfo, nil
|
||||
}
|
||||
return nil, model.ErrTokenIsInvalid
|
||||
}
|
||||
// func (a *LoginInfo) GetAuthID() string {
|
||||
// return a.ID
|
||||
// }
|
||||
|
||||
func ConvertErr2NoUser(err error, mobileNum string) error {
|
||||
if dao.IsNoRowsError(err) {
|
||||
err = ErrUserNotExist
|
||||
}
|
||||
return err
|
||||
}
|
||||
// func (a *LoginInfo) GetAuthType() string {
|
||||
// return a.LoginType
|
||||
// }
|
||||
|
||||
func (a *LoginInfo) GetAuthID() string {
|
||||
return a.ID
|
||||
}
|
||||
|
||||
func (a *LoginInfo) GetAuthType() string {
|
||||
return a.LoginType
|
||||
}
|
||||
|
||||
func (a *LoginInfo) GetUserTag() string {
|
||||
return a.ID
|
||||
}
|
||||
// func (a *LoginInfo) GetUserTag() string {
|
||||
// return a.ID
|
||||
// }
|
||||
|
||||
@@ -1,104 +1,90 @@
|
||||
package mobile
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
// const (
|
||||
// DefVerifyCodeDuration = 5 * time.Minute
|
||||
// TestMobile = "91112345678"
|
||||
// TestVerifyCode = "123456"
|
||||
// )
|
||||
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxcallback/auth"
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
aliyunsmsclient "github.com/KenmyZhang/aliyun-communicate"
|
||||
)
|
||||
// var (
|
||||
// warningMap = map[string]int{
|
||||
// "isv.AMOUNT_NOT_ENOUGH": 1,
|
||||
// "isv.ACCOUNT_ABNORMAL": 1,
|
||||
// "isv.OUT_OF_SERVICE": 1,
|
||||
// "isv.DAY_LIMIT_CONTROL": 1,
|
||||
// }
|
||||
// )
|
||||
|
||||
const (
|
||||
DefVerifyCodeDuration = 5 * time.Minute
|
||||
TestMobile = "91112345678"
|
||||
TestVerifyCode = "123456"
|
||||
)
|
||||
// const (
|
||||
// LoginType = "mobile"
|
||||
// )
|
||||
|
||||
var (
|
||||
warningMap = map[string]int{
|
||||
"isv.AMOUNT_NOT_ENOUGH": 1,
|
||||
"isv.ACCOUNT_ABNORMAL": 1,
|
||||
"isv.OUT_OF_SERVICE": 1,
|
||||
"isv.DAY_LIMIT_CONTROL": 1,
|
||||
}
|
||||
)
|
||||
// var (
|
||||
// ErrVerifyCodeIsWrong = errors.New("验证码错")
|
||||
// )
|
||||
|
||||
const (
|
||||
LoginType = "mobile"
|
||||
)
|
||||
// type Auther struct {
|
||||
// }
|
||||
|
||||
var (
|
||||
ErrVerifyCodeIsWrong = errors.New("验证码错")
|
||||
)
|
||||
// var (
|
||||
// auther *Auther
|
||||
// )
|
||||
|
||||
type Auther struct {
|
||||
}
|
||||
// func init() {
|
||||
// auther = new(Auther)
|
||||
// auth.RegisterAuther(LoginType, auther)
|
||||
// }
|
||||
|
||||
var (
|
||||
auther *Auther
|
||||
)
|
||||
// func SendVerifyCode(mobileNumber string) error {
|
||||
// code := fmt.Sprintf("%06d", rand.Intn(1000000))
|
||||
// globals.SugarLogger.Debugf("SendVerifyCode mobileNumber:%s, code:%s", mobileNumber, code)
|
||||
|
||||
func init() {
|
||||
auther = new(Auther)
|
||||
auth.RegisterAuther(LoginType, auther)
|
||||
}
|
||||
// smsClient := aliyunsmsclient.New("http://dysmsapi.aliyuncs.com/")
|
||||
// response, err := smsClient.Execute(globals.AliKey, globals.AliSecret, mobileNumber, "京西菜市", "SMS_84655036", string(utils.MustMarshal(map[string]interface{}{
|
||||
// "code": code,
|
||||
// })))
|
||||
// api.Cacher.Set(mobileNumber, code, DefVerifyCodeDuration)
|
||||
// if err == nil && response.Code == aliyunsmsclient.ResponseCodeOk {
|
||||
// } else {
|
||||
// if err == nil {
|
||||
// if warningMap[response.Code] == 1 {
|
||||
// globals.SugarLogger.Warnf("SendVerifyCode mobileNumber:%s failed with response:%s", mobileNumber, utils.Format4Output(response, false))
|
||||
// } else {
|
||||
// globals.SugarLogger.Infof("SendVerifyCode mobileNumber:%s failed with response:%s", mobileNumber, utils.Format4Output(response, false))
|
||||
// }
|
||||
// err = fmt.Errorf("发送短信出错:%s", response.Message)
|
||||
// } else {
|
||||
// globals.SugarLogger.Warnf("SendVerifyCode mobileNumber:%s failed with error:%v", mobileNumber, err)
|
||||
// }
|
||||
// }
|
||||
// return err
|
||||
// }
|
||||
|
||||
func SendVerifyCode(mobileNumber string) error {
|
||||
code := fmt.Sprintf("%06d", rand.Intn(1000000))
|
||||
globals.SugarLogger.Debugf("SendVerifyCode mobileNumber:%s, code:%s", mobileNumber, code)
|
||||
// func VerifyCode(mobileNumber, code string) (err error) {
|
||||
// globals.SugarLogger.Debugf("VerifyCode mobileNumber:%s, code:%s", mobileNumber, code)
|
||||
|
||||
smsClient := aliyunsmsclient.New("http://dysmsapi.aliyuncs.com/")
|
||||
response, err := smsClient.Execute(globals.AliKey, globals.AliSecret, mobileNumber, "京西菜市", "SMS_84655036", string(utils.MustMarshal(map[string]interface{}{
|
||||
"code": code,
|
||||
})))
|
||||
api.Cacher.Set(mobileNumber, code, DefVerifyCodeDuration)
|
||||
if err == nil && response.Code == aliyunsmsclient.ResponseCodeOk {
|
||||
} else {
|
||||
if err == nil {
|
||||
if warningMap[response.Code] == 1 {
|
||||
globals.SugarLogger.Warnf("SendVerifyCode mobileNumber:%s failed with response:%s", mobileNumber, utils.Format4Output(response, false))
|
||||
} else {
|
||||
globals.SugarLogger.Infof("SendVerifyCode mobileNumber:%s failed with response:%s", mobileNumber, utils.Format4Output(response, false))
|
||||
}
|
||||
err = fmt.Errorf("发送短信出错:%s", response.Message)
|
||||
} else {
|
||||
globals.SugarLogger.Warnf("SendVerifyCode mobileNumber:%s failed with error:%v", mobileNumber, err)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
// err = ErrVerifyCodeIsWrong
|
||||
// if mobileNumber == TestMobile && code == TestVerifyCode {
|
||||
// err = nil
|
||||
// } else {
|
||||
// if value := api.Cacher.Get(mobileNumber); value != nil {
|
||||
// if code == value.(string) {
|
||||
// api.Cacher.Del(mobileNumber)
|
||||
// err = nil
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// return err
|
||||
// }
|
||||
|
||||
func VerifyCode(mobileNumber, code string) (err error) {
|
||||
globals.SugarLogger.Debugf("VerifyCode mobileNumber:%s, code:%s", mobileNumber, code)
|
||||
// func (a *Auther) Login(mobileNum, verifyCode string) (userID, LoginType string, err error) {
|
||||
// if err = VerifyCode(mobileNum, verifyCode); err == nil {
|
||||
// _, err = dao.GetWeiXinUserByIDs(dao.GetDB(), mobileNum, "", "", "")
|
||||
// err = auth.ConvertErr2NoUser(err, mobileNum)
|
||||
// }
|
||||
// return "", "", err
|
||||
// }
|
||||
|
||||
err = ErrVerifyCodeIsWrong
|
||||
if mobileNumber == TestMobile && code == TestVerifyCode {
|
||||
err = nil
|
||||
} else {
|
||||
if value := api.Cacher.Get(mobileNumber); value != nil {
|
||||
if code == value.(string) {
|
||||
api.Cacher.Del(mobileNumber)
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (a *Auther) Login(mobileNum, verifyCode string) (userID, LoginType string, err error) {
|
||||
if err = VerifyCode(mobileNum, verifyCode); err == nil {
|
||||
_, err = dao.GetWeiXinUserByIDs(dao.GetDB(), mobileNum, "", "", "")
|
||||
err = auth.ConvertErr2NoUser(err, mobileNum)
|
||||
}
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
func (a *Auther) Logout(loginInfo *auth.LoginInfo) error {
|
||||
return nil
|
||||
}
|
||||
// func (a *Auther) Logout(loginInfo *auth.LoginInfo) error {
|
||||
// return nil
|
||||
// }
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
package mobile
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSendVerifyCode(t *testing.T) {
|
||||
err := SendVerifyCode("18180948107")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// func TestSendVerifyCode(t *testing.T) {
|
||||
// err := SendVerifyCode("18180948107")
|
||||
// if err != nil {
|
||||
// t.Fatal(err)
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -1,267 +1,249 @@
|
||||
package weixin
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
// const (
|
||||
// LoginType = "weixinsns"
|
||||
// LoginTypeMiniProgram = "weixinmini"
|
||||
// DefTempPasswordDuration = 20 * time.Minute // 登录时间限制在5分钟内
|
||||
// )
|
||||
|
||||
"git.rosy.net.cn/baseapi/platformapi/weixinapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
weixin2 "git.rosy.net.cn/jx-callback/business/auth2/authprovider/weixin"
|
||||
"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"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
)
|
||||
// const (
|
||||
// CacheKeySeparator = "/"
|
||||
// MiniVerifyCodePrefix = "MiniVerifyCode"
|
||||
// SessionKeyPrefix = "SessionKey"
|
||||
// )
|
||||
|
||||
const (
|
||||
LoginType = "weixinsns"
|
||||
LoginTypeMiniProgram = "weixinmini"
|
||||
DefTempPasswordDuration = 20 * time.Minute // 登录时间限制在5分钟内
|
||||
)
|
||||
// var (
|
||||
// StrStateIsWrong = "state:%s状态不对"
|
||||
// )
|
||||
|
||||
const (
|
||||
CacheKeySeparator = "/"
|
||||
MiniVerifyCodePrefix = "MiniVerifyCode"
|
||||
SessionKeyPrefix = "SessionKey"
|
||||
)
|
||||
// var (
|
||||
// auther *Auther
|
||||
// AutherMini *AutherMiniProgram
|
||||
// )
|
||||
|
||||
var (
|
||||
StrStateIsWrong = "state:%s状态不对"
|
||||
)
|
||||
// var (
|
||||
// ErrExceptionalLogin = errors.New("登录异常,超时,请重走绑定流程")
|
||||
// )
|
||||
|
||||
var (
|
||||
auther *Auther
|
||||
AutherMini *AutherMiniProgram
|
||||
)
|
||||
// type Auther struct {
|
||||
// }
|
||||
|
||||
var (
|
||||
ErrExceptionalLogin = errors.New("登录异常,超时,请重走绑定流程")
|
||||
)
|
||||
// type AutherMiniProgram struct {
|
||||
// }
|
||||
|
||||
type Auther struct {
|
||||
}
|
||||
// type UserInfoExt struct {
|
||||
// weixinapi.SNSUserInfo
|
||||
// TempPassword string `json:"tempPassword"` // 一段时间有效的登录密码
|
||||
// LoginInfo *auth.LoginInfo `json:"loginInfo"`
|
||||
// }
|
||||
|
||||
type AutherMiniProgram struct {
|
||||
}
|
||||
// func init() {
|
||||
// auther = new(Auther)
|
||||
// auth.RegisterAuther(LoginType, auther)
|
||||
|
||||
type UserInfoExt struct {
|
||||
weixinapi.SNSUserInfo
|
||||
TempPassword string `json:"tempPassword"` // 一段时间有效的登录密码
|
||||
LoginInfo *auth.LoginInfo `json:"loginInfo"`
|
||||
}
|
||||
// AutherMini = new(AutherMiniProgram)
|
||||
// auth.RegisterAuther(LoginTypeMiniProgram, AutherMini)
|
||||
// }
|
||||
|
||||
func init() {
|
||||
auther = new(Auther)
|
||||
auth.RegisterAuther(LoginType, auther)
|
||||
// func cacheSNSInfo(wxUserinfo *weixinapi.SNSUserInfo, password string, duration time.Duration) {
|
||||
// api.Cacher.Set(wxUserinfo.OpenID, password, duration)
|
||||
// api.Cacher.Set(wxUserinfo.OpenID+".sns", wxUserinfo, duration)
|
||||
// }
|
||||
|
||||
AutherMini = new(AutherMiniProgram)
|
||||
auth.RegisterAuther(LoginTypeMiniProgram, AutherMini)
|
||||
}
|
||||
// func getSNSInfoFromCache(openID string) (wxUserinfo *weixinapi.SNSUserInfo, password string) {
|
||||
// password, _ = api.Cacher.Get(openID).(string)
|
||||
// wxUserinfo = new(weixinapi.SNSUserInfo)
|
||||
// if err := api.Cacher.GetAs(openID+".sns", wxUserinfo); err != nil {
|
||||
// wxUserinfo = nil
|
||||
// }
|
||||
// return wxUserinfo, password
|
||||
// }
|
||||
|
||||
func cacheSNSInfo(wxUserinfo *weixinapi.SNSUserInfo, password string, duration time.Duration) {
|
||||
api.Cacher.Set(wxUserinfo.OpenID, password, duration)
|
||||
api.Cacher.Set(wxUserinfo.OpenID+".sns", wxUserinfo, duration)
|
||||
}
|
||||
// func GetWeiXinUserInfo(code string, state string) (userInfo *UserInfoExt, err error) {
|
||||
// globals.SugarLogger.Debugf("GetUserInfo code:%s", code)
|
||||
// if state == "" {
|
||||
// token, err2 := api.WeixinAPI.SNSRetrieveToken(code)
|
||||
// if err = err2; err == nil {
|
||||
// wxUserinfo, err2 := api.WeixinAPI.SNSGetUserInfo(token.AccessToken, token.OpenID)
|
||||
// if err = err2; err == nil {
|
||||
// userInfo = &UserInfoExt{
|
||||
// SNSUserInfo: *wxUserinfo,
|
||||
// TempPassword: utils.GetUUID(),
|
||||
// }
|
||||
// globals.SugarLogger.Debugf("GetUserInfo code:%s, userInfo:%s", code, utils.Format4Output(userInfo, true))
|
||||
// cacheSNSInfo(wxUserinfo, userInfo.TempPassword, DefTempPasswordDuration)
|
||||
// user, err2 := dao.GetWeiXinUserByIDs(dao.GetDB(), "", wxUserinfo.UnionID, wxUserinfo.OpenID, "")
|
||||
// if err = err2; err == nil {
|
||||
// userInfo.LoginInfo = auth.CreateLoginInfo(user.Tel, mobile.LoginType)
|
||||
// } else if !dao.IsNoRowsError(err) { // 非用户不存在错误,报错
|
||||
// return nil, err
|
||||
// }
|
||||
// return userInfo, nil
|
||||
// }
|
||||
// }
|
||||
// } else {
|
||||
// err = fmt.Errorf(StrStateIsWrong, state)
|
||||
// }
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
func getSNSInfoFromCache(openID string) (wxUserinfo *weixinapi.SNSUserInfo, password string) {
|
||||
password, _ = api.Cacher.Get(openID).(string)
|
||||
wxUserinfo = new(weixinapi.SNSUserInfo)
|
||||
if err := api.Cacher.GetAs(openID+".sns", wxUserinfo); err != nil {
|
||||
wxUserinfo = nil
|
||||
}
|
||||
return wxUserinfo, password
|
||||
}
|
||||
// // 此函数需要调整
|
||||
// func (a *Auther) Login(openid, password string) (userID, LoginType string, err error) {
|
||||
// globals.SugarLogger.Debugf("weixinsns Login openid:%s, password:%s", openid, password)
|
||||
// _, cachedPwd := getSNSInfoFromCache(openid)
|
||||
// if cachedPwd != "" && password == cachedPwd {
|
||||
// api.Cacher.Del(openid)
|
||||
// return "", "", nil
|
||||
// }
|
||||
// return "", "", ErrExceptionalLogin
|
||||
// }
|
||||
|
||||
func GetWeiXinUserInfo(code string, state string) (userInfo *UserInfoExt, err error) {
|
||||
globals.SugarLogger.Debugf("GetUserInfo code:%s", code)
|
||||
if state == "" {
|
||||
token, err2 := api.WeixinAPI.SNSRetrieveToken(code)
|
||||
if err = err2; err == nil {
|
||||
wxUserinfo, err2 := api.WeixinAPI.SNSGetUserInfo(token.AccessToken, token.OpenID)
|
||||
if err = err2; err == nil {
|
||||
userInfo = &UserInfoExt{
|
||||
SNSUserInfo: *wxUserinfo,
|
||||
TempPassword: utils.GetUUID(),
|
||||
}
|
||||
globals.SugarLogger.Debugf("GetUserInfo code:%s, userInfo:%s", code, utils.Format4Output(userInfo, true))
|
||||
cacheSNSInfo(wxUserinfo, userInfo.TempPassword, DefTempPasswordDuration)
|
||||
user, err2 := dao.GetWeiXinUserByIDs(dao.GetDB(), "", wxUserinfo.UnionID, wxUserinfo.OpenID, "")
|
||||
if err = err2; err == nil {
|
||||
userInfo.LoginInfo = auth.CreateLoginInfo(user.Tel, mobile.LoginType)
|
||||
} else if !dao.IsNoRowsError(err) { // 非用户不存在错误,报错
|
||||
return nil, err
|
||||
}
|
||||
return userInfo, nil
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = fmt.Errorf(StrStateIsWrong, state)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
// func (a *Auther) Logout(loginInfo *auth.LoginInfo) error {
|
||||
// return nil
|
||||
// }
|
||||
|
||||
// 此函数需要调整
|
||||
func (a *Auther) Login(openid, password string) (userID, LoginType string, err error) {
|
||||
globals.SugarLogger.Debugf("weixinsns Login openid:%s, password:%s", openid, password)
|
||||
_, cachedPwd := getSNSInfoFromCache(openid)
|
||||
if cachedPwd != "" && password == cachedPwd {
|
||||
api.Cacher.Del(openid)
|
||||
return "", "", nil
|
||||
}
|
||||
return "", "", ErrExceptionalLogin
|
||||
}
|
||||
// func BindMobile(token, mobileNum, code, nickname string) (err error) {
|
||||
// globals.SugarLogger.Debugf("BindMobile token:%s, mobileNum:%s, code:%s, nickname:%s", token, mobileNum, code, nickname)
|
||||
|
||||
func (a *Auther) Logout(loginInfo *auth.LoginInfo) error {
|
||||
return nil
|
||||
}
|
||||
// loginInfo := new(auth.LoginInfo)
|
||||
// if err = api.Cacher.GetAs(token, loginInfo); err == nil {
|
||||
// if err = mobile.VerifyCode(mobileNum, code); err == nil {
|
||||
// wxUserinfo, _ := getSNSInfoFromCache(loginInfo.ID)
|
||||
// if wxUserinfo == nil {
|
||||
// return fmt.Errorf("绑定超时,请重新绑定")
|
||||
// }
|
||||
// if nickname == "" {
|
||||
// nickname = wxUserinfo.NickName
|
||||
// }
|
||||
// err = auth.ConvertErr2NoUser(dao.UpdateWeiXinUser(dao.GetDB(), mobileNum, nickname, wxUserinfo.UnionID, wxUserinfo.OpenID, ""), mobileNum)
|
||||
// }
|
||||
// }
|
||||
// jxutils.HandleUserWXRemark(nil, mobileNum, false)
|
||||
// return err
|
||||
// }
|
||||
|
||||
func BindMobile(token, mobileNum, code, nickname string) (err error) {
|
||||
globals.SugarLogger.Debugf("BindMobile token:%s, mobileNum:%s, code:%s, nickname:%s", token, mobileNum, code, nickname)
|
||||
// // 绑定手机加登录
|
||||
// func BindMobile2(openid, secret, mobileNum, verifyCode, nickname string) (loginInfo *auth.LoginInfo, err error) {
|
||||
// globals.SugarLogger.Debugf("BindMobile2 openid:%s, secret:%s, mobileNum:%s, verifyCode:%s, nickname:%s", openid, secret, mobileNum, verifyCode, nickname)
|
||||
|
||||
loginInfo := new(auth.LoginInfo)
|
||||
if err = api.Cacher.GetAs(token, loginInfo); err == nil {
|
||||
if err = mobile.VerifyCode(mobileNum, code); err == nil {
|
||||
wxUserinfo, _ := getSNSInfoFromCache(loginInfo.ID)
|
||||
if wxUserinfo == nil {
|
||||
return fmt.Errorf("绑定超时,请重新绑定")
|
||||
}
|
||||
if nickname == "" {
|
||||
nickname = wxUserinfo.NickName
|
||||
}
|
||||
err = auth.ConvertErr2NoUser(dao.UpdateWeiXinUser(dao.GetDB(), mobileNum, nickname, wxUserinfo.UnionID, wxUserinfo.OpenID, ""), mobileNum)
|
||||
}
|
||||
}
|
||||
jxutils.HandleUserWXRemark(nil, mobileNum, false)
|
||||
return err
|
||||
}
|
||||
// err = ErrExceptionalLogin
|
||||
// if value := api.Cacher.Get(openid); value != nil {
|
||||
// wxUserinfo, cachedSecret := getSNSInfoFromCache(openid)
|
||||
// if wxUserinfo == nil {
|
||||
// return nil, fmt.Errorf("绑定超时,请重新绑定")
|
||||
// }
|
||||
// if secret == cachedSecret {
|
||||
// if err = mobile.VerifyCode(mobileNum, verifyCode); err == nil {
|
||||
// api.Cacher.Del(openid)
|
||||
// err = nil
|
||||
// if nickname == "" {
|
||||
// nickname = wxUserinfo.NickName
|
||||
// }
|
||||
// if err = dao.UpdateWeiXinUser(dao.GetDB(), mobileNum, nickname, wxUserinfo.UnionID, wxUserinfo.OpenID, ""); err == nil {
|
||||
// loginInfo = auth.CreateLoginInfo(mobileNum, mobile.LoginType)
|
||||
// } else {
|
||||
// err = auth.ConvertErr2NoUser(err, mobileNum)
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// jxutils.HandleUserWXRemark(nil, mobileNum, false)
|
||||
// return loginInfo, err
|
||||
// }
|
||||
|
||||
// 绑定手机加登录
|
||||
func BindMobile2(openid, secret, mobileNum, verifyCode, nickname string) (loginInfo *auth.LoginInfo, err error) {
|
||||
globals.SugarLogger.Debugf("BindMobile2 openid:%s, secret:%s, mobileNum:%s, verifyCode:%s, nickname:%s", openid, secret, mobileNum, verifyCode, nickname)
|
||||
// // 此函数已废弃
|
||||
// // 对于小程序来说,
|
||||
// // 1,用户必须先在后台创建(手机号标识)
|
||||
// // 2,用户必须先绑定微信
|
||||
// // 先以短信方式登录:
|
||||
// // SendMobileVerifyCode
|
||||
// // Login use type mobile
|
||||
// // MiniBindWeiXin
|
||||
// // 3,用户以CODE来登录(Login use type weixinmini)
|
||||
// // Login
|
||||
|
||||
err = ErrExceptionalLogin
|
||||
if value := api.Cacher.Get(openid); value != nil {
|
||||
wxUserinfo, cachedSecret := getSNSInfoFromCache(openid)
|
||||
if wxUserinfo == nil {
|
||||
return nil, fmt.Errorf("绑定超时,请重新绑定")
|
||||
}
|
||||
if secret == cachedSecret {
|
||||
if err = mobile.VerifyCode(mobileNum, verifyCode); err == nil {
|
||||
api.Cacher.Del(openid)
|
||||
err = nil
|
||||
if nickname == "" {
|
||||
nickname = wxUserinfo.NickName
|
||||
}
|
||||
if err = dao.UpdateWeiXinUser(dao.GetDB(), mobileNum, nickname, wxUserinfo.UnionID, wxUserinfo.OpenID, ""); err == nil {
|
||||
loginInfo = auth.CreateLoginInfo(mobileNum, mobile.LoginType)
|
||||
} else {
|
||||
err = auth.ConvertErr2NoUser(err, mobileNum)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
jxutils.HandleUserWXRemark(nil, mobileNum, false)
|
||||
return loginInfo, err
|
||||
}
|
||||
// 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 == nil || loginInfo.GetAuthType() != mobile.LoginType {
|
||||
// return fmt.Errorf("调用AutherMiniProgram BindWeiXin时,必须以手机验证方式登录")
|
||||
// }
|
||||
// sessionInfo, err := weixin2.ProxySNSCode2Session(code)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// err = dao.UpdateWeiXinUser(dao.GetDB(), loginInfo.GetAuthID(), nickName, sessionInfo.UnionID, "", sessionInfo.OpenID)
|
||||
// return auth.ConvertErr2NoUser(err, "")
|
||||
// }
|
||||
|
||||
// 此函数已废弃
|
||||
// 对于小程序来说,
|
||||
// 1,用户必须先在后台创建(手机号标识)
|
||||
// 2,用户必须先绑定微信
|
||||
// 先以短信方式登录:
|
||||
// SendMobileVerifyCode
|
||||
// Login use type mobile
|
||||
// MiniBindWeiXin
|
||||
// 3,用户以CODE来登录(Login use type weixinmini)
|
||||
// Login
|
||||
// // 绑定小程序
|
||||
// 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 := weixin2.ProxySNSCode2Session(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.UnionID, "", sessionInfo.OpenID), user.Tel)
|
||||
// return err
|
||||
// }
|
||||
|
||||
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 == nil || loginInfo.GetAuthType() != mobile.LoginType {
|
||||
return fmt.Errorf("调用AutherMiniProgram BindWeiXin时,必须以手机验证方式登录")
|
||||
}
|
||||
sessionInfo, err := weixin2.ProxySNSCode2Session(code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = dao.UpdateWeiXinUser(dao.GetDB(), loginInfo.GetAuthID(), nickName, sessionInfo.UnionID, "", sessionInfo.OpenID)
|
||||
return auth.ConvertErr2NoUser(err, "")
|
||||
}
|
||||
// func (a *AutherMiniProgram) Login(mobileNum, code string) (userID, LoginType string, err error) {
|
||||
// globals.SugarLogger.Debugf("AutherMiniProgram Login mobileNum:%s, code:%s", mobileNum, code)
|
||||
// sessionInfo, err := weixin2.ProxySNSCode2Session(code)
|
||||
// if err != nil {
|
||||
// return "", "", err
|
||||
// }
|
||||
// globals.SugarLogger.Debugf("AutherMiniProgram Login code:%s, unionID:%s, openID:%s", code, sessionInfo.UnionID, sessionInfo.OpenID)
|
||||
// db := dao.GetDB()
|
||||
// user, err := dao.GetWeiXinUserByIDs(db, "", sessionInfo.UnionID, "", sessionInfo.OpenID)
|
||||
// if err != nil {
|
||||
// return "", "", auth.ConvertErr2NoUser(err, mobileNum)
|
||||
// }
|
||||
// if user.OpenIDMini != sessionInfo.OpenID {
|
||||
// user.OpenIDMini = sessionInfo.OpenID
|
||||
// dao.UpdateEntity(db, user, "OpenIDMini")
|
||||
// }
|
||||
// globals.SugarLogger.Debugf("AutherMiniProgram Login user.Tel:%s, code:%s, openID:%s", user.Tel, code, sessionInfo.OpenID)
|
||||
// if mobileNum != user.Tel {
|
||||
|
||||
// 绑定小程序
|
||||
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 := weixin2.ProxySNSCode2Session(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.UnionID, "", sessionInfo.OpenID), user.Tel)
|
||||
return err
|
||||
}
|
||||
// }
|
||||
// api.Cacher.Set(composeSessionKeyCacheKey(user.Tel), sessionInfo.SessionKey, auth.DefTokenDuration)
|
||||
// return user.Tel, mobile.LoginType, err
|
||||
// }
|
||||
|
||||
func (a *AutherMiniProgram) Login(mobileNum, code string) (userID, LoginType string, err error) {
|
||||
globals.SugarLogger.Debugf("AutherMiniProgram Login mobileNum:%s, code:%s", mobileNum, code)
|
||||
sessionInfo, err := weixin2.ProxySNSCode2Session(code)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
globals.SugarLogger.Debugf("AutherMiniProgram Login code:%s, unionID:%s, openID:%s", code, sessionInfo.UnionID, sessionInfo.OpenID)
|
||||
db := dao.GetDB()
|
||||
user, err := dao.GetWeiXinUserByIDs(db, "", sessionInfo.UnionID, "", sessionInfo.OpenID)
|
||||
if err != nil {
|
||||
return "", "", auth.ConvertErr2NoUser(err, mobileNum)
|
||||
}
|
||||
if user.OpenIDMini != sessionInfo.OpenID {
|
||||
user.OpenIDMini = sessionInfo.OpenID
|
||||
dao.UpdateEntity(db, user, "OpenIDMini")
|
||||
}
|
||||
globals.SugarLogger.Debugf("AutherMiniProgram Login user.Tel:%s, code:%s, openID:%s", user.Tel, code, sessionInfo.OpenID)
|
||||
if mobileNum != user.Tel {
|
||||
// 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.GetAuthID()))
|
||||
// }
|
||||
|
||||
}
|
||||
api.Cacher.Set(composeSessionKeyCacheKey(user.Tel), sessionInfo.SessionKey, auth.DefTokenDuration)
|
||||
return user.Tel, mobile.LoginType, err
|
||||
}
|
||||
// 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().GetAuthID()), &sessionKey); err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
// decryptedData, err := weixin2.ProxySNSDecodeMiniProgramData(encryptedData, sessionKey, iv)
|
||||
// if err != nil {
|
||||
// return "", err
|
||||
// }
|
||||
// return base64.StdEncoding.EncodeToString(decryptedData), nil
|
||||
// }
|
||||
|
||||
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.GetAuthID()))
|
||||
}
|
||||
// func composeMiniVerifiyCacheKey(key string) string {
|
||||
// return MiniVerifyCodePrefix + CacheKeySeparator + key
|
||||
// }
|
||||
|
||||
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().GetAuthID()), &sessionKey); err != nil {
|
||||
return "", err
|
||||
}
|
||||
decryptedData, err := weixin2.ProxySNSDecodeMiniProgramData(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
|
||||
}
|
||||
// func composeSessionKeyCacheKey(key string) string {
|
||||
// return SessionKeyPrefix + CacheKeySeparator + key
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user