pm.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. 'use strict';
  2. /**
  3. * 标准库std 相关接口
  4. *
  5. * @author Mai
  6. * @date 2020/3/15
  7. * @version
  8. */
  9. // 加密类
  10. const jwt = require('jsonwebtoken');
  11. const privateKey = 'jl2pm3850key888sc';
  12. // todo 修改请求链接
  13. const dealCatagoryApi = '/api/external/jlbb/folder';
  14. const dealDataApi = '/api/external/jlbb/folder';
  15. const pmUtils = {
  16. getJwt() {
  17. // todo jwt加密
  18. return jwt.sign({}, privateKey, {expiresIn: '5s', algorithm: 'HS256'});
  19. },
  20. async postData(ctx, url, data) {
  21. const res = await ctx.curl(url, {
  22. method: 'GET', //'POST',
  23. contentType: 'json',
  24. data,
  25. dataType: 'json',
  26. timeout: [2000, 10000],
  27. timing: true,
  28. });
  29. if (res.status !== 200) throw '请求项目管理数据异常';
  30. if (!res.data || res.data.code) throw '项目管理返回数据异常';
  31. return res.data.data;
  32. },
  33. };
  34. const filterLeaf = function(node, result) {
  35. if (node.children) {
  36. for (const child of node.children) {
  37. filterLeaf(child, result);
  38. }
  39. } else if (node.isfolder !== undefined && !node.isfolder) result.push(node);
  40. };
  41. module.exports = {
  42. async dealCatagory(ctx, pCode) {
  43. // mock
  44. // return [ {id: 1, name: 'A标'}, {id: 2, name: 'B标'}, {id: 3, name: 'C标'}];
  45. const url = ctx.app.config.managementProxyPath + dealCatagoryApi;
  46. try {
  47. const data = await pmUtils.postData(ctx, url, { code: pCode, token: pmUtils.getJwt() });
  48. const result = [];
  49. filterLeaf(data, result);
  50. return result;
  51. } catch (err) {
  52. ctx.log(err);
  53. return [];
  54. }
  55. },
  56. async dealData(ctx, pCode, selects) {
  57. if (!selects || selects.length === 0) return {};
  58. const url = ctx.app.config.managementProxyPath + dealDataApi;
  59. try {
  60. const data = { code: pCode, token: pmUtils.getJwt(), key: ['contracts'], bidsection_id: selects };
  61. return await pmUtils.postData(ctx, url, data);
  62. } catch (err) {
  63. ctx.log(err);
  64. return {};
  65. }
  66. }
  67. };