mini_wechat_bind_logic.go 1.4 KB
package user

import (
	"context"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"

	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type MiniWechatBindLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewMiniWechatBindLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniWechatBindLogic {
	return &MiniWechatBindLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

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{
		Code:          req.WechatAuthCode,
		EncryptedData: req.WechatEncryptedData,
		IV:            req.WechatIV,
	})
	if err != nil {
		return nil, xerr.NewErrMsgErr("授权失败", err)
	}
	var conn = l.svcCtx.DefaultDBConn()
	err = saveUserWechat(l.svcCtx, l.ctx, conn, loginInfo)
	if err != nil {
		return nil, xerr.NewErrMsg("保存用户信息失败")
	}
	return &types.MiniWechatBindResponse{
		Bind:   true,
		Phone:  loginInfo.Phone,
		OpenId: loginInfo.OpenId,
	}, nil
}