js_ryl 1.0.32 → 1.0.34

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js_ryl",
3
- "version": "1.0.32",
3
+ "version": "1.0.34",
4
4
  "private": false,
5
5
  "description": "自定义通用js",
6
6
  "author": "renyuliang <785788909@qq.com>",
package/src/App.vue CHANGED
@@ -57,9 +57,11 @@ import verCode from "./verCode";
57
57
  import filterTable from "./filterTable";
58
58
  import getMonthDays from "./getMonthDays";
59
59
  import emoji from "./emoji";
60
+ import emojiToUnicode from "./emojiToUnicode";
60
61
  import highLight from "./highLight";
61
62
  import flyToCart from "./flyToCart";
62
63
  import downExcel from "./downExcel";
64
+ import getParamsUtil from './getParamsUtil'
63
65
  export default {
64
66
  data() {
65
67
  return {
@@ -94,6 +96,20 @@ export default {
94
96
  // LazyLoad,
95
97
  // },
96
98
  mounted() {
99
+ const url = 'https://example.com?name=test&age=20';
100
+ console.log(getParamsUtil.getParam('age',url),getParamsUtil.getParams(url))
101
+ const queryString = getParamsUtil.buildQueryString({ name: 'test', age: 20,'sex':1 });
102
+ console.log(queryString,'queryString')
103
+ // 示例5: 合并URL参数
104
+ const newUrl = getParamsUtil.joinParams('https://example.com', { name: 'test', age: 20 });
105
+ console.log(newUrl)
106
+ // 示例6: 移除URL参数
107
+ let cc = 'https://example.com?name=test&age=20'
108
+ const cleanUrl = getParamsUtil.removeParams(cc, ['age','name']);
109
+ console.log(cleanUrl,cc)
110
+ console.log('----------------')
111
+
112
+
97
113
  this.list = filterTable(this.list,'--',['0.00',0])
98
114
  console.log('this.list', this.list)
99
115
  console.log(time.toStamp("2021-06-02 18:17:02"), "当前时间戳");
@@ -159,6 +175,8 @@ export default {
159
175
  const emojiEx = "A0 [u+1BE4] U+1F600 \\u{1F602} U+123456";
160
176
  const output = emoji(emojiEx);
161
177
  console.log(output);
178
+ const sssttt = 'A0 ᯤ 😀 😂 U+123456'
179
+ console.log(emojiToUnicode(sssttt))
162
180
 
163
181
  },
164
182
  methods: {
@@ -1,53 +1,50 @@
1
1
  // 处理字符串中的base64图片上传和替换
2
- export default function base64Img(content, apiUrl, token, data) {
3
- // 匹配所有 base64 图片(支持多种格式)
2
+ export default function base64Img(content, apiUrl, token, data, callback = function () { }) {
4
3
  const base64Regex = /<img [^>]*src=['"](data:image[^'"]+base64,[^'"]+)['"][^>]*>/gi;
5
4
  let match;
6
5
  const replacements = [];
7
6
 
8
- // 第一步:收集所有需要替换的 base64 数据
9
7
  while ((match = base64Regex.exec(content)) !== null) {
10
8
  replacements.push({
11
- original: match[1],
12
- base64: match[1]
9
+ original: match[1],
10
+ base64: match[1]
13
11
  });
14
12
  }
15
13
 
16
- // 第二步:并行上传所有图片
17
14
  if (replacements.length > 0) {
18
15
  const uploadPromises = replacements.map((replacement) => {
19
- return fetch(apiUrl, {
20
- method: "POST",
21
- headers: {
22
- "Authorization": "Bearer " + token,
23
- "Content-Type": "application/json"
24
- },
25
- body: JSON.stringify({image: replacement.base64,...data})
26
- })
27
- .then(res => res.json())
28
- .then(data => {
29
- if (data.code === 200) {
30
- replacement.url = data.data.url;
31
- }
32
- return replacement;
33
- })
34
- .catch(error => {
35
- console.error('上传图片失败:', error);
36
- return replacement;
37
- });
16
+ return fetch(apiUrl, {
17
+ method: "POST",
18
+ headers: {
19
+ "Authorization": "Bearer " + token,
20
+ "Content-Type": "application/json"
21
+ },
22
+ body: JSON.stringify({ image: replacement.base64, ...data })
23
+ })
24
+ .then(res => res.json())
25
+ .then(result => {
26
+ if (result.code === 200) {
27
+ replacement.url = result.data.url;
28
+ } else {
29
+ callback(replacement.original, result.msg);
30
+ }
31
+ return replacement;
32
+ })
33
+ .catch(error => {
34
+ callback(replacement.original, error);
35
+ return replacement;
36
+ });
38
37
  });
39
38
 
40
- // 等待所有上传完成
41
39
  return Promise.all(uploadPromises)
42
- .then(results => {
43
- // 第三步:替换所有 base64 为图片 URL
44
- results.forEach((result) => {
45
- if (result.url) {
46
- content = content.replace(result.original, result.url);
47
- }
40
+ .then(results => {
41
+ results.forEach((result) => {
42
+ if (result.url) {
43
+ content = content.replace(result.original, result.url);
44
+ }
45
+ });
46
+ return content;
48
47
  });
49
- return content;
50
- });
51
48
  }
52
49
 
53
50
  return Promise.resolve(content);
@@ -1,4 +1,4 @@
1
- // 主转换函数
1
+ // 主转换函数-- unicode转化emoji表情
2
2
  export default function emoji(str) {
3
3
  // 支持多种 Unicode 表示格式:U+xxxx, [u+xxxx], \u{xxxx}
4
4
  const regex = /U\+([0-9A-Fa-f]{4,6})|\[u\+([0-9A-Fa-f]{4,6})\]|\\u\{([0-9A-Fa-f]{4,6})\}/g;
@@ -0,0 +1,10 @@
1
+ // 将字符串中的emoji表情转为unicode
2
+ export default function emojiToUnicode(msg) {
3
+ const rex =
4
+ /[\u{1f300}-\u{1f5ff}\u{1f900}-\u{1f9ff}\u{1f600}-\u{1f64f}\u{1f680}-\u{1f6ff}\u{2600}-\u{26ff}\u{2700}-\u{27bf}\u{1f1e6}-\u{1f1ff}\u{1f191}-\u{1f251}\u{1f004}\u{1f0cf}\u{1f170}-\u{1f171}\u{1f17e}-\u{1f17f}\u{1f18e}\u{3030}\u{2b50}\u{2b55}\u{2934}-\u{2935}\u{2b05}-\u{2b07}\u{2b1b}-\u{2b1c}\u{3297}\u{3299}\u{303d}\u{00a9}\u{00ae}\u{2122}\u{23f3}\u{24c2}\u{23e9}-\u{23ef}\u{25b6}\u{23f8}-\u{23fa}]/gu;
5
+ const updated = msg.replace(
6
+ rex,
7
+ (match) => `[u+${match.codePointAt(0).toString(16)}]`
8
+ );1
9
+ return updated;
10
+ }
@@ -0,0 +1,131 @@
1
+ /**
2
+ * URL参数处理工具
3
+ */
4
+ const getParamsUtil = {
5
+ /**
6
+ * 获取单个URL参数或字符串中的参数
7
+ * @param {string} name - 参数名
8
+ * @param {string} [url] - 可选,指定的URL字符串,默认使用当前地址栏URL
9
+ * @param {any} [defaultValue] - 可选,默认值
10
+ * @param {string} [type] - 可选,返回值类型:'string'|'number'|'boolean'|'object'
11
+ * @returns {any} - 参数值或默认值
12
+ */
13
+ getParam(name, url, defaultValue, type) {
14
+ const params = this.getParams(url);
15
+ let value = params[name];
16
+
17
+ if (value === undefined) {
18
+ return defaultValue;
19
+ }
20
+
21
+ // 类型转换
22
+ if (type === 'number') {
23
+ return Number(value);
24
+ } else if (type === 'boolean') {
25
+ return value === 'true' || value === '1';
26
+ } else if (type === 'object') {
27
+ try {
28
+ return JSON.parse(value);
29
+ } catch (e) {
30
+ return defaultValue;
31
+ }
32
+ }
33
+
34
+ return value;
35
+ },
36
+
37
+ /**
38
+ * 获取所有URL参数或字符串中的所有参数
39
+ * @param {string} [url] - 可选,指定的URL字符串,默认使用当前地址栏URL
40
+ * @returns {object} - 所有参数的对象
41
+ */
42
+ getParams(url = window.location.href) {
43
+ const params = {};
44
+ const search = url.split('?')[1];
45
+
46
+ if (!search) {
47
+ return params;
48
+ }
49
+
50
+ const pairs = search.split('&');
51
+ pairs.forEach(pair => {
52
+ const [key, value] = pair.split('=');
53
+ if (key) {
54
+ const decodedKey = decodeURIComponent(key);
55
+ const decodedValue = value ? decodeURIComponent(value) : '';
56
+
57
+ // 处理重复参数
58
+ if (params[decodedKey]) {
59
+ if (Array.isArray(params[decodedKey])) {
60
+ params[decodedKey].push(decodedValue);
61
+ } else {
62
+ params[decodedKey] = [params[decodedKey], decodedValue];
63
+ }
64
+ } else {
65
+ params[decodedKey] = decodedValue;
66
+ }
67
+ }
68
+ });
69
+
70
+ return params;
71
+ },
72
+
73
+ /**
74
+ * 构建URL参数字符串 -- 把所有的参数,拼接成一个字符串
75
+ * @param {object} params - 参数对象
76
+ * @returns {string} - 参数字符串,不包含开头的?
77
+ */
78
+ buildQueryString(params) {
79
+ const query = [];
80
+
81
+ Object.entries(params).forEach(([key, value]) => {
82
+ if (value !== undefined && value !== null) {
83
+ if (Array.isArray(value)) {
84
+ value.forEach(item => {
85
+ query.push(`${encodeURIComponent(key)}=${encodeURIComponent(item)}`);
86
+ });
87
+ } else {
88
+ query.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`);
89
+ }
90
+ }
91
+ });
92
+
93
+ return query.join('&');
94
+ },
95
+
96
+ /**
97
+ * 合并URL参数
98
+ * @param {string} url - 原始URL
99
+ * @param {object} params - 要合并的参数
100
+ * @returns {string} - 合并后的URL
101
+ */
102
+ joinParams(url, params) {
103
+ const [baseUrl, search] = url.split('?');
104
+ const existingParams = this.getParams(url);
105
+ const mergedParams = { ...existingParams, ...params };
106
+ const queryString = this.buildQueryString(mergedParams);
107
+
108
+ return queryString ? `${baseUrl}?${queryString}` : baseUrl;
109
+ },
110
+
111
+ /**
112
+ * 移除URL中的指定参数
113
+ * @param {string} url - 原始URL
114
+ * @param {string|string[]} keys - 要移除的参数名或参数名数组
115
+ * @returns {string} - 移除参数后的URL
116
+ */
117
+ removeParams(url, keys) {
118
+ const [baseUrl, search] = url.split('?');
119
+ const params = this.getParams(url);
120
+ const keysArray = Array.isArray(keys) ? keys : [keys];
121
+
122
+ keysArray.forEach(key => {
123
+ delete params[key];
124
+ });
125
+
126
+ const queryString = this.buildQueryString(params);
127
+ return queryString ? `${baseUrl}?${queryString}` : baseUrl;
128
+ }
129
+ };
130
+
131
+ export default getParamsUtil;
package/src/index.js CHANGED
@@ -19,8 +19,10 @@ import verCode from "./verCode";
19
19
  import filterTable from "./filterTable";
20
20
  // 获取当前年份中,指定月份的天数
21
21
  import getMonthDays from "./getMonthDays";
22
- // emoji表情转化
22
+ // unicode转化emoji表情
23
23
  import emoji from "./emoji";
24
+ // 将字符串中的emoji表情转为unicode
25
+ import emojiToUnicode from "./emojiToUnicode";
24
26
  // 高亮显示搜索匹配的文字
25
27
  import highLight from "./highLight";
26
28
  // 飞入购物车效果
@@ -29,6 +31,8 @@ import flyToCart from "./flyToCart";
29
31
  import downExcel from "./downExcel";
30
32
  // 处理base64的图片为链接
31
33
  import base64Img from "./base64Img";
34
+ // 字符串参数处理
35
+ import getParamsUtil from "./getParamsUtil";
32
36
 
33
37
  export default {
34
38
  reg,
@@ -42,8 +46,10 @@ export default {
42
46
  filterTable,
43
47
  getMonthDays,
44
48
  emoji,
49
+ emojiToUnicode,
45
50
  highLight,
46
51
  flyToCart,
47
52
  downExcel,
48
- base64Img
53
+ base64Img,
54
+ getParamsUtil
49
55
  };