首次完整推送,

V:1.20240808.006
This commit is contained in:
fm453
2024-08-13 18:32:37 +08:00
parent 15be3e9373
commit c62d15b288
939 changed files with 111777 additions and 0 deletions

View File

@ -0,0 +1,131 @@
'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
};

View File

@ -0,0 +1,197 @@
'use strict';
const {
PlatformType
} = require('./consts.js')
const configCenter = require('uni-config-center')
const OauthConfig = {
'mp-weixin': ['oauth', 'weixin'],
'h5-weixin': ['oauth', 'weixin']
}
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
}
const oauthConfig = this.getOauthConfig(appConfig, platformName)
if (!oauthConfig) {
continue
}
this._tasks.push({
platform: platformName,
tasks: scheduleTask.tasks,
dcloudAppid: dcloudAppid,
appid: oauthConfig.appid,
secret: oauthConfig.secret
})
}
}
isSupport(platformName) {
return (OpenBridgeConfig.Support_Platforms.indexOf(platformName) >= 0)
}
getOauthConfig(appConfig, platformName) {
const platformConfig = appConfig[platformName]
if (!platformConfig) {
return null
}
let tree = OauthConfig[platformName]
let node = platformConfig
for (let i = 0; i < tree.length; i++) {
let nodeName = tree[i]
if (node[nodeName]) {
node = node[nodeName]
} else {
node = null
break
}
}
if (node && node.appid && node.appsecret) {
return {
appid: node.appid,
secret: node.appsecret
}
}
return null
}
}
OpenBridgeConfig.Support_Platforms = ['mp-weixin', 'h5-weixin']
module.exports = {
OpenBridgeConfig
};

View File

@ -0,0 +1,19 @@
'use strict';
const HTTP_STATUS = {
SUCCESS: 200
}
const PlatformType = {
MP_WEIXIN: 'mp-weixin',
H5_WEIXIN: 'h5-weixin',
APP_WEIXIN: 'app-weixin',
WEB_WEIXIN: 'web-weixin',
MP_QQ: 'mp-qq',
APP_QQ: 'app-qq'
}
module.exports = {
HTTP_STATUS,
PlatformType
}

View File

@ -0,0 +1,93 @@
'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;

View File

@ -0,0 +1,62 @@
'use strict';
const {
PlatformType
} = require('./consts.js')
const runTask = require('./index.task.js')
const weixinCommand = require('./index.mp-weixin.js')
async function executeCommand() {
const methodName = this.getMethodName()
const parameters = JSON.parse(this.getHttpInfo().body)
if (parameters.platform === PlatformType.MP_WEIXIN) {
return await weixinCommand.execute(methodName, parameters)
}
throw new Error('Invalid Platform')
}
module.exports = {
async _timing() {
console.log('triggered by timing')
await runTask()
},
async _before() {
const clientInfo = this.getClientInfo()
if (!weixinCommand.checkIP(clientInfo.clientIP)) {
throw new Error("Invalid IP:" + clientInfo.clientIP)
}
},
// async runTask() {
// await runTask()
// },
async getAccessToken() {
return await executeCommand.call(this)
},
async setAccessToken() {
return await executeCommand.call(this)
},
async removeAccessToken() {
return await executeCommand.call(this)
},
async getUserKey() {
return await executeCommand.call(this)
},
async setUserKey() {
return await executeCommand.call(this)
},
async removeUserKey() {
return await executeCommand.call(this)
},
async getTicket() {
return await executeCommand.call(this)
},
async setTicket() {
return await executeCommand.call(this)
},
async removeTicket() {
return await executeCommand.call(this)
}
}

View File

@ -0,0 +1,86 @@
'use strict';
const {
OpenBridgeConfig
} = require('./config.js')
const {
TaskManager
} = require('./basic.js')
const {
TaskAccessTokenMP
} = require('./task-mp-weixin.js')
const {
TaskAccessTokenH5,
TaskTicket
} = require('./task-h5-weixin.js')
const TaskMapping = {
'mp-weixin': {
'accessToken': TaskAccessTokenMP
},
'h5-weixin': {
'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;

View File

@ -0,0 +1,10 @@
{
"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"
},
"extensions": {
"uni-cloud-jql": {}
}
}

View File

@ -0,0 +1,67 @@
'use strict';
const {
getAccessToken,
setAccessToken,
removeAccessToken,
getTicket,
setTicket,
removeTicket
} = require('uni-open-bridge-common')
const {
Task
} = require('./basic.js')
const {
PlatformType
} = require('./consts.js')
class TaskAccessTokenH5 extends Task {
constructor(config) {
super()
this._config = config || null
}
async run() {
const key = {
dcloudAppid: this._config.dcloudAppid,
platform: PlatformType.H5_WEIXIN
}
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.H5_WEIXIN
}
const result = await getTicket(key)
console.log("setTicket...", key, result)
}
}
TaskTicket.ID = 'TaskTicket'
module.exports = {
TaskAccessTokenH5,
TaskTicket
}

View File

@ -0,0 +1,41 @@
'use strict';
const {
getAccessToken,
setAccessToken,
removeAccessToken
} = require('uni-open-bridge-common')
const {
Task
} = require('./basic.js')
const {
PlatformType
} = require('./consts.js')
class TaskAccessTokenMP extends Task {
constructor(config) {
super()
this._config = config || null
}
async run() {
const key = {
dcloudAppid: this._config.dcloudAppid,
platform: PlatformType.MP_WEIXIN
}
const result = await getAccessToken(key)
console.log("setAccessToken...", key, result)
}
}
TaskAccessTokenMP.ID = 'TaskAccessTokenMP'
module.exports = {
TaskAccessTokenMP
}