qrcode.go
2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package domain
import (
"encoding/base64"
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-gateway/pkg/util"
"log"
"time"
jwt "github.com/dgrijalva/jwt-go"
)
const (
qrcodeTokenSecret string = "bbe35ad433dd8e67"
qrcodeCodeExpire int64 = 60 * 30 //15分钟过期
)
var aecSecret = []byte("mmm.qrcode.(%^&)")
var loginHost = "https://api.fjmaimaimai.com/app/auth/login/qrcode?key="
type QrcodeMessage struct {
jwt.StandardClaims
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) GenerateImageBase64() ([]byte, error) {
nowTime := time.Now().Unix()
qrmsg.StandardClaims = jwt.StandardClaims{
NotBefore: nowTime,
IssuedAt: nowTime,
ExpiresAt: nowTime + qrcodeCodeExpire,
Issuer: "allied_creation_gateway",
}
qrmsg.Id = fmt.Sprintf("%d", time.Now().UnixNano())
token := jwt.NewWithClaims(jwt.SigningMethodHS256, *qrmsg)
str, err := token.SignedString([]byte(qrcodeTokenSecret))
if err != nil {
return nil, err
}
key := loginHost + str
encryptedData, err := util.AesEncrypt([]byte(key), aecSecret)
qrmsg.Token = base64.StdEncoding.EncodeToString(encryptedData)
//初始化数据
//qrmsg.Token = string(encryptedData)
qrmsg.IsLogin = false
// 输入日志
decrypted, _ := util.AesDecrypt(encryptedData, aecSecret)
if string(decrypted) == key {
log.Println("token:", str, "\n encrypt:", key, "\n decrypt:", string(decrypted))
}
//qrCode, err := qr.Encode(str, qr.M, qr.Auto)
//if err != nil {
// return nil, err
//}
//qrCode, err = barcode.Scale(qrCode, 200, 200)
//if err != nil {
// return nil, err
//}
//var buf bytes.Buffer
//err = png.Encode(&buf, qrCode)
//if err != nil {
// return nil, err
//}
//var result []byte
//base64.StdEncoding.Encode(result, buf.Bytes())
return encryptedData, err
}
func (qrmsg *QrcodeMessage) ParseToken(str string) error {
tokenClaims, err := jwt.ParseWithClaims(
str,
qrmsg,
func(token *jwt.Token) (interface{}, error) {
return []byte(loginTokenSecret), nil
})
if err != nil {
return err
}
if claim, ok := tokenClaims.Claims.(*QrcodeMessage); ok && tokenClaims.Valid {
*qrmsg = *claim
}
return nil
}
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
}