jiezhangxin-utils 0.0.2 → 0.0.3
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/.gitattributes +19 -0
- package/README.md +80 -42
- package/dist/jiezhangxin-utils.cjs.js +2 -370
- package/dist/jiezhangxin-utils.cjs.js.map +1 -0
- package/dist/jiezhangxin-utils.esm.js +2 -348
- package/dist/jiezhangxin-utils.esm.js.map +1 -0
- package/dist/jiezhangxin-utils.umd.js +2 -1
- package/dist/jiezhangxin-utils.umd.js.map +1 -0
- package/package.json +36 -7
- package/src/cache/cookies.js +152 -0
- package/src/constants.js +30 -0
- package/src/directives/input-limit.js +100 -0
- package/src/index.js +8 -10
- package/src/scrollToId/scrollToId.js +47 -0
- package/src/utils.js +726 -0
- package/src/array/arrayUtils.js +0 -103
- package/src/date/dateUtils.js +0 -75
- package/src/storage/storageUtils.js +0 -105
- package/src/string/stringUtils.js +0 -73
package/.gitattributes
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# 设置默认换行符
|
|
2
|
+
* text=auto eol=lf
|
|
3
|
+
|
|
4
|
+
# 特定文件类型设置
|
|
5
|
+
*.js text eol=lf
|
|
6
|
+
*.ts text eol=lf
|
|
7
|
+
*.json text eol=lf
|
|
8
|
+
*.md text eol=lf
|
|
9
|
+
*.html text eol=lf
|
|
10
|
+
*.css text eol=lf
|
|
11
|
+
*.vue text eol=lf
|
|
12
|
+
|
|
13
|
+
# 二进制文件不转换
|
|
14
|
+
*.png binary
|
|
15
|
+
*.jpg binary
|
|
16
|
+
*.gif binary
|
|
17
|
+
*.pdf binary
|
|
18
|
+
*.woff binary
|
|
19
|
+
*.woff2 binary
|
package/README.md
CHANGED
|
@@ -1,63 +1,101 @@
|
|
|
1
1
|
# jiezhangxin-utils
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
A utility toolkit for Vue 3 projects, providing common functions for date handling, string manipulation, array processing, and storage management.
|
|
3
|
+
A utility toolkit for Vue 3 projects, supporting tree-shaking and按需引入.
|
|
6
4
|
|
|
7
5
|
## Installation
|
|
8
|
-
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install jiezhangxin-utils
|
|
9
|
+
# or
|
|
10
|
+
yarn add jiezhangxin-utils
|
|
11
|
+
# or
|
|
12
|
+
pnpm add jiezhangxin-utils
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Features
|
|
16
|
+
|
|
17
|
+
- ✅ **按需引入**:支持ES模块tree-shaking
|
|
18
|
+
- ✅ **多种格式**:CJS、ESM、UMD
|
|
19
|
+
- ✅ **Vue 3友好**:兼容Vue 3项目
|
|
20
|
+
- ✅ **类型安全**:良好的类型提示
|
|
21
|
+
|
|
9
22
|
## Usage
|
|
10
23
|
|
|
11
|
-
###
|
|
12
|
-
|
|
13
|
-
|
|
24
|
+
### 整体引入
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
import * as utils from 'jiezhangxin-utils';
|
|
28
|
+
|
|
29
|
+
// 使用工具函数
|
|
30
|
+
utils.formatMoney(1000);
|
|
31
|
+
utils.handleViewFile('https://example.com/file.pdf');
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
### 按需引入(推荐)
|
|
35
|
+
|
|
36
|
+
```javascript
|
|
37
|
+
import { formatMoney, handleViewFile } from 'jiezhangxin-utils';
|
|
38
|
+
|
|
39
|
+
// 只引入需要的函数
|
|
40
|
+
formatMoney(1000);
|
|
41
|
+
handleViewFile('https://example.com/file.pdf');
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### 按需引入具体模块
|
|
45
|
+
|
|
46
|
+
```javascript
|
|
47
|
+
// 引入工具函数
|
|
48
|
+
import { formatMoney } from 'jiezhangxin-utils';
|
|
14
49
|
|
|
15
|
-
//
|
|
16
|
-
import {
|
|
17
|
-
### Example
|
|
18
|
-
// Format date
|
|
19
|
-
console.log(formatDate(new Date(), 'yyyy-MM-dd HH:mm:ss'));
|
|
50
|
+
// 引入cookie相关函数
|
|
51
|
+
import { getToken, setToken } from 'jiezhangxin-utils';
|
|
20
52
|
|
|
21
|
-
//
|
|
22
|
-
|
|
53
|
+
// 引入常量
|
|
54
|
+
import { CacheKey } from 'jiezhangxin-utils';
|
|
55
|
+
```
|
|
23
56
|
|
|
24
|
-
|
|
25
|
-
console.log(uniqueArray([1, 2, 2, 3, 3, 3])); // Output: [1, 2, 3]
|
|
57
|
+
## API
|
|
26
58
|
|
|
27
|
-
|
|
28
|
-
setLocalStorage('userInfo', { name: 'John', age: 30 });
|
|
29
|
-
console.log(getLocalStorage('userInfo'));
|
|
30
|
-
## API Documentation
|
|
59
|
+
### 工具函数
|
|
31
60
|
|
|
32
|
-
|
|
61
|
+
- `formatAmountToUnits(amount)` - 将金额格式化为单位数组
|
|
62
|
+
- `getCurrentMonth()` - 获取当前月份
|
|
63
|
+
- `formatMoney(money, decimalPlaces)` - 格式化金额,添加千分位
|
|
64
|
+
- `handleViewFile(fileUrl)` - 安全打开远程文件/PDF
|
|
65
|
+
- `exportFile(exportLoading, exportUrl, name)` - 导出文件
|
|
66
|
+
- `postExportFile(exportLoading, exportUrl, name, params)` - POST方式导出文件
|
|
67
|
+
- `getFileTypeFromUrlRegex(url)` - 从URL获取文件类型
|
|
33
68
|
|
|
34
|
-
|
|
35
|
-
- `getDaysBetween(startDate, endDate)`: Get days between two dates
|
|
36
|
-
- `isToday(date)`: Check if date is today
|
|
69
|
+
### Cookie相关
|
|
37
70
|
|
|
38
|
-
|
|
71
|
+
- `getToken()` - 获取token
|
|
72
|
+
- `setToken(token)` - 设置token
|
|
73
|
+
- `removeToken()` - 移除token
|
|
74
|
+
- `getUserInfo()` - 获取用户信息
|
|
75
|
+
- `setUserInfo(userInfo)` - 设置用户信息
|
|
76
|
+
- `removeUserInfo()` - 移除用户信息
|
|
77
|
+
- `getCompanyCode()` - 获取公司编码
|
|
78
|
+
- `setCompanyCode(code)` - 设置公司编码
|
|
79
|
+
- `removeCompanyCode()` - 移除公司编码
|
|
80
|
+
- `getCompanyName()` - 获取公司名称
|
|
81
|
+
- `setCompanyName(name)` - 设置公司名称
|
|
82
|
+
- `removeCompanyName()` - 移除公司名称
|
|
39
83
|
|
|
40
|
-
|
|
41
|
-
- `capitalize(str)`: Capitalize the first letter of string
|
|
42
|
-
- `maskStr(str, startLen, endLen, replaceChar)`: Mask string for privacy
|
|
43
|
-
- `isEmail(str)`: Check if string is a valid email
|
|
44
|
-
- `isPhone(str)`: Check if string is a valid phone number
|
|
84
|
+
### 常量
|
|
45
85
|
|
|
46
|
-
|
|
86
|
+
- `CacheKey` - Cookie缓存键名常量
|
|
87
|
+
- `publicKey` - 公钥
|
|
88
|
+
- `version` - 工具库版本
|
|
47
89
|
|
|
48
|
-
|
|
49
|
-
- `sortArray(arr, key, isAsc)`: Sort array
|
|
50
|
-
- `groupArray(arr, key)`: Group array by key
|
|
51
|
-
- `flattenArray(arr)`: Flatten nested array
|
|
90
|
+
## Build
|
|
52
91
|
|
|
53
|
-
|
|
92
|
+
```bash
|
|
93
|
+
# 构建生产版本
|
|
94
|
+
npm run build
|
|
54
95
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
- `getSessionStorage(key)`: Get item from sessionStorage
|
|
59
|
-
- `setSessionStorage(key, value)`: Set item to sessionStorage
|
|
60
|
-
- `removeSessionStorage(key)`: Remove item from sessionStorage
|
|
96
|
+
# 开发模式(监听文件变化)
|
|
97
|
+
npm run dev
|
|
98
|
+
```
|
|
61
99
|
|
|
62
100
|
## License
|
|
63
101
|
|
|
@@ -1,370 +1,2 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* 格式化日期
|
|
7
|
-
* @param {Date|string|number} date - 日期
|
|
8
|
-
* @param {string} format - 格式字符串,例如:'yyyy-MM-dd HH:mm:ss'
|
|
9
|
-
* @returns {string} 格式化后的日期字符串
|
|
10
|
-
*/
|
|
11
|
-
function formatDate(date, format = 'yyyy-MM-dd') {
|
|
12
|
-
if (!date) {
|
|
13
|
-
return '';
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
// 处理日期参数
|
|
17
|
-
if (!(date instanceof Date)) {
|
|
18
|
-
date = new Date(date);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// 检查日期是否有效
|
|
22
|
-
if (isNaN(date.getTime())) {
|
|
23
|
-
return '';
|
|
24
|
-
}
|
|
25
|
-
const o = {
|
|
26
|
-
'M+': date.getMonth() + 1,
|
|
27
|
-
// 月份
|
|
28
|
-
'd+': date.getDate(),
|
|
29
|
-
// 日
|
|
30
|
-
'H+': date.getHours(),
|
|
31
|
-
// 小时
|
|
32
|
-
'm+': date.getMinutes(),
|
|
33
|
-
// 分
|
|
34
|
-
's+': date.getSeconds(),
|
|
35
|
-
// 秒
|
|
36
|
-
'q+': Math.floor((date.getMonth() + 3) / 3),
|
|
37
|
-
// 季度
|
|
38
|
-
'S': date.getMilliseconds() // 毫秒
|
|
39
|
-
};
|
|
40
|
-
if (/(y+)/.test(format)) {
|
|
41
|
-
format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
|
|
42
|
-
}
|
|
43
|
-
for (const k in o) {
|
|
44
|
-
if (new RegExp('(' + k + ')').test(format)) {
|
|
45
|
-
format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
return format;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* 获取两个日期之间的天数差
|
|
53
|
-
* @param {Date|string|number} startDate - 开始日期
|
|
54
|
-
* @param {Date|string|number} endDate - 结束日期
|
|
55
|
-
* @returns {number} 天数差
|
|
56
|
-
*/
|
|
57
|
-
function getDaysBetween(startDate, endDate) {
|
|
58
|
-
const start = new Date(startDate);
|
|
59
|
-
const end = new Date(endDate);
|
|
60
|
-
if (isNaN(start.getTime()) || isNaN(end.getTime())) {
|
|
61
|
-
return 0;
|
|
62
|
-
}
|
|
63
|
-
const oneDay = 24 * 60 * 60 * 1000; // 毫秒
|
|
64
|
-
return Math.round(Math.abs((end - start) / oneDay));
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* 判断是否为今天
|
|
69
|
-
* @param {Date|string|number} date - 日期
|
|
70
|
-
* @returns {boolean} 是否为今天
|
|
71
|
-
*/
|
|
72
|
-
function isToday(date) {
|
|
73
|
-
const target = new Date(date);
|
|
74
|
-
const today = new Date();
|
|
75
|
-
return target.getDate() === today.getDate() && target.getMonth() === today.getMonth() && target.getFullYear() === today.getFullYear();
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* 去除字符串首尾空格
|
|
80
|
-
* @param {string} str - 输入字符串
|
|
81
|
-
* @returns {string} 处理后的字符串
|
|
82
|
-
*/
|
|
83
|
-
function trim(str) {
|
|
84
|
-
if (typeof str !== 'string') {
|
|
85
|
-
return '';
|
|
86
|
-
}
|
|
87
|
-
return str.trim();
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* 首字母大写
|
|
92
|
-
* @param {string} str - 输入字符串
|
|
93
|
-
* @returns {string} 处理后的字符串
|
|
94
|
-
*/
|
|
95
|
-
function capitalize(str) {
|
|
96
|
-
if (typeof str !== 'string' || str.length === 0) {
|
|
97
|
-
return '';
|
|
98
|
-
}
|
|
99
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* 字符串脱敏处理
|
|
104
|
-
* @param {string} str - 输入字符串
|
|
105
|
-
* @param {number} startLen - 保留开始长度
|
|
106
|
-
* @param {number} endLen - 保留结束长度
|
|
107
|
-
* @param {string} replaceChar - 替换字符
|
|
108
|
-
* @returns {string} 脱敏后的字符串
|
|
109
|
-
*/
|
|
110
|
-
function maskStr(str, startLen = 3, endLen = 4, replaceChar = '*') {
|
|
111
|
-
if (typeof str !== 'string' || str.length === 0) {
|
|
112
|
-
return '';
|
|
113
|
-
}
|
|
114
|
-
const len = str.length;
|
|
115
|
-
if (startLen + endLen >= len) {
|
|
116
|
-
return str;
|
|
117
|
-
}
|
|
118
|
-
const maskLen = len - startLen - endLen;
|
|
119
|
-
const mask = replaceChar.repeat(maskLen);
|
|
120
|
-
return str.substring(0, startLen) + mask + str.substring(len - endLen);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* 判断字符串是否为邮箱格式
|
|
125
|
-
* @param {string} str - 输入字符串
|
|
126
|
-
* @returns {boolean} 是否为邮箱
|
|
127
|
-
*/
|
|
128
|
-
function isEmail(str) {
|
|
129
|
-
if (typeof str !== 'string') {
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
const reg = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
|
|
133
|
-
return reg.test(str);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* 判断字符串是否为手机号格式
|
|
138
|
-
* @param {string} str - 输入字符串
|
|
139
|
-
* @returns {boolean} 是否为手机号
|
|
140
|
-
*/
|
|
141
|
-
function isPhone(str) {
|
|
142
|
-
if (typeof str !== 'string') {
|
|
143
|
-
return false;
|
|
144
|
-
}
|
|
145
|
-
const reg = /^1[3-9]\d{9}$/;
|
|
146
|
-
return reg.test(str);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* 数组去重
|
|
151
|
-
* @param {Array} arr - 输入数组
|
|
152
|
-
* @returns {Array} 去重后的数组
|
|
153
|
-
*/
|
|
154
|
-
function uniqueArray(arr) {
|
|
155
|
-
if (!Array.isArray(arr)) {
|
|
156
|
-
return [];
|
|
157
|
-
}
|
|
158
|
-
return [...new Set(arr)];
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* 数组排序
|
|
163
|
-
* @param {Array} arr - 输入数组
|
|
164
|
-
* @param {string} key - 排序的键名(对象数组时使用)
|
|
165
|
-
* @param {boolean} isAsc - 是否升序
|
|
166
|
-
* @returns {Array} 排序后的数组
|
|
167
|
-
*/
|
|
168
|
-
function sortArray(arr, key, isAsc = true) {
|
|
169
|
-
if (!Array.isArray(arr)) {
|
|
170
|
-
return [];
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
// 复制数组避免修改原数组
|
|
174
|
-
const newArr = [...arr];
|
|
175
|
-
newArr.sort((a, b) => {
|
|
176
|
-
let valueA = a;
|
|
177
|
-
let valueB = b;
|
|
178
|
-
|
|
179
|
-
// 如果指定了key,说明是对象数组
|
|
180
|
-
if (key) {
|
|
181
|
-
valueA = a[key];
|
|
182
|
-
valueB = b[key];
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// 处理数字情况
|
|
186
|
-
if (typeof valueA === 'number' && typeof valueB === 'number') {
|
|
187
|
-
return isAsc ? valueA - valueB : valueB - valueA;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
// 处理字符串情况
|
|
191
|
-
const strA = String(valueA).toLowerCase();
|
|
192
|
-
const strB = String(valueB).toLowerCase();
|
|
193
|
-
if (strA < strB) {
|
|
194
|
-
return isAsc ? -1 : 1;
|
|
195
|
-
}
|
|
196
|
-
if (strA > strB) {
|
|
197
|
-
return isAsc ? 1 : -1;
|
|
198
|
-
}
|
|
199
|
-
return 0;
|
|
200
|
-
});
|
|
201
|
-
return newArr;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
/**
|
|
205
|
-
* 数组分组
|
|
206
|
-
* @param {Array} arr - 输入数组
|
|
207
|
-
* @param {Function|string} key - 分组依据的函数或键名
|
|
208
|
-
* @returns {Object} 分组后的对象
|
|
209
|
-
*/
|
|
210
|
-
function groupArray(arr, key) {
|
|
211
|
-
if (!Array.isArray(arr)) {
|
|
212
|
-
return {};
|
|
213
|
-
}
|
|
214
|
-
return arr.reduce((result, item) => {
|
|
215
|
-
let groupKey;
|
|
216
|
-
if (typeof key === 'function') {
|
|
217
|
-
groupKey = key(item);
|
|
218
|
-
} else if (typeof key === 'string') {
|
|
219
|
-
groupKey = item[key];
|
|
220
|
-
} else {
|
|
221
|
-
groupKey = item;
|
|
222
|
-
}
|
|
223
|
-
if (!result[groupKey]) {
|
|
224
|
-
result[groupKey] = [];
|
|
225
|
-
}
|
|
226
|
-
result[groupKey].push(item);
|
|
227
|
-
return result;
|
|
228
|
-
}, {});
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* 数组扁平化
|
|
233
|
-
* @param {Array} arr - 输入数组
|
|
234
|
-
* @returns {Array} 扁平化后的数组
|
|
235
|
-
*/
|
|
236
|
-
function flattenArray(arr) {
|
|
237
|
-
if (!Array.isArray(arr)) {
|
|
238
|
-
return [];
|
|
239
|
-
}
|
|
240
|
-
return arr.reduce((result, item) => {
|
|
241
|
-
return result.concat(Array.isArray(item) ? flattenArray(item) : item);
|
|
242
|
-
}, []);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* 从localStorage获取数据
|
|
247
|
-
* @param {string} key - 存储键名
|
|
248
|
-
* @returns {any} 存储的值
|
|
249
|
-
*/
|
|
250
|
-
function getLocalStorage(key) {
|
|
251
|
-
if (!key || typeof key !== 'string') {
|
|
252
|
-
return null;
|
|
253
|
-
}
|
|
254
|
-
try {
|
|
255
|
-
const value = localStorage.getItem(key);
|
|
256
|
-
if (value === null) {
|
|
257
|
-
return null;
|
|
258
|
-
}
|
|
259
|
-
// 尝试解析JSON
|
|
260
|
-
return JSON.parse(value);
|
|
261
|
-
} catch (e) {
|
|
262
|
-
// 如果解析失败,直接返回原始值
|
|
263
|
-
return localStorage.getItem(key);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* 向localStorage存储数据
|
|
269
|
-
* @param {string} key - 存储键名
|
|
270
|
-
* @param {any} value - 存储的值
|
|
271
|
-
* @returns {boolean} 是否存储成功
|
|
272
|
-
*/
|
|
273
|
-
function setLocalStorage(key, value) {
|
|
274
|
-
if (!key || typeof key !== 'string') {
|
|
275
|
-
return false;
|
|
276
|
-
}
|
|
277
|
-
try {
|
|
278
|
-
// 转换为JSON字符串存储
|
|
279
|
-
localStorage.setItem(key, JSON.stringify(value));
|
|
280
|
-
return true;
|
|
281
|
-
} catch (e) {
|
|
282
|
-
return false;
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
/**
|
|
287
|
-
* 从localStorage删除数据
|
|
288
|
-
* @param {string} key - 存储键名
|
|
289
|
-
*/
|
|
290
|
-
function removeLocalStorage(key) {
|
|
291
|
-
if (key && typeof key === 'string') {
|
|
292
|
-
localStorage.removeItem(key);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* 从sessionStorage获取数据
|
|
298
|
-
* @param {string} key - 存储键名
|
|
299
|
-
* @returns {any} 存储的值
|
|
300
|
-
*/
|
|
301
|
-
function getSessionStorage(key) {
|
|
302
|
-
if (!key || typeof key !== 'string') {
|
|
303
|
-
return null;
|
|
304
|
-
}
|
|
305
|
-
try {
|
|
306
|
-
const value = sessionStorage.getItem(key);
|
|
307
|
-
if (value === null) {
|
|
308
|
-
return null;
|
|
309
|
-
}
|
|
310
|
-
// 尝试解析JSON
|
|
311
|
-
return JSON.parse(value);
|
|
312
|
-
} catch (e) {
|
|
313
|
-
// 如果解析失败,直接返回原始值
|
|
314
|
-
return sessionStorage.getItem(key);
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
/**
|
|
319
|
-
* 向sessionStorage存储数据
|
|
320
|
-
* @param {string} key - 存储键名
|
|
321
|
-
* @param {any} value - 存储的值
|
|
322
|
-
* @returns {boolean} 是否存储成功
|
|
323
|
-
*/
|
|
324
|
-
function setSessionStorage(key, value) {
|
|
325
|
-
if (!key || typeof key !== 'string') {
|
|
326
|
-
return false;
|
|
327
|
-
}
|
|
328
|
-
try {
|
|
329
|
-
// 转换为JSON字符串存储
|
|
330
|
-
sessionStorage.setItem(key, JSON.stringify(value));
|
|
331
|
-
return true;
|
|
332
|
-
} catch (e) {
|
|
333
|
-
return false;
|
|
334
|
-
}
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
/**
|
|
338
|
-
* 从sessionStorage删除数据
|
|
339
|
-
* @param {string} key - 存储键名
|
|
340
|
-
*/
|
|
341
|
-
function removeSessionStorage(key) {
|
|
342
|
-
if (key && typeof key === 'string') {
|
|
343
|
-
sessionStorage.removeItem(key);
|
|
344
|
-
}
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
// 导出日期工具
|
|
348
|
-
|
|
349
|
-
// 工具包版本信息
|
|
350
|
-
const version = '1.0.0';
|
|
351
|
-
|
|
352
|
-
exports.capitalize = capitalize;
|
|
353
|
-
exports.flattenArray = flattenArray;
|
|
354
|
-
exports.formatDate = formatDate;
|
|
355
|
-
exports.getDaysBetween = getDaysBetween;
|
|
356
|
-
exports.getLocalStorage = getLocalStorage;
|
|
357
|
-
exports.getSessionStorage = getSessionStorage;
|
|
358
|
-
exports.groupArray = groupArray;
|
|
359
|
-
exports.isEmail = isEmail;
|
|
360
|
-
exports.isPhone = isPhone;
|
|
361
|
-
exports.isToday = isToday;
|
|
362
|
-
exports.maskStr = maskStr;
|
|
363
|
-
exports.removeLocalStorage = removeLocalStorage;
|
|
364
|
-
exports.removeSessionStorage = removeSessionStorage;
|
|
365
|
-
exports.setLocalStorage = setLocalStorage;
|
|
366
|
-
exports.setSessionStorage = setSessionStorage;
|
|
367
|
-
exports.sortArray = sortArray;
|
|
368
|
-
exports.trim = trim;
|
|
369
|
-
exports.uniqueArray = uniqueArray;
|
|
370
|
-
exports.version = version;
|
|
1
|
+
"use strict";var e=require("element-plus"),t=require("jsrsasign");const o={TOKEN:"token",USER_INFO:"userInfo",COMPANYCODE:"companyCode",COMPANYNAME:"companyName"};function r(){return localStorage.getItem(o.TOKEN)}const n=(e,t=!1)=>e.map(e=>{const o={title:e.name};return e.url&&(o.path=e.url),e.icon&&(o.svgName=e.icon),e.type&&(o.type=e.type),t?e.children&&e.children.length>0&&0===e.type&&(o.children=n(e.children)):e.children&&e.children.length>0&&(o.children=n(e.children)),o});exports.CacheKey=o,exports.MENU_DATA_KEY="user_menu_data",exports.WHITE_LIST=["/login","/404","/403","/register","/file-views","/iitc","/account"],exports.copyText=t=>{if(!t)return void e.ElMessage.warning("暂无可复制内容");const o=document.createElement("input");o.value=t,document.body.appendChild(o),o.select(),document.execCommand("copy"),document.body.removeChild(o),e.ElMessage.success("复制成功")},exports.debounce=(e,t=300)=>{let o=null;return function(...r){o&&clearTimeout(o),o=setTimeout(()=>{e.apply(this,r)},t)}},exports.downloadFile=(e,t)=>{const o=document.createElement("a");if(o.style.display="none","string"==typeof e)o.href=e;else{if(!(e instanceof Blob))return void console.error("不支持的文件源类型");o.href=URL.createObjectURL(e)}o.setAttribute("download",t),document.body.appendChild(o),o.click(),document.body.removeChild(o),e instanceof Blob&&window.URL.revokeObjectURL(o.href)},exports.exportFile=async(t,o,n)=>{try{const t=await fetch(o,{method:"get",headers:{token:r()},responseType:"blob"});t.ok||e.ElMessage.error("请求失败");const s=await t.blob(),a=t.headers.get("content-disposition");let c=n;if(a){const e=a.match(/filename="?([^"]+)"?/);e&&e[1]&&(c=decodeURIComponent(e[1]))}const i=window.URL.createObjectURL(s),l=document.createElement("a");l.href=i,l.download=c,document.body.appendChild(l),l.click(),e.ElMessage.success("下载成功"),window.URL.revokeObjectURL(i),document.body.removeChild(l)}catch(t){console.error("下载Excel失败:",t),e.ElMessage.error("下载失败")}},exports.extractPropValues=(e=[],t="",{keepEmpty:o=!1}={})=>{if(!Array.isArray(e))return[];const r=t.split(".").filter(e=>e.trim());return e.map(e=>{let t=e;for(const e of r)if(t=t?.[e],null==t)break;return t}).filter(e=>o||null!=e)},exports.formatAmount=e=>null==e||isNaN(e)?"":Number(e).toLocaleString("zh-CN",{minimumFractionDigits:2,maximumFractionDigits:2}),exports.formatAmountToUnits=function(e){const t=["","","","","","","","","","",""];if("number"!=typeof e||isNaN(e)||!isFinite(e))return t;if(e>999999999.99)return console.warn("金额过大,最大支持999999999.99元"),t;if(e<0)return console.warn("金额不能为负数"),t;if(0===e)return t;const o=Math.abs(e).toFixed(2),[r,n]=o.split("."),s=r.length;for(let e=0;e<s;e++){const o=r[s-1-e],n=8-e;n>=0&&(t[n]=o)}return(parseInt(r)>0||parseInt(n)>0)&&(t[9]=n[0],t[10]=n[1]),t},exports.formatMenuData=n,exports.formatMenuTree=function e(t){return Array.isArray(t)?t.filter(e=>1===e.type||void 0===e.type).map(t=>{const o={title:t.title,...t.path&&{path:t.path},...t.svgName&&{svgName:t.svgName}};if(t.children&&t.children.length){const r=e(t.children);r.length&&(o.children=r)}return o}):[]},exports.formatMoney=(e,t,o,r=!0)=>{if(null==e||isNaN(Number(e)))return r?"0":"";let n=Number(e);if(0===n&&!r)return"";void 0!==o&&"number"==typeof o&&o>=0?n=n.toFixed(o):void 0!==t&&"number"==typeof t&&t>=0&&(n=n.toFixed(t));const s=n.toString().split("."),a=s[0],c=s[1]||"",i=a.replace(/\B(?=(\d{3})+(?!\d))/g,",");return c?`${i}.${c}`:i},exports.formatToFirstDay=function(e){if(!e)return"";const[t,o]=e.split("-");return`${t}-${o}-01`},exports.generate32BitRandomString=function(){const e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";let t="";for(let o=0;o<32;o++)if("undefined"!=typeof crypto&&crypto.getRandomValues){const o=new Uint8Array(1);crypto.getRandomValues(o);const r=o[0]%62;t+=e.charAt(r)}else{const o=Math.floor(62*Math.random());t+=e.charAt(o)}return t},exports.generateTimestamp=function(e=13){if(10===e)return Math.floor(Date.now()/1e3);if(13===e)return Date.now();throw new Error("只支持10位或13位时间戳")},exports.getCompanyAccountPeriod=function(){return localStorage.getItem(o.COMPANYACCOUNTPERIOD)},exports.getCompanyAccountPeriodNow=function(){return localStorage.getItem(o.COMPANYACCOUNTPERIODNOW)},exports.getCompanyAuditStatusMsg=function(){return localStorage.getItem(o.COMPANYAUDITSTATUSMSG)},exports.getCompanyAuditStatusMsgNow=function(){return localStorage.getItem(o.COMPANYAUDITSTATUSMSGNOW)},exports.getCompanyCode=function(){return localStorage.getItem(o.COMPANYCODE)},exports.getCompanyName=function(){return localStorage.getItem(o.COMPANYNAME)},exports.getCompanyType=function(){return localStorage.getItem(o.COMPANYTYPE)},exports.getCurrentMonth=()=>{const e=new Date;return`${e.getFullYear()}-${(e.getMonth()+1).toString().padStart(2,"0")}`},exports.getFileTypeFromUrlRegex=e=>{if(!e||"string"!=typeof e)return"unknown";const t=e.split("?")[0].match(/\.([a-zA-Z0-9]+)$/);if(!t)return"unknown";const o=t[1].toLowerCase();return/^pdf$/i.test(o)?"pdf":/^(jpg|jpeg|png|gif|bmp|webp|svg|ico|tiff|tif|heic|heif)$/i.test(o)?"image":"unknown"},exports.getFinancialStandardDictType=function(){return localStorage.getItem(o.FINANCIALSTANDARDDICTTYPE)},exports.getFormattedDateTime=function(){const e=new Date;return`${e.getFullYear()}${String(e.getMonth()+1).padStart(2,"0")}${String(e.getDate()).padStart(2,"0")}${String(e.getHours()).padStart(2,"0")}${String(e.getMinutes()).padStart(2,"0")}${String(e.getSeconds()).padStart(2,"0")}`},exports.getToken=r,exports.getUserInfo=function(){return localStorage.getItem(o.USER_INFO)},exports.handleViewFile=function(e){if("string"!=typeof e){const t="文件地址必须是字符串,当前传入类型:"+typeof e;return console.error("[handleViewFile] 错误:",t),{success:!1,error:t}}if(!e.trim()){const e="文件地址不能为空或仅空格";return console.error("[handleViewFile] 错误:",e),{success:!1,error:e}}let t;try{t=new URL(e)}catch(t){const o=`无效的URL格式:${e}(请检查是否包含特殊字符或缺失协议)`;return console.error("[handleViewFile] URL解析错误:",t,"| 地址:",e),{success:!1,error:o}}if(!["http:","https:"].includes(t.protocol)){const e=`仅支持HTTP/HTTPS协议,当前协议:${t.protocol}`;return console.error("[handleViewFile] 协议错误:",e),{success:!1,error:e}}const o=t.pathname,r=o.lastIndexOf(".");if(-1===r){const t=`文件地址不含后缀名:${e}(需是jpg/png/pdf)`;return console.error("[handleViewFile] 格式错误:",t),{success:!1,error:t}}const n=o.slice(r+1).toLowerCase(),s=["jpg","jpeg","png","pdf"];if(!s.includes(n)){const e=`不支持的文件格式:.${n},仅支持${s.join("、")}`;return console.error("[handleViewFile] 格式错误:",e),{success:!1,error:e}}try{if(!window.open(e,"_blank","noopener,noreferrer,width=1000,height=800")){const e="文件窗口被浏览器拦截,请允许弹窗后重试(地址栏右侧查看拦截提示)";return console.warn("[handleViewFile] 弹窗被拦截:",e),{success:!1,error:e}}return console.log("[handleViewFile] 成功:文件已在新窗口打开,地址:",e),{success:!0,message:"文件已在新窗口打开"}}catch(t){const o=`打开文件失败:${t.message}(可能是浏览器安全设置或网络问题)`;return console.error("[handleViewFile] 打开失败:",t,"| 地址:",e),{success:!1,error:o}}},exports.percentToNumber=function(e){const t=String(e).replace(/%/g,""),o=Number(t);return isNaN(o)?0:o},exports.periodDisabledDate=e=>{const t=new Date,o=t.getFullYear(),r=t.getMonth(),n=e.getFullYear(),s=e.getMonth();if(0===r){return!(n===o-1&&11===s||n===o)}return n!==o},exports.periodDisabledDateAllAfter=e=>{const t=new Date,o=t.getFullYear(),r=t.getMonth(),n=e.getFullYear(),s=e.getMonth();return n>o||n===o&&s>=r},exports.phoneRegex=/^1[3-9]\d{9}$/,exports.postExportFile=async(t,o,n,s)=>{try{const t=await fetch(o,{method:"post",headers:{token:r(),"Content-Type":"application/json"},responseType:"blob",body:JSON.stringify(s)});t.ok||e.ElMessage.error("请求失败");const a=await t.blob(),c=t.headers.get("content-disposition");let i=n;if(c){const e=c.match(/filename="?([^"]+)"?/);e&&e[1]&&(i=decodeURIComponent(e[1]))}const l=window.URL.createObjectURL(a),u=document.createElement("a");u.href=l,u.download=i,document.body.appendChild(u),u.click(),e.ElMessage.success("下载成功"),window.URL.revokeObjectURL(l),document.body.removeChild(u)}catch(t){console.error("下载Excel失败:",t),e.ElMessage.error("下载失败")}},exports.processVoucherArrayEnhanced=function(e,t={}){const{emptyObject:o={},preserveOriginal:r=!1}=t;if(!Array.isArray(e))throw new Error("参数必须是数组");const n=[];return e.forEach((e,t)=>{if(!e||"object"!=typeof e)return;const s=e.voucherList;if(!Array.isArray(s)){const s=r?{...e}:{...e,_metadata:{originalIndex:t,isPadded:!0,originalLength:0}};return s.voucherList=[{...o},{...o},{...o},{...o},{...o}],s.page=1,s.pageCount=1,void n.push(s)}const a=Math.ceil(s.length/5);if(s.length>5)for(let c=0;c<s.length;c+=5){const i=s.slice(c,c+5),l=Math.floor(c/5)+1,u=[...i];for(;u.length<5;)u.push({...o});const p=r?{...e}:{...e,_metadata:{originalIndex:t,isSplit:c>0,chunkIndex:Math.floor(c/5),totalChunks:a}};p.voucherList=u,p.page=l,p.pageCount=a,n.push(p)}else if(s.length<5){const a=s.map(e=>({...e}));for(;a.length<5;)a.push({...o});const c=r?{...e}:{...e,_metadata:{originalIndex:t,isPadded:!0,originalLength:s.length}};c.voucherList=a,c.page=1,c.pageCount=1,n.push(c)}else{const o=r?{...e}:{...e,_metadata:{originalIndex:t,isOriginal:!0}};o.page=1,o.pageCount=1,n.push(o)}}),n},exports.publicKey="MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAL/EwnX/3BaH5gJXJiv7qKhuKKol5JSgayGyYMWDdRO8KKAtOFrniXp/iAGGoV6Y0GF9Z8iY3xHOiU3rHeSJ3G6wFDY87Qr4k2m48/6YRX+s/iMGJ5j5obdCFiDkFUzoR/EHTqC6PM3h6bhDNNIXQpqVbQrg28tW5Y57+yxqRrUjAgMBAAECgYAHlgB9S3xI1eJm6Eylw8RFuWnZZTtYF8p1IEnMtmkMuU5AG9nVWsBo/J+EM++xPslLefr/h/CXEz0CPJFHp551HucLlRA7EOXSPfwLGODlz5rMnXyM8jD3ZMACXsRLvLEiNt+0nP+G7M+Dxuf6SLkmvqYa/NdJACb5/16E2P+pNQJBAO0hgUcKYfvJSXVolonVQNpR8Hu/4LH+5bBVLef+D6JNP6x0hr9U/ce+nI7I5wIPbBgFknvCWfVbgz16tPZhmC0CQQDPBzLArfdUfMs2h/3cxQAeZnX0ybt3F6CR3K9dliDJRn1p3ovdK0OmyggpKfx94KH+VRcJSaWTHLeFvub8KgSPAkEAhUWcY7HZKeqSB5OVb7fT8B+3OcW4YjFxI+5rk4Q9gylJTQJcrPH2g6mEUddlxcmnJcUT88xiRLx/ATGSvRpSrQJAJSfTkgUm6IDlL8pEY4TvuoUFEoPKI1uYM9V5m8A/7h+6jm+khnMnVx3XTRv99tGEAVNgKQiQsNBsl5JlfkhEPQJBAJ2nPXpGolp/R5usP+LP314snwj93G54+HaZnuOoIB0ApusErnpWCSHXbQtvXKo1n6SE3ccp010CPJBnOm/LRaA=",exports.recalculateCashFlowData=e=>{if(!e||!e.length)return;e.forEach(e=>{1==e.draftCalType&&(e.currentAmount=0)});let t=null;e.forEach(e=>{if(1==e.draftCalType)t=e;else if((2==e.draftCalType||3==e.draftCalType)&&t){const o=Number(e.currentAmount)||0,r=-1===e.calDirection?-1:1;t.currentAmount=(t.currentAmount||0)+o*r}})},exports.removeCompanyAccountPeriod=function(){localStorage.removeItem(o.COMPANYACCOUNTPERIOD)},exports.removeCompanyAccountPeriodNow=function(){localStorage.removeItem(o.COMPANYACCOUNTPERIODNOW)},exports.removeCompanyAuditStatusMsg=function(){localStorage.removeItem(o.COMPANYAUDITSTATUSMSG)},exports.removeCompanyAuditStatusMsgNow=function(){localStorage.removeItem(o.COMPANYAUDITSTATUSMSGNOW)},exports.removeCompanyCode=function(){localStorage.removeItem(o.COMPANYCODE)},exports.removeCompanyName=function(){localStorage.removeItem(o.COMPANYNAME)},exports.removeCompanyType=function(){localStorage.removeItem(o.COMPANYTYPE)},exports.removeFinancialStandardDictType=function(){localStorage.removeItem(o.FINANCIALSTANDARDDICTTYPE)},exports.removeToken=function(){localStorage.removeItem(o.TOKEN)},exports.removeUserInfo=function(){localStorage.removeItem(o.USER_INFO)},exports.scrollToId=function(e,t=0,o=500){if("string"!=typeof e||!e.trim())return console.error("[scrollToId] 错误:id必须是非空字符串"),!1;const r=document.getElementById(e);if(!r)return console.error(`[scrollToId] 错误:未找到ID为"${e}"的元素`),!1;const n=r.getBoundingClientRect().top+window.pageYOffset,s=window.pageYOffset,a=n-s-t;let c=null;return requestAnimationFrame(function e(t){null===c&&(c=t);const r=t-c,n=(i=r,l=s,u=a,(i/=o/2)<1?u/2*i*i+l:(i--,-u/2*(i*(i-2)-1)+l));var i,l,u;window.scrollTo(0,n),r<o&&requestAnimationFrame(e)}),!0},exports.setCompanyAccountPeriod=function(e){localStorage.setItem(o.COMPANYACCOUNTPERIOD,e)},exports.setCompanyAccountPeriodNow=function(e){localStorage.setItem(o.COMPANYACCOUNTPERIODNOW,e)},exports.setCompanyAuditStatusMsg=function(e){localStorage.setItem(o.COMPANYAUDITSTATUSMSG,e)},exports.setCompanyAuditStatusMsgNow=function(e){localStorage.setItem(o.COMPANYAUDITSTATUSMSGNOW,e)},exports.setCompanyCode=function(e){localStorage.setItem(o.COMPANYCODE,e)},exports.setCompanyName=function(e){localStorage.setItem(o.COMPANYNAME,e)},exports.setCompanyType=function(e){localStorage.setItem(o.COMPANYTYPE,e)},exports.setFinancialStandardDictType=function(e){localStorage.setItem(o.FINANCIALSTANDARDDICTTYPE,e)},exports.setToken=function(e){localStorage.setItem(o.TOKEN,e)},exports.setUserInfo=function(e){localStorage.setItem(o.USER_INFO,e)},exports.signWithSHA256RSA=function(e,o){console.log(e,o);const r=new t.KJUR.crypto.Signature({alg:"SHA256withRSA",prov:"cryptojs/jsrsa"});return r.init(o),r.updateString(e),r.sign()},exports.svgConst={viewBox:"0 0 1536 1024",version:"1.1",xmlns:"http://www.w3.org/2000/svg",width:"1em",height:"1em"},exports.svgSjPath={d:"M73.75498878 312.79929781L689.59344939 960.31767875A98.63975344 98.63975344 0 0 0 762.55326659 991.9976a98.63975344 98.63975344 0 0 0 72.71981813-31.67992125L1451.3515444 312.79929781a106.55973375 106.55973375 0 0 0 23.99994-114.23971406 113.75971594 113.75971594 0 0 0-96.95975719-70.55982375l-1231.19692219 0a113.27971688 113.27971688 0 0 0-97.19975718 70.31982375 106.55973375 106.55973375 0 0 0 23.75994094 114.47971406z",fill:"#001533"},exports.truncateToTwoDecimals=function(e,t){if(!isFinite(e)||0===e)return e.toFixed(2);const o=Math.trunc(100*e)/100;return Object.is(o,-0)?"-0.00":o.toFixed(t)},exports.unclosablePaths=["/accounting/accounting-voucher-detail","/accounting/accounting-voucher-add","/accounting/accounting-voucher-red"],exports.version="0.0.3";
|
|
2
|
+
//# sourceMappingURL=jiezhangxin-utils.cjs.js.map
|