'use strict'; // 企业微信一些方法 const axios = require('axios'); class wxWork { constructor(ctx) { this.ctx = ctx; } async setSuiteTicket(suite_ticket) { return await this.ctx.app.redis.set('suite_ticket', suite_ticket, 'EX', 1800);// 最多半小时有效期,企业微信会自动请求刷新获取 } async getSuiteTicket() { try { const suite_ticket = this.ctx.app.redis.get('suite_ticket'); if (!suite_ticket) { throw '还没有设置过'; } return suite_ticket; } catch (error) { console.log(error); return false; } } async getSuiteAccessToken() { const suite_access_token = await this.ctx.app.redis.get('suite_access_token'); if (!suite_access_token) { const suite_ticket = await this.getSuiteTicket(); const post_data = { suite_id: this.ctx.app.config.qywx.suiteID, suite_secret: this.ctx.app.config.qywx.suiteSecret, suite_ticket, }; // const response = await this.ctx.helper.sendRequest('https://qyapi.work.weixin.qq.com/cgi-bin/service/get_suite_token', post_data, 'POST'); // console.log(response); const { data } = await axios.post('https://qyapi.weixin.qq.com/cgi-bin/service/get_suite_token', post_data); const { suite_access_token, expires_in } = data; if (suite_access_token) { await this.ctx.app.redis.set('suite_access_token', suite_access_token, 'EX', expires_in);// 最多两个小时有效期 return suite_access_token; } } return suite_access_token; } async savePermanentCode(temporary_code) { return await this.ctx.app.redis.set('temporary_code', temporary_code, 'EX', 600);// 最多10分钟有效期,企业信息要入库 } // 设置永久授权码(代开发授权凭证) async setPermanentCode() { const temporary_code = await this.ctx.app.redis.get('temporary_code'); if (!temporary_code) { console.log('不存在授权码'); return false; } const suite_access_token = await this.getSuiteAccessToken(); const { data } = await axios.post(`https://qyapi.weixin.qq.com/cgi-bin/service/get_permanent_code?suite_access_token=${suite_access_token}`, { auth_code: temporary_code, }); const { permanent_code, auth_corp_info, auth_user_info, auth_info, } = data; if (permanent_code) { const agentid = auth_info.agent[0].agentid; const result = await this.ctx.service.wxWork.insertCorp(permanent_code, agentid, auth_corp_info, auth_user_info); console.log(result, permanent_code, agentid, auth_corp_info, auth_user_info); console.log(auth_corp_info.corp_name + ' 授权企业成功'); await this.ctx.app.redis.del('temporary_code'); return result; } console.error('获取永久授权码失败', data); return false; } // 读取企业的通讯录 access_token async getCorpTxlAccessToken(corpID) { try { let token = await this.ctx.app.redis.get(`${corpID}_txl_access_token`); if (!token) { const corpInfo = await this.ctx.service.wxWork.getDataByCondition({ corpid: corpID }); if (!corpInfo) { throw '未绑定该企业'; } const { data } = await axios.get(`https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${corpID}&corpsecret=8SZKdgpUICzj9ImEjrqHHVCD6NxEpgWqkoEb0KzzG40`); const { access_token, expires_in } = data; if (!access_token) { throw '获取 corp_access_token 失败'; } await this.ctx.app.redis.set(`${corpID}_txl_access_token`, access_token, 'EX', expires_in); token = access_token; } return token; } catch (e) { console.log(e); return null; } } // 读取企业的 access_token async getCorpAccessToken(corpID) { try { let token = await this.ctx.app.redis.get(`${corpID}_access_token`); if (!token) { const corpInfo = await this.ctx.service.wxWork.getDataByCondition({ corpid: corpID }); if (!corpInfo) { throw '未绑定该企业'; } const { data } = await axios.get(`https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=${corpID}&corpsecret=${corpInfo.permanent_code}`); const { access_token, expires_in } = data; if (!access_token) { throw '获取 corp_access_token 失败'; } await this.ctx.app.redis.set(`${corpID}_access_token`, access_token, 'EX', expires_in); token = access_token; } return token; } catch (e) { console.log(e); return null; } } // 读取企业用户信息, 只获取一次 async getCorpUser(token, code) { try { const { data: user_data } = await axios.get(`https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token=${token}&code=${code}`); if (user_data.errcode !== 0) { throw '获取企业微信用户信息失败'; } const user_ticket = user_data.user_ticket; const { data: user_detail_data } = await axios.post(`https://qyapi.weixin.qq.com/cgi-bin/auth/getuserdetail?access_token=${token}`, { user_ticket, }); if (user_detail_data.errcode !== 0) { throw '获取企业微信用户详细信息失败'; } const user_info = { userid: user_data.userid, avatar: user_detail_data.avatar !== undefined ? user_detail_data.avatar : null, gender: user_detail_data.gender !== undefined ? user_detail_data.gender : null, }; return user_info; } catch (e) { console.log(e); return null; } } // 读取企业用户信息 async getCorpUserCommonData(token, userid, corpID) { try { const { data: user_common_data } = await axios.get(`https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=${token}&userid=${userid}`); if (user_common_data.errcode !== 0) { throw '获取企业微信用户详细信息失败'; } const corpInfo = await this.ctx.service.wxWork.getDataByCondition({ corpid: corpID }); const user_info = { userid: user_common_data.userid, name: user_common_data.name, department: user_common_data.department, position: user_common_data.position, company: corpInfo.corp_name, }; return user_info; } catch (e) { console.log(e); return null; } } // 获取通讯录 async getUserList(corpid) { const token = await this.getCorpTxlAccessToken(corpid); const { data: user_data } = await axios.post(`https://qyapi.weixin.qq.com/cgi-bin/user/list_id?access_token=${token}`); console.log(user_data, user_data.dept_user); return user_data.dept_user; } // 发送模板卡片信息 async sendTemplateCard(userids, corpid, title, desc, content, url = '', btntxt = null) { try { const corpInfo = await this.ctx.service.wxWork.getDataByCondition({ corpid }); if (!corpInfo) { throw '该企业不存在'; } const token = await this.getCorpAccessToken(corpid); const templateCard = { card_type: 'text_notice', main_title: { title, desc, }, horizontal_content_list: content, }; if (btntxt) { templateCard.jump_list = { type: 1, title: btntxt, url, }; } templateCard.card_action = { type: 1, url, }; const data = await axios.post(`https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${token}`, { touser: userids.join('|'), msgtype: 'template_card', agentid: corpInfo.agentid, template_card: templateCard, }); return data; } catch (e) { console.log(e); return null; } } // 发送文本卡片信息 async sendTextCard(userids, corpid, title, content, url = '', btntxt = null) { try { const corpInfo = await this.ctx.service.wxWork.getDataByCondition({ corpid }); if (!corpInfo) { throw '该企业不存在'; } const token = await this.getCorpAccessToken(corpid); const textCard = { title, description: content, }; if (url) textCard.url = url ? url : ''; if (btntxt) textCard.btntxt = btntxt; const data = await axios.post(`https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=${token}`, { touser: userids.join('|'), msgtype: 'textcard', agentid: corpInfo.agentid, textcard: textCard, }); return data; } catch (e) { console.log(e); return null; } } } module.exports = wxWork;