提交 4b8092a3 编写于 作者: d-u-a's avatar d-u-a

add uni-open-bridge

上级 6487bc8c
......@@ -4,6 +4,10 @@
> `云函数公共模块`是不同云函数共享代码的一种方式。如果你不了解什么是`云函数公共模块`,请另读文档[公共模块](https://uniapp.dcloud.io/uniCloud/cf-common)
调用微信绝大多数后台接口时都需使用 `access_token``session_key``ticket`,开发者需要进行妥善保存。为了解决这个问题,使用公共模块 `uni-open-bridge-common` 统一调用
`uni-open-bridge-common` 有配套的云对象 `uni-open-bridge`, 可免维护 `access_token``ticket` 调用,[详情](/uni-open-bridge)
`uni-open-bridge-common` 提供了 `access_token``session_key``encrypt_key``ticket` 的读取、写入、删除操作。
`uni-open-bridge-common` 支持多层 读取 / 写入 机制,`redis -> database -> fallback`,优先级如下:
......@@ -18,7 +22,7 @@
`access_token` 是微信小程序全局唯一后台接口调用凭据,调用绝大多数后台接口时都需使用。开发者可以通过 getAccessToken 接口获取并进行妥善保存。[详情](https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/backend-api.html#access_token)
### getAccessToken(key: Object)
### getAccessToken(key: Object, fallback: Function)
读取 access_token
......@@ -39,7 +43,6 @@
|:-: |:-: |:-: |:-: |
|dcloudAppid|String |是 |DCloud应用appid。[详情](https://ask.dcloud.net.cn/article/35907) |
|platform |String |是 |[详情](#platform) |
|fallback |Function |否 |[详情](#fallback) |
### value 属性
......@@ -101,7 +104,7 @@ exports.main = async (event, context) => {
|微信小程序 |session_key|微信小程序会话密钥。[详情](https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.html) |
### getUserKey(key: Object)
### getUserKey(key: Object, fallback: Function)
读取 user_key
......@@ -184,7 +187,7 @@ exports.main = async (event, context) => {
开发者可以分别通过小程序前端和微信后台提供的接口,获取用户的加密 key。
### getEncryptKey(key: Object)
### getEncryptKey(key: Object, fallback: Function)
读取 encrypt_key
......@@ -266,10 +269,10 @@ exports.main = async (event, context) => {
## ticket
`ticket` 是公众号用于调用微信 JS 接口的临时票据。[详情](https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#72)
`ticket` 是公众号用于调用微信 JS 接口的临时票据。[详情](https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#62)
### getTicket(key: Object)
### getTicket(key: Object, fallback: Function)
读取 ticket
......@@ -368,9 +371,9 @@ exports.main = async (event, context) => {
}
```
为了简化调用 `getAccessToken()``getTicket()` 已内置 `fallback` 到微信的服务器,需要在 `config-center` 中配置 `appid` `appsecret`
注意事项
- 所有方法类型为 `async`,需要使用 `await`
- 所有方法校验 `key` 属性是否有效,无效则 `throw new Error()`,对 `value` 仅校验是否为 `undefined`
# uni-open-bridge
`uni-open-bridge``uni-id` 体系中用于 `开放平台数据` 定时刷新 `access_token``ticket` 的云对象,使用 `uni-open-bridge` 可免维护 `access_token``ticket` 的定时刷新
`uni-open-bridge` 从微信的服务器获取数据,通过 [uni-open-bridge-commmon](https://uniapp.dcloud.net.cn/uniCloud/uni-open-bridge-common) 保存到 `Redis``database`
`uni-open-bridge` 提供了 http 请求的操作方式
## config.json
配置文件需要依赖 `uni-config-center`,在 `uni-config-center` 根目录添加文件夹 `uni-open-bridge`, 新增 `config.json`, 内容如下
```json
{
"schedule": {
"__UNI__xxxxxx": { // dcloudAppid,需要在 `uni-config-center` uni-id 中配置
"enable": true, // 任务全局开关,优先级最高
"mp-weixin": { // 平台,目前仅支持 微信小程序、微信 H5,详情参见 https://uniapp.dcloud.net.cn/uniCloud/uni-open-bridge#platform
"enable": true, // 当前平台任务开关
"tasks": ["accessToken"] // 要执行的任务,微信小程序支持 accessToken
},
"h5-weixin": {
"enable": false,
"tasks": ["ticket"] // 支持微信 H5 ticket,因 ticker 依赖微信 H5 accessToken,内部自动先获取 accessToken。此处的 accessToken 和微信小程序的 accessToken 不是一个值
}
}
},
"ipWhitelist": ["0.0.0.0"] // 用于 http 调用的服务器IP白名单,支持配置多个,阿里云暂不支持独立IP,可以先使用腾讯云,如果需要使用阿里云,可以通过http的方式将值保存
}
```
注意:拷贝此文件内容到 `config.json` 时需要移除`注释`
## http 调用
请求类型 `POST`, 需要配置IP白名单字段 `ipWhitelist`,参见 `config.json`
同时需要开启 [URL化](https://uniapp.dcloud.net.cn/uniCloud/http.html),详见 [uniCloud Web](https://unicloud.dcloud.net.cn/) 控制台
### getAccessToken
Url
```
https://xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx.bspapp.com/uni-open-bridge/getAccessToken
```
参数
```json
{
"dcloudAppid": "__UNI__xxx",
"platform": "mp-weixin"
}
```
### setAccessToken
Url
```
https://xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx.bspapp.com/uni-open-bridge/setAccessToken
```
```json
{
"dcloudAppid": "__UNI__xxx",
"platform": "mp-weixin",
"value": {
"access_token": ""
},
"expiresIn": 7200
}
```
### removeAccessToken
Url
```
https://xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx.bspapp.com/uni-open-bridge/removeAccessToken
```
参数
```json
{
"dcloudAppid": "__UNI__xxx",
"platform": "mp-weixin"
}
```
### getUserKey
Url
```
https://xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx.bspapp.com/uni-open-bridge/getUserKey
```
参数
```json
{
"dcloudAppid": "__UNI__xxx",
"platform": "mp-weixin",
"openid": ""
}
```
### setUserKey
Url
```
https://xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx.bspapp.com/uni-open-bridge/setUserKey
```
参数
```json
{
"dcloudAppid": "__UNI__xxx",
"platform": "mp-weixin",
"openid": "",
"value": {
"session_key": ""
},
"expiresIn": 7200
}
```
### removeUserKey
Url
```
https://xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx.bspapp.com/uni-open-bridge/removeUserKey
```
参数
```json
{
"dcloudAppid": "__UNI__xxx",
"platform": "mp-weixin",
"openid": ""
}
```
### getTicket
Url
```
https://xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx.bspapp.com/uni-open-bridge/getTicket
```
参数
```json
{
"dcloudAppid": "__UNI__xxx",
"platform": "mp-weixin"
}
```
### setTicket
Url
```
https://xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx.bspapp.com/uni-open-bridge/setTicket
```
参数
```json
{
"dcloudAppid": "__UNI__xxx",
"platform": "mp-weixin",
"value": {
"ticket": ""
},
"expiresIn": 7200
}
```
### removeTicket
Url
```
https://xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx.bspapp.com/uni-open-bridge/removeTicket
```
参数
```json
{
"dcloudAppid": "__UNI__xxx",
"platform": "mp-weixin"
}
```
注意:部署后将自动开启定时任务,间隔1小时执行
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册