From 44c371e4a378f54de1e7a0986ce21eb5878ae71d Mon Sep 17 00:00:00 2001 From: zhl Date: Tue, 9 May 2023 13:53:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=AE=A1=E6=89=B9=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=E5=92=8Ccomment=E4=B8=AD=E7=9A=84=E4=BB=98=E4=BB=B6?= =?UTF-8?q?=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controllers/workflow.controller.ts | 7 ++- src/service/wechatwork.service.ts | 86 ++++++++++++++++++++++++-- 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/src/controllers/workflow.controller.ts b/src/controllers/workflow.controller.ts index 8929c2b..4adf932 100644 --- a/src/controllers/workflow.controller.ts +++ b/src/controllers/workflow.controller.ts @@ -7,6 +7,7 @@ import { RequestTask, TaskTypeMap } from 'models/RequestTask' import { BlockChain } from 'chain/BlockChain' import { ChainTask } from 'models/ChainTask' import { isObjectId } from 'utils/string.util' +import { WechatWorkService } from 'service/wechatwork.service' class WorkFlowController extends BaseController { @role(ROLE_ANON) @@ -110,10 +111,10 @@ class WorkFlowController extends BaseController { @router('get /workflow/test') async test(req, res) { // let file_path = '/Users/zhl/Documents/workspace/tools/excel2json/test.xlsx' - // let fileId = 'WWME_g-oYEAAAzSUkPNpznkoGbgD2f1bDCA.xlsx' - // await new WechatWorkService().fetchFile(fileId) + // let fileId = 'WWME_g-oYEAAACj738mha3is3XxxDavhb5w' + // await new WechatWorkService().fetchFile(fileId, true) // console.log('11') - let spNo = '202304260007' + let spNo = '202305090004' new TaskQueue().addTaskToQueue(spNo) // let task = await ChainTask.findById('642fe42611845ce0e1def316') // for (let tid of task.tasks) { diff --git a/src/service/wechatwork.service.ts b/src/service/wechatwork.service.ts index f768a88..8ae9909 100644 --- a/src/service/wechatwork.service.ts +++ b/src/service/wechatwork.service.ts @@ -221,38 +221,114 @@ export class WechatWorkService { fileId = value.files[0].file_id } } + if (fileId) { + let checked = await this.checkFileMatch(fileId) + if (!checked) { + fileId = '' + } + } + if (!fileId) { + fileId = await this.queryMedidaIdFromSprecord(info.sp_record) + } + if (!fileId) { + fileId = await this.queryMedidaIdFromComment(info.comments) + } if (!fileId) { throw new Error('no file') } let userInfo = await this.fetchUserInfo(starter) let starterName = userInfo.name - let filePath = await this.fetchFile(fileId) - let data = excelToJson(filePath) + let { filename } = await this.fetchFile(fileId) + let data = excelToJson(filename) return { taskId: spNo, name, desc, data, starter, starterName } } + // 检查审核记录中的文件 + private async queryMedidaIdFromSprecord(datas: any) { + let files = [] + for (let data of datas) { + if (!data.details || data.details.length === 0) { + continue + } + for (let detail of data.details) { + if (!detail.media_id || detail.media_id.length === 0) { + continue + } + files = files.concat(detail.media_id) + } + } + let result = '' + if (files.length > 0) { + for (let file of files) { + if (await this.checkFileMatch(file)) { + result = file + break + } + } + } + return result + } + // 检查comment中的文件 + private async queryMedidaIdFromComment(datas: any) { + let files = [] + for (let data of datas) { + if (!data.media_id || data.media_id.length === 0) { + continue + } + files = files.concat(data.media_id) + } + let result = '' + if (files.length > 0) { + for (let file of files) { + if (await this.checkFileMatch(file)) { + result = file + break + } + } + } + return result + } + + private async checkFileMatch(mediaId: string) { + let { filename, type } = await this.fetchFile(mediaId, true) + return filename.indexOf('.xlsx') > 0 && type === 'application/octet-stream' + } /** * 根据media_id获取文件 * https://developer.work.weixin.qq.com/devtool/interface/alone?id=18615 * @param mediaId */ - public async fetchFile(mediaId: string) { + public async fetchFile(mediaId: string, queryOnly = false) { + const source = axios.CancelToken.source() const url = `${WX_API_HOST}/cgi-bin/media/get` const access_token = await this.getAccessToken() let config: AxiosRequestConfig = { method: 'get', url, responseType: 'arraybuffer', + cancelToken: source.token, params: { access_token, media_id: mediaId, }, } - let filename = `${mediaId}.xlsx` + const res = await axios.request(config) + if (res.status !== 200) { + return { filename: '' } + } + let regex = /.+?filename="(.+?)"/ + const match = res.headers['content-disposition'].match(regex) + let remoteName = match ? match[1] : '' + console.log('filename: ' + remoteName + ' type: ' + res.headers['content-type']) + if (queryOnly) { + source.cancel('cancel') + return { filename: remoteName, type: res.headers['content-type'] } + } + let filename = `${mediaId}.xlsx` const filePath = path.join(os.tmpdir(), filename) fs.writeFileSync(filePath, res.data) - return filePath + return { filename: filePath } } // 查询审批列表 public async queryTasks() {