| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 | 'use strict';/** * 大司空相关接口 * * @author CaiAoLin * @date 2018/1/25 * @version */const crypto = require('crypto');const moment = require('moment');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');    }    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 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 item.type === 'highway' && (item.name === '全国公路(2018)' || item.name === '广东公路(2018)'); });    }    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;
 |