Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dcsunny committed Jun 3, 2019
1 parent 411938c commit 887c66b
Show file tree
Hide file tree
Showing 17 changed files with 916 additions and 278 deletions.
121 changes: 113 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[![Build Status](https://travis-ci.org/silenceper/wechat.svg?branch=master)](https://travis-ci.org/silenceper/wechat)
[![Go Report Card](https://goreportcard.com/badge/github.com/silenceper/wechat)](https://goreportcard.com/report/github.com/silenceper/wechat)
[![GoDoc](http://godoc.org/github.com/silenceper/wechat?status.svg)](http://godoc.org/github.com/silenceper/wechat)
test


使用Golang开发的微信SDK,简单、易用。

Expand Down Expand Up @@ -96,6 +96,7 @@ Cache主要用来保存全局access_token以及js-sdk中的ticket:
- 检验access_token是否有效
- 获取js-sdk配置
- [素材管理](#素材管理)
- [小程序开发](#小程序开发)

## 消息管理

Expand Down Expand Up @@ -272,7 +273,7 @@ type Reply struct {
}

```
注意:`retrun nil`表示什么也不做
注意:`return nil`表示什么也不做

#### 回复文本消息
```go
Expand Down Expand Up @@ -335,14 +336,14 @@ Url :点击图文消息跳转链接

## 自定义菜单

通过` wechat.GetMenu(req, writer)`获取menu的实例
通过` wechat.GetMenu()`获取menu的实例

### 自定义菜单创建接口

以下是一个创建二级菜单的例子

```go
mu := wc.GetMenu(c.Request, c.Writer)
mu := wc.GetMenu()

buttons := make([]*menu.Button, 1)
btn := new(menu.Button)
Expand Down Expand Up @@ -402,15 +403,15 @@ func (btn *Button) SetViewLimitedButton(name, mediaID string) {
### 自定义菜单查询接口
```go
mu := wc.GetMenu(c.Request, c.Writer)
mu := wc.GetMenu()
resMenu,err:=mu.GetMenu()
```
>返回结果 resMenu 结构参考 ./menu/menu.go 中ResMenu 结构体
### 自定义菜单删除接口
```go
mu := wc.GetMenu(c.Request, c.Writer)
mu := wc.GetMenu()
err:=mu.DeleteMenu()
```
Expand Down Expand Up @@ -458,7 +459,7 @@ func (menu *Menu) GetCurrentSelfMenuInfo() (resSelfMenuInfo ResSelfMenuInfo, err
**1.发起授权**
```go
oauth := wc.GetOauth(c.Request, c.Writer)
oauth := wc.GetOauth()
err := oauth.Redirect("跳转的绝对地址", "snsapi_userinfo", "123dd123")
if err != nil {
fmt.Println(err)
Expand Down Expand Up @@ -505,7 +506,7 @@ func (oauth *Oauth) CheckAccessToken(accessToken, openID string) (b bool, err er
### 获取js-sdk配置
```go
js := wc.GetJs(c.Request, c.Writer)
js := wc.GetJs()
cfg, err := js.GetConfig("传入需要的调用js-sdk的url地址")
if err != nil {
fmt.Println(err)
Expand All @@ -529,6 +530,110 @@ type Config struct {
[素材管理API](https://godoc.org/github.com/silenceper/wechat/material#Material)
## 小程序开发
获取小程序操作对象
``` go
memCache=cache.NewMemcache("127.0.0.1:11211")
config := &wechat.Config{
AppID: "xxx",
AppSecret: "xxx",
Cache: memCache=cache.NewMemcache("127.0.0.1:11211"),
}
wc := wechat.NewWechat(config)

wxa := wc.GetMiniProgram()
```
### 小程序登录凭证校验
``` go
func (wxa *MiniProgram) Code2Session(jsCode string) (result ResCode2Session, err error)
```
### 小程序数据统计
**获取用户访问小程序日留存**
``` go
func (wxa *MiniProgram) GetAnalysisDailyRetain(beginDate, endDate string) (result ResAnalysisRetain, err error)
```
**获取用户访问小程序月留存**
``` go
func (wxa *MiniProgram) GetAnalysisMonthlyRetain(beginDate, endDate string) (result ResAnalysisRetain, err error)
```
**获取用户访问小程序周留存**
``` go
func (wxa *MiniProgram) GetAnalysisWeeklyRetain(beginDate, endDate string) (result ResAnalysisRetain, err error)
```
**获取用户访问小程序数据概况**
``` go
func (wxa *MiniProgram) GetAnalysisDailySummary(beginDate, endDate string) (result ResAnalysisDailySummary, err error)
```
**获取用户访问小程序数据日趋势**
``` go
func (wxa *MiniProgram) GetAnalysisDailyVisitTrend(beginDate, endDate string) (result ResAnalysisVisitTrend, err error)
```
**获取用户访问小程序数据月趋势**
``` go
func (wxa *MiniProgram) GetAnalysisMonthlyVisitTrend(beginDate, endDate string) (result ResAnalysisVisitTrend, err error)
```
**获取用户访问小程序数据周趋势**
``` go
func (wxa *MiniProgram) GetAnalysisWeeklyVisitTrend(beginDate, endDate string) (result ResAnalysisVisitTrend, err error)
```
**获取小程序新增或活跃用户的画像分布数据**
``` go
func (wxa *MiniProgram) GetAnalysisUserPortrait(beginDate, endDate string) (result ResAnalysisUserPortrait, err error)
```
**获取用户小程序访问分布数据**
``` go
func (wxa *MiniProgram) GetAnalysisVisitDistribution(beginDate, endDate string) (result ResAnalysisVisitDistribution, err error)
```
**获取小程序页面访问数据**
``` go
func (wxa *MiniProgram) GetAnalysisVisitPage(beginDate, endDate string) (result ResAnalysisVisitPage, err error)
```
### 小程序二维码生成
**获取小程序二维码,适用于需要的码数量较少的业务场景**
``` go
func (wxa *MiniProgram) CreateWXAQRCode(coderParams QRCoder) (response []byte, err error)
```
**获取小程序码,适用于需要的码数量较少的业务场景**
``` go
func (wxa *MiniProgram) GetWXACode(coderParams QRCoder) (response []byte, err error)
```
**获取小程序码,适用于需要的码数量极多的业务场景**
``` go
func (wxa *MiniProgram) GetWXACodeUnlimit(coderParams QRCoder) (response []byte, err error)
```
更多API使用请参考 godoc :
[https://godoc.org/github.com/silenceper/wechat](https://godoc.org/github.com/silenceper/wechat)
Expand Down
12 changes: 11 additions & 1 deletion context/access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,26 @@ type ResAccessToken struct {
ExpiresIn int64 `json:"expires_in"`
}

//GetAccessTokenFunc 获取 access token 的函数签名
type GetAccessTokenFunc func(ctx *Context) (accessToken string, err error)

//SetAccessTokenLock 设置读写锁(一个appID一个读写锁)
func (ctx *Context) SetAccessTokenLock(l *sync.RWMutex) {
ctx.accessTokenLock = l
}

//SetGetAccessTokenFunc 设置自定义获取accessToken的方式, 需要自己实现缓存
func (ctx *Context) SetGetAccessTokenFunc(f GetAccessTokenFunc) {
ctx.accessTokenFunc = f
}

//GetAccessToken 获取access_token
func (ctx *Context) GetAccessToken() (accessToken string, err error) {
ctx.accessTokenLock.Lock()
defer ctx.accessTokenLock.Unlock()

if ctx.accessTokenFunc != nil {
return ctx.accessTokenFunc(ctx)
}
accessTokenCacheKey := fmt.Sprintf(define.AccessTokenCacheKey, ctx.AppID)
accessToken = ctx.Cache.GetString(accessTokenCacheKey)
if accessToken != "" {
Expand Down
30 changes: 30 additions & 0 deletions context/access_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package context

import (
"sync"
"testing"
)

func TestContext_SetCustomAccessTokenFunc(t *testing.T) {
ctx := Context{
accessTokenLock: new(sync.RWMutex),
}
f := func(ctx *Context) (accessToken string, err error) {
return "fake token", nil
}
ctx.SetGetAccessTokenFunc(f)
res, err := ctx.GetAccessToken()
if res != "fake token" || err != nil {
t.Error("expect fake token but error")
}
}

func TestContext_NoSetCustomAccessTokenFunc(t *testing.T) {
ctx := Context{
accessTokenLock: new(sync.RWMutex),
}

if ctx.accessTokenFunc != nil {
t.Error("error accessTokenFunc")
}
}
3 changes: 3 additions & 0 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type Context struct {

//jsAPITicket 读写锁 同一个AppID一个
jsAPITicketLock *sync.RWMutex

//accessTokenFunc 自定义获取 access token 的方法
accessTokenFunc GetAccessTokenFunc
}

// Query returns the keyed url query value if it exists
Expand Down
18 changes: 18 additions & 0 deletions define/define.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package define

import (
"encoding/json"
"fmt"
)

const (
AccessTokenCacheKey = "access_token:%s"
MiniAccessTokenCacheKey = "mini_access_token_:%s"
Expand All @@ -11,3 +16,16 @@ type CommonError struct {
ErrCode int64 `json:"errcode"`
ErrMsg string `json:"errmsg"`
}

// DecodeWithCommonError 将返回值按照CommonError解析
func DecodeWithCommonError(response []byte, apiName string) (err error) {
var commError CommonError
err = json.Unmarshal(response, &commError)
if err != nil {
return
}
if commError.ErrCode != 0 {
return fmt.Errorf("%s Error , errcode=%d , errmsg=%s", apiName, commError.ErrCode, commError.ErrMsg)
}
return nil
}
Loading

0 comments on commit 887c66b

Please sign in to comment.