ls-pro-common 1.0.0

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.
@@ -0,0 +1,306 @@
1
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
2
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
3
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
4
+ import { extend } from 'umi-request';
5
+ import { getCache, getUrlQuery, showError } from '../utils'; //默认超时时间为1分钟
6
+
7
+ var request = extend({
8
+ timeout: 60000
9
+ });
10
+ /** 请求拦截器,统一添加token */
11
+
12
+ request.interceptors.request.use(function (url, options) {
13
+ var token = getCache("token");
14
+
15
+ var opts = _objectSpread({}, options);
16
+
17
+ if (token) {
18
+ Object.assign(opts, {
19
+ headers: {
20
+ token: token
21
+ }
22
+ });
23
+ }
24
+
25
+ return {
26
+ url: url,
27
+ options: opts
28
+ };
29
+ });
30
+ /** 请求返回拦截器,统一处理业务异常 */
31
+
32
+ request.interceptors.response.use( /*#__PURE__*/function () {
33
+ var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(response, options) {
34
+ var noMsg, data, flag, status;
35
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
36
+ while (1) {
37
+ switch (_context.prev = _context.next) {
38
+ case 0:
39
+ noMsg = getUrlQuery('noMsg', options.url);
40
+
41
+ if (!(response.status < 200 || response.status >= 300)) {
42
+ _context.next = 5;
43
+ break;
44
+ }
45
+
46
+ showError(options.url + response.statusText);
47
+ _context.next = 12;
48
+ break;
49
+
50
+ case 5:
51
+ if (!(noMsg != '1')) {
52
+ _context.next = 12;
53
+ break;
54
+ }
55
+
56
+ _context.next = 8;
57
+ return response.clone().json();
58
+
59
+ case 8:
60
+ data = _context.sent;
61
+ flag = data.flag;
62
+ status = (getUrlQuery('apiStatus') || '0').split(',');
63
+
64
+ if (flag && !status.includes(flag.retCode || '')) {
65
+ showError(flag.retMsg || '访问接口出错');
66
+ }
67
+
68
+ case 12:
69
+ return _context.abrupt("return", response);
70
+
71
+ case 13:
72
+ case "end":
73
+ return _context.stop();
74
+ }
75
+ }
76
+ }, _callee);
77
+ }));
78
+
79
+ return function (_x, _x2) {
80
+ return _ref.apply(this, arguments);
81
+ };
82
+ }());
83
+ /**
84
+ * get请求
85
+ * @param url 接口
86
+ * @param params 参数{key:value}
87
+ * @returns Promise<ApiResponse>
88
+ */
89
+
90
+ export function httpGet(_x3) {
91
+ return _httpGet.apply(this, arguments);
92
+ }
93
+ /**
94
+ * post请求
95
+ * @param url 接口
96
+ * @param data 参数{key:value}
97
+ * @param isJson json请求还是form请求,默认为json请求
98
+ * @returns Promise<ApiResponse>
99
+ */
100
+
101
+ function _httpGet() {
102
+ _httpGet = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(url) {
103
+ var params,
104
+ _args2 = arguments;
105
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
106
+ while (1) {
107
+ switch (_context2.prev = _context2.next) {
108
+ case 0:
109
+ params = _args2.length > 1 && _args2[1] !== undefined ? _args2[1] : {};
110
+ return _context2.abrupt("return", request.get(url, {
111
+ params: params
112
+ }));
113
+
114
+ case 2:
115
+ case "end":
116
+ return _context2.stop();
117
+ }
118
+ }
119
+ }, _callee2);
120
+ }));
121
+ return _httpGet.apply(this, arguments);
122
+ }
123
+
124
+ export function httpPost(_x4) {
125
+ return _httpPost.apply(this, arguments);
126
+ }
127
+ /**
128
+ * put 请求
129
+ * @param url 接口
130
+ * @param data 参数{key:value}
131
+ * @returns Promise<ApiResponse>
132
+ */
133
+
134
+ function _httpPost() {
135
+ _httpPost = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(url) {
136
+ var data,
137
+ isJson,
138
+ _args3 = arguments;
139
+ return _regeneratorRuntime.wrap(function _callee3$(_context3) {
140
+ while (1) {
141
+ switch (_context3.prev = _context3.next) {
142
+ case 0:
143
+ data = _args3.length > 1 && _args3[1] !== undefined ? _args3[1] : {};
144
+ isJson = _args3.length > 2 && _args3[2] !== undefined ? _args3[2] : true;
145
+ return _context3.abrupt("return", request.post(url, {
146
+ data: data,
147
+ requestType: isJson ? 'json' : 'form'
148
+ }));
149
+
150
+ case 3:
151
+ case "end":
152
+ return _context3.stop();
153
+ }
154
+ }
155
+ }, _callee3);
156
+ }));
157
+ return _httpPost.apply(this, arguments);
158
+ }
159
+
160
+ export function httpPut(_x5) {
161
+ return _httpPut.apply(this, arguments);
162
+ }
163
+ /**
164
+ * delete 请求
165
+ * @param url 接口
166
+ * @param data 参数[]
167
+ * @returns Promise<ApiResponse>
168
+ */
169
+
170
+ function _httpPut() {
171
+ _httpPut = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(url) {
172
+ var data,
173
+ _args4 = arguments;
174
+ return _regeneratorRuntime.wrap(function _callee4$(_context4) {
175
+ while (1) {
176
+ switch (_context4.prev = _context4.next) {
177
+ case 0:
178
+ data = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : {};
179
+ return _context4.abrupt("return", request.put(url, {
180
+ data: data
181
+ }));
182
+
183
+ case 2:
184
+ case "end":
185
+ return _context4.stop();
186
+ }
187
+ }
188
+ }, _callee4);
189
+ }));
190
+ return _httpPut.apply(this, arguments);
191
+ }
192
+
193
+ export function httpDelete(_x6, _x7) {
194
+ return _httpDelete.apply(this, arguments);
195
+ }
196
+ /**
197
+ * 读取数据字典
198
+ * @param dictCode 字典编码
199
+ * @returns Promise<Record<string,string>>
200
+ */
201
+
202
+ function _httpDelete() {
203
+ _httpDelete = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5(url, data) {
204
+ return _regeneratorRuntime.wrap(function _callee5$(_context5) {
205
+ while (1) {
206
+ switch (_context5.prev = _context5.next) {
207
+ case 0:
208
+ return _context5.abrupt("return", request.delete(url, {
209
+ data: data
210
+ }));
211
+
212
+ case 1:
213
+ case "end":
214
+ return _context5.stop();
215
+ }
216
+ }
217
+ }, _callee5);
218
+ }));
219
+ return _httpDelete.apply(this, arguments);
220
+ }
221
+
222
+ export function getDict(_x8) {
223
+ return _getDict.apply(this, arguments);
224
+ }
225
+ /**
226
+ * 加载下拉框的数据源
227
+ * @param url 后端接口
228
+ * @param param 请求参数
229
+ * @param valueField 值字段
230
+ * @param labelField 显示字段
231
+ * @param showValue 显示值
232
+ * @returns
233
+ */
234
+
235
+ function _getDict() {
236
+ _getDict = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6(dictCode) {
237
+ var api, param;
238
+ return _regeneratorRuntime.wrap(function _callee6$(_context6) {
239
+ while (1) {
240
+ switch (_context6.prev = _context6.next) {
241
+ case 0:
242
+ api = '/lesoon-integration/sysDictDtl';
243
+ param = {
244
+ where: {
245
+ dictCode: dictCode
246
+ },
247
+ ifPage: 0
248
+ };
249
+ return _context6.abrupt("return", fetchOptions(api, param, 'dictValue', 'displayName'));
250
+
251
+ case 3:
252
+ case "end":
253
+ return _context6.stop();
254
+ }
255
+ }
256
+ }, _callee6);
257
+ }));
258
+ return _getDict.apply(this, arguments);
259
+ }
260
+
261
+ export function fetchOptions(_x9, _x10, _x11, _x12) {
262
+ return _fetchOptions.apply(this, arguments);
263
+ }
264
+
265
+ function _fetchOptions() {
266
+ _fetchOptions = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(url, param, valueField, labelField) {
267
+ var showValue,
268
+ _yield$httpGet,
269
+ _yield$httpGet$rows,
270
+ rows,
271
+ data,
272
+ _args7 = arguments;
273
+
274
+ return _regeneratorRuntime.wrap(function _callee7$(_context7) {
275
+ while (1) {
276
+ switch (_context7.prev = _context7.next) {
277
+ case 0:
278
+ showValue = _args7.length > 4 && _args7[4] !== undefined ? _args7[4] : true;
279
+ _context7.next = 3;
280
+ return httpGet(url, param);
281
+
282
+ case 3:
283
+ _yield$httpGet = _context7.sent;
284
+ _yield$httpGet$rows = _yield$httpGet.rows;
285
+ rows = _yield$httpGet$rows === void 0 ? [] : _yield$httpGet$rows;
286
+ data = (rows || []).map(function (o) {
287
+ return {
288
+ value: o[valueField],
289
+ text: o[labelField],
290
+ label: (showValue ? o[valueField] + '→' : '') + o[labelField],
291
+ status: o.status
292
+ };
293
+ });
294
+ return _context7.abrupt("return", data);
295
+
296
+ case 8:
297
+ case "end":
298
+ return _context7.stop();
299
+ }
300
+ }
301
+ }, _callee7);
302
+ }));
303
+ return _fetchOptions.apply(this, arguments);
304
+ }
305
+
306
+ export default request;
package/es/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ import EditModalForm from './components/EditModalForm';
2
+ import BaseService from './service/BaseService';
3
+ import request, { httpGet, httpPut, httpPost, httpDelete, getDict, fetchOptions } from './http';
4
+ import * as utils from './utils';
5
+ /** Hooks */
6
+ import useSingle from './hooks/useSingle';
7
+ /** Type */
8
+ import type { ApiResponse, TableToolbar, BaseApiType, MethodType } from './typing';
9
+ export type { ApiResponse, TableToolbar, BaseApiType, MethodType };
10
+ export { EditModalForm, BaseService, request, httpDelete, httpGet, httpPost, httpPut, getDict, fetchOptions, utils, useSingle };
package/es/index.js ADDED
@@ -0,0 +1,8 @@
1
+ import EditModalForm from './components/EditModalForm';
2
+ import BaseService from './service/BaseService';
3
+ import request, { httpGet, httpPut, httpPost, httpDelete, getDict, fetchOptions } from './http';
4
+ import * as utils from './utils';
5
+ /** Hooks */
6
+
7
+ import useSingle from './hooks/useSingle';
8
+ export { EditModalForm, BaseService, request, httpDelete, httpGet, httpPost, httpPut, getDict, fetchOptions, utils, useSingle };
@@ -0,0 +1,15 @@
1
+ import type { BaseApiType, MethodType } from '../typing';
2
+ /** 基本增删改查服务 */
3
+ declare class BaseService {
4
+ /** 定义每个服务的api */
5
+ api: BaseApiType;
6
+ /** 定义每个服务接口对应的请求方法,默认为 restful */
7
+ method: MethodType;
8
+ /** 加载数据方法 @param param 请求参数对象 */
9
+ load(param: Record<string, any>): Promise<any>;
10
+ /** 保存数据方法 @param data 对象数据,当存在id时即为更改 */
11
+ save(data: Record<string, any>): Promise<any>;
12
+ /** 批量删除对象方法,@param data id集合 */
13
+ remove(data: any[]): Promise<any>;
14
+ }
15
+ export default BaseService;
@@ -0,0 +1,202 @@
1
+ import _regeneratorRuntime from "@babel/runtime/regenerator";
2
+ import "antd/es/message/style";
3
+ import _message from "antd/es/message";
4
+ import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
5
+ import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
6
+ import _createClass from "@babel/runtime/helpers/esm/createClass";
7
+ import { httpGet, httpPut, httpPost, httpDelete } from '../http';
8
+ /** 基本增删改查服务 */
9
+
10
+ var BaseService = /*#__PURE__*/function () {
11
+ function BaseService() {
12
+ _classCallCheck(this, BaseService);
13
+
14
+ this.api = {};
15
+ this.method = {
16
+ get: 'get',
17
+ edit: 'put',
18
+ delete: 'delete'
19
+ };
20
+ }
21
+
22
+ _createClass(BaseService, [{
23
+ key: "load",
24
+ value:
25
+ /** 加载数据方法 @param param 请求参数对象 */
26
+ function () {
27
+ var _load = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(param) {
28
+ var _this$api;
29
+
30
+ var url;
31
+ return _regeneratorRuntime.wrap(function _callee$(_context) {
32
+ while (1) {
33
+ switch (_context.prev = _context.next) {
34
+ case 0:
35
+ url = (_this$api = this.api) === null || _this$api === void 0 ? void 0 : _this$api.load;
36
+
37
+ if (url) {
38
+ _context.next = 4;
39
+ break;
40
+ }
41
+
42
+ _message.error("加载方法没有配置api");
43
+
44
+ return _context.abrupt("return", Promise.reject("加载方法没有配置api"));
45
+
46
+ case 4:
47
+ if (!(this.method.get === 'post')) {
48
+ _context.next = 8;
49
+ break;
50
+ }
51
+
52
+ _context.next = 7;
53
+ return httpPost(url, param);
54
+
55
+ case 7:
56
+ return _context.abrupt("return", _context.sent);
57
+
58
+ case 8:
59
+ _context.next = 10;
60
+ return httpGet(url, param);
61
+
62
+ case 10:
63
+ return _context.abrupt("return", _context.sent);
64
+
65
+ case 11:
66
+ case "end":
67
+ return _context.stop();
68
+ }
69
+ }
70
+ }, _callee, this);
71
+ }));
72
+
73
+ function load(_x) {
74
+ return _load.apply(this, arguments);
75
+ }
76
+
77
+ return load;
78
+ }()
79
+ /** 保存数据方法 @param data 对象数据,当存在id时即为更改 */
80
+
81
+ }, {
82
+ key: "save",
83
+ value: function () {
84
+ var _save = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(data) {
85
+ var _this$api2;
86
+
87
+ var url;
88
+ return _regeneratorRuntime.wrap(function _callee2$(_context2) {
89
+ while (1) {
90
+ switch (_context2.prev = _context2.next) {
91
+ case 0:
92
+ url = (_this$api2 = this.api) === null || _this$api2 === void 0 ? void 0 : _this$api2.add;
93
+
94
+ if (data.id && this.api.edit) {
95
+ url = this.api.edit;
96
+ }
97
+
98
+ if (url) {
99
+ _context2.next = 5;
100
+ break;
101
+ }
102
+
103
+ _message.error("保存方法没有配置api");
104
+
105
+ return _context2.abrupt("return", Promise.reject("保存方法没有配置api"));
106
+
107
+ case 5:
108
+ if (!(data.id && this.method.edit === 'put')) {
109
+ _context2.next = 9;
110
+ break;
111
+ }
112
+
113
+ _context2.next = 8;
114
+ return httpPut(url, data);
115
+
116
+ case 8:
117
+ return _context2.abrupt("return", _context2.sent);
118
+
119
+ case 9:
120
+ _context2.next = 11;
121
+ return httpPost(url, data);
122
+
123
+ case 11:
124
+ return _context2.abrupt("return", _context2.sent);
125
+
126
+ case 12:
127
+ case "end":
128
+ return _context2.stop();
129
+ }
130
+ }
131
+ }, _callee2, this);
132
+ }));
133
+
134
+ function save(_x2) {
135
+ return _save.apply(this, arguments);
136
+ }
137
+
138
+ return save;
139
+ }()
140
+ /** 批量删除对象方法,@param data id集合 */
141
+
142
+ }, {
143
+ key: "remove",
144
+ value: function () {
145
+ var _remove = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(data) {
146
+ var _this$api3;
147
+
148
+ var url;
149
+ return _regeneratorRuntime.wrap(function _callee3$(_context3) {
150
+ while (1) {
151
+ switch (_context3.prev = _context3.next) {
152
+ case 0:
153
+ url = (_this$api3 = this.api) === null || _this$api3 === void 0 ? void 0 : _this$api3.delete;
154
+
155
+ if (url) {
156
+ _context3.next = 4;
157
+ break;
158
+ }
159
+
160
+ _message.error("加载方法没有配置api");
161
+
162
+ return _context3.abrupt("return", Promise.reject("加载方法没有配置api"));
163
+
164
+ case 4:
165
+ if (!(this.method.delete === 'post')) {
166
+ _context3.next = 8;
167
+ break;
168
+ }
169
+
170
+ _context3.next = 7;
171
+ return httpPost(url, data);
172
+
173
+ case 7:
174
+ return _context3.abrupt("return", _context3.sent);
175
+
176
+ case 8:
177
+ _context3.next = 10;
178
+ return httpDelete(url, data);
179
+
180
+ case 10:
181
+ return _context3.abrupt("return", _context3.sent);
182
+
183
+ case 11:
184
+ case "end":
185
+ return _context3.stop();
186
+ }
187
+ }
188
+ }, _callee3, this);
189
+ }));
190
+
191
+ function remove(_x3) {
192
+ return _remove.apply(this, arguments);
193
+ }
194
+
195
+ return remove;
196
+ }()
197
+ }]);
198
+
199
+ return BaseService;
200
+ }();
201
+
202
+ export default BaseService;
package/es/typing.d.ts ADDED
@@ -0,0 +1,43 @@
1
+ /** @name 接口返回值 */
2
+ export declare type ApiResponse = Record<string, any> & {
3
+ flag?: {
4
+ retCode?: string;
5
+ retMsg?: string;
6
+ retDetail?: string;
7
+ };
8
+ rows?: [];
9
+ data?: any;
10
+ total?: 0;
11
+ };
12
+ export declare type TableToolbar = {
13
+ add?: boolean;
14
+ edit?: boolean;
15
+ remove?: boolean;
16
+ export?: boolean;
17
+ import?: boolean;
18
+ print?: boolean;
19
+ };
20
+ /** 基础模块接口类型 */
21
+ export declare type BaseApiType = {
22
+ /**加载数据api */
23
+ load?: string;
24
+ /**新增数据api */
25
+ add?: string;
26
+ /**更改数据api,如果跟新增接口一样,可不指定 */
27
+ edit?: string;
28
+ /**删除数据api */
29
+ delete?: string;
30
+ /**导入数据api */
31
+ import?: string;
32
+ /**导出数据api,如果跟加载数据api一致,可不指定 */
33
+ export?: string;
34
+ };
35
+ /**请求后端接口方法 */
36
+ export declare type MethodType = {
37
+ /**加载数据对应的方法,默认为get, 如果请求参数太长时,后端可能会改为post */
38
+ get: 'get' | 'post';
39
+ /** 更改数据的请求方法,默认为 put, 也可以是post,根据后端接口而定 */
40
+ edit: 'put' | 'post';
41
+ /** 删除数据的请求方法,默认为delete, 也可以是post,根据后端接口而定 */
42
+ delete: 'delete' | 'post';
43
+ };
package/es/typing.js ADDED
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,80 @@
1
+ export declare const getUrlQuery: (name: string, url?: string) => string;
2
+ /**
3
+ * 设置url传参
4
+ * @param {*} url
5
+ * @param {*} keyvals
6
+ * @returns
7
+ */
8
+ export declare const setUrlQuery: (url: string, keyvals?: Record<string, any>) => string;
9
+ /**
10
+ * 设置文档title
11
+ * @param {*} title
12
+ * @returns
13
+ */
14
+ export declare const setTitle: (title: string) => void;
15
+ /**
16
+ * 获取Cookie
17
+ * @param {String} name cookie名
18
+ * @returns
19
+ */
20
+ export declare const getCookie: (name: string) => string | null;
21
+ /**
22
+ * 设置Cookie
23
+ * @param {*} key Cookie名称
24
+ * @param {*} value Cookie 值
25
+ * @param { Number } day 有效天数 默认1天,
26
+ */
27
+ export declare const setCookie: (key: string, value: string | number, day?: number) => void;
28
+ /**
29
+ * 判断是否登录
30
+ * @returns
31
+ */
32
+ export declare const isLogin: () => boolean;
33
+ /**
34
+ * 设置本地缓存
35
+ * @param { String } key 关键字
36
+ * @param { Object } data 值
37
+ */
38
+ export declare const setCache: (key: string, data: any) => void;
39
+ /**
40
+ * 读取本地缓存
41
+ * @param { String } key 关键字
42
+ * @returns 关键字对应的值
43
+ */
44
+ export declare const getCache: (key: string) => string | null;
45
+ /**
46
+ * 简单日期格式化
47
+ * @param {*} dt 传入时间参数,若不传取当前时间
48
+ * @param {*} showTime 是否返回当前时间,默认返回时间
49
+ * @returns xxxx年-xx月-xx日 [xx时:xx分:xx秒]
50
+ */
51
+ export declare const dateFormat: (dt: Date | undefined | null, showTime?: boolean) => string;
52
+ /** @name 显示错误 */
53
+ export declare const showError: (text: string) => void;
54
+ /** @name 显示警示 */
55
+ export declare const showWarn: (text: string) => void;
56
+ /** @name 显示成功 */
57
+ export declare const showSuccess: (text: string) => void;
58
+ /** @name 显示确认 */
59
+ export declare const showConfirm: (text: string, title?: string) => Promise<unknown>;
60
+ /**
61
+ * 日期组选择转换
62
+ * @param values 选择的值
63
+ * @param startField 第一个字段
64
+ * @param endField 第二个字段
65
+ * @param endSuffix
66
+ * @returns
67
+ */
68
+ export declare const rangeToSearch: (values: any, startField: string, endField: string, endSuffix?: string) => any;
69
+ /** @name 状态列表 */
70
+ export declare const statusList: {
71
+ value: number;
72
+ text: string;
73
+ label: string;
74
+ }[];
75
+ /** @name 是否列表 */
76
+ export declare const yesnoList: {
77
+ value: number;
78
+ text: string;
79
+ label: string;
80
+ }[];