+GetWeimobToken
This commit is contained in:
@@ -13,8 +13,9 @@ import (
|
|||||||
const (
|
const (
|
||||||
EventCategory = "sys"
|
EventCategory = "sys"
|
||||||
|
|
||||||
EventTypeWXToken = "wxtoken"
|
EventTypeWXToken = "wxToken"
|
||||||
EventTypeYLYToken = "ylytoken"
|
EventTypeYLYToken = "ylyToken"
|
||||||
|
EventTypeWeimobToken = "weimobToken"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Hub struct {
|
type Hub struct {
|
||||||
@@ -63,6 +64,13 @@ func (h *Hub) OnNewYLYToken(token string) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Hub) OnNewWeimobToken(token string) {
|
||||||
|
h.eventHub.PostNewEvent(EventCategory, &eventhub.EventInfo{
|
||||||
|
Type: EventTypeWeimobToken,
|
||||||
|
Data: token,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func (h *Hub) GetToken(tokenType, oldToken string, waitTime time.Duration) (tokenInfo *TokenInfo) {
|
func (h *Hub) GetToken(tokenType, oldToken string, waitTime time.Duration) (tokenInfo *TokenInfo) {
|
||||||
var token string
|
var token string
|
||||||
switch tokenType {
|
switch tokenType {
|
||||||
@@ -70,6 +78,10 @@ func (h *Hub) GetToken(tokenType, oldToken string, waitTime time.Duration) (toke
|
|||||||
token = api.WeixinAPI.CBGetToken()
|
token = api.WeixinAPI.CBGetToken()
|
||||||
case EventTypeYLYToken:
|
case EventTypeYLYToken:
|
||||||
token = api.YilianyunAPI.GetToken()
|
token = api.YilianyunAPI.GetToken()
|
||||||
|
case EventTypeWeimobToken:
|
||||||
|
if weimobToken := api.WeimobAPI.GetToken(); weimobToken != nil {
|
||||||
|
token = string(utils.MustMarshal(weimobToken))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if token != oldToken {
|
if token != oldToken {
|
||||||
tokenInfo = &TokenInfo{
|
tokenInfo = &TokenInfo{
|
||||||
@@ -85,7 +97,7 @@ func (h *Hub) GetToken(tokenType, oldToken string, waitTime time.Duration) (toke
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
globals.SugarLogger.Debugf("GetToken tokenInfo:%s", utils.Format4Output(tokenInfo, true))
|
globals.SugarLogger.Debugf("GetToken tokenType:%s tokenInfo:%s", tokenType, utils.Format4Output(tokenInfo, true))
|
||||||
return tokenInfo
|
return tokenInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,3 +108,7 @@ func (h *Hub) GetWXToken(oldToken string, waitTime time.Duration) (tokenInfo *To
|
|||||||
func (h *Hub) GetYLYToken(oldToken string, waitTime time.Duration) (tokenInfo *TokenInfo) {
|
func (h *Hub) GetYLYToken(oldToken string, waitTime time.Duration) (tokenInfo *TokenInfo) {
|
||||||
return h.GetToken(EventTypeYLYToken, oldToken, waitTime)
|
return h.GetToken(EventTypeYLYToken, oldToken, waitTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (h *Hub) GetWeimobToken(oldToken string, waitTime time.Duration) (tokenInfo *TokenInfo) {
|
||||||
|
return h.GetToken(EventTypeWeimobToken, oldToken, waitTime)
|
||||||
|
}
|
||||||
|
|||||||
@@ -115,19 +115,17 @@ func RefreshWeixinToken() (err error) {
|
|||||||
if api.WeixinAPI != nil {
|
if api.WeixinAPI != nil {
|
||||||
err = RefreshConfig("wechat", weixinTokenExpires, func() (token string, expireTimeStr string) {
|
err = RefreshConfig("wechat", weixinTokenExpires, func() (token string, expireTimeStr string) {
|
||||||
globals.SugarLogger.Debugf("RefreshWeixinToken RunMode:%s", beego.BConfig.RunMode)
|
globals.SugarLogger.Debugf("RefreshWeixinToken RunMode:%s", beego.BConfig.RunMode)
|
||||||
if globals.IsProductEnv() || beego.BConfig.RunMode == "alpha" {
|
if globals.IsMainProductEnv() {
|
||||||
if globals.IsMainProductEnv() {
|
if tokenInfo, err := api.WeixinAPI.CBRetrieveToken(); err == nil {
|
||||||
if tokenInfo, err := api.WeixinAPI.CBRetrieveToken(); err == nil {
|
globals.SugarLogger.Debugf("RefreshWeixinToken tokenInfo:%s", utils.Format4Output(tokenInfo, true))
|
||||||
globals.SugarLogger.Debugf("RefreshWeixinToken tokenInfo:%s", utils.Format4Output(tokenInfo, true))
|
token = tokenInfo.AccessToken
|
||||||
token = tokenInfo.AccessToken
|
|
||||||
} else {
|
|
||||||
globals.SugarLogger.Errorf("RefreshWeixinToken RefreshToken failed with error:%v", err)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if tokenInfo := getWXTokenFromRemote(api.WeixinAPI.CBGetToken()); tokenInfo != nil {
|
globals.SugarLogger.Errorf("RefreshWeixinToken RefreshToken failed with error:%v", err)
|
||||||
expireTimeStr = utils.Time2Str(time.Now().Add(-weixinTokenExpires))
|
}
|
||||||
token = tokenInfo.Token
|
} else {
|
||||||
}
|
if tokenInfo := getWXTokenFromRemote(api.WeixinAPI.CBGetToken()); tokenInfo != nil {
|
||||||
|
expireTimeStr = utils.Time2Str(time.Now().Add(-weixinTokenExpires))
|
||||||
|
token = tokenInfo.Token
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return token, expireTimeStr
|
return token, expireTimeStr
|
||||||
@@ -142,17 +140,28 @@ func RefreshWeixinToken() (err error) {
|
|||||||
|
|
||||||
func RefreshWeimobToken() (err error) {
|
func RefreshWeimobToken() (err error) {
|
||||||
if api.WeimobAPI != nil {
|
if api.WeimobAPI != nil {
|
||||||
err = RefreshConfig("weimob", weimobTokenExpires, func() (string, string) {
|
err = RefreshConfig("weimob", weimobTokenExpires, func() (token string, expireTimeStr string) {
|
||||||
|
globals.SugarLogger.Debugf("RefreshWeimobToken RunMode:%s", beego.BConfig.RunMode)
|
||||||
if globals.IsMainProductEnv() {
|
if globals.IsMainProductEnv() {
|
||||||
if tokenInfo, err := api.WeimobAPI.RefreshTokenByRefreshToken(); err == nil {
|
if tokenInfo, err := api.WeimobAPI.RefreshTokenByRefreshToken(); err == nil {
|
||||||
return string(utils.MustMarshal(tokenInfo)), utils.Time2Str(time.Now().Add((time.Duration(tokenInfo.ExpiresIn) - weimobTokenExpires/time.Second) * time.Second))
|
return string(utils.MustMarshal(tokenInfo)), utils.Time2Str(time.Now().Add((time.Duration(tokenInfo.ExpiresIn) - weimobTokenExpires/time.Second) * time.Second))
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
curToken := ""
|
||||||
|
if tokenInfo := api.WeimobAPI.GetToken(); tokenInfo != nil {
|
||||||
|
curToken = string(utils.MustMarshal(tokenInfo))
|
||||||
|
}
|
||||||
|
if tokenInfo := getWeimobTokenFromRemote(curToken); tokenInfo != nil {
|
||||||
|
expireTimeStr = utils.Time2Str(time.Now().Add(-weimobTokenExpires))
|
||||||
|
token = tokenInfo.Token
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return "", ""
|
return token, expireTimeStr
|
||||||
}, func(value string) {
|
}, func(value string) {
|
||||||
var tokenInfo *weimobapi.TokenInfo
|
var tokenInfo *weimobapi.TokenInfo
|
||||||
err := utils.UnmarshalUseNumber([]byte(value), &tokenInfo)
|
err := utils.UnmarshalUseNumber([]byte(value), &tokenInfo)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
syseventhub.SysEventHub.OnNewWeimobToken(string(utils.MustMarshal(tokenInfo)))
|
||||||
api.WeimobAPI.SetToken(tokenInfo)
|
api.WeimobAPI.SetToken(tokenInfo)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -216,19 +225,6 @@ func RefreshYilianyunToken() (err error) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func getWXTokenFromRemote(oldToken string) (tokenInfo *syseventhub.TokenInfo) {
|
|
||||||
if IsGetWXTokenFromRemote() {
|
|
||||||
tokenInfo = PollingRemotEvent(globals.GetWeixinTokenURL, 0, map[string]interface{}{
|
|
||||||
"oldToken": oldToken,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return tokenInfo
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsGetWXTokenFromRemote() bool {
|
|
||||||
return !globals.IsMainProductEnv() && globals.GetWeixinTokenKey != "" && globals.GetWeixinTokenURL != ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func PollingRemotEvent(remoteURL string, waitSecond int, params map[string]interface{}) (tokenInfo *syseventhub.TokenInfo) {
|
func PollingRemotEvent(remoteURL string, waitSecond int, params map[string]interface{}) (tokenInfo *syseventhub.TokenInfo) {
|
||||||
if waitSecond == 0 {
|
if waitSecond == 0 {
|
||||||
waitSecond = 5 * 60
|
waitSecond = 5 * 60
|
||||||
@@ -272,6 +268,15 @@ func PollingRemotEvent(remoteURL string, waitSecond int, params map[string]inter
|
|||||||
return tokenInfo
|
return tokenInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getWXTokenFromRemote(oldToken string) (tokenInfo *syseventhub.TokenInfo) {
|
||||||
|
if !globals.IsMainProductEnv() && globals.GetWeixinTokenKey != "" && globals.GetWeixinTokenURL != "" {
|
||||||
|
tokenInfo = PollingRemotEvent(globals.GetWeixinTokenURL, 0, map[string]interface{}{
|
||||||
|
"oldToken": oldToken,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return tokenInfo
|
||||||
|
}
|
||||||
|
|
||||||
func getYLYTokenFromRemote(oldToken string) (tokenInfo *syseventhub.TokenInfo) {
|
func getYLYTokenFromRemote(oldToken string) (tokenInfo *syseventhub.TokenInfo) {
|
||||||
if !globals.IsMainProductEnv() && globals.GetWeixinTokenKey != "" && globals.GetYLYTokenURL != "" {
|
if !globals.IsMainProductEnv() && globals.GetWeixinTokenKey != "" && globals.GetYLYTokenURL != "" {
|
||||||
tokenInfo = PollingRemotEvent(globals.GetYLYTokenURL, 0, map[string]interface{}{
|
tokenInfo = PollingRemotEvent(globals.GetYLYTokenURL, 0, map[string]interface{}{
|
||||||
@@ -280,3 +285,12 @@ func getYLYTokenFromRemote(oldToken string) (tokenInfo *syseventhub.TokenInfo) {
|
|||||||
}
|
}
|
||||||
return tokenInfo
|
return tokenInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getWeimobTokenFromRemote(oldToken string) (tokenInfo *syseventhub.TokenInfo) {
|
||||||
|
if !globals.IsMainProductEnv() && globals.GetWeixinTokenKey != "" && globals.GetWeimobTokenURL != "" {
|
||||||
|
tokenInfo = PollingRemotEvent(globals.GetWeimobTokenURL, 0, map[string]interface{}{
|
||||||
|
"oldToken": oldToken,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
return tokenInfo
|
||||||
|
}
|
||||||
|
|||||||
@@ -118,8 +118,8 @@ weixinToken = "17_roSCZgkCxhRnyFVtei0KdfHwdGP8PmLzJFhCieka4_X4_d-lgfaTxF6oIS6FE5
|
|||||||
|
|
||||||
dbConnectStr = "root:WebServer@1@tcp(127.0.0.1:3306)/jxd_dev_0?charset=utf8mb4&loc=Local&parseTime=true"
|
dbConnectStr = "root:WebServer@1@tcp(127.0.0.1:3306)/jxd_dev_0?charset=utf8mb4&loc=Local&parseTime=true"
|
||||||
|
|
||||||
getWeixinTokenURL = "http://beta.jxc4.com/v2/sys/GetWXToken"
|
getWeixinTokenURL = "http://www.jxc4.com/v2/sys/GetWXToken"
|
||||||
getYLYTokenURL = "http://beta.jxc4.com/v2/sys/GetYLYToken"
|
getYLYTokenURL = "http://www.jxc4.com/v2/sys/GetYLYToken"
|
||||||
|
|
||||||
[prod]
|
[prod]
|
||||||
EnableDocs = false
|
EnableDocs = false
|
||||||
@@ -311,5 +311,6 @@ weixinMiniAppID2 = "wx4b5930c13f8b1170"
|
|||||||
weixinMiniSecret2 = "2a57228a716ce991a52739f0ff41111d"
|
weixinMiniSecret2 = "2a57228a716ce991a52739f0ff41111d"
|
||||||
|
|
||||||
getWeixinTokenURL = "http://www.jxc4.com/v2/sys/GetWXToken"
|
getWeixinTokenURL = "http://www.jxc4.com/v2/sys/GetWXToken"
|
||||||
|
getWeimobTokenURL = "http://www.jxc4.com/v2/sys/GetWeimobToken"
|
||||||
|
|
||||||
dbConnectStr = "root:WebServer@1@tcp(127.0.0.1:3306)/jxd_dev_0?charset=utf8mb4&loc=Local&parseTime=true"
|
dbConnectStr = "root:WebServer@1@tcp(127.0.0.1:3306)/jxd_dev_0?charset=utf8mb4&loc=Local&parseTime=true"
|
||||||
|
|||||||
@@ -54,6 +54,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 /GetWeimobToken [get]
|
||||||
|
func (c *SysController) GetWeimobToken() {
|
||||||
|
c.callGetWeimobToken(func(params *tSysGetWeimobTokenParams) (retVal interface{}, errCode string, err error) {
|
||||||
|
if params.AccessKey == globals.GetWeixinTokenKey {
|
||||||
|
retVal = syseventhub.SysEventHub.GetWeimobToken(params.OldToken, time.Duration(params.WaitSecond)*time.Second)
|
||||||
|
}
|
||||||
|
return retVal, "", err
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// @Title 得到饿百RTF转换内容
|
// @Title 得到饿百RTF转换内容
|
||||||
// @Description 得到饿百RTF转换内容
|
// @Description 得到饿百RTF转换内容
|
||||||
// @Param imgListStr query string true "逗号分隔的图片列表可以是转义后的"
|
// @Param imgListStr query string true "逗号分隔的图片列表可以是转义后的"
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ var (
|
|||||||
GetWeixinTokenURL string
|
GetWeixinTokenURL string
|
||||||
GetWeixinTokenKey string
|
GetWeixinTokenKey string
|
||||||
GetYLYTokenURL string
|
GetYLYTokenURL string
|
||||||
|
GetWeimobTokenURL string
|
||||||
|
|
||||||
StoreName string
|
StoreName string
|
||||||
|
|
||||||
@@ -84,6 +85,7 @@ func Init() {
|
|||||||
|
|
||||||
GetWeixinTokenURL = beego.AppConfig.DefaultString("getWeixinTokenURL", "")
|
GetWeixinTokenURL = beego.AppConfig.DefaultString("getWeixinTokenURL", "")
|
||||||
GetYLYTokenURL = beego.AppConfig.DefaultString("getYLYTokenURL", "")
|
GetYLYTokenURL = beego.AppConfig.DefaultString("getYLYTokenURL", "")
|
||||||
|
GetWeimobTokenURL = beego.AppConfig.DefaultString("getWeimobTokenURL", "")
|
||||||
GetWeixinTokenKey = beego.AppConfig.DefaultString("getWeixinTokenKey", "")
|
GetWeixinTokenKey = beego.AppConfig.DefaultString("getWeixinTokenKey", "")
|
||||||
|
|
||||||
StoreName = beego.AppConfig.DefaultString("storeName", "京西菜市")
|
StoreName = beego.AppConfig.DefaultString("storeName", "京西菜市")
|
||||||
|
|||||||
@@ -1647,6 +1647,15 @@ func init() {
|
|||||||
Filters: nil,
|
Filters: nil,
|
||||||
Params: 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: "GetWeimobToken",
|
||||||
|
Router: `/GetWeimobToken`,
|
||||||
|
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.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:SysController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:SysController"],
|
||||||
beego.ControllerComments{
|
beego.ControllerComments{
|
||||||
Method: "GetYLYToken",
|
Method: "GetYLYToken",
|
||||||
|
|||||||
Reference in New Issue
Block a user