dsk.js 3.6 KB

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