dsk.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. 'use strict';
  2. /**
  3. * 大司空相关接口
  4. *
  5. * @author CaiAoLin
  6. * @date 2018/1/25
  7. * @version
  8. */
  9. const crypto = require('crypto');
  10. const moment = require('moment');
  11. const jwt = require('jsonwebtoken');
  12. const privateKey = 'zhzhjwtqwerty';
  13. class DSK {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. this.ctx = ctx;
  22. this.url = 'https://dsk.smartcost.com.cn/';
  23. this.tokenKey = 'jH2=lj4j@!$#421?{S54n';
  24. }
  25. getToken() {
  26. // return crypto.createHash('md5').update(crypto.createHash('md5').update(this.tokenKey).digest('hex') + moment().format('DD-HH')).digest('hex');
  27. return jwt.sign({ iat: Date.now() }, privateKey);
  28. }
  29. async sendSms(mobile) {
  30. const url = this.url + 'api/sms/external/getSmsCode';
  31. const postData = {
  32. mobile,
  33. };
  34. let result = false;
  35. try {
  36. const response = await this.ctx.helper.sendRequest(url, postData, 'GET', 'json', true);
  37. result = true;
  38. } catch (error) {
  39. console.log(error);
  40. result = false;
  41. }
  42. return result;
  43. }
  44. async accountAuth(mobile, method, value) {
  45. const url = this.url + 'api/user/external/auth';
  46. const postData = {
  47. mobile,
  48. loginMode: method === 1 ? 'password' : 'sms',
  49. };
  50. if (method === 1) {
  51. postData.password = value;
  52. } else {
  53. postData.code = value;
  54. }
  55. return this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true));
  56. }
  57. dealWith(result) {
  58. if (result.status !== 200) {
  59. throw result.data ? result.data.message : '大司空接口接入失败';
  60. }
  61. if (!(result.data && result.data.errno === 0 && result.data.data)) {
  62. throw result.data ? result.data.message : '大司空接口接入失败';
  63. }
  64. return result.data.data;
  65. }
  66. async getCompilation() {
  67. const validName = ['全国公路(2018)', '广东公路(2018)', '浙江公路(2025)', '重庆建设(2018)', '广东建设(2018)'];
  68. const url = this.url + 'api/compilation/external/list';
  69. const postData = {};
  70. const result = this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'GET', 'json', true));
  71. return result.filter(item => { return validName.indexOf(item.name) >= 0; });
  72. }
  73. async getProjectList(mobile, compilationId) {
  74. const url = this.url + 'api/project/external/list';
  75. const token = this.getToken();
  76. const postData = {
  77. compilationID: compilationId,
  78. token,
  79. mobile,
  80. };
  81. const result = this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true));
  82. // return result.filter(item => { return !item.property || !item.property.fileType || (item.property.fileType && [1, 5, 15, 16].indexOf(item.property.fileType) === -1); });
  83. return result;
  84. }
  85. async getProjectTree(compilationId, projectId) {
  86. const url = this.url + 'api/project/external/tree';
  87. const token = this.getToken();
  88. const postData = {
  89. compilationID: compilationId,
  90. token,
  91. ID: projectId,
  92. };
  93. return this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true));
  94. }
  95. async getProjectBills(compilationId, treeId) {
  96. const url = this.url + 'api/project/external/bills';
  97. const token = this.getToken();
  98. const postData = {
  99. compilationID: compilationId,
  100. token,
  101. ID: treeId,
  102. };
  103. return this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true));
  104. }
  105. }
  106. module.exports = DSK;