dsk.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, captchaVerifyParam) {
  30. const url = this.url + 'api/sms/external/getSmsCode';
  31. const postData = {
  32. mobile,
  33. captchaVerifyParam,
  34. };
  35. let result = false;
  36. try {
  37. const response = await this.ctx.helper.sendRequest(url, postData, 'GET', 'json', true);
  38. result = true;
  39. } catch (error) {
  40. console.log(error);
  41. result = false;
  42. }
  43. return result;
  44. }
  45. async accountAuth(mobile, method, value) {
  46. const url = this.url + 'api/user/external/auth';
  47. const postData = {
  48. mobile,
  49. loginMode: method === 1 ? 'password' : 'sms',
  50. };
  51. if (method === 1) {
  52. postData.password = value;
  53. } else {
  54. postData.code = value;
  55. }
  56. return this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true));
  57. }
  58. dealWith(result) {
  59. if (result.status !== 200) {
  60. throw result.data ? result.data.message : '大司空接口接入失败';
  61. }
  62. if (!(result.data && result.data.errno === 0 && result.data.data)) {
  63. throw result.data ? result.data.message : '大司空接口接入失败';
  64. }
  65. return result.data.data;
  66. }
  67. async getCompilation() {
  68. const validName = ['全国公路(2018)', '广东公路(2023)', '浙江公路(2025)', '重庆建设(2018)', '广东建设(2018)'];
  69. const url = this.url + 'api/compilation/external/list';
  70. const postData = {};
  71. const result = this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'GET', 'json', true));
  72. return result.filter(item => { return validName.indexOf(item.name) >= 0; });
  73. }
  74. async getProjectList(mobile, compilationId) {
  75. const url = this.url + 'api/project/external/list';
  76. const token = this.getToken();
  77. const postData = {
  78. compilationID: compilationId,
  79. token,
  80. mobile,
  81. };
  82. const result = this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true));
  83. // return result.filter(item => { return !item.property || !item.property.fileType || (item.property.fileType && [1, 5, 15, 16].indexOf(item.property.fileType) === -1); });
  84. return result;
  85. }
  86. async getProjectTree(compilationId, projectId) {
  87. const url = this.url + 'api/project/external/tree';
  88. const token = this.getToken();
  89. const postData = {
  90. compilationID: compilationId,
  91. token,
  92. ID: projectId,
  93. };
  94. return this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true));
  95. }
  96. async getProjectBills(compilationId, treeId) {
  97. const url = this.url + 'api/project/external/bills';
  98. const token = this.getToken();
  99. const postData = {
  100. compilationID: compilationId,
  101. token,
  102. ID: treeId,
  103. };
  104. return this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true));
  105. }
  106. }
  107. module.exports = DSK;