作者 zhuangmx

Merge branch 'dev' into test

... ... @@ -2,10 +2,10 @@ package user
import (
"context"
"fmt"
"github.com/silenceper/wechat/v2"
"github.com/silenceper/wechat/v2/cache"
miniConfig "github.com/silenceper/wechat/v2/miniprogram/config"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/gateway/smslib"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/tool"
... ... @@ -53,11 +53,6 @@ func (l *MiniUserLoginLogic) MiniUserLogin(req *types.MiniUserLoginRequest) (res
var users []*domain.User
conn := l.svcCtx.DefaultDBConn()
//保存微信用户信息
err = saveUserWechat(l.svcCtx, l.ctx, conn, loginInfo)
if err != nil {
return nil, err
}
_, users, err = l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().
MustWithKV("phone", loginInfo.Phone).
MustWithKV("auditStatus", []int{domain.UserAuditStatusWait, domain.UserAuditStatusPassed}).
... ... @@ -92,23 +87,6 @@ func (l *MiniUserLoginLogic) MiniUserLogin(req *types.MiniUserLoginRequest) (res
return
}
// saveUserWechat 保存微信授权信息
func saveUserWechat(svcCtx *svc.ServiceContext, ctx context.Context, conn transaction.Conn, loginInfo *domain.LoginInfo) error {
if loginInfo.Phone != "" && loginInfo.OpenId != "" {
_, err := svcCtx.UserWechatRepository.FindOneByPhone(ctx, conn, loginInfo.Phone)
if err != nil { // 未存储
_, err = svcCtx.UserWechatRepository.Insert(ctx, conn, &domain.UserWechat{
OpenId: loginInfo.OpenId,
Phone: loginInfo.Phone,
})
if err != nil {
return err
}
}
}
return nil
}
func generateToken(svcCtx *svc.ServiceContext, user *domain.User) (token string, err error) {
var userJwtToken = tool.UserToken{}
if user != nil {
... ... @@ -138,25 +116,14 @@ func (c WxClientLogin) WechatPhoneLogin(r domain.WechatLoginRequest) (*domain.Lo
AppSecret: c.svcCtx.Config.Wechat.AppSecret,
Cache: cache.NewMemory(),
})
result, err := miniprogram.GetAuth().Code2Session(code)
if err != nil {
return nil, xerr.NewErrMsgErr("发起授权请求失败", err)
}
plainData, err := miniprogram.GetEncryptor().Decrypt(result.SessionKey, r.EncryptedData, r.IV)
if err != nil {
return nil, xerr.NewErrMsgErr("获取授权用户失败", err)
}
//增加微信用户openId
response.OpenId = plainData.OpenID
response.Phone = plainData.PhoneNumber
/*authResult, err := miniprogram.GetAuth().GetPhoneNumber(code)
authResult, err := miniprogram.GetAuth().GetPhoneNumber(code)
if err != nil || authResult.ErrCode != 0 || authResult.PhoneInfo.PhoneNumber == "" {
return response, xerr.NewCodeErrMsg(xerr.ErrWxMiniAuthFailError, nil, fmt.Sprintf("发起授权请求失败1 err : %v , code : %s , authResult : %+v", err, code, authResult))
}
var (
phone = authResult.PhoneInfo.PhoneNumber
)
response.Phone = phone*/
response.Phone = phone
return response, nil
}
... ... @@ -164,6 +131,23 @@ func (c WxClientLogin) WechatLogin(r domain.WechatLoginRequest) (*domain.LoginIn
return nil, nil
}
func (c WxClientLogin) GetOpenId(r domain.WechatLoginRequest) (string, error) {
miniprogram := wechat.NewWechat().GetMiniProgram(&miniConfig.Config{
AppID: c.svcCtx.Config.Wechat.AppID,
AppSecret: c.svcCtx.Config.Wechat.AppSecret,
Cache: cache.NewMemory(),
})
result, err := miniprogram.GetAuth().Code2Session(r.Code)
if err != nil {
return "", xerr.NewErrMsgErr("发起授权请求失败", err)
}
plainData, err := miniprogram.GetEncryptor().Decrypt(result.SessionKey, r.EncryptedData, r.IV)
if err != nil {
return "", xerr.NewErrMsgErr("获取授权用户失败", err)
}
return plainData.OpenID, nil
}
func (c WxClientLogin) PhonePasswordLogin(phone string, password string) (*domain.LoginInfo, error) {
panic("implement me")
}
... ...
... ... @@ -3,6 +3,7 @@ package user
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
... ... @@ -27,7 +28,7 @@ func NewMiniWechatBindLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Mi
func (l *MiniWechatBindLogic) MiniWechatBind(req *types.MiniWechatBindRequest) (resp *types.MiniWechatBindResponse, err error) {
var loginCreator domain.LoginCreator = WxClientLogin{svcCtx: l.svcCtx, ctx: l.ctx}
loginInfo, err := loginCreator.WechatPhoneLogin(domain.WechatLoginRequest{
openId, err := loginCreator.GetOpenId(domain.WechatLoginRequest{
Code: req.WechatAuthCode,
EncryptedData: req.WechatEncryptedData,
IV: req.WechatIV,
... ... @@ -35,14 +36,30 @@ func (l *MiniWechatBindLogic) MiniWechatBind(req *types.MiniWechatBindRequest) (
if err != nil {
return nil, xerr.NewErrMsgErr("授权失败", err)
}
var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
userId := userToken.UserId
var conn = l.svcCtx.DefaultDBConn()
err = saveUserWechat(l.svcCtx, l.ctx, conn, loginInfo)
//获取用户信息
userInfo, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, userId)
if err != nil {
return nil, xerr.NewErrMsgErr("获取用户信息失败", err)
}
_, err = l.svcCtx.UserWechatRepository.FindOneByPhone(l.ctx, conn, userInfo.Phone)
if err != nil { // 未存储
_, err = l.svcCtx.UserWechatRepository.Insert(l.ctx, conn, &domain.UserWechat{
Phone: userInfo.Phone,
OpenId: openId,
})
if err != nil {
return nil, xerr.NewErrMsgErr("授权失败", err)
}
}
if err != nil {
return nil, xerr.NewErrMsg("保存用户信息失败")
}
return &types.MiniWechatBindResponse{
Bind: true,
Phone: loginInfo.Phone,
OpenId: loginInfo.OpenId,
Phone: userInfo.Phone,
OpenId: openId,
}, nil
}
... ...
... ... @@ -9,7 +9,7 @@ import (
type UserWechat struct {
Id int64 `json:"id"` // 唯一标识
OpenId string `json:"openId" gorm:"unique"` // 微信openId
OpenId string `json:"openId"` // 微信openId
Phone string `json:"phone" gorm:"unique"` // 微信手机号
CreatedAt int64 `json:",omitempty"` // 创建时间
UpdatedAt int64 `json:",omitempty"` // 更新时间
... ...
... ... @@ -151,6 +151,7 @@ type (
PhonePasswordLogin(phone string, password string) (*LoginInfo, error)
PhoneSmsCodeLogin(phone string, code string) (*LoginInfo, error)
WechatPhoneLogin(r WechatLoginRequest) (*LoginInfo, error)
GetOpenId(r WechatLoginRequest) (string, error)
}
WechatLoginRequest struct {
Code string // 授权码
... ... @@ -161,6 +162,5 @@ type (
User *User
Phone string
Message string
OpenId string
}
)
... ...