Skip to content

Commit

Permalink
增加小程序-云开发http api
Browse files Browse the repository at this point in the history
  • Loading branch information
silenceper committed Nov 26, 2019
1 parent 7ea817a commit 96678d2
Show file tree
Hide file tree
Showing 12 changed files with 831 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ _testmain.go
.vscode/
vendor/*/
.idea/
examples/tcb/*
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Cache主要用来保存全局access_token以及js-sdk中的ticket:
- 获取js-sdk配置
- [素材管理](#素材管理)
- [小程序开发](#小程序开发)
- [小程序-云开发](./tcb)

## 消息管理

Expand Down
20 changes: 17 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,29 @@ go 1.13
require (
github.com/astaxie/beego v1.7.1
github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/gin-gonic/gin v1.1.4
github.com/golang/protobuf v0.0.0-20161117033126-8ee79997227b // indirect
github.com/gogo/protobuf v1.3.1 // indirect
github.com/gomodule/redigo v2.0.1-0.20180627144507-2cd21d9966bf+incompatible
github.com/hashicorp/golang-lru v0.5.3 // indirect
github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739 // indirect
github.com/mattn/go-isatty v0.0.0-20161123143637-30a891c33c7c // indirect
github.com/stretchr/testify v1.4.0 // indirect
github.com/neelance/parallel v0.0.0-20160708114440-4de9ce63d14c // indirect
github.com/opentracing/basictracer-go v1.0.0 // indirect
github.com/opentracing/opentracing-go v1.1.0 // indirect
github.com/prometheus/client_golang v1.2.1 // indirect
github.com/prometheus/procfs v0.0.7 // indirect
github.com/ramya-rao-a/go-outline v0.0.0-20181122025142-7182a932836a // indirect
github.com/slimsag/godocmd v0.0.0-20161025000126-a1005ad29fe3 // indirect
github.com/sourcegraph/ctxvfs v0.0.0-20180418081416-2b65f1b1ea81 // indirect
github.com/sourcegraph/go-langserver v2.0.0+incompatible // indirect
github.com/sourcegraph/jsonrpc2 v0.0.0-20191113080033-cee7209801bf // indirect
golang.org/x/crypto v0.0.0-20191117063200-497ca9f6d64f
golang.org/x/net v0.0.0-20191119073136-fc4aabc6c914 // indirect
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e // indirect
golang.org/x/sys v0.0.0-20191119195528-f068ffe820e4 // indirect
golang.org/x/tools v0.0.0-20191120001058-ad01d5993d97 // indirect
golang.org/x/tools/gopls v0.2.1 // indirect
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 // indirect
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
gopkg.in/go-playground/validator.v8 v8.18.2 // indirect
gopkg.in/yaml.v2 v2.2.6 // indirect
Expand Down
131 changes: 131 additions & 0 deletions go.sum

Large diffs are not rendered by default.

32 changes: 32 additions & 0 deletions tcb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 小程序-云开发 SDK

Tencent Cloud Base [文档](https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/)

## 使用说明

**初始化配置**

```golang
//使用memcache保存access_token,也可选择redis或自定义cache
memCache=cache.NewMemcache("127.0.0.1:11211")

//配置小程序参数
config := &wechat.Config{
AppID: "your app id",
AppSecret: "your app secret",
Cache: memCache,
}
wc := wechat.NewWechat(config)
wcTcb := wc.GetTcb()
```

### 举例
#### 触发云函数
```golang
res, err := wcTcb.UploadFile("test-6ku2s", "golang.png")
if err != nil {
panic(err)
}
```

更多使用方法参考[GODOC](https://godoc.org/github.com/silenceper/wechat/tcb)
35 changes: 35 additions & 0 deletions tcb/cloudfunction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tcb

import (
"fmt"

"github.com/silenceper/wechat/util"
)

const (
//触发云函数
invokeCloudFunctionURL = "https://api.weixin.qq.com/tcb/invokecloudfunction"
)

//InvokeCloudFunctionRes 云函数调用返回结果
type InvokeCloudFunctionRes struct {
util.CommonError
RespData string `json:"resp_data"` //云函数返回的buffer
}

//InvokeCloudFunction 云函数调用
//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/functions/invokeCloudFunction.html
func (tcb *Tcb) InvokeCloudFunction(env, name, args string) (*InvokeCloudFunctionRes, error) {
accessToken, err := tcb.GetAccessToken()
if err != nil {
return nil, err
}
uri := fmt.Sprintf("%s?access_token=%s&env=%s&name=%s", invokeCloudFunctionURL, accessToken, env, name)
response, err := util.HTTPPost(uri, args)
if err != nil {
return nil, err
}
invokeCloudFunctionRes := &InvokeCloudFunctionRes{}
err = util.DecodeWithError(response, invokeCloudFunctionRes, "InvokeCloudFunction")
return invokeCloudFunctionRes, err
}
Loading

0 comments on commit 96678d2

Please sign in to comment.