@zgfe/business-lib 1.1.53 → 1.1.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/es/utils/ajax.js CHANGED
@@ -6,6 +6,7 @@ import Request, { extend } from 'umi-request';
6
6
  import { notification, message } from 'antd';
7
7
  import { globalConfig } from '../config';
8
8
  import { ResponseStatus } from './type';
9
+ import util from './util';
9
10
  var codeMessage = {
10
11
  200: '服务器成功返回请求的数据。',
11
12
  201: '新建或修改数据成功。',
@@ -44,6 +45,20 @@ var errorHandler = function errorHandler(error) {
44
45
  }
45
46
  return response;
46
47
  };
48
+ var downloadFile = function downloadFile(resData, name) {
49
+ var blob = resData.data;
50
+ var fileName = resData.response.headers.get('Filename');
51
+ if (name) {
52
+ var fileType = fileName.split('.')[1];
53
+ fileName = "".concat(name, ".").concat(fileType);
54
+ }
55
+ var href = URL.createObjectURL(blob);
56
+ var a = document.createElement('a');
57
+ a.href = href;
58
+ a.download = fileName;
59
+ a.click();
60
+ return null;
61
+ };
47
62
  export function responseErrorHandler(url, apiResult, errorTitle, notifyType) {
48
63
  if (/\.jsp/.test(url)) {
49
64
  return apiResult;
@@ -85,7 +100,7 @@ function request(_x, _x2) {
85
100
  }
86
101
  function _request() {
87
102
  _request = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(url, options) {
88
- var _globalConfig$request, _globalConfig$request2, err, option, list, optionsWrapper, responseWrapper, params, res;
103
+ var _globalConfig$request, _globalConfig$request2, _params$options, _params$options2, err, option, list, optionsWrapper, responseWrapper, params, res;
89
104
  return _regeneratorRuntime().wrap(function _callee$(_context) {
90
105
  while (1) switch (_context.prev = _context.next) {
91
106
  case 0:
@@ -115,20 +130,35 @@ function _request() {
115
130
  url: url,
116
131
  options: options
117
132
  };
118
- _context.next = 13;
133
+ if (((_params$options = params.options) === null || _params$options === void 0 ? void 0 : _params$options.responseType) === 'blob') {
134
+ params.options.getResponse = true;
135
+ }
136
+ _context.next = 14;
119
137
  return req(params.url, params.options);
120
- case 13:
138
+ case 14:
121
139
  res = _context.sent;
140
+ if (!(((_params$options2 = params.options) === null || _params$options2 === void 0 ? void 0 : _params$options2.responseType) === 'blob')) {
141
+ _context.next = 21;
142
+ break;
143
+ }
144
+ if (!util.isBlob(res.data)) {
145
+ _context.next = 20;
146
+ break;
147
+ }
148
+ return _context.abrupt("return", downloadFile(res, params.options.fileName));
149
+ case 20:
150
+ res = res.data;
151
+ case 21:
122
152
  return _context.abrupt("return", responseWrapper ? responseWrapper(url, responseErrorHandler(url, res, options === null || options === void 0 ? void 0 : options.errorTitle, options === null || options === void 0 ? void 0 : options.notifyType)) : responseErrorHandler(url, res, options === null || options === void 0 ? void 0 : options.errorTitle, options === null || options === void 0 ? void 0 : options.notifyType));
123
- case 17:
124
- _context.prev = 17;
153
+ case 24:
154
+ _context.prev = 24;
125
155
  _context.t0 = _context["catch"](0);
126
156
  return _context.abrupt("return", null);
127
- case 20:
157
+ case 27:
128
158
  case "end":
129
159
  return _context.stop();
130
160
  }
131
- }, _callee, null, [[0, 17]]);
161
+ }, _callee, null, [[0, 24]]);
132
162
  }));
133
163
  return _request.apply(this, arguments);
134
164
  }
@@ -0,0 +1,2 @@
1
+ declare const _default: () => JSX.Element;
2
+ export default _default;
@@ -0,0 +1,25 @@
1
+ import { Button } from 'antd';
2
+ import React from 'react';
3
+ import { ajax } from '@zgfe/business-lib';
4
+ import { DemoWrapper } from '@zgfe/business-lib';
5
+ export default (function () {
6
+ var onDownload = function onDownload() {
7
+ ajax('/zg/web/v2/openapi/label/excel/export', {
8
+ method: 'post',
9
+ data: {
10
+ appId: 501648,
11
+ platform: 0,
12
+ pageNo: 1,
13
+ pageSize: 10
14
+ },
15
+ responseType: 'blob',
16
+ fileName: 'ceshi',
17
+ getResponse: true
18
+ }).then(function () {});
19
+ };
20
+ return /*#__PURE__*/React.createElement(DemoWrapper, {
21
+ needMeta: true
22
+ }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Button, {
23
+ onClick: onDownload
24
+ }, "\u4E0B\u8F7D\u6587\u4EF6")));
25
+ });
@@ -7,6 +7,7 @@ declare let util: {
7
7
  isNumber(obj: any): boolean;
8
8
  isDate(obj: any): boolean;
9
9
  isFunction(obj: any): boolean;
10
+ isBlob(obj: any): boolean;
10
11
  isEqual(a?: {}, b?: {}): boolean;
11
12
  equal(a: any, b: any): boolean;
12
13
  random(min: number, max: number): number;
package/es/utils/util.js CHANGED
@@ -22,6 +22,9 @@ var util = {
22
22
  isFunction: function isFunction(obj) {
23
23
  return this.type(obj) === '[object Function]';
24
24
  },
25
+ isBlob: function isBlob(obj) {
26
+ return this.type(obj) === '[object Blob]';
27
+ },
25
28
  isEqual: function isEqual() {
26
29
  var a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
27
30
  var b = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zgfe/business-lib",
3
- "version": "1.1.53",
3
+ "version": "1.1.54",
4
4
  "scripts": {
5
5
  "start": "dumi dev",
6
6
  "docs:build": "dumi build",
@@ -60,5 +60,5 @@
60
60
  "react": "^16.12.0 || ^17.0.0",
61
61
  "yorkie": "^2.0.0"
62
62
  },
63
- "gitHead": "8b0de5490014d6a853e82ec29c55543533f881d1"
63
+ "gitHead": "e9ce0312ab0ccfee25abf8221aaccc95db814bab"
64
64
  }