From 96678d227971f7953732e6307494c65c453c4e1a Mon Sep 17 00:00:00 2001 From: silenceper Date: Tue, 26 Nov 2019 13:09:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=B0=8F=E7=A8=8B=E5=BA=8F-?= =?UTF-8?q?=E4=BA=91=E5=BC=80=E5=8F=91http=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + README.md | 1 + go.mod | 20 ++- go.sum | 131 ++++++++++++++ tcb/README.md | 32 ++++ tcb/cloudfunction.go | 35 ++++ tcb/database.go | 413 +++++++++++++++++++++++++++++++++++++++++++ tcb/file.go | 134 ++++++++++++++ tcb/tcb.go | 16 ++ util/error.go | 26 +++ util/http.go | 20 ++- wechat.go | 11 +- 12 files changed, 831 insertions(+), 9 deletions(-) create mode 100644 tcb/README.md create mode 100644 tcb/cloudfunction.go create mode 100644 tcb/database.go create mode 100644 tcb/file.go create mode 100644 tcb/tcb.go diff --git a/.gitignore b/.gitignore index 0c874bc..0a87732 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,4 @@ _testmain.go .vscode/ vendor/*/ .idea/ +examples/tcb/* diff --git a/README.md b/README.md index 73d36a4..a65cd92 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ Cache主要用来保存全局access_token以及js-sdk中的ticket: - 获取js-sdk配置 - [素材管理](#素材管理) - [小程序开发](#小程序开发) +- [小程序-云开发](./tcb) ## 消息管理 diff --git a/go.mod b/go.mod index 18dbe77..571c7c9 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 954b060..de9673a 100644 --- a/go.sum +++ b/go.sum @@ -1,45 +1,176 @@ +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/astaxie/beego v1.7.1 h1:TuqX4F9e3ujVEycudgWrwUj11WMppLZyunJKIBoxTFw= github.com/astaxie/beego v1.7.1/go.mod h1:0R4++1tUqERR0WYFWdfkcrsyoVBCG4DgpDGokT3yb+U= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/bradfitz/gomemcache v0.0.0-20160117192205-fb1f79c6b65a h1:k5TuEkqEYCRs8+66WdOkswWOj+L/YbP5ruainvn94wg= github.com/bradfitz/gomemcache v0.0.0-20160117192205-fb1f79c6b65a/go.mod h1:PmM6Mmwb0LSuEubjR8N7PtNe1KxZLtOUHtbeikc5h60= github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b h1:L/QXpzIa3pOvUGt1D1lA5KjYhPBAN/3iWdP7xeFS9F0= github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA= +github.com/cespare/xxhash/v2 v2.1.0 h1:yTUvW7Vhb89inJ+8irsUqiWjh8iT6sQPZiQzI6ReGkA= +github.com/cespare/xxhash/v2 v2.1.0/go.mod h1:dgIUBU3pDso/gPgZ1osOZ0iQf77oPR28Tjxl5dIMyVM= +github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= +github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gin-gonic/gin v1.1.4 h1:XLaCFbU39SSGRQrEeP7Z7mM3lvRqC4vE5tEaVdLDdSE= github.com/gin-gonic/gin v1.1.4/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y= +github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gogo/protobuf v1.1.1 h1:72R+M5VuhED/KujmZVcIquuo8mBgX4oVda//DQb3PXo= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= +github.com/gogo/protobuf v1.3.1 h1:DqDEcV5aeaTmdFBePNpYsp3FlcVH/2ISVVM9Qf8PSls= +github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= github.com/golang/protobuf v0.0.0-20161117033126-8ee79997227b h1:fE/yi9pibxGEc0gSJuEShcsBXE2d5FW3OudsjE9tKzQ= github.com/golang/protobuf v0.0.0-20161117033126-8ee79997227b/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/gomodule/redigo v2.0.1-0.20180627144507-2cd21d9966bf+incompatible h1:QJ4V3LjaRe/6NKoaaj2QzQZcezt5gNXdPv0axxS4VNA= github.com/gomodule/redigo v2.0.1-0.20180627144507-2cd21d9966bf+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/hashicorp/golang-lru v0.5.3 h1:YPkqC67at8FYaadspW/6uE0COsBxS2656RLEr8Bppgk= +github.com/hashicorp/golang-lru v0.5.3/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739 h1:ykXz+pRRTibcSjG1yRhpdSHInF8yZY/mfn+Rz2Nd1rE= github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739/go.mod h1:zUx1mhth20V3VKgL5jbd1BSQcW4Fy6Qs4PZvQwRFwzM= github.com/mattn/go-isatty v0.0.0-20161123143637-30a891c33c7c h1:YHHK/dEmr2Jo1cWD1VMB2waEeHJhHFp3CEylwWy/VcY= github.com/mattn/go-isatty v0.0.0-20161123143637-30a891c33c7c/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= +github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/neelance/parallel v0.0.0-20160708114440-4de9ce63d14c h1:NZOii9TDGRAfCS5VM16XnF4K7afoLQmIiZX8EkKnxtE= +github.com/neelance/parallel v0.0.0-20160708114440-4de9ce63d14c/go.mod h1:eTBvSIlRgLo+CNFFQRQTwUGTZOEdvXIKeZS/xG+D2yU= +github.com/opentracing/basictracer-go v1.0.0 h1:YyUAhaEfjoWXclZVJ9sGoNct7j4TVk7lZWlQw5UXuoo= +github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74= +github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= +github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= +github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= +github.com/prometheus/client_golang v1.2.1 h1:JnMpQc6ppsNgw9QPAGF6Dod479itz7lvlsMzzNayLOI= +github.com/prometheus/client_golang v1.2.1/go.mod h1:XMU6Z2MjaRKVu/dC1qupJI9SiNkDYzz3xecMgSW/F+U= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4 h1:gQz4mCbXsO+nc9n1hCxHcGA3Zx3Eo+UHZoInFGUIXNM= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/common v0.7.0 h1:L+1lyG48J1zAQXA3RBX/nG/B3gjlHq0zTt2tlbJLyCY= +github.com/prometheus/common v0.7.0/go.mod h1:DjGbpBbp5NYNiECxcL/VnbXCCaQpKd3tt26CguLLsqA= +github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/procfs v0.0.5 h1:3+auTFlqw+ZaQYJARz6ArODtkaIwtvBTx3N2NehQlL8= +github.com/prometheus/procfs v0.0.5/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDaekg4FpcdQ= +github.com/prometheus/procfs v0.0.7 h1:RS5GAlMbnkWkhs4+bPocMTmGjYkuCY5djjqEDdXOhcQ= +github.com/prometheus/procfs v0.0.7/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A= +github.com/ramya-rao-a/go-outline v0.0.0-20181122025142-7182a932836a h1:rJS9v8WlLfIQ/22PlTXc47p5jB8RaY9XnTkX8Uols7w= +github.com/ramya-rao-a/go-outline v0.0.0-20181122025142-7182a932836a/go.mod h1:1WL5IqM+CnRCAbXetRnL1YVoS9KtU2zMhOi/5oAVPo4= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= +github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ= +github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/slimsag/godocmd v0.0.0-20161025000126-a1005ad29fe3 h1:sAARUcYbwxnebBeWHzKX2MeyXtzy25TEglCTz9BhueY= +github.com/slimsag/godocmd v0.0.0-20161025000126-a1005ad29fe3/go.mod h1:AIBPxLCkKUFc2ZkjCXzs/Kk9OUhQLw/Zicdd0Rhqz2U= +github.com/sourcegraph/ctxvfs v0.0.0-20180418081416-2b65f1b1ea81 h1:v4/JVxZSPWifxmICRqgXK7khThjw03RfdGhyeA2S4EQ= +github.com/sourcegraph/ctxvfs v0.0.0-20180418081416-2b65f1b1ea81/go.mod h1:xIvvI5FiHLxhv8prbzVpaMHaaGPFPFQSuTcxC91ryOo= +github.com/sourcegraph/go-langserver v2.0.0+incompatible h1:lj2sRU7ZMIkW372IDVGb6fE8VAY4c/EMsiDzrB9vmiU= +github.com/sourcegraph/go-langserver v2.0.0+incompatible/go.mod h1:bBMjfpzEHd6ijPRoQ7f+knFfw+e8R+W158/MsqAy77c= +github.com/sourcegraph/jsonrpc2 v0.0.0-20191113080033-cee7209801bf h1:Gebz6aYuWZGkk0GSIRmykKRiN6Z1qgVisisVYERT3IQ= +github.com/sourcegraph/jsonrpc2 v0.0.0-20191113080033-cee7209801bf/go.mod h1:ZafdZgk/axhT1cvZAPOhw+95nz2I/Ra5qMlU4gTRwIo= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191117063200-497ca9f6d64f h1:kz4KIr+xcPUsI3VMoqWfPMvtnJ6MGfiVwsWSVzphMO4= golang.org/x/crypto v0.0.0-20191117063200-497ca9f6d64f/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20191119073136-fc4aabc6c914 h1:MlY3mEfbnWGmUi4rtHOtNnnnN4UJRGSyLPx+DXA5Sq4= golang.org/x/net v0.0.0-20191119073136-fc4aabc6c914/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY= +golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191119195528-f068ffe820e4 h1:FjhQftcbpdYXneEYSWZO7+6Bu+Bi1A8VPvGYWOIzIbw= +golang.org/x/sys v0.0.0-20191119195528-f068ffe820e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20181030221726-6c7e314b6563/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190621195816-6e04913cbbac/go.mod h1:/rFqwRUd4F7ZHNgwSSTFct+R/Kf4OFW1sUzUTQQTgfc= +golang.org/x/tools v0.0.0-20191108194844-46f05828f2fe/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools v0.0.0-20191120001058-ad01d5993d97 h1:u8hSFDulpuhoY0GHMbG6Rp23PzphtTnZrQX3dOvEae0= +golang.org/x/tools v0.0.0-20191120001058-ad01d5993d97/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/tools/gopls v0.2.1 h1:qXHEqJw4CqROYvoDXinY1ZOkyAFS+jcPOvLRK76dvwY= +golang.org/x/tools/gopls v0.2.1/go.mod h1:r4JFjFfxpKo0rD4xMAAP1NAYwefX6vgQhjjVbNqDoFo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/go-playground/assert.v1 v1.2.1 h1:xoYuJVE7KT85PYWrN730RguIQO0ePzVRfFMXadIrXTM= gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE= gopkg.in/go-playground/validator.v8 v8.18.2 h1:lFB4DoMU6B626w8ny76MV7VX6W2VHct2GVOI3xgiMrQ= gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y= +gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.6 h1:97YCGUei5WVbkKfogoJQsLwUJ17cWvpLrgNvlcbxikE= gopkg.in/yaml.v2 v2.2.6/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM= +honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= diff --git a/tcb/README.md b/tcb/README.md new file mode 100644 index 0000000..ac99c8e --- /dev/null +++ b/tcb/README.md @@ -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) \ No newline at end of file diff --git a/tcb/cloudfunction.go b/tcb/cloudfunction.go new file mode 100644 index 0000000..6cbfbe1 --- /dev/null +++ b/tcb/cloudfunction.go @@ -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 +} diff --git a/tcb/database.go b/tcb/database.go new file mode 100644 index 0000000..36705c4 --- /dev/null +++ b/tcb/database.go @@ -0,0 +1,413 @@ +package tcb + +import ( + "fmt" + + "github.com/silenceper/wechat/util" +) + +const ( + //数据库导入 + databaseMigrateImportURL = "https://api.weixin.qq.com/tcb/databasemigrateimport" + //数据库导出 + databaseMigrateExportURL = "https://api.weixin.qq.com/tcb/databasemigrateexport" + //数据库迁移状态查询 + databaseMigrateQueryInfoURL = "https://api.weixin.qq.com/tcb/databasemigratequeryinfo" + //变更数据库索引 + updateIndexURL = "https://api.weixin.qq.com/tcb/updateindex" + //新增集合 + databaseCollectionAddURL = "https://api.weixin.qq.com/tcb/databasecollectionadd" + //删除集合 + databaseCollectionDeleteURL = "https://api.weixin.qq.com/tcb/databasecollectiondelete" + //获取特定云环境下集合信息 + databaseCollectionGetURL = "https://api.weixin.qq.com/tcb/databasecollectionget" + //数据库插入记录 + databaseAddURL = "https://api.weixin.qq.com/tcb/databaseadd" + //数据库删除记录 + databaseDeleteURL = "https://api.weixin.qq.com/tcb/databasedelete" + //数据库更新记录 + databaseUpdateURL = "https://api.weixin.qq.com/tcb/databaseupdate" + //数据库查询记录 + databaseQueryURL = "https://api.weixin.qq.com/tcb/databasequery" + //统计集合记录数或统计查询语句对应的结果记录数 + databaseCountURL = "https://api.weixin.qq.com/tcb/databasecount" + + //冲突处理模式 + ConflictModeInster ConflictMode = 1 + ConflictModeUpsert ConflictMode = 2 + + //file_type 的合法值 + FileTypeJson FileType = 1 + FileTypeCsv FileType = 2 +) + +type ConflictMode int +type FileType int + +//ValidDirections 合法的direction值 +var ValidDirections = []string{"1", "-1", "2dsphere"} + +//DatabaseMigrateExportReq 数据库出 请求参数 +type DatabaseMigrateExportReq struct { + Env string `json:"env,omitempty"` //云环境ID + FilePath string `json:"file_path,omitempty"` //导出文件路径(导入文件需先上传到同环境的存储中,可使用开发者工具或 HTTP API的上传文件 API上传) + FileType FileType `json:"file_type,omitempty"` //导出文件类型,文件格式参考数据库导入指引中的文件格式部分 1:json 2:csv + Query string `json:"query,omitempty"` //导出条件 +} + +//DatabaseMigrateExportRes 数据库导出 返回结果 +type DatabaseMigrateExportRes struct { + util.CommonError + JobID int64 `json:"job_id"` //导出任务ID,可使用数据库迁移进度查询 API 查询导入进度及结果 +} + +//DatabaseMigrateImportReq 数据库导入 请求参数 +type DatabaseMigrateImportReq struct { + Env string `json:"env,omitempty"` //云环境ID + CollectionName string `json:"collection_name,omitempty"` //集合名称 + FilePath string `json:"file_path,omitempty"` //导出文件路径(文件会导出到同环境的云存储中,可使用获取下载链接 API 获取下载链接) + FileType FileType `json:"file_type,omitempty"` //导入文件类型,文件格式参考数据库导入指引中的文件格式部分 1:json 2:csv + StopOnError bool `json:"stop_on_error,omitempty"` //是否在遇到错误时停止导入 + ConflictMode ConflictMode `json:"conflict_mode,omitempty"` //冲突处理模式 1:inster 2:UPSERT +} + +//DatabaseMigrateImportReq 数据库导入 返回结果 +type DatabaseMigrateImportRes struct { + util.CommonError + JobID int64 `json:"job_id"` //导入任务ID,可使用数据库迁移进度查询 API 查询导入进度及结果 +} + +//DatabaseMigrateQueryInfoRes 数据库迁移状态查询 +type DatabaseMigrateQueryInfoRes struct { + util.CommonError + Status string `json:"status"` //导出状态 + RecordSuccess int64 `json:"record_success"` //导出成功记录数 + RecordFail int64 `json:"record_fail"` //导出失败记录数 + ErrMsg string `json:"err_msg"` //导出错误信息 + FileURL string `json:"file_url"` //导出文件下载地址 +} + +//UpdateIndexReq 变更数据库索引 请求参数 +type UpdateIndexReq struct { + Env string `json:"env,omitempty"` //云环境ID + CollectionName string `json:"collection_name,omitempty"` //集合名称 + CreateIndexes []CreateIndex `json:"create_indexes,omitempty"` //新增索引 + DropIndexes []DropIndex `json:"drop_indexes,omitempty"` //删除索引 +} + +//CreateIndex 新增索引 +type CreateIndex struct { + Name string `json:"name,omitempty"` //索引名 + Unique bool `json:"unique,omitempty"` //是否唯一 + Keys []CreateIndexKey `json:"keys,omitempty"` //索引字段 +} + +//CreateIndexKey create index key +type CreateIndexKey struct { + Name string `json:"name,omitempty"` //字段名 + Direction string `json:"direction,omitempty"` //字段排序 +} + +//DropIndex 删除索引 +type DropIndex struct { + Name string `json:"name,omitempty"` +} + +//DatabaseCollectionReq 新增/删除集合请求参数 +type DatabaseCollectionReq struct { + Env string `json:"env,omitempty"` //云环境ID + CollectionName string `json:"collection_name,omitempty"` //集合名称 +} + +//DatabaseCollectionGetReq 获取特定云环境下集合信息请求 +type DatabaseCollectionGetReq struct { + Env string `json:"env,omitempty"` //云环境ID + Limit int64 `json:"limit,omitempty"` //获取数量限制 + Offset int64 `json:"offset,omitempty"` //偏移量 +} + +//DatabaseCollectionGetRes 获取特定云环境下集合信息结果 +type DatabaseCollectionGetRes struct { + util.CommonError + Pager struct { + Limit int64 `json:"limit"` //单次查询限制 + Offset int64 `json:"offset"` //偏移量 + Total int64 `json:"total"` //符合查询条件的记录总数 + } `json:"pager"` + Collections []struct { + Name string `json:"name"` //集合名 + Count int64 `json:"count"` //表中文档数量 + Size int64 `json:"size"` //表的大小(即表中文档总大小),单位:字节 + IndexCount int64 `json:"index_count"` //索引数量 + IndexSize int64 `json:"index_size"` //索引占用大小,单位:字节 + } `json:"collections"` +} + +//DatabaseReq 数据库插入/删除/更新/查询/统计记录请求参数 +type DatabaseReq struct { + Env string `json:"env,omitempty"` //云环境ID + Query string `json:"query,omitempty"` //数据库操作语句 +} + +//DatabaseAddRes 数据库插入记录返回结果 +type DatabaseAddRes struct { + util.CommonError + IDList []string `json:"id_list"` //插入成功的数据集合主键_id。 +} + +//DatabaseDeleteRes 数据库删除记录返回结果 +type DatabaseDeleteRes struct { + util.CommonError + Deleted int64 `json:"deleted"` //删除记录数量 +} + +//DatabaseUpdateRes 数据库更新记录返回结果 +type DatabaseUpdateRes struct { + util.CommonError + Matched int64 `json:"matched"` //更新条件匹配到的结果数 + Modified int64 `json:"modified"` //修改的记录数,注意:使用set操作新插入的数据不计入修改数目 + ID string `json:"id"` +} + +//DatabaseQueryRes 数据库查询记录 返回结果 +type DatabaseQueryRes struct { + util.CommonError + Pager struct { + Limit int64 `json:"limit"` //单次查询限制 + Offset int64 `json:"offset"` //偏移量 + Total int64 `json:"total"` //符合查询条件的记录总数 + } `json:"pager"` + Data []string `json:"data"` +} + +//DatabaseCountRes 统计集合记录数或统计查询语句对应的结果记录数 返回结果 +type DatabaseCountRes struct { + util.CommonError + Count int64 `json:"count"` //记录数量 +} + +//DatabaseMigrateImport 数据库导入 +//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseMigrateImport.html +func (tcb *Tcb) DatabaseMigrateImport(req *DatabaseMigrateImportReq) (*DatabaseMigrateImportRes, error) { + accessToken, err := tcb.GetAccessToken() + if err != nil { + return nil, err + } + uri := fmt.Sprintf("%s?access_token=%s", databaseMigrateImportURL, accessToken) + response, err := util.PostJSON(uri, req) + if err != nil { + return nil, err + } + databaseMigrateImportRes := &DatabaseMigrateImportRes{} + err = util.DecodeWithError(response, databaseMigrateImportRes, "DatabaseMigrateImport") + return databaseMigrateImportRes, err +} + +//DatabaseMigrateExport 数据库导出 +//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseMigrateExport.html +func (tcb *Tcb) DatabaseMigrateExport(req *DatabaseMigrateExportReq) (*DatabaseMigrateExportRes, error) { + accessToken, err := tcb.GetAccessToken() + if err != nil { + return nil, err + } + uri := fmt.Sprintf("%s?access_token=%s", databaseMigrateExportURL, accessToken) + response, err := util.PostJSON(uri, req) + if err != nil { + return nil, err + } + databaseMigrateExportRes := &DatabaseMigrateExportRes{} + err = util.DecodeWithError(response, databaseMigrateExportRes, "DatabaseMigrateExport") + return databaseMigrateExportRes, err +} + +//databaseMigrateQueryInfo 数据库迁移状态查询 +//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseMigrateQueryInfo.html +func (tcb *Tcb) DatabaseMigrateQueryInfo(env string, jobID int64) (*DatabaseMigrateQueryInfoRes, error) { + accessToken, err := tcb.GetAccessToken() + if err != nil { + return nil, err + } + uri := fmt.Sprintf("%s?access_token=%s", databaseMigrateQueryInfoURL, accessToken) + response, err := util.PostJSON(uri, map[string]interface{}{ + "env": env, + "job_id": jobID, + }) + if err != nil { + return nil, err + } + databaseMigrateQueryInfoRes := &DatabaseMigrateQueryInfoRes{} + err = util.DecodeWithError(response, databaseMigrateQueryInfoRes, "DatabaseMigrateQueryInfo") + return databaseMigrateQueryInfoRes, err +} + +//updateIndex 变更数据库索引 +//https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/updateIndex.html +func (tcb *Tcb) UpdateIndex(req *UpdateIndexReq) error { + accessToken, err := tcb.GetAccessToken() + if err != nil { + return err + } + uri := fmt.Sprintf("%s?access_token=%s", updateIndexURL, accessToken) + response, err := util.PostJSON(uri, req) + if err != nil { + return err + } + return util.DecodeWithCommonError(response, "UpdateIndex") +} + +//databaseCollectionAdd 新增集合 +//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseCollectionAdd.html +func (tcb *Tcb) DatabaseCollectionAdd(env, collectionName string) error { + accessToken, err := tcb.GetAccessToken() + if err != nil { + return err + } + uri := fmt.Sprintf("%s?access_token=%s", databaseCollectionAddURL, accessToken) + response, err := util.PostJSON(uri, &DatabaseCollectionReq{ + Env: env, + CollectionName: collectionName, + }) + if err != nil { + return err + } + return util.DecodeWithCommonError(response, "DatabaseCollectionAdd") +} + +//DatabaseCollectionDelete 删除集合 +//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseCollectionDelete.html +func (tcb *Tcb) DatabaseCollectionDelete(env, collectionName string) error { + accessToken, err := tcb.GetAccessToken() + if err != nil { + return err + } + uri := fmt.Sprintf("%s?access_token=%s", databaseCollectionDeleteURL, accessToken) + response, err := util.PostJSON(uri, &DatabaseCollectionReq{ + Env: env, + CollectionName: collectionName, + }) + if err != nil { + return err + } + return util.DecodeWithCommonError(response, "DatabaseCollectionDelete") +} + +//DatabaseCollectionGet 获取特定云环境下集合信息 +//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseCollectionGet.html +func (tcb *Tcb) DatabaseCollectionGet(env string, limit, offset int64) (*DatabaseCollectionGetRes, error) { + accessToken, err := tcb.GetAccessToken() + if err != nil { + return nil, err + } + uri := fmt.Sprintf("%s?access_token=%s", databaseCollectionGetURL, accessToken) + response, err := util.PostJSON(uri, &DatabaseCollectionGetReq{ + Env: env, + Limit: limit, + Offset: offset, + }) + if err != nil { + return nil, err + } + databaseCollectionGetRes := &DatabaseCollectionGetRes{} + err = util.DecodeWithError(response, databaseCollectionGetRes, "DatabaseCollectionGet") + return databaseCollectionGetRes, err +} + +//databaseAdd 数据库插入记录 +//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseAdd.html +func (tcb *Tcb) DatabaseAdd(env, query string) (*DatabaseAddRes, error) { + accessToken, err := tcb.GetAccessToken() + if err != nil { + return nil, err + } + uri := fmt.Sprintf("%s?access_token=%s", databaseAddURL, accessToken) + response, err := util.PostJSON(uri, &DatabaseReq{ + Env: env, + Query: query, + }) + if err != nil { + return nil, err + } + databaseAddRes := &DatabaseAddRes{} + err = util.DecodeWithError(response, databaseAddRes, "DatabaseAdd") + return databaseAddRes, err +} + +//DatabaseDelete 数据库插入记录 +//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseDelete.html +func (tcb *Tcb) DatabaseDelete(env, query string) (*DatabaseDeleteRes, error) { + accessToken, err := tcb.GetAccessToken() + if err != nil { + return nil, err + } + uri := fmt.Sprintf("%s?access_token=%s", databaseDeleteURL, accessToken) + response, err := util.PostJSON(uri, &DatabaseReq{ + Env: env, + Query: query, + }) + if err != nil { + return nil, err + } + databaseDeleteRes := &DatabaseDeleteRes{} + err = util.DecodeWithError(response, databaseDeleteRes, "DatabaseDelete") + return databaseDeleteRes, err +} + +//DatabaseUpdate 数据库插入记录 +//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseUpdate.html +func (tcb *Tcb) DatabaseUpdate(env, query string) (*DatabaseUpdateRes, error) { + accessToken, err := tcb.GetAccessToken() + if err != nil { + return nil, err + } + uri := fmt.Sprintf("%s?access_token=%s", databaseUpdateURL, accessToken) + response, err := util.PostJSON(uri, &DatabaseReq{ + Env: env, + Query: query, + }) + if err != nil { + return nil, err + } + databaseUpdateRes := &DatabaseUpdateRes{} + err = util.DecodeWithError(response, databaseUpdateRes, "DatabaseUpdate") + return databaseUpdateRes, err +} + +//DatabaseQuery 数据库查询记录 +//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseQuery.html +func (tcb *Tcb) DatabaseQuery(env, query string) (*DatabaseQueryRes, error) { + accessToken, err := tcb.GetAccessToken() + if err != nil { + return nil, err + } + uri := fmt.Sprintf("%s?access_token=%s", databaseQueryURL, accessToken) + response, err := util.PostJSON(uri, &DatabaseReq{ + Env: env, + Query: query, + }) + if err != nil { + return nil, err + } + databaseQueryRes := &DatabaseQueryRes{} + err = util.DecodeWithError(response, databaseQueryRes, "DatabaseQuery") + return databaseQueryRes, err +} + +//DatabaseCount 统计集合记录数或统计查询语句对应的结果记录数 +//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/database/databaseCount.html +func (tcb *Tcb) DatabaseCount(env, query string) (*DatabaseCountRes, error) { + accessToken, err := tcb.GetAccessToken() + if err != nil { + return nil, err + } + uri := fmt.Sprintf("%s?access_token=%s", databaseCountURL, accessToken) + response, err := util.PostJSON(uri, &DatabaseReq{ + Env: env, + Query: query, + }) + if err != nil { + return nil, err + } + databaseCountRes := &DatabaseCountRes{} + err = util.DecodeWithError(response, databaseCountRes, "DatabaseCount") + return databaseCountRes, err +} diff --git a/tcb/file.go b/tcb/file.go new file mode 100644 index 0000000..3eaabe1 --- /dev/null +++ b/tcb/file.go @@ -0,0 +1,134 @@ +package tcb + +import ( + "fmt" + + "github.com/silenceper/wechat/util" +) + +const ( + //获取文件上传链接 + uploadFilePathURL = "https://api.weixin.qq.com/tcb/uploadfile" + //获取文件下载链接 + batchDownloadFileURL = "https://api.weixin.qq.com/tcb/batchdownloadfile" + //删除文件链接 + batchDeleteFileURL = "https://api.weixin.qq.com/tcb/batchdeletefile" +) + +//UploadFileReq 上传文件请求值 +type UploadFileReq struct { + Env string `json:"env,omitempty"` + Path string `json:"path,omitempty"` +} + +//UploadFileRes 上传文件返回结果 +type UploadFileRes struct { + util.CommonError + URL string `json:"url"` //上传url + Token string `json:"token"` //token + Authorization string `json:"authorization"` //authorization + FileID string `json:"file_id"` //文件ID + CosFileID string `json:"cos_file_id"` //cos文件ID +} + +//BatchDownloadFileReq 上传文件请求值 +type BatchDownloadFileReq struct { + Env string `json:"env,omitempty"` + FileList []*DownloadFile `json:"file_list,omitempty"` +} + +//DownloadFile 文件信息 +type DownloadFile struct { + FileID string `json:"fileid"` //文件ID + MaxAge int64 `json:"max_age"` //下载链接有效期 +} + +//BatchDownloadFileRes 上传文件返回结果 +type BatchDownloadFileRes struct { + util.CommonError + FileList []struct { + FileID string `json:"file_id"` //文件ID + DownloadURL string `json:"download_url"` //下载链接 + Status int64 `json:"status"` //状态码 + ErrMsg string `json:"errmsg"` //该文件错误信息 + } `json:"file_list"` +} + +//BatchDeleteFileReq 批量删除文件请求参数 +type BatchDeleteFileReq struct { + Env string `json:"env,omitempty"` + FileIDList []string `json:"fileid_list,omitempty"` +} + +//BatchDeleteFileRes 批量删除文件返回结果 +type BatchDeleteFileRes struct { + util.CommonError + DeleteList []struct { + FileID string `json:"fileid"` + Status int64 `json:"status"` + ErrMsg string `json:"errmsg"` + } `json:"delete_list"` +} + +//UploadFile 上传文件 +//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/uploadFile.html +func (tcb *Tcb) UploadFile(env, path string) (*UploadFileRes, error) { + accessToken, err := tcb.GetAccessToken() + if err != nil { + return nil, err + } + uri := fmt.Sprintf("%s?access_token=%s", uploadFilePathURL, accessToken) + req := &UploadFileReq{ + Env: env, + Path: path, + } + response, err := util.PostJSON(uri, req) + if err != nil { + return nil, err + } + uploadFileRes := &UploadFileRes{} + err = util.DecodeWithError(response, uploadFileRes, "UploadFile") + return uploadFileRes, err +} + +//BatchDownloadFile 获取文件下载链接 +//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/batchDownloadFile.html +func (tcb *Tcb) BatchDownloadFile(env string, fileList []*DownloadFile) (*BatchDownloadFileRes, error) { + accessToken, err := tcb.GetAccessToken() + if err != nil { + return nil, err + } + uri := fmt.Sprintf("%s?access_token=%s", batchDownloadFileURL, accessToken) + req := &BatchDownloadFileReq{ + Env: env, + FileList: fileList, + } + response, err := util.PostJSON(uri, req) + if err != nil { + return nil, err + } + batchDownloadFileRes := &BatchDownloadFileRes{} + err = util.DecodeWithError(response, batchDownloadFileRes, "BatchDownloadFile") + return batchDownloadFileRes, err +} + +//BatchDeleteFile 批量删除文件 +//reference:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-http-api/storage/batchDeleteFile.html +func (tcb *Tcb) BatchDeleteFile(env string, fileIDList []string) (*BatchDeleteFileRes, error) { + accessToken, err := tcb.GetAccessToken() + if err != nil { + return nil, err + } + uri := fmt.Sprintf("%s?access_token=%s", batchDeleteFileURL, accessToken) + req := &BatchDeleteFileReq{ + Env: env, + FileIDList: fileIDList, + } + response, err := util.PostJSON(uri, req) + if err != nil { + return nil, err + } + batchDeleteFileRes := &BatchDeleteFileRes{} + err = util.DecodeWithError(response, batchDeleteFileRes, "BatchDeleteFile") + return batchDeleteFileRes, nil +} diff --git a/tcb/tcb.go b/tcb/tcb.go new file mode 100644 index 0000000..a8ba59b --- /dev/null +++ b/tcb/tcb.go @@ -0,0 +1,16 @@ +package tcb + +import "github.com/silenceper/wechat/context" + +//Tcb Tencent Cloud Base +type Tcb struct{ + *context.Context +} + +//NewTcb new Tencent Cloud Base +func NewTcb(context *context.Context)*Tcb{ + return &Tcb{ + context, + } +} + diff --git a/util/error.go b/util/error.go index 79ad14d..b971c47 100644 --- a/util/error.go +++ b/util/error.go @@ -3,6 +3,7 @@ package util import ( "encoding/json" "fmt" + "reflect" ) // CommonError 微信返回的通用错误json @@ -23,3 +24,28 @@ func DecodeWithCommonError(response []byte, apiName string) (err error) { } return nil } + +// DecodeWithError 将返回值按照解析 +func DecodeWithError(response []byte, obj interface{}, apiName string) error { + err := json.Unmarshal(response, obj) + if err != nil { + return fmt.Errorf("json Unmarshal Error, err=%v", err) + } + responseObj := reflect.ValueOf(obj) + if !responseObj.IsValid() { + return fmt.Errorf("obj is invalid") + } + commonError := responseObj.Elem().FieldByName("CommonError") + if !commonError.IsValid() || commonError.Kind() != reflect.Struct { + return fmt.Errorf("commonError is invalid or not struct") + } + errCode := commonError.FieldByName("ErrCode") + errMsg := commonError.FieldByName("ErrMsg") + if !errCode.IsValid() || !errMsg.IsValid() { + return fmt.Errorf("errcode or errmsg is invalid") + } + if errCode.Int() != 0 { + return fmt.Errorf("%s Error , errcode=%d , errmsg=%s", apiName, errCode.Int(), errMsg.String()) + } + return nil +} diff --git a/util/http.go b/util/http.go index 33deb34..44b4028 100644 --- a/util/http.go +++ b/util/http.go @@ -7,13 +7,14 @@ import ( "encoding/pem" "encoding/xml" "fmt" - "golang.org/x/crypto/pkcs12" "io" "io/ioutil" "log" "mime/multipart" "net/http" "os" + + "golang.org/x/crypto/pkcs12" ) //HTTPGet get 请求 @@ -30,17 +31,30 @@ func HTTPGet(uri string) ([]byte, error) { return ioutil.ReadAll(response.Body) } +//HTTPPost post 请求 +func HTTPPost(uri string, data string) ([]byte, error) { + body := bytes.NewBuffer([]byte(data)) + response, err := http.Post(uri, "", body) + if err != nil { + return nil, err + } + + defer response.Body.Close() + if response.StatusCode != http.StatusOK { + return nil, fmt.Errorf("http get error : uri=%v , statusCode=%v", uri, response.StatusCode) + } + return ioutil.ReadAll(response.Body) +} + //PostJSON post json 数据请求 func PostJSON(uri string, obj interface{}) ([]byte, error) { jsonData, err := json.Marshal(obj) if err != nil { return nil, err } - jsonData = bytes.Replace(jsonData, []byte("\\u003c"), []byte("<"), -1) jsonData = bytes.Replace(jsonData, []byte("\\u003e"), []byte(">"), -1) jsonData = bytes.Replace(jsonData, []byte("\\u0026"), []byte("&"), -1) - body := bytes.NewBuffer(jsonData) response, err := http.Post(uri, "application/json;charset=utf-8", body) if err != nil { diff --git a/wechat.go b/wechat.go index a843937..d8ba2a0 100644 --- a/wechat.go +++ b/wechat.go @@ -1,22 +1,22 @@ package wechat import ( - "github.com/silenceper/wechat/device" - "github.com/silenceper/wechat/message" - "net/http" "sync" "github.com/silenceper/wechat/cache" "github.com/silenceper/wechat/context" + "github.com/silenceper/wechat/device" "github.com/silenceper/wechat/js" "github.com/silenceper/wechat/material" "github.com/silenceper/wechat/menu" + "github.com/silenceper/wechat/message" "github.com/silenceper/wechat/miniprogram" "github.com/silenceper/wechat/oauth" "github.com/silenceper/wechat/pay" "github.com/silenceper/wechat/qr" "github.com/silenceper/wechat/server" + "github.com/silenceper/wechat/tcb" "github.com/silenceper/wechat/user" ) @@ -118,3 +118,8 @@ func (wc *Wechat) GetMiniProgram() *miniprogram.MiniProgram { func (wc *Wechat) GetDevice() *device.Device { return device.NewDevice(wc.Context) } + +// GetTcb 获取小程序-云开发的实例 +func (wc *Wechat) GetTcb() *tcb.Tcb { + return tcb.NewTcb(wc.Context) +}