From 6935c6417d44fe8a675af36c15bacd4c2e454f6b Mon Sep 17 00:00:00 2001 From: wangyaqi Date: Fri, 12 Aug 2022 17:34:31 +0800 Subject: [PATCH] deps: bump uni-open-bridge to pre-release version --- uni_modules/uni-open-bridge/changelog.md | 2 + uni_modules/uni-open-bridge/package.json | 20 +- uni_modules/uni-open-bridge/readme.md | 576 +----------------- .../common/uni-open-bridge-common/config.js | 10 +- .../common/uni-open-bridge-common/index.js | 49 +- .../uni-open-bridge-common/validator.js | 3 + .../cloudfunctions/uni-open-bridge/bridge.js | 126 ++++ .../cloudfunctions/uni-open-bridge/config.js | 8 +- .../uni-open-bridge/index.mp-weixin.js | 93 --- .../uni-open-bridge/index.obj.js | 66 +- .../uni-open-bridge/package.json | 16 +- 11 files changed, 249 insertions(+), 720 deletions(-) create mode 100644 uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/bridge.js delete mode 100644 uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/index.mp-weixin.js diff --git a/uni_modules/uni-open-bridge/changelog.md b/uni_modules/uni-open-bridge/changelog.md index e69de29..d720307 100644 --- a/uni_modules/uni-open-bridge/changelog.md +++ b/uni_modules/uni-open-bridge/changelog.md @@ -0,0 +1,2 @@ +## 1.0.0(2022-08-05) +- 首次发布 diff --git a/uni_modules/uni-open-bridge/package.json b/uni_modules/uni-open-bridge/package.json index e88a0a7..666c8a5 100644 --- a/uni_modules/uni-open-bridge/package.json +++ b/uni_modules/uni-open-bridge/package.json @@ -2,13 +2,17 @@ "id": "uni-open-bridge", "displayName": "uni-open-bridge", "version": "1.0.0", - "description": "uni-open-bridge", + "description": "uni-open-bridge 是统一接管微信等三方平台认证的开源库", "keywords": [ - "uni-open-bridge" + "uni-open-bridge-common", + "access_token", + "session_key", + "ticket", + "微信" ], "repository": "", "engines": { - "HBuilderX": "^3.1.0" + "HBuilderX": "^3.5.1" }, "dcloudext": { "category": [ @@ -27,9 +31,9 @@ "qq": "" }, "declaration": { - "ads": "", - "data": "", - "permissions": "" + "ads": "无", + "data": "无", + "permissions": "无" }, "npmurl": "" }, @@ -38,8 +42,8 @@ "encrypt": [], "platforms": { "cloud": { - "tcb": "u", - "aliyun": "u" + "tcb": "y", + "aliyun": "y" }, "client": { "Vue": { diff --git a/uni_modules/uni-open-bridge/readme.md b/uni_modules/uni-open-bridge/readme.md index 9f4f9f7..d0a16ad 100644 --- a/uni_modules/uni-open-bridge/readme.md +++ b/uni_modules/uni-open-bridge/readme.md @@ -1,577 +1,5 @@ # uni-open-bridge -`uni-open-bridge` +`uni-open-bridge` 是统一接管微信等三方平台认证的开源库 - -## config.json - -```json -{ - "schedule": { - "__UNI__xxxxxx": { - "enable": true, - "mp-weixin": { - "enable": true, - "tasks": ["accessToken"] - }, - "h5-weixin": { - "enable": false, - "tasks": ["ticket"] - } - } - }, - "ipWhitelist": ["0.0.0.0"] -} -``` - -## http 调用 - -请求类型 `POST`, 需要配置IP白名单字段 `ipWhitelist`,参见 `config.json` - -### 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" -} -``` - - - - - -# uni-open-bridge-common - -`uni-open-bridge-common` 是 `uni-id` 体系中用于 `开放平台数据` 管理的公共模块。 - -> `云函数公共模块`是不同云函数共享代码的一种方式。如果你不了解什么是`云函数公共模块`,请另读文档[公共模块](https://uniapp.dcloud.io/uniCloud/cf-common) - -`uni-open-bridge-common` 提供了 `access_token`、`session_key`、`encrypt_key`、`ticket` 的读取、写入、删除操作。 - -`uni-open-bridge-common` 支持多层 读取 / 写入 机制,`redis -> database -> fallback`,优先级如下: - -如果用户没有开通 `redis` 或者操作失败,透传到 `database`,`database` 失败后,如果用户配置了 `fallback`,继续调用 `fallback` 方法,否则抛出 `Error` - -`database` 对应的表为: `opendb-open-data` - - -## access_token - -`access_token` 是微信小程序全局唯一后台接口调用凭据,调用绝大多数后台接口时都需使用。开发者可以通过 getAccessToken 接口获取并进行妥善保存。[详情](https://developers.weixin.qq.com/miniprogram/dev/framework/server-ability/backend-api.html#access_token) - - -### getAccessToken(key: Object) - -读取 access_token - - -### setAccessToken(key: Object, value: Object, expiresIn: Number) - -写入 access_token - - -### removeAccessToken(key: Object) - -删除 access_token - - -### key 属性 - -|参数 |类型 |必填 |描述 | -|:-: |:-: |:-: |:-: | -|dcloudAppid|String |是 |DCloud应用appid。[详情](https://ask.dcloud.net.cn/article/35907) | -|platform |String |是 |[详情](#platform) | -|fallback |Function |否 |[详情](#fallback) | - -### value 属性 - -|参数 |类型 |描述 | -|:-: |:-: |:-: | -|access_token |String | | - -### expiresIn - -有效时间(秒) - - -### 示例代码 - -```js -'use strict'; - -const { - getAccessToken, - setAccessToken, - removeAccessToken -} = require('uni-open-bridge-common') - -exports.main = async (event, context) => { - const key = { - dcloudAppid: '', - platform: '' - } - const value = { - access_token: '' - } - const expiresIn = 7200 - - // 写入 (redis / 数据库) - await setAccessToken(key, value, expiresIn) - - // 读取 (redis / 数据库) - let result1 = await getAccessToken(key) - - // 删除 - await removeAccessToken(key) - - - // 删除后读取, 返回 null - let result2 = await getAccessToken(key) - console.log(result2) // null - - return null -}; -``` - - -## user_key - -平台对应的值 - -|平台 |值 |描述 | -|:-: |:-: |:-: | -|微信小程序 |session_key|微信小程序会话密钥。[详情](https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.html) | - - -### getUserKey(key: Object) - -读取 user_key - - -### setUserKey(key: Object, value: Object, expiresIn: Number) - -写入 user_key - - -### removeUserKey(key: Object) - -删除 user_key - - -### key 属性 - -|参数 |类型 |必填 |描述 | -|:-: |:-: |:-: |:-: | -|dcloudAppid|String |是 |DCloud应用appid。[详情](https://ask.dcloud.net.cn/article/35907) | -|platform |String |是 |[详情](#platform) | -|openid |String |是 | | -|fallback |Function |否 |[详情](#fallback) | - -### value 属性 - -|参数 |类型 |描述 | -|:-: |:-: |:-: | -|session_key|String |微信小程序会话密钥 | - -### expiresIn - -有效时间(秒) - - -### 示例代码 - -```js -'use strict'; - -const { - getUserKey, - setUserKey, - removeUserKey, -} = require('uni-open-bridge-common') - -exports.main = async (event, context) => { - const key = { - dcloudAppid: '', - platform: '', - openid: '' - } - const value = { - 'session_key': '' - } - const expiresIn = 7200 - - // 写入 (redis / 数据库) - await setUserKey(key, value, expiresIn) - - // 读取 (redis / 数据库) - let result1 = await getUserKey(key) - - // 删除 - await removeUserKey(key) - - - // 删除后读取, 返回 null - let result2 = await getUserKey(key) - console.log(result2) // null - - return null -}; -``` - - -## encrypt_key - -为了避免小程序与开发者后台通信时数据被截取和篡改,微信侧维护了一个用户维度的可靠key,用于小程序和后台通信时进行加密和签名。[详情](https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/user-encryptkey.html) - -开发者可以分别通过小程序前端和微信后台提供的接口,获取用户的加密 key。 - - -### getEncryptKey(key: Object) - -读取 encrypt_key - - -### setEncryptKey(key: Object, value: Object, expiresIn: Number) - -写入 encrypt_key - - -### removeEncryptKey(key: Object) - -删除 encrypt_key - - -### key 属性 - -|参数 |类型 |必填 |描述 | -|:-: |:-: |:-: |:-: | -|dcloudAppid|String |是 |DCloud应用appid。[详情](https://ask.dcloud.net.cn/article/35907) | -|platform |String |是 |[详情](#platform) | -|openid |String |是 | | -|version |Number |是 |版本 | -|fallback |Function |否 |[详情](#fallback) | - - -### value 属性 - -|参数 |类型 |描述 | -|:-: |:-: |:-: | -|encrypt_key|String |加密 key | -|iv |String |加密 iv | - -### expiresIn - -有效时间(秒) - - -### 示例代码 - -```js -'use strict'; - -const { - getEncryptKey, - setEncryptKey, - removeEncryptKey -} = require('uni-open-bridge-common') - -exports.main = async (event, context) => { - const key = { - dcloudAppid: '', - platform: '', - openid: '', - version: 1 - } - const value = { - encrypt_key: '', - iv: '' - } - const expiresIn = 7200 - - // 写入 (redis / 数据库) - await setEncryptKey(key, value, expiresIn) - - // 读取 (redis / 数据库) - let result1 = await getEncryptKey(key) - - // 删除 - await removeEncryptKey(key) - - // 删除后读取, 返回 null - let result2 = await getEncryptKey(key) - console.log(result2) // null - - return null -}; -``` - - -## ticket - -`ticket` 是公众号用于调用微信 JS 接口的临时票据。[详情](https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#62) - - -### getTicket(key: Object) - -读取 ticket - - -### setTicket(key: Object, value: Object, expiresIn: Number) - -写入 ticket - - -### removeTicket(key: Object) - -删除 ticket - - -### key 属性 - -|参数 |类型 |必填 |描述 | -|:-: |:-: |:-: |:-: | -|dcloudAppid|String |是 |DCloud应用appid。[详情](https://ask.dcloud.net.cn/article/35907) | -|platform |String |是 |[详情](#platform) | -|fallback |Function |否 |[详情](#fallback) | - -### value 属性 - -|参数 |类型 |描述 | -|:-: |:-: |:-: | -|ticket |String | | - -### expiresIn - -有效时间(秒) - - -### 示例代码 - -```js -'use strict'; - -const { - getTicket, - setTicket, - removeTicket -} = require('uni-open-bridge-common') - -exports.main = async (event, context) => { - const key = { - dcloudAppid: '', - platform: '' - } - const value = { - ticket: '' - } - const expiresIn = 7200 - - // 写入 (redis / 数据库) - await setTicket(key, value, expiresIn) - - // 读取 (redis / 数据库) - let result1 = await getTicket(key) - - // 删除 - await removeTicket(key) - - - // 删除后读取, 返回 null - let result2 = await getTicket(key) - console.log(result2) // null - - return null -}; -``` - - -## Platform@platform - -平台对应的值 - -|值 |描述 | -|:-: |:-: | -|mp-weixin |微信小程序 | -|app-weixin |微信 App | -|h5-weixin |微信公众号 | -|web-weixin |微信pc网页 | -|mp-qq |QQ 小程序 | -|app-qq |QQ App | - - -## fallback@fallback - -可选 `async function fallback()`,当 `reids -> database` 都找不到对应 `key` 时,调用此方法,需要返回数据格式如下 - -```json -{ - value: null, - duration: 1 -} -``` - - - -注意事项 - -- 所有方法类型为 `async`,需要使用 `await` -- 所有方法校验 `key` 属性是否有效,无效则 `throw new Error()`,对 `value` 仅校验是否为 `undefined` +文档链接 [https://uniapp.dcloud.net.cn/uniCloud/uni-open-bridge](https://uniapp.dcloud.net.cn/uniCloud/uni-open-bridge) diff --git a/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/common/uni-open-bridge-common/config.js b/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/common/uni-open-bridge-common/config.js index 0e93113..89e81d4 100644 --- a/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/common/uni-open-bridge-common/config.js +++ b/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/common/uni-open-bridge-common/config.js @@ -52,15 +52,7 @@ class AppConfig extends ConfigBase { return null } - const oauthConfig = this.getOauthConfig(appConfig, platform) - if (!oauthConfig) { - return null - } - - return { - appid: oauthConfig.appid, - secret: oauthConfig.secret - } + return this.getOauthConfig(appConfig, platform) } isSupport(platformName) { diff --git a/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/common/uni-open-bridge-common/index.js b/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/common/uni-open-bridge-common/index.js index 2ad145c..2c42593 100644 --- a/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/common/uni-open-bridge-common/index.js +++ b/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/common/uni-open-bridge-common/index.js @@ -31,7 +31,8 @@ class AccessToken extends Storage { async fallback(parameters) { const oauthConfig = appConfig.get(parameters.dcloudAppid, parameters.platform) - const methodName = (parameters.platform === PlatformType.MP_WEIXIN) ? 'GetMPAccessTokenData' : 'GetH5AccessTokenData' + const methodName = (parameters.platform === PlatformType.MP_WEIXIN) ? 'GetMPAccessTokenData' : + 'GetH5AccessTokenData' const responseData = await WeixinServer[methodName](oauthConfig) const duration = responseData.expires_in @@ -44,10 +45,17 @@ class AccessToken extends Storage { } } -class UserKey extends Storage { +class UserAccessToken extends Storage { constructor() { - super('user-key', ['dcloudAppid', 'platform', 'openid']) + super('user-access-token', ['dcloudAppid', 'platform', 'openid']) + } +} + +class SessionKey extends Storage { + + constructor() { + super('session-key', ['dcloudAppid', 'platform', 'openid']) } } @@ -70,7 +78,7 @@ class Encryptkey extends Storage { async fallback(parameters) { const accessToken = await Factory.Get(AccessToken, parameters) - const userSession = await Factory.Get(UserKey, parameters) + const userSession = await Factory.Get(SessionKey, parameters) const responseData = await WeixinServer.GetUserEncryptKeyData({ openid: parameters.openid, @@ -133,16 +141,28 @@ async function removeAccessToken(key) { await Factory.Remove(AccessToken, key) } -async function getUserKey(key, fallback) { - return await Factory.Get(UserKey, key, fallback) +async function getUserAccessToken(key, fallback) { + return await Factory.Get(UserAccessToken, key, fallback) +} + +async function setUserAccessToken(key, value, expiresIn) { + await Factory.Set(UserAccessToken, key, value, expiresIn) +} + +async function removeUserAccessToken(key) { + await Factory.Remove(UserAccessToken, key) +} + +async function getSessionKey(key, fallback) { + return await Factory.Get(SessionKey, key, fallback) } -async function setUserKey(key, value, expiresIn) { - await Factory.Set(UserKey, key, value, expiresIn) +async function setSessionKey(key, value, expiresIn) { + await Factory.Set(SessionKey, key, value, expiresIn) } -async function removeUserKey(key) { - await Factory.Remove(UserKey, key) +async function removeSessionKey(key) { + await Factory.Remove(SessionKey, key) } async function getEncryptKey(key, fallback) { @@ -173,9 +193,12 @@ module.exports = { getAccessToken, setAccessToken, removeAccessToken, - getUserKey, - setUserKey, - removeUserKey, + getUserAccessToken, + setUserAccessToken, + removeUserAccessToken, + getSessionKey, + setSessionKey, + removeSessionKey, getEncryptKey, setEncryptKey, removeEncryptKey, diff --git a/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/common/uni-open-bridge-common/validator.js b/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/common/uni-open-bridge-common/validator.js index 1d0c9ca..47a455b 100644 --- a/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/common/uni-open-bridge-common/validator.js +++ b/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/common/uni-open-bridge-common/validator.js @@ -16,6 +16,9 @@ const Validator = { if (value === undefined) { Validator.ThrowNewError('Invalid Value') } + if (typeof value !== 'object') { + Validator.ThrowNewError('Invalid Value Type') + } }, ThrowNewError(message) { diff --git a/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/bridge.js b/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/bridge.js new file mode 100644 index 0000000..b8897ab --- /dev/null +++ b/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/bridge.js @@ -0,0 +1,126 @@ +'use strict'; + +const { + getAccessToken, + setAccessToken, + removeAccessToken, + getUserAccessToken, + setUserAccessToken, + removeUserAccessToken, + getSessionKey, + setSessionKey, + removeSessionKey, + getEncryptKey, + setEncryptKey, + removeEncryptKey, + getTicket, + setTicket, + removeTicket +} = require('uni-open-bridge-common') + +const { + Command +} = require('./basic.js'); + +const { + OpenBridgeConfig +} = require('./config.js') + +const openBridgeConfig = new OpenBridgeConfig() + +const Commands = [ + 'getAccessToken', + 'setAccessToken', + 'removeAccessToken', + 'getUserAccessToken', + 'setUserAccessToken', + 'removeUserAccessToken', + 'getSessionKey', + 'setSessionKey', + 'removeSessionKey', + 'getEncryptKey', + 'setEncryptKey', + 'removeEncryptKey', + 'getTicket', + 'setTicket', + 'removeTicket' +] + +class MainFrame extends Command { + + constructor() { + super() + + Commands.forEach((name) => { + this.register(name, this[name].bind(this)) + }) + } + + async getAccessToken(parameters) { + return await getAccessToken(parameters) + } + + async setAccessToken(parameters) { + return await setAccessToken(parameters, parameters.value, parameters.expiresIn) + } + + async removeAccessToken(parameters) { + return await removeAccessToken(parameters) + } + + async getUserAccessToken(parameters) { + return await getUserAccessToken(parameters) + } + + async setUserAccessToken(parameters) { + return await setUserAccessToken(parameters, parameters.value, parameters.expiresIn) + } + + async removeUserAccessToken(parameters) { + return await removeUserAccessToken(parameters) + } + + async getSessionKey(parameters) { + return await getSessionKey(parameters, parameters.fallback || null) + } + + async setSessionKey(parameters) { + return await setSessionKey(parameters, parameters.value, parameters.expiresIn) + } + + async removeSessionKey(parameters) { + return await removeSessionKey(parameters) + } + + async getEncryptKey(parameters) { + return await getEncryptKey(parameters, null) + } + + async setEncryptKey(parameters) { + return await setEncryptKey(parameters, parameters.value, parameters.expiresIn) + } + + async removeEncryptKey(parameters) { + return await removeEncryptKey(parameters) + } + + async getTicket(parameters) { + return await getTicket(parameters, null) + } + + async setTicket(parameters) { + return await setTicket(parameters, parameters.value, parameters.expiresIn) + } + + async removeTicket(parameters) { + return await removeTicket(parameters) + } + + checkIP(ip) { + return openBridgeConfig.inWhiteList(ip) + } +} + +const mainFrame = new MainFrame(); + +module.exports = mainFrame; diff --git a/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/config.js b/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/config.js index 83adffa..bc7c850 100644 --- a/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/config.js +++ b/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/config.js @@ -73,16 +73,16 @@ class ConfigBase { return null } - inWhitelist(ip) { - return (this.ipWhitelist.indexOf(ip) > -1) + inWhiteList(ip) { + return (this.ipWhiteList.indexOf(ip) > -1) } get openBridge() { return this._openBridge } - get ipWhitelist() { - return this._openBridge.ipWhitelist + get ipWhiteList() { + return this._openBridge.ipWhiteList } get ready() { diff --git a/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/index.mp-weixin.js b/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/index.mp-weixin.js deleted file mode 100644 index 25556ef..0000000 --- a/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/index.mp-weixin.js +++ /dev/null @@ -1,93 +0,0 @@ -'use strict'; - -const { - getAccessToken, - setAccessToken, - removeAccessToken, - getUserKey, - setUserKey, - removeUserKey, - getEncryptKey, - setEncryptKey, - removeEncryptKey, - getTicket, - setTicket, - removeTicket -} = require('uni-open-bridge-common') - -const { - Command -} = require('./basic.js'); - -const { - OpenBridgeConfig -} = require('./config.js') - -const openBridgeConfig = new OpenBridgeConfig() - -class MainFrame extends Command { - - constructor() { - super() - - MainFrame.Commands.forEach((name) => { - this.register(name, this[name].bind(this)) - }) - } - - async getAccessToken(parameters) { - return await getAccessToken(parameters) - } - - async setAccessToken(parameters) { - return await setAccessToken(parameters, parameters.value, parameters.expiresIn) - } - - async removeAccessToken(parameters) { - return await removeAccessToken(parameters) - } - - async getUserKey(parameters) { - return await getUserKey(parameters, null) - } - - async setUserKey(parameters) { - return await setUserKey(parameters, parameters.value, parameters.expiresIn) - } - - async removeUserKey(parameters) { - return await removeUserKey(parameters) - } - - async getTicket(parameters) { - return await getTicket(parameters, null) - } - - async setTicket(parameters) { - return await setTicket(parameters, parameters.value, parameters.expiresIn) - } - - async removeTicket(parameters) { - return await removeTicket(parameters) - } - - checkIP(ip) { - return openBridgeConfig.inWhitelist(ip) - } -} - -MainFrame.Commands = [ - 'getAccessToken', - 'setAccessToken', - 'removeAccessToken', - 'getUserKey', - 'setUserKey', - 'removeUserKey', - 'getTicket', - 'setTicket', - 'removeTicket' -]; - -const commands = new MainFrame(); - -module.exports = commands; diff --git a/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/index.obj.js b/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/index.obj.js index 71d6ae9..a34b737 100644 --- a/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/index.obj.js +++ b/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/index.obj.js @@ -1,21 +1,33 @@ 'use strict'; -const { - PlatformType -} = require('./consts.js') - const runTask = require('./index.task.js') -const weixinCommand = require('./index.mp-weixin.js') +const command = require('./bridge.js') async function executeCommand() { const methodName = this.getMethodName() - const parameters = JSON.parse(this.getHttpInfo().body) + const clientInfo = this.getClientInfo() - if (parameters.platform === PlatformType.MP_WEIXIN) { - return await weixinCommand.execute(methodName, parameters) + let parameters + if (clientInfo.source === 'http') { + const postData = this.getHttpInfo().body + if (!postData || postData.length < 4) { + throw new Error('Invalid parameter(s)::' + postData) + } + parameters = JSON.parse(postData) + } else if (clientInfo.source === 'function') { + const args = this.getParams() + parameters = args[0] + if (args.length === 2) { + parameters.value = args[1] + } + if (args.length === 3) { + parameters.expiresIn = args[2] + } + } else { + throw new Error('Invalid') } - throw new Error('Invalid Platform') + return await command.execute(methodName, parameters) } module.exports = { @@ -25,13 +37,11 @@ module.exports = { }, async _before() { const clientInfo = this.getClientInfo() - if (!weixinCommand.checkIP(clientInfo.clientIP)) { - throw new Error("Invalid IP:" + clientInfo.clientIP) + if (clientInfo.source === 'http' && !command.checkIP(clientInfo.clientIP)) { + throw new Error('Invalid IP::' + clientInfo.clientIP) } }, - // async runTask() { - // await runTask() - // }, + /// AccessToken async getAccessToken() { return await executeCommand.call(this) }, @@ -41,15 +51,37 @@ module.exports = { async removeAccessToken() { return await executeCommand.call(this) }, - async getUserKey() { + /// UserAccessToken + async getUserAccessToken() { + return await executeCommand.call(this) + }, + async setUserAccessToken() { + return await executeCommand.call(this) + }, + async removeUserAccessToken() { + return await executeCommand.call(this) + }, + /// SessionKey + async getSessionKey() { + return await executeCommand.call(this) + }, + async setSessionKey() { + return await executeCommand.call(this) + }, + async removeSessionKey() { + return await executeCommand.call(this) + }, + /// EncryptKey + async getEncryptKey() { return await executeCommand.call(this) }, - async setUserKey() { + async setEncryptKey() { return await executeCommand.call(this) }, - async removeUserKey() { + async removeEncryptKey() { return await executeCommand.call(this) }, + /// Ticket async getTicket() { return await executeCommand.call(this) }, diff --git a/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/package.json b/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/package.json index 7aad3d9..b3fe006 100644 --- a/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/package.json +++ b/uni_modules/uni-open-bridge/uniCloud/cloudfunctions/uni-open-bridge/package.json @@ -4,7 +4,19 @@ "uni-open-bridge-common": "file:../common/uni-open-bridge-common", "uni-config-center": "file:../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center" }, + "cloudfunction-config": { + "memorySize": 256, + "timeout": 60, + "triggers": [{ + "name": "uni-open-bridge", + "type": "timer", + "config": "0 0 * * * * *" + }], + "path": "", + "runtime": "Nodejs8" + }, "extensions": { - "uni-cloud-jql": {} + "uni-cloud-jql": {}, + "uni-cloud-redis": {} } -} \ No newline at end of file +} -- GitLab