qrcode.go 1.5 KB
package domain

import (
	"encoding/base64"
	"fmt"
	"github.com/forgoer/openssl"
	"github.com/google/uuid"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/constant"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/log"
	"strings"
)

const (
	QrcodeCodeExpire int64 = 60 * 5 //5分钟过期
)

var (
	aecSecret = []byte("mmm.qrcode.ecb.1")
	qrcodeLogin = "/v1/auth/login/qrcode?key="
)

type QrcodeMessage struct {
	Id      string `json:"id"`
	Token   string `json:"token"`
	IsLogin bool   `json:"isLogin"`
	//用户id
	UserId     int64 `json:"userId"`
	UserBaseId int64 `json:"userBaseId"`
	// 账号
	Account string `json:"account"`
	// 公司id
	CompanyId int64 `json:"companyId"`
	// 组织id
	OrgId int64 `json:"orgId"`
}

func (qrmsg *QrcodeMessage) Init() ([]byte, error) {
	str := strings.Replace(uuid.New().String(), "-", "", -1)
	key := constant.ALLIED_CREATION_GATEWAY_HOST + qrcodeLogin + str
	log.Logger.Debug("key:" + key)
	encryptedData, err := openssl.AesECBEncrypt([]byte(key), aecSecret, openssl.PKCS7_PADDING)
	if err != nil {
		return nil, err
	}
	qrmsg.Id = str
	qrmsg.Token = base64.StdEncoding.EncodeToString(encryptedData)
	qrmsg.IsLogin = false
	return encryptedData, err
}

func (qrmsg *QrcodeMessage) BindUser(operator Operator) error {
	if qrmsg.IsLogin {
		return fmt.Errorf("登录中")
	}
	qrmsg.UserId = operator.UserId
	qrmsg.OrgId = operator.OrgId
	qrmsg.CompanyId = operator.CompanyId
	qrmsg.UserBaseId = operator.UserBaseId
	qrmsg.Account = operator.Phone
	qrmsg.IsLogin = true
	return nil
}