mini_wechat_info_logic.go 1.4 KB
package user

import (
	"context"
	"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"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"

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

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

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

func (l *MiniWechatInfoLogic) MiniWechatInfo(req *types.MiniWechatInfoRequest) (resp *types.MiniWechatInfoResponse, err error) {
	var userToken = contextdata.GetUserTokenFromCtx(l.ctx)
	userId := userToken.UserId
	var conn = l.svcCtx.DefaultDBConn()
	user, err := l.svcCtx.UserRepository.FindOne(l.ctx, conn, userId)
	if err != nil {
		return nil, xerr.NewErrMsg("未获取到用户信息")
	}
	userWechat, err := l.svcCtx.UserWechatRepository.FindOneByPhone(l.ctx, conn, user.Phone)
	if err != nil {
		return &types.MiniWechatInfoResponse{
			Bind: false,
		}, nil
	} else {
		return &types.MiniWechatInfoResponse{
			Bind:   true,
			OpenId: userWechat.OpenId,
			Phone:  userWechat.Phone,
		}, nil
	}
}