dsk.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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'; });
  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. return this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true));
  78. }
  79. async getProjectTree(compilationId, projectId) {
  80. const url = this.url + 'api/project/external/tree';
  81. const token = this.getToken();
  82. const postData = {
  83. compilationID: compilationId,
  84. token,
  85. ID: projectId,
  86. };
  87. return this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true));
  88. }
  89. async getProjectBills(compilationId, treeId) {
  90. const url = this.url + 'api/project/external/bills';
  91. const token = this.getToken();
  92. const postData = {
  93. compilationID: compilationId,
  94. token,
  95. ID: treeId,
  96. };
  97. return this.dealWith(await this.ctx.helper.sendRequest(url, postData, 'POST', 'json', true));
  98. }
  99. }
  100. module.exports = DSK;