dsk.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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)', '重庆公路(2018)'];
  69. // 含两个广东建设
  70. const validId = ['5de61133d46f6f000d15d347', '63f31fe113566500140e4902', '66b1f3f6ad66620013c13883', '5b52b027fd3bb0000b257cf8', '5c66649650da2d000d8d37ba', '68da3abe86d439035390afb6', '68946f6f6b1989001314bc07'];
  71. const url = this.url + 'api/compilation/external/list';
  72. const postData = {};
  73. const result = this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'GET', 'json', true));
  74. // return result.filter(item => { return validName.indexOf(item.name) >= 0; });
  75. return result.filter(item => { return validId.indexOf(item.ID) >= 0; });
  76. }
  77. async getProjectList(mobile, compilationId) {
  78. const url = this.url + 'api/project/external/list';
  79. const token = this.getToken();
  80. const postData = {
  81. compilationID: compilationId,
  82. token,
  83. mobile,
  84. };
  85. const result = this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true));
  86. // return result.filter(item => { return !item.property || !item.property.fileType || (item.property.fileType && [1, 5, 15, 16].indexOf(item.property.fileType) === -1); });
  87. return result;
  88. }
  89. async getProjectTree(compilationId, projectId) {
  90. const url = this.url + 'api/project/external/tree';
  91. const token = this.getToken();
  92. const postData = {
  93. compilationID: compilationId,
  94. token,
  95. ID: projectId,
  96. };
  97. return this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true));
  98. }
  99. async getProjectBills(compilationId, treeId) {
  100. const url = this.url + 'api/project/external/bills';
  101. const token = this.getToken();
  102. const postData = {
  103. compilationID: compilationId,
  104. token,
  105. ID: treeId,
  106. };
  107. return this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true));
  108. }
  109. }
  110. module.exports = DSK;