1
0
mirror of https://github.com/silenceper/wechat.git synced 2026-02-08 06:32:27 +08:00

oauth2,jssdk

This commit is contained in:
wenzl
2016-09-15 01:27:28 +08:00
parent d9075933c1
commit a89065d952
21 changed files with 913 additions and 43 deletions

View File

@@ -16,11 +16,10 @@ const (
//ResAccessToken struct
type ResAccessToken struct {
ErrorCode int32 `json:"errcode"`
ErrorMsg string `json:"errmsg"`
util.CommonError
AccessToken string `json:"access_token"`
ExpiresIn int32 `json:"expires_in"`
ExpiresIn int64 `json:"expires_in"`
}
//SetAccessTokenLock 设置读写锁一个appID一个读写锁
@@ -60,12 +59,13 @@ func (ctx *Context) GetAccessTokenFromServer() (resAccessToken ResAccessToken, e
if err != nil {
return
}
if resAccessToken.ErrorMsg != "" {
err = fmt.Errorf("get access_token error : errcode=%v , errormsg=%v", resAccessToken.ErrorCode, resAccessToken.ErrorMsg)
if resAccessToken.ErrMsg != "" {
err = fmt.Errorf("get access_token error : errcode=%v , errormsg=%v", resAccessToken.ErrCode, resAccessToken.ErrMsg)
return
}
accessTokenCacheKey := fmt.Sprintf("access_token_%s", ctx.AppID)
err = ctx.Cache.Set(accessTokenCacheKey, resAccessToken.AccessToken, time.Duration(resAccessToken.ExpiresIn)-1500/time.Second)
expires := resAccessToken.ExpiresIn - 1500
err = ctx.Cache.Set(accessTokenCacheKey, resAccessToken.AccessToken, time.Duration(expires)*time.Second)
return
}

View File

@@ -21,6 +21,9 @@ type Context struct {
//accessTokenLock 读写锁 同一个AppID一个
accessTokenLock *sync.RWMutex
//jsapiTicket 读写锁 同一个AppID一个
jsApiTicketLock *sync.RWMutex
}
// Query returns the keyed url query value if it exists
@@ -37,3 +40,12 @@ func (ctx *Context) GetQuery(key string) (string, bool) {
}
return "", false
}
//SetJsApiTicket 设置jsApiTicket的lock
func (ctx *Context) SetJsApiTicketLock(lock *sync.RWMutex) {
ctx.jsApiTicketLock = lock
}
func (ctx *Context) GetJsApiTicketLock() *sync.RWMutex {
return ctx.jsApiTicketLock
}

View File

@@ -10,6 +10,8 @@ var plainContentType = []string{"text/plain; charset=utf-8"}
//Render render from bytes
func (ctx *Context) Render(bytes []byte) {
//debug
//fmt.Println("response msg = ", string(bytes))
ctx.Writer.WriteHeader(200)
_, err := ctx.Writer.Write(bytes)
if err != nil {