pm.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.err) throw '项目管理返回数据异常';
  31. return res.data.data;
  32. },
  33. };
  34. module.exports = {
  35. async dealCatagory(ctx, pCode) {
  36. // mock
  37. // return [ {id: 1, name: 'A标'}, {id: 2, name: 'B标'}, {id: 3, name: 'C标'}];
  38. const url = ctx.app.config.managementProxyPath + dealCatagoryApi;
  39. try {
  40. const data = { code: pCode, token: pmUtils.getJwt() };
  41. return await pmUtils.postData(ctx, url, data);
  42. } catch (err) {
  43. ctx.log(err);
  44. return [];
  45. }
  46. },
  47. async dealData(ctx, pCode, selects) {
  48. if (!selects || selects.length === 0) return {};
  49. const url = ctx.app.config.managementProxyPath + dealDataApi;
  50. try {
  51. const data = { code: pCode, token: pmUtils.getJwt(), key: ['contracts'], bidsection_id: selects };
  52. return await pmUtils.postData(ctx, url, data);
  53. } catch (err) {
  54. ctx.log(err);
  55. return {};
  56. }
  57. }
  58. };