-
Notifications
You must be signed in to change notification settings - Fork 3
/
cgi-bin.token.db.go
47 lines (42 loc) · 1.26 KB
/
cgi-bin.token.db.go
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
package wechatoffice
import (
"github.com/dtapps/go-library/utils/gotime"
"time"
)
type WechatAccessTokenDbModel struct {
ID int64 `json:"id"`
AppID string `json:"app_id"`
AppSecret string `json:"app_secret"`
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
ExpiresTime string `json:"expires_time"`
CreateTime time.Time `gorm:"autoCreateTime" json:"-"`
}
func (m *WechatAccessTokenDbModel) TableName() string {
return "wechat_access_token"
}
func (app *App) wechatAccessTokenModelTake() (result WechatAccessTokenDbModel) {
app.Db.Where("app_id = ?", app.AppId).Where("expires_time > ?", gotime.Current().Format()).Take(&result)
return result
}
func (app *App) GetAccessTokenDb() string {
wat := app.wechatAccessTokenModelTake()
if wat.AccessToken != "" {
return wat.AccessToken
} else {
token := app.AuthGetAccessToken()
if token.Result.AccessToken == "" {
return ""
} else {
// 创建
app.Db.Create(&WechatAccessTokenDbModel{
AppID: app.AppId,
AppSecret: app.AppSecret,
AccessToken: token.Result.AccessToken,
ExpiresIn: token.Result.ExpiresIn,
ExpiresTime: gotime.Current().AfterSeconds(7000).Format(),
})
return token.Result.AccessToken
}
}
}