parent
ad2cdde40e
commit
ea5476f1e4
@ -0,0 +1,48 @@
|
||||
package wechatoffice
|
||||
|
||||
import (
|
||||
"gopkg.in/dtapps/go-library.v3/utils/gotime"
|
||||
"time"
|
||||
)
|
||||
|
||||
type WechatJsapiTicketDbModel struct {
|
||||
ID int64 `json:"id"`
|
||||
AppID string `json:"app_id"`
|
||||
AppSecret string `json:"app_secret"`
|
||||
Ticket string `json:"ticket"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
ExpiresTime string `json:"expires_time"`
|
||||
CreateTime time.Time `gorm:"autoCreateTime" json:"-"`
|
||||
}
|
||||
|
||||
func (m *WechatJsapiTicketDbModel) TableName() string {
|
||||
return "wechat_jsapi_ticket"
|
||||
}
|
||||
|
||||
func (app *App) wechatJsapiTicketModelTake() (result WechatJsapiTicketDbModel) {
|
||||
app.Db.Where("app_id = ?", app.AppId).Where("expires_time > ?", gotime.Current().Format()).Take(&result)
|
||||
return result
|
||||
}
|
||||
|
||||
// GetJsapiTicketDb 获取api_ticket
|
||||
func (app *App) GetJsapiTicketDb() string {
|
||||
wat := app.wechatJsapiTicketModelTake()
|
||||
if wat.Ticket != "" {
|
||||
return wat.Ticket
|
||||
} else {
|
||||
token := app.GetTicket(app.GetAccessTokenDb(), "jsapi")
|
||||
if token.Result.Ticket == "" {
|
||||
return ""
|
||||
} else {
|
||||
// 创建
|
||||
app.Db.Create(&WechatJsapiTicketDbModel{
|
||||
AppID: app.AppId,
|
||||
AppSecret: app.AppSecret,
|
||||
Ticket: token.Result.Ticket,
|
||||
ExpiresIn: token.Result.ExpiresIn,
|
||||
ExpiresTime: gotime.Current().AfterSeconds(7000).Format(),
|
||||
})
|
||||
return token.Result.Ticket
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package wechatoffice
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
// AuthGetJsapiTicketMonitor 监控api_ticket
|
||||
func (app *App) AuthGetJsapiTicketMonitor(qdType string) error {
|
||||
result := app.GetCallBackIp()
|
||||
if len(result.Result.IpList) <= 0 {
|
||||
switch qdType {
|
||||
case qdTypeDb:
|
||||
app.GetJsapiTicketDb()
|
||||
return nil
|
||||
case qdTypeRdb:
|
||||
app.GetJsapiTicketRDb()
|
||||
return nil
|
||||
default:
|
||||
return errors.New("驱动类型不在范围内")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package wechatoffice
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gopkg.in/dtapps/go-library.v3/utils/goredis"
|
||||
)
|
||||
|
||||
// GetJsapiTicketRDb 获取api_ticket
|
||||
func (app *App) GetJsapiTicketRDb() string {
|
||||
cacheName := fmt.Sprintf("wechat_jsapi_ticket:%v", app.AppId)
|
||||
redis := goredis.App{
|
||||
Rdb: app.RDb,
|
||||
}
|
||||
newCache := redis.NewSimpleStringCache(redis.NewStringOperation(), 7000)
|
||||
newCache.DBGetter = func() string {
|
||||
token := app.GetTicket(app.GetAccessTokenRDb(), "jsapi")
|
||||
return token.Result.Ticket
|
||||
}
|
||||
return newCache.GetCache(cacheName)
|
||||
}
|
Loading…
Reference in new issue