BIP出差申请生成钉钉出差单

2025-01-31  本文已影响0人  更真实

业务场景:BIP出差申请单审批通过后钉钉才可以打外勤
实现:BIP出差申请审批通过后生成钉钉出差单,在钉钉上配置存在出差单后才可以打外勤

1.新增事件订阅

2.创建API发布函数

let AbstractAPIHandler = require('AbstractAPIHandler');
class MyAPIHandler extends AbstractAPIHandler {
  execute(request) {
    const content = JSON.parse(request.params.content);
    const bill = content.data[0];
    // 判断出差申请的状态如果不是已审核不生成钉钉出差单
    if (bill.verifystate == 0 || bill.verifystate == 0) {
      return {};
    }
    /*
    * todo 出差申请单、出差申请变更单审核通过、取消审批修改钉钉出差单
    */ 


    // 获取出差申请的申请人手机号
    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;

    // 根据手机号获取钉钉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 });

上一篇 下一篇

猜你喜欢

热点阅读