index.obj.js 1.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 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)
  }
}