排版效果
第一步:获取审批单号
参考:https://developer.work.weixin.qq.com/document/path/91816
请求方式:POST(HTTPS)
请求地址:https://qyapi.weixin.qq.com/cgi-bin/oa/getapprovalinfo?access_token=ACCESS_TOKEN
请求示例:
{"starttime" : "1569546000","endtime" : "1569718800","cursor" : 0 ,"size" : 100 ,"filters" : [{"key": "template_id","value": "ZLqk8pcsAoaXZ1eY56vpAgfX28MPdYU3ayMaSPHaaa"},{"key" : "creator","value" : "WuJunJie"},{"key" : "department","value" : "1"},{"key" : "sp_status","value" : "1"}]}
第二步:根据获取的审批单号再去查询审批的详情
参考:https://developer.work.weixin.qq.com/document/path/91983
请求方式:POST(HTTPS)
请求地址: https://qyapi.weixin.qq.com/cgi-bin/oa/getapprovaldetail?access_token=ACCESS_TOKEN
请求示例:
{"sp_no" : "201909270001"}
JAVA程序
package com.tj.qywx.service.impl;import com.alibaba.excel.util.ListUtils;import com.alibaba.excel.util.MapUtils;import com.alibaba.fastjson.JSON;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;import com.tj.base.service.BaseNumvarService;import com.tj.qywx.domain.WxApproval;import com.tj.qywx.domain.WxUserlist;import com.tj.qywx.service.WxApprovalService;import com.tj.qywx.service.WxDepartmentService;import com.tj.qywx.service.WxUserlistService;import com.tj.utils.TjDateUtils;import lombok.extern.slf4j.Slf4j;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.web.client.RestTemplate;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;@Slf4j@Servicepublic class WxApprovalServiceImpl implements WxApprovalService {@Autowiredprivate BaseNumvarService baseNumvarService;@Autowiredprivate RestTemplate restTemplate;@Autowiredprivate WxUserlistService wxUserlistService;@Autowiredprivate WxDepartmentService wxDepartmentService;@Overridepublic Map<String, Object> show(Integer pagenum, Integer pagesize,Long starttime, Long endtime,String applyerName,String sp_status,String record_type,List deptid) {if (starttime == null) {//参数:开始时间戳(秒)//30天前0:0:0的时间戳(秒)starttime = TjDateUtils.chooseDay(TjDateUtils.getTodayBegin(), -30).getTime() / 1000;}if (endtime == null) {//参数:结束时间戳,当前时间的时间戳(秒)endtime = System.currentTimeMillis() / 1000;}String token = baseNumvarService.getAccessTokenBySecret(baseNumvarService.getSecretSp());String url = "https://qyapi.weixin.qq.com/cgi-bin/oa/getapprovalinfo?access_token=" + token;//请求体数据,使用hashmap封装HashMap<String, Object> hashMap = MapUtils.newHashMap();hashMap.put("starttime", starttime);hashMap.put("endtime", endtime);hashMap.put("cursor", (pagenum - 1) * pagesize + 1);hashMap.put("size", pagesize);//其他条件ArrayList<Map> otherList = ListUtils.newArrayList();otherList.clear();//判断用户姓名条件if (applyerName != null) {LambdaQueryWrapper<WxUserlist> lqwUser = new LambdaQueryWrapper<>();lqwUser.eq(WxUserlist::getName, applyerName).last("LIMIT 1");//只取1条记录WxUserlist one = wxUserlistService.getOne(lqwUser);if (one != null) {otherListAdd("creator", one.getUserid(), otherList);}}//判断审批状态if (StringUtils.isNotEmpty(sp_status)) {otherListAdd("sp_status", sp_status, otherList);}//判断审批单类型属性if (StringUtils.isNotEmpty(record_type)) {otherListAdd("record_type", record_type, otherList);}//部门条件判断if (deptid != null && deptid.size() > 0) {for (Object id : deptid) {otherListAdd("department", String.valueOf(id), otherList);}}//判断filters里的查询条件if (otherList != null && otherList.size() > 0) {hashMap.put("filters", otherList);}log.info("请求体数据:{}", hashMap);Map map = restTemplate.postForObject(url, hashMap, Map.class);log.info("审批单号信息:{}", map);//用来缓存返回结果HashMap<String, Object> resMap = MapUtils.newHashMap();//判断是否有下页,记录总页数if (map.get("next_cursor") != null) {resMap.put("total", (Integer) (map.get("next_cursor")));} else {resMap.put("total", pagenum * pagesize);}if ((Integer) map.get("errcode") == 0) {List<String> list = (List<String>) map.get("sp_no_list");if (list != null && list.size() > 0) {ArrayList<WxApproval> wxApprovalList = ListUtils.newArrayList();HashMap<String, Object> hashMap1 = MapUtils.newHashMap();for (String spNo : list) {hashMap1.clear();//根据审批单号去查询详情String urlDetail = "https://qyapi.weixin.qq.com/cgi-bin/oa/getapprovaldetail?access_token=" + token;hashMap1.put("sp_no", spNo);Map mapDetail = restTemplate.postForObject(urlDetail, hashMap1, Map.class);log.info("转换后前》》》》审批单号:{},详情:{}", spNo, mapDetail);if ((Integer) mapDetail.get("errcode") == 0) {WxApproval wxApproval = JSON.parseObject(JSON.toJSONString(mapDetail.get("info")), WxApproval.class);//申请人信息Map applyer = wxApproval.getApplyer();//申请人useridString userid = (String) applyer.get("userid");wxApproval.setApplyerName(wxUserlistService.getById(userid).getName());//申请人所在部门idInteger partyid = Integer.valueOf((String) applyer.get("partyid"));wxApproval.setPartyName(wxDepartmentService.getById(partyid).getName());log.info("转换后》》》》{}", wxApproval);wxApprovalList.add(wxApproval);}}resMap.put("list", wxApprovalList);return resMap;}}return null;}/*** 审批单号,fiters的条件添加** @param key* @param value* @param otherList*/private void otherListAdd(String key, String value, List otherList) {HashMap<String, String> map = MapUtils.newHashMap();map.put("key", key);map.put("value", value);otherList.add(map);}}
前端查询部分
<template><div><el-form ref="searchForm" :inline="true" :model="data" class="tj-form-search"label-width="80px"><el-form-item label="提交日期"><el-date-picker @change="datechange" v-model="date" type="daterange" align="right"unlink-panels range-separator="~" start-placeholder="开始日期"end-placeholder="结束日期" :picker-options="pickerOptions"></el-date-picker></el-form-item><el-form-item label="审批状态"><el-select clearable v-model="data.sp_status" placeholder="请选择"><el-option v-for="item in options.status" :key="item.value" :label="item.label":value="item.value"></el-option></el-select></el-form-item><el-form-item label="审批类型"><el-select clearable v-model="data.record_type" placeholder="请选择"><el-option v-for="item in options.type" :key="item.value" :label="item.label":value="item.value"></el-option></el-select></el-form-item><el-form-item label="部门"><el-cascader v-model="data.deptid" :options="options.wxdepts" :props="deptsProps":show-all-levels='false' collapse-tags clearable @change="wxdeptChange"></el-cascader></el-form-item><el-form-item label="姓名"><el-input v-model.trim="data.applyerName" placeholder="请输入完整名称"></el-input></el-form-item><el-form-item><el-button @click="search" type="primary" icon="el-icon-search">查询</el-button><el-button @click="reset" icon="el-icon-refresh">重置</el-button></el-form-item></el-form></div></template><script>import { WXdeptsTree } from '@/utils/common'import dateoption from '@/utils/dateoption.js'export default {props: {query: { //初始条件type: String}},data() {return {date: [],data: {starttime: null,endtime: null,filters: []},// 级联配置deptsProps: {multiple: true,value: 'id',label: 'name',emitPath: false,checkStrictly: true,//父子互不关联},// 全部optionsoptions: {wxdepts: [],// 类型type: [{ label: '请假', value: '1' },{ label: '打卡补卡', value: '2' },{ label: '出差', value: '3' },{ label: '外出', value: '4' },{ label: '加班', value: '5' },{ label: '调班', value: '6' },{ label: '会议室预定', value: '7' },{ label: '退款审批', value: '8' },{ label: '红包报销审批', value: '9' },],// 状态status: [{ label: '审批中', value: '1' },{ label: '已通过', value: '2' },{ label: '已驳回', value: '3' },{ label: '已撤销', value: '4' },{ label: '通过后撤销', value: '6' },{ label: '已删除', value: '7' },{ label: '已支付', value: '10' },],},// 日期快捷选项pickerOptions: dateoption.dayrange,}},methods: {// 重置reset() {this.data = this.$options.data().datathis.date = []let p = { pagenum: 1 }console.log('查询条件', p);this.$emit('search', p)},// 查询async search() {// 条件合并this.data.pagenum = 1// 日期if (this.date && this.date.length > 0) {this.data.starttime = this.$dayjs(this.$dayjs(this.date[0]).format('YYYY-MM-DD 00:00:00')).unix() // 时间戳this.data.endtime = this.$dayjs(this.$dayjs(this.date[1]).format('YYYY-MM-DD 23:59:59')).unix() // 时间戳} else {this.data.starttime = nullthis.data.endtime = null}console.log('查询栏输出的条件', this.data);this.$emit('search', this.data)},datechange() {console.log(this.date)},// 用户ID汇总inUserid(data) {let userids = []if (data && data.length > 0) {userids = data.map(item => {return `"${item.userid}"`})}return userids.join()},// 获取所有部门信息async getAllWXdepts() {let res = await WXdeptsTree()console.log('res', res);this.options = {...this.options, ...{ wxdepts: res }}},// 部门变化wxdeptChange(val) {console.log('val', val);console.log('this.data.deptid', this.data.deptid);},},created() {this.getAllWXdepts()},}</script>
注意点
获取审批单号的查询条件filters里面的值全部是字符类型的,里面部门查询的条件是可以多选的,但是值不是数组,而是应该像下面这样写
filters需要根据前端提供的数据,后端使用map进行组装,
{
"filters" : [
{
"key" : "department",
"value" : "1"
},
{
"key" : "department",
"value" : "5"
},
{
"key" : "department",
"value" : "12"
}
]
}
