This commit is contained in:
苏尹岚
2020-09-11 17:10:10 +08:00
parent 642b1a2730
commit d05b37e762
8 changed files with 98 additions and 5 deletions

View File

@@ -13,10 +13,10 @@ import (
const (
EventCategory = "sys"
EventTypeWXToken = "wxToken"
EventTypeWX2Token = "wx2Token"
EventTypeWX3Token = "wx3Token"
EventTypeWXToken = "wxToken"
EventTypeWX2Token = "wx2Token"
EventTypeWX3Token = "wx3Token"
EventTypePushToken = "pushToken"
EventTypeYLYToken = "ylyToken"
EventTypeWeimobToken = "weimobToken"
)
@@ -67,6 +67,13 @@ func (h *Hub) OnNewWX2Token(token string) {
})
}
func (h *Hub) OnNewPushToken(token string) {
h.eventHub.PostNewEvent(EventCategory, &eventhub.EventInfo{
Type: EventTypePushToken,
Data: token,
})
}
func (h *Hub) OnNewWX3Token(token string) {
h.eventHub.PostNewEvent(EventCategory, &eventhub.EventInfo{
Type: EventTypeWX3Token,
@@ -99,6 +106,8 @@ func (h *Hub) GetToken(tokenType, oldToken string, waitTime time.Duration) (toke
if weimobToken := api.WeimobAPI.GetToken(); weimobToken != nil {
token = string(utils.MustMarshal(weimobToken))
}
case EventTypePushToken:
token = api.PushAPI.CBGetToken()
case EventTypeWX2Token:
token = api.WeixinMiniAPI2.CBGetToken()
}
@@ -136,3 +145,7 @@ func (h *Hub) GetYLYToken(oldToken string, waitTime time.Duration) (tokenInfo *T
func (h *Hub) GetWeimobToken(oldToken string, waitTime time.Duration) (tokenInfo *TokenInfo) {
return h.GetToken(EventTypeWeimobToken, oldToken, waitTime)
}
func (h *Hub) GetPushToken(oldToken string, waitTime time.Duration) (tokenInfo *TokenInfo) {
return h.GetToken(EventTypePushToken, oldToken, waitTime)
}

View File

@@ -28,6 +28,7 @@ const (
dingdingTokenExpires = 7200 * time.Second
weimobTokenExpires = 7200 * time.Second
yilianyunTokenExpires = 30 * 24 * 3600 * time.Second
pushTokenExpires = 7200 * time.Second
maxRefreshGap = 5 * 60 * time.Second
errRefreshGap = 10 * time.Second
@@ -165,6 +166,33 @@ func RefreshWeixin2Token() (err error) {
return err
}
func RefreshPushToken() (err error) {
if api.PushAPI != nil {
err = RefreshConfig("push", pushTokenExpires, func() (token string, expireTimeStr string) {
globals.SugarLogger.Debugf("RefreshPushToken RunMode:%s", beego.BConfig.RunMode)
if globals.IsMainProductEnv() {
if tokenInfo, err := api.PushAPI.CBRetrieveToken(); err == nil {
globals.SugarLogger.Debugf("RefreshPushToken tokenInfo:%s", utils.Format4Output(tokenInfo, true))
token = tokenInfo.Token
} else {
globals.SugarLogger.Errorf("RefreshPushToken RefreshToken failed with error:%v", err)
}
} else {
if tokenInfo := getPushTokenFromRemote(api.PushAPI.CBGetToken()); tokenInfo != nil {
expireTimeStr = utils.Time2Str(time.Now().Add(-pushTokenExpires))
token = tokenInfo.Token
}
}
return token, expireTimeStr
}, func(value string) {
globals.SugarLogger.Debugf("RefreshPushToken setter value:%s", value)
syseventhub.SysEventHub.OnNewPushToken(value)
api.PushAPI.CBSetToken(value)
})
}
return err
}
func RefreshWeixin3Token() (err error) {
// if api.WeixinMiniAPI3 != nil {
// err = RefreshConfig("wechat3", weixinTokenExpires, func() (token string, expireTimeStr string) {
@@ -342,6 +370,15 @@ func getWX2TokenFromRemote(oldToken string) (tokenInfo *syseventhub.TokenInfo) {
return tokenInfo
}
func getPushTokenFromRemote(oldToken string) (tokenInfo *syseventhub.TokenInfo) {
if !globals.IsMainProductEnv() && globals.GetWeixinTokenKey != "" && globals.GetPushTokenURL != "" {
tokenInfo = PollingRemotEvent(globals.GetPushTokenURL, 0, map[string]interface{}{
"oldToken": oldToken,
})
}
return tokenInfo
}
func getWX3TokenFromRemote(oldToken string) (tokenInfo *syseventhub.TokenInfo) {
if !globals.IsMainProductEnv() && globals.GetWeixinTokenKey != "" && globals.GetWeixinTokenURL != "" {
tokenInfo = PollingRemotEvent(globals.GetWeixinTokenURL, 0, map[string]interface{}{

View File

@@ -243,6 +243,11 @@ dingdingSecret = "LWrZAFeqUfuVv7n_tc8vPpCAx6PT4CwManx2XCVhJOqGsx2L5XCDuX1sAN_Jtv
dingdingCallbackURL = "http://callback.jxc4.com/dingding/msg"
pushAppID = "5lyyrvHODG6wC8Sdr3a9h"
pushAppKey = "iFrkUDmR2g5eqQpfh2kQ57"
pushAppSecret = "WTn53qd6WAAdLMXfmXvzb7"
pushMasterSecret= "dGZcR0XGGg7H5Pd7FR3n47"
[jxgy]
httpport = 8088
EnableDocs = false
@@ -310,6 +315,7 @@ dingdingCallbackURL = "http://callback-jxgy.jxc4.com/dingding/msg"
getWeixinTokenURL = "http://www.jxc4.com/v2/sys/GetWXToken"
getYLYTokenURL = "http://www.jxc4.com/v2/sys/GetYLYToken"
getPushTokenURL = "http://www.jxc4.com/v2/sys/GetPushToken"
jdEclpAccessToken = "bcb6201b5b3c45a0976dcda5e2dea8aejiwm"
jdEclpAppKey = "0D397F05CF11C51BCDCC81744680EBC3"
@@ -445,6 +451,7 @@ disableWeimob = false
getWeixinTokenURL = "http://www.jxc4.com/v2/sys/GetWXToken"
getWeixin2TokenURL = "http://www.jxc4.com/v2/sys/GetWX2Token"
getWeimobTokenURL = "http://www.jxc4.com/v2/sys/GetWeimobToken"
getPushTokenURL = "http://www.jxc4.com/v2/sys/GetPushToken"
dbConnectStr = "root:WebServer@1@tcp(127.0.0.1:3306)/jxd_dev_0?charset=utf8mb4&loc=Local&parseTime=true"

View File

@@ -71,6 +71,23 @@ func (c *SysController) GetYLYToken() {
})
}
// @Title 得到个推token
// @Description 得到个推token
// @Param accessKey query string true "假token"
// @Param oldToken query string false "之前的token"
// @Param waitSecond query int false "等待秒数"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetPushToken [get]
func (c *SysController) GetPushToken() {
c.callGetPushToken(func(params *tSysGetPushTokenParams) (retVal interface{}, errCode string, err error) {
if params.AccessKey == globals.GetWeixinTokenKey {
retVal = syseventhub.SysEventHub.GetPushToken(params.OldToken, time.Duration(params.WaitSecond)*time.Second)
}
return retVal, "", err
})
}
// @Title 得到微盟token
// @Description 得到微盟token
// @Param accessKey query string true "假token"

View File

@@ -3,6 +3,8 @@ package api
import (
"time"
"git.rosy.net.cn/baseapi/platformapi/unipushapi"
"git.rosy.net.cn/baseapi/platformapi/jdshopapi"
"git.rosy.net.cn/baseapi/platformapi/aliupcapi"
@@ -87,6 +89,8 @@ var (
YilianyunAPI2 *yilianyunapi.API
ZhongwuAPI *zhongwuapi.API
PushAPI *unipushapi.API
Cacher cache.ICacher
SMSClient *aliyunsmsclient.SmsClient
)
@@ -243,6 +247,7 @@ func Init() {
YilianyunAPI = yilianyunapi.New(beego.AppConfig.DefaultString("yilianyunClientID", ""), beego.AppConfig.DefaultString("yilianyunClientSecret", ""))
YilianyunAPI2 = yilianyunapi.New(beego.AppConfig.DefaultString("yilianyunClientID2", ""), beego.AppConfig.DefaultString("yilianyunClientSecret2", ""))
ZhongwuAPI = zhongwuapi.New(beego.AppConfig.DefaultInt("zhongwuAppID", 0), beego.AppConfig.DefaultString("zhongwuAppSecret", ""))
PushAPI = unipushapi.New(beego.AppConfig.DefaultString("pushAppID", ""), beego.AppConfig.DefaultString("pushAppKey", ""), beego.AppConfig.DefaultString("pushAppSecret", ""), beego.AppConfig.DefaultString("pushMasterSecret", ""))
SMSClient = aliyunsmsclient.New("http://dysmsapi.aliyuncs.com/")
}

View File

@@ -38,6 +38,7 @@ var (
GetWeixin2TokenURL string
GetWeixinTokenKey string
GetYLYTokenURL string
GetPushTokenURL string
GetWeimobTokenURL string
StoreName string
@@ -111,6 +112,7 @@ func Init() {
GetYLYTokenURL = beego.AppConfig.DefaultString("getYLYTokenURL", "")
GetWeimobTokenURL = beego.AppConfig.DefaultString("getWeimobTokenURL", "")
GetWeixinTokenKey = beego.AppConfig.DefaultString("getWeixinTokenKey", "")
GetPushTokenURL = beego.AppConfig.DefaultString("getPushTokenURL", "")
StoreName = beego.AppConfig.String("storeName")
StoreNameMtwm = beego.AppConfig.String("storeNameMtwm")

View File

@@ -147,7 +147,10 @@ func main() {
if err := tasks.RefreshYilianyunToken(); err != nil {
globals.SugarLogger.Errorf("RefreshYilianyunToken failed with error:%s", err)
return
}
if err := tasks.RefreshPushToken(); err != nil {
globals.SugarLogger.Errorf("RefreshPushToken failed with error:%s", err)
return
}
// if globals.IsProductEnv() {
// orderman.SaveJdsOrders(nil, time.Now().Add(-2*time.Hour), time.Now())

View File

@@ -2583,6 +2583,15 @@ func init() {
Filters: nil,
Params: nil})
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:SysController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:SysController"],
beego.ControllerComments{
Method: "GetPushToken",
Router: `/GetPushToken`,
AllowHTTPMethods: []string{"get"},
MethodParams: param.Make(),
Filters: nil,
Params: nil})
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:SysController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:SysController"],
beego.ControllerComments{
Method: "GetWX2Token",