@peng_kai/kit 0.2.3 → 0.2.4

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/libs/dayjs.ts CHANGED
@@ -1,6 +1,8 @@
1
1
  import dayjs from 'dayjs/esm';
2
2
 
3
3
  import relativeTime from 'dayjs/esm/plugin/relativeTime';
4
+ import weekday from 'dayjs/esm/plugin/weekday';
5
+ import localeData from 'dayjs/esm/plugin/localeData';
4
6
  import 'dayjs/esm/locale/zh';
5
7
  import 'dayjs/esm/locale/en';
6
8
 
@@ -9,3 +11,5 @@ export default dayjs;
9
11
 
10
12
  dayjs.locale('zh');
11
13
  dayjs.extend(relativeTime);
14
+ dayjs.extend(weekday);
15
+ dayjs.extend(localeData);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@peng_kai/kit",
3
3
  "type": "module",
4
- "version": "0.2.3",
4
+ "version": "0.2.4",
5
5
  "description": "",
6
6
  "author": "",
7
7
  "license": "ISC",
@@ -25,6 +25,7 @@
25
25
  "@babel/traverse": "^7.24.0",
26
26
  "@babel/types": "^7.24.0",
27
27
  "@ckeditor/ckeditor5-vue": "^5.1.0",
28
+ "@fingerprintjs/fingerprintjs": "^4.2.2",
28
29
  "@tanstack/vue-query": "^5.25.0",
29
30
  "@vueuse/components": "^10.9.0",
30
31
  "@vueuse/core": "^10.9.0",
@@ -0,0 +1,31 @@
1
+ import type { AxiosInterceptorManager } from 'axios';
2
+ import FingerprintJS from '@fingerprintjs/fingerprintjs';
3
+ import type { GetResult } from '@fingerprintjs/fingerprintjs';
4
+
5
+ /**
6
+ * 【请求拦截器】检索设备信息并在请求中设置“Device”头。
7
+ * @param callback - 一个回调函数,接受基本设备信息和完整设备信息作为参数,并返回一个字符串。
8
+ */
9
+ export function getDeviceInfo(callback: (base: Record<string, any>, all: GetResult) => string): Parameters<AxiosInterceptorManager<any>['use']> {
10
+ const fpPromise = FingerprintJS.load();
11
+
12
+ return [
13
+ async (req) => {
14
+ const fp = await fpPromise;
15
+ const result: any = await fp.get();
16
+ const base = {
17
+ languages: result.components.languages.value?.join(','),
18
+ platform: result.components.platform.value,
19
+ screen_rolution: result.components.screenResolution.value?.join('x'),
20
+ timezone: result.components.timezone.value,
21
+ vendor: result.components.vendor.value,
22
+ vendor_flavors: result.components.vendorFlavors.value?.join(','),
23
+ visitor_id: result.visitorId,
24
+ };
25
+
26
+ req.headers.set('Device', callback(base, result));
27
+
28
+ return req;
29
+ },
30
+ ];
31
+ }
@@ -5,6 +5,7 @@ export { checkCode } from './checkCode';
5
5
  export { formatPaging } from './formatPaging';
6
6
  export { filterEmptyValue } from './filterEmptyValue';
7
7
  export { toLogin } from './toLogin';
8
+ export { getDeviceInfo } from './getDeviceInfo';
8
9
 
9
10
  declare module 'axios' {
10
11
  interface AxiosRequestConfig {
package/utils/date.ts CHANGED
@@ -20,6 +20,34 @@ export function endOfDay(day: dayjs.ConfigType) {
20
20
  return _day.isValid() ? _day.endOf('day').valueOf() : undefined;
21
21
  }
22
22
 
23
+ /**
24
+ * 将一个值转换为 Day.js 实例。
25
+ * @param day - 要转换的值。
26
+ * @returns 表示给定值的 Day.js 实例。
27
+ */
28
+ export function toDayjs(day: dayjs.ConfigType) {
29
+ if (typeof day === 'number' || typeof day === 'string') {
30
+ const tsStr = day.toString();
31
+ const tsNum = Number(tsStr.length === 10 ? `${tsStr}000` : tsStr);
32
+ return dayjs(tsNum);
33
+ }
34
+ else {
35
+ return dayjs(day);
36
+ }
37
+ }
38
+
39
+ /**
40
+ * 将给定的日期转换为10位时间戳。
41
+ * @param day - 要转换的日期。
42
+ * @returns 10位时间戳。
43
+ */
44
+ export function to10Timestamp(day: dayjs.ConfigType) {
45
+ const _day = toDayjs(day);
46
+ const ts10 = Math.floor(_day.valueOf() / 1000);
47
+
48
+ return ts10;
49
+ }
50
+
23
51
  /**
24
52
  * 将秒数转换为时长格式化时间
25
53
  * @param seconds 秒数
@@ -0,0 +1,24 @@
1
+ import { type Component, reactive } from 'vue';
2
+ import type { ComponentExposed } from 'vue-component-type-helpers';
3
+ import { makeDestructurable } from '@vueuse/core';
4
+
5
+ export function useTemplateRefs<T extends Record<string, Component | HTMLElement>>() {
6
+ type TK = keyof T;
7
+ type TI = {
8
+ [K in TK]?: T[K] extends Component ? ComponentExposed<T[K]> : T[K]
9
+ };
10
+
11
+ const refs = reactive({} as TI);
12
+ const setRefs = new Proxy({} as { [K in TK]: (t: any) => void }, {
13
+ get(_, p: any) {
14
+ return (t: any) => {
15
+ (refs as any)[p] = t ?? undefined;
16
+ };
17
+ },
18
+ });
19
+
20
+ return makeDestructurable(
21
+ { refs, setRefs } as const,
22
+ [refs, setRefs] as const,
23
+ );
24
+ }
package/vue/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { useComponentRef } from './hooks/useComponentRef';
2
+ export { useTemplateRefs } from './hooks/useTemplateRefs';
2
3
  export { useTeleportTarget, createTeleportSelectors } from './hooks/useTeleportTarget';
3
4
  export { useIsMounted } from './hooks/useIsMounted';
4
5
  export { useIsDark } from './hooks/useIsDark';