增加审批记录和comment中的付件获取

This commit is contained in:
zhl 2023-05-09 13:53:05 +08:00
parent a7c3d02f01
commit 44c371e4a3
2 changed files with 85 additions and 8 deletions

View File

@ -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) {

View File

@ -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() {