index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const httpRequest_1 = __importDefault(require("../utils/httpRequest"));
  7. const utils_1 = require("../utils/utils");
  8. const code_1 = require("../const/code");
  9. function validateCrossAccount(config, opts = {}) {
  10. let getCrossAccountInfo = opts.getCrossAccountInfo || config.getCrossAccountInfo;
  11. if (getCrossAccountInfo) {
  12. throw utils_1.E(Object.assign({}, code_1.ERROR.INVALID_PARAM, { message: 'invalid config: getCrossAccountInfo' }));
  13. }
  14. }
  15. async function callWxOpenApi(cloudbase, { apiName, apiOptions, cgiName, requestData }, opts) {
  16. let transformRequestData;
  17. try {
  18. transformRequestData = requestData ? JSON.stringify(requestData) : '';
  19. }
  20. catch (e) {
  21. throw utils_1.E(Object.assign({}, e, { code: code_1.ERROR.INVALID_PARAM.code, message: '对象出现了循环引用' }));
  22. }
  23. validateCrossAccount(cloudbase.config, opts);
  24. const params = {
  25. action: 'wx.api',
  26. apiName,
  27. apiOptions,
  28. cgiName,
  29. requestData: transformRequestData
  30. };
  31. return httpRequest_1.default({
  32. config: cloudbase.config,
  33. params,
  34. method: 'post',
  35. opts,
  36. headers: {
  37. 'content-type': 'application/json'
  38. }
  39. }).then(res => {
  40. if (res.code) {
  41. return res;
  42. }
  43. // throw E({ ...res })
  44. // } else {
  45. let result;
  46. try {
  47. result = JSON.parse(res.data.responseData);
  48. }
  49. catch (e) {
  50. result = res.data.responseData;
  51. }
  52. return {
  53. result,
  54. requestId: res.requestId
  55. };
  56. // }
  57. });
  58. }
  59. exports.callWxOpenApi = callWxOpenApi;
  60. /**
  61. * 调用wxopenAPi
  62. * @param {String} apiName 接口名
  63. * @param {Buffer} requestData
  64. * @return {Promise} 正常内容为buffer,报错为json {code:'', message:'', resquestId:''}
  65. */
  66. async function callCompatibleWxOpenApi(cloudbase, { apiName, apiOptions, cgiName, requestData }, opts) {
  67. validateCrossAccount(cloudbase.config, opts);
  68. const params = {
  69. action: 'wx.openApi',
  70. apiName,
  71. apiOptions,
  72. cgiName,
  73. requestData
  74. };
  75. return httpRequest_1.default({
  76. config: cloudbase.config,
  77. method: 'post',
  78. headers: { 'content-type': 'multipart/form-data' },
  79. params,
  80. isFormData: true,
  81. opts
  82. }).then(res => res);
  83. }
  84. exports.callCompatibleWxOpenApi = callCompatibleWxOpenApi;
  85. /**
  86. * wx.wxPayApi 微信支付用
  87. * @param {String} apiName 接口名
  88. * @param {Buffer} requestData
  89. * @return {Promise} 正常内容为buffer,报错为json {code:'', message:'', resquestId:''}
  90. */
  91. async function callWxPayApi(cloudbase, { apiName, apiOptions, cgiName, requestData }, opts) {
  92. validateCrossAccount(cloudbase.config, opts);
  93. const params = {
  94. action: 'wx.wxPayApi',
  95. apiName,
  96. apiOptions,
  97. cgiName,
  98. requestData
  99. };
  100. return httpRequest_1.default({
  101. config: cloudbase.config,
  102. method: 'post',
  103. headers: { 'content-type': 'multipart/form-data' },
  104. params,
  105. isFormData: true,
  106. opts
  107. });
  108. }
  109. exports.callWxPayApi = callWxPayApi;