提交 4ed51ea9 编写于 作者: DCloud_JSON's avatar DCloud_JSON
module.exports = {
beforeRegister: function({
userRecord,
clientInfo
} = {}){
console.log(clientInfo);
userRecord.role = ['test-role']
return userRecord
}
}
\ No newline at end of file
{
"name": "uni-id-co",
"version": "1.0.1",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "uni-id-co",
"version": "1.0.1",
"dependencies": {
"jsonwebtoken": "8.5.1",
"lodash.merge": "^4.6.2",
"uni-captcha": "file:../../../../uni-captcha/uniCloud/cloudfunctions/common/uni-captcha",
"uni-config-center": "file:../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center",
"uni-id-common": "file:../../../../uni-id-common/uniCloud/cloudfunctions/common/uni-id-common",
"uni-open-bridge-common": "file:../../../../uni-open-bridge/uniCloud/cloudfunctions/common/uni-open-bridge-common"
}
},
"../../../../uni-captcha/uniCloud/cloudfunctions/common/uni-captcha": {
"version": "0.2.2",
"license": "Apache-2.0",
"dependencies": {
"uni-config-center": "file:../../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center"
}
},
"../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center": {
"version": "0.0.2",
"license": "Apache-2.0"
},
"../../../../uni-id-common/uniCloud/cloudfunctions/common/uni-id-common": {
"version": "1.0.13",
"license": "Apache-2.0",
"dependencies": {
"uni-config-center": "file:../../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center"
}
},
"../../../../uni-id/uniCloud/cloudfunctions/common/uni-id": {
"version": "4.0.0",
"extraneous": true,
"license": "Apache-2.0",
"dependencies": {
"uni-config-center": "file:../../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center"
}
},
"../../../../uni-open-bridge/uniCloud/cloudfunctions/common/uni-open-bridge-common": {
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"uni-config-center": "file:../../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center"
}
},
"node_modules/buffer-equal-constant-time": {
"version": "1.0.1",
"license": "BSD-3-Clause"
},
"node_modules/ecdsa-sig-formatter": {
"version": "1.0.11",
"license": "Apache-2.0",
"dependencies": {
"safe-buffer": "^5.0.1"
}
},
"node_modules/jsonwebtoken": {
"version": "8.5.1",
"license": "MIT",
"dependencies": {
"jws": "^3.2.2",
"lodash.includes": "^4.3.0",
"lodash.isboolean": "^3.0.3",
"lodash.isinteger": "^4.0.4",
"lodash.isnumber": "^3.0.3",
"lodash.isplainobject": "^4.0.6",
"lodash.isstring": "^4.0.1",
"lodash.once": "^4.0.0",
"ms": "^2.1.1",
"semver": "^5.6.0"
},
"engines": {
"node": ">=4",
"npm": ">=1.4.28"
}
},
"node_modules/jwa": {
"version": "1.4.1",
"license": "MIT",
"dependencies": {
"buffer-equal-constant-time": "1.0.1",
"ecdsa-sig-formatter": "1.0.11",
"safe-buffer": "^5.0.1"
}
},
"node_modules/jws": {
"version": "3.2.2",
"license": "MIT",
"dependencies": {
"jwa": "^1.4.1",
"safe-buffer": "^5.0.1"
}
},
"node_modules/lodash.includes": {
"version": "4.3.0",
"license": "MIT"
},
"node_modules/lodash.isboolean": {
"version": "3.0.3",
"license": "MIT"
},
"node_modules/lodash.isinteger": {
"version": "4.0.4",
"license": "MIT"
},
"node_modules/lodash.isnumber": {
"version": "3.0.3",
"license": "MIT"
},
"node_modules/lodash.isplainobject": {
"version": "4.0.6",
"license": "MIT"
},
"node_modules/lodash.isstring": {
"version": "4.0.1",
"license": "MIT"
},
"node_modules/lodash.merge": {
"version": "4.6.2",
"resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz",
"integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ=="
},
"node_modules/lodash.once": {
"version": "4.1.1",
"license": "MIT"
},
"node_modules/ms": {
"version": "2.1.3",
"license": "MIT"
},
"node_modules/safe-buffer": {
"version": "5.2.1",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT"
},
"node_modules/semver": {
"version": "5.7.1",
"license": "ISC",
"bin": {
"semver": "bin/semver"
}
},
"node_modules/uni-captcha": {
"resolved": "../../../../uni-captcha/uniCloud/cloudfunctions/common/uni-captcha",
"link": true
},
"node_modules/uni-config-center": {
"resolved": "../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center",
"link": true
},
"node_modules/uni-id-common": {
"resolved": "../../../../uni-id-common/uniCloud/cloudfunctions/common/uni-id-common",
"link": true
},
"node_modules/uni-open-bridge-common": {
"resolved": "../../../../uni-open-bridge/uniCloud/cloudfunctions/common/uni-open-bridge-common",
"link": true
}
},
"dependencies": {
"buffer-equal-constant-time": {
"version": "1.0.1"
},
"ecdsa-sig-formatter": {
"version": "1.0.11",
"requires": {
"safe-buffer": "^5.0.1"
}
},
"jsonwebtoken": {
"version": "8.5.1",
"requires": {
"jws": "^3.2.2",
"lodash.includes": "^4.3.0",
"lodash.isboolean": "^3.0.3",
"lodash.isinteger": "^4.0.4",
"lodash.isnumber": "^3.0.3",
"lodash.isplainobject": "^4.0.6",
"lodash.isstring": "^4.0.1",
"lodash.once": "^4.0.0",
"ms": "^2.1.1",
"semver": "^5.6.0"
}
},
"jwa": {
"version": "1.4.1",
"requires": {
"buffer-equal-constant-time": "1.0.1",
"ecdsa-sig-formatter": "1.0.11",
"safe-buffer": "^5.0.1"
}
},
"jws": {
"version": "3.2.2",
"requires": {
"jwa": "^1.4.1",
"safe-buffer": "^5.0.1"
}
},
"lodash.includes": {
"version": "4.3.0"
},
"lodash.isboolean": {
"version": "3.0.3"
},
"lodash.isinteger": {
"version": "4.0.4"
},
"lodash.isnumber": {
"version": "3.0.3"
},
"lodash.isplainobject": {
"version": "4.0.6"
},
"lodash.isstring": {
"version": "4.0.1"
},
"lodash.merge": {
"version": "4.6.2",
"resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz",
"integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ=="
},
"lodash.once": {
"version": "4.1.1"
},
"ms": {
"version": "2.1.3"
},
"safe-buffer": {
"version": "5.2.1"
},
"semver": {
"version": "5.7.1"
},
"uni-captcha": {
"version": "file:../../../../uni-captcha/uniCloud/cloudfunctions/common/uni-captcha",
"requires": {
"uni-config-center": "file:../../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center"
}
},
"uni-config-center": {
"version": "file:../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center"
},
"uni-id-common": {
"version": "file:../../../../uni-id-common/uniCloud/cloudfunctions/common/uni-id-common",
"requires": {
"uni-config-center": "file:../../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center"
}
},
"uni-open-bridge-common": {
"version": "file:../../../../uni-open-bridge/uniCloud/cloudfunctions/common/uni-open-bridge-common",
"requires": {
"uni-config-center": "file:../../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center"
}
}
}
"name": "uni-id-co",
"version": "1.0.9",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "uni-id-co",
"version": "1.0.9",
"dependencies": {
"jsonwebtoken": "8.5.1",
"lodash.merge": "^4.6.2",
"uni-captcha": "file:../../../../uni-captcha/uniCloud/cloudfunctions/common/uni-captcha",
"uni-config-center": "file:../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center",
"uni-id-common": "file:../../../../uni-id-common/uniCloud/cloudfunctions/common/uni-id-common",
"uni-open-bridge-common": "file:../../../../uni-open-bridge-common/uniCloud/cloudfunctions/common/uni-open-bridge-common"
}
},
"../../../../uni-captcha/uniCloud/cloudfunctions/common/uni-captcha": {
"version": "0.2.2",
"license": "Apache-2.0",
"dependencies": {
"uni-config-center": "file:../../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center"
}
},
"../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center": {
"version": "0.0.2",
"license": "Apache-2.0"
},
"../../../../uni-id-common/uniCloud/cloudfunctions/common/uni-id-common": {
"version": "1.0.13",
"license": "Apache-2.0",
"dependencies": {
"uni-config-center": "file:../../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center"
}
},
"../../../../uni-open-bridge-common/uniCloud/cloudfunctions/common/uni-open-bridge-common": {
"version": "1.0.0",
"license": "ISC",
"dependencies": {
"uni-config-center": "file:../../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center"
}
},
"node_modules/buffer-equal-constant-time": {
"version": "1.0.1",
"license": "BSD-3-Clause"
},
"node_modules/ecdsa-sig-formatter": {
"version": "1.0.11",
"license": "Apache-2.0",
"dependencies": {
"safe-buffer": "^5.0.1"
}
},
"node_modules/jsonwebtoken": {
"version": "8.5.1",
"license": "MIT",
"dependencies": {
"jws": "^3.2.2",
"lodash.includes": "^4.3.0",
"lodash.isboolean": "^3.0.3",
"lodash.isinteger": "^4.0.4",
"lodash.isnumber": "^3.0.3",
"lodash.isplainobject": "^4.0.6",
"lodash.isstring": "^4.0.1",
"lodash.once": "^4.0.0",
"ms": "^2.1.1",
"semver": "^5.6.0"
},
"engines": {
"node": ">=4",
"npm": ">=1.4.28"
}
},
"node_modules/jwa": {
"version": "1.4.1",
"license": "MIT",
"dependencies": {
"buffer-equal-constant-time": "1.0.1",
"ecdsa-sig-formatter": "1.0.11",
"safe-buffer": "^5.0.1"
}
},
"node_modules/jws": {
"version": "3.2.2",
"license": "MIT",
"dependencies": {
"jwa": "^1.4.1",
"safe-buffer": "^5.0.1"
}
},
"node_modules/lodash.includes": {
"version": "4.3.0",
"license": "MIT"
},
"node_modules/lodash.isboolean": {
"version": "3.0.3",
"license": "MIT"
},
"node_modules/lodash.isinteger": {
"version": "4.0.4",
"license": "MIT"
},
"node_modules/lodash.isnumber": {
"version": "3.0.3",
"license": "MIT"
},
"node_modules/lodash.isplainobject": {
"version": "4.0.6",
"license": "MIT"
},
"node_modules/lodash.isstring": {
"version": "4.0.1",
"license": "MIT"
},
"node_modules/lodash.merge": {
"version": "4.6.2",
"license": "MIT"
},
"node_modules/lodash.once": {
"version": "4.1.1",
"license": "MIT"
},
"node_modules/ms": {
"version": "2.1.3",
"license": "MIT"
},
"node_modules/safe-buffer": {
"version": "5.2.1",
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
],
"license": "MIT"
},
"node_modules/semver": {
"version": "5.7.1",
"license": "ISC",
"bin": {
"semver": "bin/semver"
}
},
"node_modules/uni-captcha": {
"resolved": "../../../../uni-captcha/uniCloud/cloudfunctions/common/uni-captcha",
"link": true
},
"node_modules/uni-config-center": {
"resolved": "../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center",
"link": true
},
"node_modules/uni-id-common": {
"resolved": "../../../../uni-id-common/uniCloud/cloudfunctions/common/uni-id-common",
"link": true
},
"node_modules/uni-open-bridge-common": {
"resolved": "../../../../uni-open-bridge-common/uniCloud/cloudfunctions/common/uni-open-bridge-common",
"link": true
}
},
"dependencies": {
"buffer-equal-constant-time": {
"version": "1.0.1"
},
"ecdsa-sig-formatter": {
"version": "1.0.11",
"requires": {
"safe-buffer": "^5.0.1"
}
},
"jsonwebtoken": {
"version": "8.5.1",
"requires": {
"jws": "^3.2.2",
"lodash.includes": "^4.3.0",
"lodash.isboolean": "^3.0.3",
"lodash.isinteger": "^4.0.4",
"lodash.isnumber": "^3.0.3",
"lodash.isplainobject": "^4.0.6",
"lodash.isstring": "^4.0.1",
"lodash.once": "^4.0.0",
"ms": "^2.1.1",
"semver": "^5.6.0"
}
},
"jwa": {
"version": "1.4.1",
"requires": {
"buffer-equal-constant-time": "1.0.1",
"ecdsa-sig-formatter": "1.0.11",
"safe-buffer": "^5.0.1"
}
},
"jws": {
"version": "3.2.2",
"requires": {
"jwa": "^1.4.1",
"safe-buffer": "^5.0.1"
}
},
"lodash.includes": {
"version": "4.3.0"
},
"lodash.isboolean": {
"version": "3.0.3"
},
"lodash.isinteger": {
"version": "4.0.4"
},
"lodash.isnumber": {
"version": "3.0.3"
},
"lodash.isplainobject": {
"version": "4.0.6"
},
"lodash.isstring": {
"version": "4.0.1"
},
"lodash.merge": {
"version": "4.6.2"
},
"lodash.once": {
"version": "4.1.1"
},
"ms": {
"version": "2.1.3"
},
"safe-buffer": {
"version": "5.2.1"
},
"semver": {
"version": "5.7.1"
},
"uni-captcha": {
"version": "file:../../../../uni-captcha/uniCloud/cloudfunctions/common/uni-captcha",
"requires": {
"uni-config-center": "file:../../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center"
}
},
"uni-config-center": {
"version": "file:../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center"
},
"uni-id-common": {
"version": "file:../../../../uni-id-common/uniCloud/cloudfunctions/common/uni-id-common",
"requires": {
"uni-config-center": "file:../../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center"
}
},
"uni-open-bridge-common": {
"version": "file:../../../../uni-open-bridge-common/uniCloud/cloudfunctions/common/uni-open-bridge-common",
"requires": {
"uni-config-center": "file:../../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center"
}
}
}
}
{
"name": "uni-id-co",
"version": "1.0.8",
"version": "1.0.9",
"description": "",
"main": "index.js",
"keywords": [],
......@@ -10,7 +10,8 @@
"lodash.merge": "^4.6.2",
"uni-captcha": "file:../../../../uni-captcha/uniCloud/cloudfunctions/common/uni-captcha",
"uni-config-center": "file:../../../../uni-config-center/uniCloud/cloudfunctions/common/uni-config-center",
"uni-id-common": "file:../../../../uni-id-common/uniCloud/cloudfunctions/common/uni-id-common"
"uni-id-common": "file:../../../../uni-id-common/uniCloud/cloudfunctions/common/uni-id-common",
"uni-open-bridge-common": "file:../../../../uni-open-bridge-common/uniCloud/cloudfunctions/common/uni-open-bridge-common"
},
"extensions": {}
}
\ No newline at end of file
const clientInfo = {
"PLATFORM": "web",
"OS": "ios",
"APPID": "__UNI__ECAF623",
"DEVICEID": "16558192370391366615",
"scene": 1001,
"appId": "__UNI__ECAF623",
"appLanguage": "zh-Hans",
"appName": "uni-id-pages",
"appVersion": "1.0.0",
"appVersionCode": "100",
"browserName": "safari",
"browserVersion": "13.0.3",
"deviceId": "16558192370391366615",
"deviceModel": "iPhone",
"deviceOrientation": "portrait",
"devicePixelRatio": 2.0000000298023224,
"deviceType": "phone",
"hostLanguage": "en-US",
"hostName": "safari",
"hostVersion": "13.0.3",
"osName": "ios",
"osVersion": "13.2.3",
"safeArea": {
"left": 0,
"right": 375,
"top": 0,
"bottom": 667,
"width": 375,
"height": 667
},
"safeAreaInsets": {
"top": 0,
"right": 0,
"bottom": 0,
"left": 0
},
"screenHeight": 667,
"screenWidth": 375,
"statusBarHeight": 0,
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1",
"uniCompileVersion": "3.5.4",
"uniPlatform": "web",
"uniRuntimeVersion": "3.5.4",
"windowBottom": 0,
"windowHeight": 667,
"windowTop": 0,
"windowWidth": 375,
"locale": "zh-Hans",
"LOCALE": "zh-Hans"
}
registerUser({
"username": "qqq2",
"nickname": "",
"password": "qqq11122",
"password2": "qqq11122",
"captcha": "3IPP"
})
\ No newline at end of file
{
"id": "uni-open-bridge",
"displayName": "uni-open-bridge",
"id": "uni-open-bridge-common",
"displayName": "uni-open-bridge-common",
"version": "1.0.0",
"description": "uni-open-bridge 是统一接管微信等三方平台认证的开源库",
"description": "统一接管微信等三方平台认证凭据",
"keywords": [
"uni-open-bridge-common",
"access_token",
"session_key",
"ticket",
"微信"
"ticket"
],
"repository": "",
"engines": {
"HBuilderX": "^3.5.1"
"HBuilderX": "^3.5.2"
},
"dcloudext": {
"category": [
"uniCloud",
"云函数模板"
],
"type": "unicloud-template-function",
"sale": {
"regular": {
"price": "0.00"
......
# uni-open-bridge-common
`uni-open-bridge-common` 是统一接管微信等三方平台认证凭据(包括但不限于`access_token``session_key``encrypt_key``ticket`)的开源库。
文档链接 [https://uniapp.dcloud.net.cn/uniCloud/uni-open-bridge#common](https://uniapp.dcloud.net.cn/uniCloud/uni-open-bridge#common)
# uni-open-bridge
`uni-open-bridge` 是统一接管微信等三方平台认证凭据(包括但不限于`access_token``session_key``encrypt_key``ticket`)的开源库。
文档链接 [https://uniapp.dcloud.net.cn/uniCloud/uni-open-bridge](https://uniapp.dcloud.net.cn/uniCloud/uni-open-bridge)
'use strict';
class Command {
constructor() {
this._registered = {}
}
async execute(name, args) {
if (this._registered[name]) {
return await this._registered[name].execute(args)
}
}
canExecute(name, args) {
if (this._registered[name] && this._registered[name].canExecute) {
this._registered[name].canExecute(args)
}
return false
}
register(name, execute, canExecute) {
this._registered[name] = {
execute,
canExecute
}
}
}
class Task {
constructor(id) {
this._id = id || this._newTaskId()
this._state = Task.TASK_STATE.WAITING
}
async run() {}
async cancel() {}
async taskAction() {}
_newTaskId() {
let guid = ''
const format = 'xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx'
for (let i = 0; i < format.length; i++) {
if (format[i] === 'x') {
guid += (Math.random() * 16 | 0).toString(16)
} else {
guid += format[i]
}
}
return guid.toUpperCase()
}
get id() {
return this._id
}
set id(value) {
this._id = value
}
get state() {
return this._state
}
set state(value) {
this._state = value
}
}
Task.TASK_STATE = {
WAITING: "WAITING",
RESOLVING: "RESOLVING",
RESOLVED: "RESOLVED",
EXECUTING: "EXECUTING",
ERROR: "ERROR",
COMPLETED: "COMPLETED",
CANCELLING: "CANCELLING",
CANCELLED: "CANCELLED"
}
class TaskManager {
constructor() {
this._tasks = []
}
get tasks() {
return this._tasks
}
clear() {
this._tasks.length = 0
}
getTask(id) {
return this._tasks.find((item) => {
return (item.id == id)
})
}
addTask(task) {
this._tasks.push(task)
}
deleteTask(id) {
const index = this.findIndex(id)
if (index < 0) {
return
}
this._tasks[index].cancel()
if (index > -1) {
this._tasks.splice(index, 1)
}
}
findIndex(id) {
return this._tasks.findIndex((item) => {
return (item.id == id)
})
}
}
module.exports = {
Command,
Task,
TaskManager
};
'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;
'use strict';
const configCenter = require('uni-config-center')
class TaskConfig {
constructor(options) {
this._dcloudAppid = options.dcloudAppid
this._appid = options.appid
this._secret = options.secret
this._platform = options.platform
this._tasks = options.tasks
this._timeout = 1000 * 10
}
get dcloudAppid() {
return this._dcloudAppid
}
get appid() {
return this._appid
}
get secret() {
return this._secret
}
get platform() {
return this._platform
}
get tasks() {
return this._tasks
}
}
class ConfigBase {
constructor() {
const uniIdConfig = configCenter({
pluginId: 'uni-id'
})
const openBridgeConfig = configCenter({
pluginId: 'uni-open-bridge'
})
this._uniId = uniIdConfig.config()
this._openBridge = openBridgeConfig.config()
this._ready = true
}
getAppConfig(appid) {
if (Array.isArray(this._uniId)) {
return this._uniId.find((item) => {
return (item.dcloudAppid === appid)
})
}
if (this._uniId.dcloudAppid === appid) {
return this._uniId
}
return null
}
inWhiteList(ip) {
return (this.ipWhiteList.indexOf(ip) > -1)
}
get openBridge() {
return this._openBridge
}
get ipWhiteList() {
return this._openBridge.ipWhiteList
}
get ready() {
return this._ready
}
}
class OpenBridgeConfig extends ConfigBase {
constructor() {
super()
this._tasks = []
this.resolve()
}
get tasks() {
return this._tasks
}
resolve() {
this._tasks.length = 0
const appids = Object.keys(this.openBridge.schedule)
for (let i = 0; i < appids.length; i++) {
const appid = appids[i]
let appConfig = this.getAppConfig(appid)
if (appConfig != null) {
const schedule = this.openBridge.schedule[appid]
if (schedule) {
this.resolveSchedule(appid, appConfig, schedule)
}
}
}
}
resolveSchedule(dcloudAppid, appConfig, schedule) {
if (schedule.enable !== true) {
return
}
const schedulePlatforms = Object.keys(schedule)
for (let i = 0; i < schedulePlatforms.length; i++) {
const platformName = schedulePlatforms[i]
const scheduleTask = schedule[platformName]
if (!scheduleTask.enable) {
continue
}
if (!this.isSupport(platformName)) {
continue
}
this._tasks.push({
dcloudAppid: dcloudAppid,
platform: platformName,
tasks: scheduleTask.tasks
})
}
}
isSupport(platformName) {
return (OpenBridgeConfig.Support_Platforms.indexOf(platformName) >= 0)
}
}
OpenBridgeConfig.Support_Platforms = ['weixin-mp', 'weixin-h5']
module.exports = {
OpenBridgeConfig
};
'use strict';
const runTask = require('./index.task.js')
const command = require('./bridge.js')
async function executeCommand() {
const methodName = this.getMethodName()
const clientInfo = this.getClientInfo()
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')
}
return await command.execute(methodName, parameters)
}
module.exports = {
async _timing() {
console.log('triggered by timing')
await runTask()
},
async _before() {
const clientInfo = this.getClientInfo()
if (clientInfo.source === 'http' && !command.checkIP(clientInfo.clientIP)) {
throw new Error('Invalid IP::' + clientInfo.clientIP)
}
},
/// AccessToken
async getAccessToken() {
return await executeCommand.call(this)
},
async setAccessToken() {
return await executeCommand.call(this)
},
async removeAccessToken() {
return await executeCommand.call(this)
},
/// 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 setEncryptKey() {
return await executeCommand.call(this)
},
async removeEncryptKey() {
return await executeCommand.call(this)
},
/// Ticket
async getTicket() {
return await executeCommand.call(this)
},
async setTicket() {
return await executeCommand.call(this)
},
async removeTicket() {
return await executeCommand.call(this)
}
}
'use strict';
const {
OpenBridgeConfig
} = require('./config.js')
const {
TaskManager
} = require('./basic.js')
const {
TaskAccessTokenMP,
TaskAccessTokenH5,
TaskTicket
} = require('./task-weixin.js')
const TaskMapping = {
'weixin-mp': {
'accessToken': TaskAccessTokenMP
},
'weixin-h5': {
'accessToken': TaskAccessTokenH5,
'ticket': TaskTicket
}
}
class ScheduleManager extends TaskManager {
constructor() {
super()
}
async runAll() {
for (let i = 0; i < this.tasks.length; i++) {
const task = this.tasks[i]
try {
await task.run()
} catch (e) {
console.log("task.run::", e)
}
}
}
newTask(T, config) {
const newTask = new T(config)
super.addTask(newTask)
return newTask
}
}
ScheduleManager.instance = function() {
if (!ScheduleManager._instance) {
ScheduleManager._instance = new ScheduleManager()
}
return ScheduleManager._instance
}
async function main() {
const openBridgeConfig = new OpenBridgeConfig()
ScheduleManager.instance().clear()
for (let taskConfig of openBridgeConfig.tasks) {
let {
platform,
tasks
} = taskConfig
for (let taskName of tasks) {
const platformTask = TaskMapping[platform]
if (platformTask) {
ScheduleManager.instance().newTask(platformTask[taskName], taskConfig)
}
}
}
await ScheduleManager.instance().runAll()
}
module.exports = main;
{
"name": "uni-open-bridge",
"dependencies": {
"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-redis": {}
}
}
'use strict';
const {
getAccessToken,
getTicket,
PlatformType
} = require('uni-open-bridge-common')
const {
Task
} = require('./basic.js')
class TaskAccessTokenMP extends Task {
constructor(config) {
super()
this._config = config || null
}
async run() {
const key = {
dcloudAppid: this._config.dcloudAppid,
platform: PlatformType.WEIXIN_MP
}
const result = await getAccessToken(key)
console.log("setAccessToken...", key, result)
}
}
TaskAccessTokenMP.ID = 'TaskAccessTokenMP'
class TaskAccessTokenH5 extends Task {
constructor(config) {
super()
this._config = config || null
}
async run() {
const key = {
dcloudAppid: this._config.dcloudAppid,
platform: PlatformType.WEIXIN_H5
}
const result = await getAccessToken(key)
console.log("setAccessToken...", key, result)
}
}
TaskAccessTokenH5.ID = 'TaskAccessTokenH5'
class TaskTicket extends Task {
constructor(config) {
super()
this._config = config || null
}
async run() {
const key = {
dcloudAppid: this._config.dcloudAppid,
platform: PlatformType.WEIXIN_H5
}
const result = await getTicket(key)
console.log("setTicket...", key, result)
}
}
TaskTicket.ID = 'TaskTicket'
module.exports = {
TaskAccessTokenMP,
TaskAccessTokenH5,
TaskTicket
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册