'use strict'; /** * 大司空相关接口 * * @author CaiAoLin * @date 2018/1/25 * @version */ const crypto = require('crypto'); const moment = require('moment'); const jwt = require('jsonwebtoken'); const privateKey = 'zhzhjwtqwerty'; class DSK { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { this.ctx = ctx; this.url = 'https://dsk.smartcost.com.cn/'; this.tokenKey = 'jH2=lj4j@!$#421?{S54n'; } getToken() { // return crypto.createHash('md5').update(crypto.createHash('md5').update(this.tokenKey).digest('hex') + moment().format('DD-HH')).digest('hex'); return jwt.sign({ iat: Date.now() }, privateKey); } async sendSms(mobile) { const url = this.url + 'api/sms/external/getSmsCode'; const postData = { mobile, }; let result = false; try { const response = await this.ctx.helper.sendRequest(url, postData, 'GET', 'json', true); result = true; } catch (error) { console.log(error); result = false; } return result; } async accountAuth(mobile, method, value) { const url = this.url + 'api/user/external/auth'; const postData = { mobile, loginMode: method === 1 ? 'password' : 'sms', }; if (method === 1) { postData.password = value; } else { postData.code = value; } return this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true)); } dealWith(result) { if (result.status !== 200) { throw result.data ? result.data.message : '大司空接口接入失败'; } if (!(result.data && result.data.errno === 0 && result.data.data)) { throw result.data ? result.data.message : '大司空接口接入失败'; } return result.data.data; } async getCompilation() { const validName = ['全国公路(2018)', '广东公路(2018)', '浙江公路(2025)', '重庆建设(2018)', '广东建设(2018)']; const url = this.url + 'api/compilation/external/list'; const postData = {}; const result = this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'GET', 'json', true)); return result.filter(item => { return validName.indexOf(item.name) >= 0; }); } async getProjectList(mobile, compilationId) { const url = this.url + 'api/project/external/list'; const token = this.getToken(); const postData = { compilationID: compilationId, token, mobile, }; const result = this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true)); // return result.filter(item => { return !item.property || !item.property.fileType || (item.property.fileType && [1, 5, 15, 16].indexOf(item.property.fileType) === -1); }); return result; } async getProjectTree(compilationId, projectId) { const url = this.url + 'api/project/external/tree'; const token = this.getToken(); const postData = { compilationID: compilationId, token, ID: projectId, }; return this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true)); } async getProjectBills(compilationId, treeId) { const url = this.url + 'api/project/external/bills'; const token = this.getToken(); const postData = { compilationID: compilationId, token, ID: treeId, }; return this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true)); } } module.exports = DSK;