增加审批记录和comment中的付件获取
This commit is contained in:
parent
a7c3d02f01
commit
44c371e4a3
@ -7,6 +7,7 @@ import { RequestTask, TaskTypeMap } from 'models/RequestTask'
|
|||||||
import { BlockChain } from 'chain/BlockChain'
|
import { BlockChain } from 'chain/BlockChain'
|
||||||
import { ChainTask } from 'models/ChainTask'
|
import { ChainTask } from 'models/ChainTask'
|
||||||
import { isObjectId } from 'utils/string.util'
|
import { isObjectId } from 'utils/string.util'
|
||||||
|
import { WechatWorkService } from 'service/wechatwork.service'
|
||||||
|
|
||||||
class WorkFlowController extends BaseController {
|
class WorkFlowController extends BaseController {
|
||||||
@role(ROLE_ANON)
|
@role(ROLE_ANON)
|
||||||
@ -110,10 +111,10 @@ class WorkFlowController extends BaseController {
|
|||||||
@router('get /workflow/test')
|
@router('get /workflow/test')
|
||||||
async test(req, res) {
|
async test(req, res) {
|
||||||
// let file_path = '/Users/zhl/Documents/workspace/tools/excel2json/test.xlsx'
|
// let file_path = '/Users/zhl/Documents/workspace/tools/excel2json/test.xlsx'
|
||||||
// let fileId = 'WWME_g-oYEAAAzSUkPNpznkoGbgD2f1bDCA.xlsx'
|
// let fileId = 'WWME_g-oYEAAACj738mha3is3XxxDavhb5w'
|
||||||
// await new WechatWorkService().fetchFile(fileId)
|
// await new WechatWorkService().fetchFile(fileId, true)
|
||||||
// console.log('11')
|
// console.log('11')
|
||||||
let spNo = '202304260007'
|
let spNo = '202305090004'
|
||||||
new TaskQueue().addTaskToQueue(spNo)
|
new TaskQueue().addTaskToQueue(spNo)
|
||||||
// let task = await ChainTask.findById('642fe42611845ce0e1def316')
|
// let task = await ChainTask.findById('642fe42611845ce0e1def316')
|
||||||
// for (let tid of task.tasks) {
|
// for (let tid of task.tasks) {
|
||||||
|
@ -221,38 +221,114 @@ export class WechatWorkService {
|
|||||||
fileId = value.files[0].file_id
|
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) {
|
if (!fileId) {
|
||||||
throw new Error('no file')
|
throw new Error('no file')
|
||||||
}
|
}
|
||||||
let userInfo = await this.fetchUserInfo(starter)
|
let userInfo = await this.fetchUserInfo(starter)
|
||||||
let starterName = userInfo.name
|
let starterName = userInfo.name
|
||||||
let filePath = await this.fetchFile(fileId)
|
let { filename } = await this.fetchFile(fileId)
|
||||||
let data = excelToJson(filePath)
|
let data = excelToJson(filename)
|
||||||
return { taskId: spNo, name, desc, data, starter, starterName }
|
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获取文件
|
* 根据media_id获取文件
|
||||||
* https://developer.work.weixin.qq.com/devtool/interface/alone?id=18615
|
* https://developer.work.weixin.qq.com/devtool/interface/alone?id=18615
|
||||||
* @param mediaId
|
* @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 url = `${WX_API_HOST}/cgi-bin/media/get`
|
||||||
const access_token = await this.getAccessToken()
|
const access_token = await this.getAccessToken()
|
||||||
let config: AxiosRequestConfig = {
|
let config: AxiosRequestConfig = {
|
||||||
method: 'get',
|
method: 'get',
|
||||||
url,
|
url,
|
||||||
responseType: 'arraybuffer',
|
responseType: 'arraybuffer',
|
||||||
|
cancelToken: source.token,
|
||||||
params: {
|
params: {
|
||||||
access_token,
|
access_token,
|
||||||
media_id: mediaId,
|
media_id: mediaId,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
let filename = `${mediaId}.xlsx`
|
|
||||||
const res = await axios.request(config)
|
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)
|
const filePath = path.join(os.tmpdir(), filename)
|
||||||
fs.writeFileSync(filePath, res.data)
|
fs.writeFileSync(filePath, res.data)
|
||||||
return filePath
|
return { filename: filePath }
|
||||||
}
|
}
|
||||||
// 查询审批列表
|
// 查询审批列表
|
||||||
public async queryTasks() {
|
public async queryTasks() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user