業(yè)務(wù)場(chǎng)景:BIP出差申請(qǐng)單審批通過(guò)后釘釘才可以打外勤
實(shí)現(xiàn):BIP出差申請(qǐng)審批通過(guò)后生成釘釘出差單前酿,在釘釘上配置存在出差單后才可以打外勤
1.新增事件訂閱
2.創(chuàng)建API發(fā)布函數(shù)
let AbstractAPIHandler = require('AbstractAPIHandler');
class MyAPIHandler extends AbstractAPIHandler {
execute(request) {
const content = JSON.parse(request.params.content);
const bill = content.data[0];
// 判斷出差申請(qǐng)的狀態(tài)如果不是已審核不生成釘釘出差單
if (bill.verifystate == 0 || bill.verifystate == 0) {
return {};
}
/*
* todo 出差申請(qǐng)單谷婆、出差申請(qǐng)變更單審核通過(guò)、取消審批修改釘釘出差單
*/
// 獲取出差申請(qǐng)的申請(qǐng)人手機(jī)號(hào)
let mobileSql = 'select mobile from bd.staff.StaffNew where id=' + bill.pk_handlepsn;
var mobileDatas = ObjectStore.queryByYonQL(mobileSql, 'ucf-staff-center');
let mobile = mobileDatas[0].mobile;
if (mobile.startsWith('+86-')) {
mobile = mobile.substring(4);
}
// 獲取釘釘token
const tokenBody = { appKey: 'ding0d4y5ezha8ghiezu', appSecret: '54G2-_1gmq3WuxULCq4DkM1GrfxZJd6cB5R3JJ1gWJumKAZz6R8qqPEuPaMST8OR' };
const header = { 'Content-Type': 'application/json;utf-8' };
const tokenUrl = 'https://api.dingtalk.com/v1.0/oauth2/accessToken';
const tokenStr = postman('post', tokenUrl, JSON.stringify(header), JSON.stringify(tokenBody));
const accessToken = JSON.parse(tokenStr).accessToken;
// 根據(jù)手機(jī)號(hào)獲取釘釘userId
const userIdUrl = `https://oapi.dingtalk.com/topapi/v2/user/getbymobile?access_token=${accessToken}`;
const userIdBody = { mobile: mobile };
const userIdStr = postman('post', userIdUrl, JSON.stringify(header), JSON.stringify(userIdBody));
const userData = JSON.parse(userIdStr);
let userId;
if (userData && userData.errcode == 0) {
userId = userData.result.userid;
}
// 生成出差單劈愚,只支持出差單表體一行
const attendanceBody = {
topCalculateApproveDurationParam: {
bizType: 2,
fromTime: bill.MemoapplyBVO[0].dbegindate,
toTime: bill.MemoapplyBVO[0].denddate,
durationUnit: 'day',
calculateModel: 0,
},
tagName: '出差',
subType: bill.vapplyname,
approveId: '' + bill.id,
jumpUrl: 'https://open.dingtalk.com/',
};
const attendanceUrl = `https://api.dingtalk.com/v1.0/attendance/approvals/finish?userId=${userId}`;
header['x-acs-dingtalk-access-token'] = accessToken;
const attendanceStr = postman('post', attendanceUrl, JSON.stringify(header), JSON.stringify(attendanceBody));
return { userIdStr };
}
}
exports({ entryPoint: MyAPIHandler });