Comments (7)
可以考虑自己重写一部分信息。在切换的时候,更改appid。
from wechat.
我重新实现了中控服务器接口,用缓存存储token,但是没做并发安全。
`package wx
import (
"time"
"fmt"
// "sync"
"net/http"
"net/url"
"github.com/chanxuehong/wechat/util"
"github.com/chanxuehong/wechat/mp/core"
"encoding/json"
)
type Cache interface{
Get(k string) (interface{}, bool)
SetDefault(k string, x interface{})
Set(k string, x interface{}, d time.Duration)
}
// DefaultAccessTokenServer 实现了 AccessTokenServer 接口.
// 用于多进程环境.
type DefaultAccessTokenServer struct {
appId string
appSecret string
httpClient *http.Client
cache Cache
// accessTokenLock *sync.RWMutex // 读写锁 同一个AppID一个
}
// NewDefaultAccessTokenServer 创建一个新的 DefaultAccessTokenServer, 如果 httpClient == nil 则默认使用 util.DefaultHttpClient.
func NewDefaultAccessTokenServer(appId, appSecret string, c Cache) (srv *DefaultAccessTokenServer) {
srv = &DefaultAccessTokenServer{
appId: url.QueryEscape(appId),
appSecret: url.QueryEscape(appSecret),
httpClient: util.DefaultHttpClient,
cache: c,
}
return
}
func (srv *DefaultAccessTokenServer) IID01332E16DF5011E5A9D5A4DB30FED8E1() {}
func (srv *DefaultAccessTokenServer) Token() (token string, err error) {
// srv.accessTokenLock.Lock()
// defer srv.accessTokenLock.Unlock()
accessTokenCacheKey := fmt.Sprintf("access_token_%s", srv.appId)
val,ok:= srv.cache.Get(accessTokenCacheKey)
if ok {
token = val.(string)
return
}
//从微信服务器获取
var resAccessToken *accessToken
resAccessToken, err = srv.GetAccessTokenFromServer()
if err != nil {
return
}
expires := resAccessToken.ExpiresIn - 1500
srv.cache.Set(accessTokenCacheKey, resAccessToken.Token, time.Duration(expires)*time.Second)
token = resAccessToken.Token
return
}
type accessToken struct {
Token string json:"access_token"
ExpiresIn int64 json:"expires_in"
}
// updateToken 从微信服务器获取新的 access_token 并存入缓存, 同时返回该 access_token.
func (srv *DefaultAccessTokenServer) GetAccessTokenFromServer() (token *accessToken, err error) {
url := "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + srv.appId +
"&secret=" + srv.appSecret
// api.DebugPrintGetRequest(url)
httpResp, err := srv.httpClient.Get(url)
if err != nil {
// atomic.StorePointer(&srv.tokenCache, nil)
return
}
defer httpResp.Body.Close()
if httpResp.StatusCode != http.StatusOK {
// atomic.StorePointer(&srv.tokenCache, nil)
err = fmt.Errorf("http.Status: %s", httpResp.Status)
return
}
var result struct {
core.Error
accessToken
}
if err = json.NewDecoder(httpResp.Body).Decode(&result); err != nil {
// atomic.StorePointer(&srv.tokenCache, nil)
return
}
if result.ErrCode != core.ErrCodeOK {
// atomic.StorePointer(&srv.tokenCache, nil)
err = &result.Error
return
}
tokenCopy := result.accessToken
// atomic.StorePointer(&srv.tokenCache, unsafe.Pointer(&tokenCopy))
token = &tokenCopy
return
}
func (srv *DefaultAccessTokenServer)RefreshToken(currentToken string) (token string, err error){
return "",nil
}`
from wechat.
@caoyanbin1993 这个只有自己进行改造。并发安全那边需要一个分布式锁(redis实现)。其实都还好。这个包很久没有更新了。有不少bug
from wechat.
为啥用分布式锁呢,单机模式用普通的锁可以不?希望大神看看我这个代码,能不能帮忙完善下。
from wechat.
from wechat.
为什么是三个服务呢,做的是分布式部署吧?用缓存还是redis的关键是,锁的实现方式吧?access_token可以用一个服务进行管理,其他应用都请求他,应该是可以的。关键是写入时,要加锁。
from wechat.
from wechat.
Related Issues (20)
- 这儿有文档解释有点小问题。pagePath 应该是说是小程序的页面
- 测试实践后 模板小程序部分参数 微信文档与微信后台不一致。这儿的pagepath实际为path。否则 模板报错"errcode":40165 HOT 1
- 建议:添加微信 仿真系统功能
- 刚提交了pull requests,新增对客服消息中的菜单消息的支持,能帮忙merge一下么 HOT 1
- mp部分文档是否有问题?
- 没文档? HOT 4
- 建议把v1 v2打成tag,好让go module拉取
- 有没大神给个gin调用的例子呢 HOT 2
- 媒体/素材涉及Download的方法,在文件扩展名未知的情况下,如何传filepath参数?
- 微信个人号的API 推荐大家看这个 https://wkteam.gitbook.io/api/ HOT 1
- 吐槽一下,怎么调 全靠看源码 , fuck 弃坑 HOT 1
- 支持小程序支付吗?
- amr 怎么转mp3格式,有工具类么?
- 能提供文档介绍下怎么使用吗
- 企业微信群控CRM
- 有没有自动提交小程序代码和提交审核?
- 出现了xml解析的panic HOT 1
- go不弄个发红包码 HOT 2
- 请问支持企业微信SDK么? HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wechat.