修改微信登录 bug
This commit is contained in:
@ -5,6 +5,7 @@ import (
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"server/utility/wechat"
|
||||
"sort"
|
||||
"strings"
|
||||
@ -17,15 +18,17 @@ func (c *ControllerV1) WeChatVertify(ctx context.Context, req *v1.WeChatVertifyR
|
||||
// 1. 排序
|
||||
params := []string{wechat.GetWeChatClient().GetToken(), req.Timestamp, req.Nonce}
|
||||
sort.Strings(params)
|
||||
glog.Infof(ctx, "排序后的参数: %s", params)
|
||||
|
||||
// 2. 拼接成字符串
|
||||
str := strings.Join(params, "")
|
||||
glog.Infof(ctx, "拼接后的字符串: %s", str)
|
||||
|
||||
// 3. SHA1 加密
|
||||
h := sha1.New()
|
||||
h.Write([]byte(str))
|
||||
sha1Str := fmt.Sprintf("%x", h.Sum(nil))
|
||||
|
||||
glog.Infof(ctx, "SHA1 加密后的字符串: %s", sha1Str)
|
||||
// 4. 比较签名
|
||||
if sha1Str != req.Signature {
|
||||
return nil, fmt.Errorf("签名错误")
|
||||
|
||||
@ -53,7 +53,7 @@ func New() service.IUser {
|
||||
}
|
||||
|
||||
func (s *sUser) Login(ctx context.Context, in *model.UserLoginIn) (out *model.UserLoginOut, err error) {
|
||||
value, err := dao.Roles.Ctx(ctx).Where(do.Roles{Code: consts.UserRoleCode}).Fields(dao.Roles.Columns().Code).Value()
|
||||
value, err := dao.Roles.Ctx(ctx).Where(do.Roles{Code: consts.UserRoleCode}).Fields(dao.Roles.Columns().Code, dao.Roles.Columns().Id).One()
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("查找角色失败")
|
||||
}
|
||||
@ -93,6 +93,7 @@ func (s *sUser) Login(ctx context.Context, in *model.UserLoginIn) (out *model.Us
|
||||
LastLoginAt: gtime.Now(),
|
||||
WxPopenId: utility.GenerateUserID("WX"),
|
||||
QqPopenId: utility.GenerateUserID("QQ"),
|
||||
RoleId: value[dao.Roles.Columns().Id].Int64(),
|
||||
}
|
||||
result, err := dao.Users.Ctx(ctx).Insert(user)
|
||||
if err != nil {
|
||||
@ -117,7 +118,7 @@ func (s *sUser) Login(ctx context.Context, in *model.UserLoginIn) (out *model.Us
|
||||
// 生成 token
|
||||
token, err := jwt.GenerateToken(&jwt.TokenIn{
|
||||
UserId: userId,
|
||||
Role: value.String(),
|
||||
Role: value[dao.Roles.Columns().Code].String(),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, ecode.Fail.Sub("生成token失败")
|
||||
|
||||
@ -40,6 +40,7 @@ func init() {
|
||||
AppId: g.Config().MustGet(ctx, "wechat.appId").String(),
|
||||
AppSecret: g.Config().MustGet(ctx, "wechat.appSecret").String(),
|
||||
TicketExpire: g.Config().MustGet(ctx, "wechat.ticketExpire").Int(),
|
||||
Token: g.Config().MustGet(ctx, "wechat.token").String(),
|
||||
}
|
||||
|
||||
go instance.autoRefreshToken(ctx)
|
||||
@ -168,3 +169,7 @@ func (c *weChatClient) GetQrCode(ticket string) (imagePath string, err error) {
|
||||
func (c *weChatClient) GetToken() string {
|
||||
return c.Token
|
||||
}
|
||||
|
||||
func (c *weChatClient) GetUserUnionId(openid string) (unionId string, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user