@solar-taro/core 0.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.
- package/CHANGELOG.md +3 -0
- package/README.md +11 -0
- package/eslint.config.cjs +22 -0
- package/package.json +18 -0
- package/project.json +42 -0
- package/src/hooks/filter-list.ts +23 -0
- package/src/hooks/index.ts +3 -0
- package/src/hooks/list.ts +122 -0
- package/src/hooks/request.ts +51 -0
- package/src/index.ts +5 -0
- package/src/operators.ts +26 -0
- package/src/services/bluetooth.ts +23 -0
- package/src/services/index.ts +4 -0
- package/src/services/overlay.ts +60 -0
- package/src/services/router.ts +125 -0
- package/src/services/vibrator.ts +22 -0
- package/src/setup.ts +6 -0
- package/src/utils/amount.spec.ts +10 -0
- package/src/utils/amount.ts +24 -0
- package/src/utils/array.spec.ts +22 -0
- package/src/utils/array.ts +8 -0
- package/src/utils/date.ts +90 -0
- package/src/utils/helper.ts +8 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/math.ts +9 -0
- package/src/utils/service.ts +19 -0
- package/src/utils/style.ts +9 -0
- package/src/utils/window.ts +17 -0
- package/tsconfig.json +21 -0
- package/tsconfig.lib.json +24 -0
- package/tsconfig.spec.json +28 -0
- package/typedoc.json +6 -0
- package/vite.config.ts +24 -0
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const baseConfig = require('../../eslint.config.cjs');
|
|
2
|
+
|
|
3
|
+
module.exports = [
|
|
4
|
+
...baseConfig,
|
|
5
|
+
{
|
|
6
|
+
files: ['**/*.json'],
|
|
7
|
+
rules: {
|
|
8
|
+
'@nx/dependency-checks': [
|
|
9
|
+
'error',
|
|
10
|
+
{
|
|
11
|
+
ignoredFiles: [
|
|
12
|
+
'{projectRoot}/eslint.config.{js,cjs,mjs}',
|
|
13
|
+
'{projectRoot}/vite.config.{js,ts,mjs,mts}',
|
|
14
|
+
],
|
|
15
|
+
},
|
|
16
|
+
],
|
|
17
|
+
},
|
|
18
|
+
languageOptions: {
|
|
19
|
+
parser: require('jsonc-eslint-parser'),
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
];
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@solar-taro/core",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"sideEffects": false,
|
|
5
|
+
"publishConfig": {
|
|
6
|
+
"access": "public"
|
|
7
|
+
},
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"tslib": "^2.3.0",
|
|
10
|
+
"lodash-es": "^4.17.0"
|
|
11
|
+
},
|
|
12
|
+
"devDependencies": {
|
|
13
|
+
"@types/lodash-es": "^4.17.0"
|
|
14
|
+
},
|
|
15
|
+
"peerDependencies": {
|
|
16
|
+
"@tarojs/taro": "^3.6.0"
|
|
17
|
+
}
|
|
18
|
+
}
|
package/project.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "core",
|
|
3
|
+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
+
"sourceRoot": "packages/core/src",
|
|
5
|
+
"projectType": "library",
|
|
6
|
+
"tags": [],
|
|
7
|
+
"targets": {
|
|
8
|
+
"build": {
|
|
9
|
+
"executor": "@nx/js:tsc",
|
|
10
|
+
"outputs": [
|
|
11
|
+
"{options.outputPath}"
|
|
12
|
+
],
|
|
13
|
+
"options": {
|
|
14
|
+
"outputPath": "dist/core",
|
|
15
|
+
"main": "packages/core/src/index.ts",
|
|
16
|
+
"tsConfig": "packages/core/tsconfig.lib.json",
|
|
17
|
+
"assets": [
|
|
18
|
+
"packages/core/*.md"
|
|
19
|
+
]
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"postbuild": {
|
|
23
|
+
"executor": "nx:run-commands",
|
|
24
|
+
"dependsOn": [
|
|
25
|
+
"build"
|
|
26
|
+
],
|
|
27
|
+
"options": {
|
|
28
|
+
"commands": [
|
|
29
|
+
"node scripts/postbuild.js core"
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"release:dev": {
|
|
34
|
+
"executor": "nx:run-commands",
|
|
35
|
+
"options": {
|
|
36
|
+
"commands": [
|
|
37
|
+
"yalc publish dist/core"
|
|
38
|
+
]
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { computed, MaybeRefOrGetter, Ref, ref, toValue } from 'vue';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* const { keyword, filteredList } = useFilterList(
|
|
7
|
+
* list,
|
|
8
|
+
* (item, keyword) => item.fullName.includes(keyword.value)
|
|
9
|
+
* )
|
|
10
|
+
* ```
|
|
11
|
+
* @param list
|
|
12
|
+
* @param filter
|
|
13
|
+
*/
|
|
14
|
+
export function useFilterList<T>(list: MaybeRefOrGetter<T[]>, filter: (item: T, keyword: Ref<string>) => boolean) {
|
|
15
|
+
const keyword = ref('');
|
|
16
|
+
const filteredList = computed(() => {
|
|
17
|
+
return toValue(list).filter(item =>
|
|
18
|
+
filter(item, keyword)
|
|
19
|
+
)
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return { keyword, filteredList };
|
|
23
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { IdEntity } from '@solar-kit/planets/sun';
|
|
2
|
+
import { debounce } from 'lodash-es';
|
|
3
|
+
import { finalize, lastValueFrom, Observable, tap } from 'rxjs';
|
|
4
|
+
import { Ref, ref } from 'vue';
|
|
5
|
+
|
|
6
|
+
export interface UseListConfig {
|
|
7
|
+
/** 每页大小,默认15 */
|
|
8
|
+
pageSize?: number;
|
|
9
|
+
/** 急性加载,默认开启 */
|
|
10
|
+
eager?: boolean;
|
|
11
|
+
/** 通过 id 控制列表项在列表中是唯一的 */
|
|
12
|
+
unique?: number;
|
|
13
|
+
debounceWait?: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface Pageable {
|
|
17
|
+
/** 每页大小 */
|
|
18
|
+
size: number;
|
|
19
|
+
/** 页码 */
|
|
20
|
+
number: number;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const { list, refreshing, loading, onRefresh, refresh } = useList(
|
|
27
|
+
* () => service.list().pipe(unpackResult())
|
|
28
|
+
* )
|
|
29
|
+
* ```
|
|
30
|
+
* @param fetchFn
|
|
31
|
+
* @param config
|
|
32
|
+
*/
|
|
33
|
+
export function useList<T>(fetchFn: (pageable: Pageable) => Observable<T[]>, config?: Partial<UseListConfig>) {
|
|
34
|
+
const _config: UseListConfig = Object.assign({ eager: true, debounceWait: 300 }, config);
|
|
35
|
+
const refreshing = ref(false);
|
|
36
|
+
const loading = ref(false);
|
|
37
|
+
const list = ref<T[]>([]) as Ref<T[]>;
|
|
38
|
+
const pageNumber = ref(1);
|
|
39
|
+
const pageSize = ref(_config.pageSize || 15);
|
|
40
|
+
|
|
41
|
+
function loadData() {
|
|
42
|
+
loading.value = true;
|
|
43
|
+
return lastValueFrom(
|
|
44
|
+
fetchFn({ number: pageNumber.value, size: pageSize.value }).pipe(
|
|
45
|
+
tap(value => {
|
|
46
|
+
if (pageNumber.value === 1) {
|
|
47
|
+
list.value = [];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (value.length < pageSize.value) {
|
|
51
|
+
pageNumber.value = 0;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (_config.unique) {
|
|
55
|
+
// 过滤出不在原列表中的数据
|
|
56
|
+
value = value.filter(newItem =>
|
|
57
|
+
// 找出存在于列表项中的数据,并取反
|
|
58
|
+
!list.value.some(oldItem => (oldItem as IdEntity).id === (newItem as IdEntity).id)
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
list.value = list.value.concat(value);
|
|
63
|
+
}),
|
|
64
|
+
finalize(() => {
|
|
65
|
+
refreshing.value = false;
|
|
66
|
+
loading.value = false;
|
|
67
|
+
})
|
|
68
|
+
)
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function loadMore() {
|
|
73
|
+
if (pageNumber.value && !loading.value) {
|
|
74
|
+
pageNumber.value++;
|
|
75
|
+
return loadData();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return Promise.resolve();
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function refresh() {
|
|
82
|
+
pageNumber.value = 1;
|
|
83
|
+
return loadData();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const debounceRefresh = debounce(refresh, _config.debounceWait!);
|
|
87
|
+
|
|
88
|
+
function onRefresh() {
|
|
89
|
+
if (!refreshing.value) {
|
|
90
|
+
refreshing.value = true;
|
|
91
|
+
return refresh();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return Promise.resolve();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (_config.eager) {
|
|
98
|
+
refresh();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
refreshing,
|
|
103
|
+
loading,
|
|
104
|
+
list,
|
|
105
|
+
/**
|
|
106
|
+
* 加载下一页,如果到了页尾,无法继续加载,会返回 `Promise<undefined>`
|
|
107
|
+
*/
|
|
108
|
+
loadMore,
|
|
109
|
+
/**
|
|
110
|
+
* 刷新函数,调用后主动刷新一次
|
|
111
|
+
*/
|
|
112
|
+
refresh,
|
|
113
|
+
/**
|
|
114
|
+
* 防抖刷新函数
|
|
115
|
+
*/
|
|
116
|
+
debounceRefresh,
|
|
117
|
+
/**
|
|
118
|
+
* 刷新时的事件回调函数,触发刷新事件时需要主动调用
|
|
119
|
+
*/
|
|
120
|
+
onRefresh,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { takeUntilDestroyed } from '@solar-vue/core';
|
|
2
|
+
import { finalize, Observable, Subject, switchMap, tap } from 'rxjs';
|
|
3
|
+
import { onUnmounted, Ref, ref } from 'vue';
|
|
4
|
+
|
|
5
|
+
export interface UseRequestConfig {
|
|
6
|
+
/** 急性加载,默认开启 */
|
|
7
|
+
eager?: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* 通用的请求 Hook
|
|
12
|
+
* @param reqFn
|
|
13
|
+
* @param defaultValue
|
|
14
|
+
* @param config
|
|
15
|
+
*/
|
|
16
|
+
export function useRequest<T>(reqFn: () => Observable<T>, defaultValue: T, config?: UseRequestConfig): { loading: Ref<boolean>, data: Ref<T>, refresh: () => void };
|
|
17
|
+
export function useRequest<T>(reqFn: () => Observable<T>, defaultValue?: T, config?: UseRequestConfig): { loading: Ref<boolean>, data: Ref<T | undefined>, refresh: () => void };
|
|
18
|
+
export function useRequest<T>(reqFn: () => Observable<T>, defaultValue?: T, config?: UseRequestConfig) {
|
|
19
|
+
const _config: UseRequestConfig = Object.assign({ eager: true }, config);
|
|
20
|
+
const loading = ref(false);
|
|
21
|
+
const data = ref(defaultValue) as Ref<T>;
|
|
22
|
+
const refresh$ = new Subject<void>();
|
|
23
|
+
|
|
24
|
+
refresh$.pipe(
|
|
25
|
+
tap(() => loading.value = true),
|
|
26
|
+
switchMap(() => reqFn().pipe(
|
|
27
|
+
takeUntilDestroyed(),
|
|
28
|
+
finalize(() => loading.value = false)
|
|
29
|
+
)),
|
|
30
|
+
).subscribe(value => {
|
|
31
|
+
data.value = value;
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
function refresh() {
|
|
35
|
+
refresh$.next();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (_config.eager) {
|
|
39
|
+
refresh();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
onUnmounted(() => {
|
|
43
|
+
refresh$.complete();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
return {
|
|
47
|
+
loading,
|
|
48
|
+
data,
|
|
49
|
+
refresh
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/index.ts
ADDED
package/src/operators.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Observable, tap } from 'rxjs';
|
|
2
|
+
import { overlay } from './services/overlay';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 为异步操作添加 Loading 浮层
|
|
6
|
+
* @param msg
|
|
7
|
+
*/
|
|
8
|
+
export function withLoading<T>(msg?: string) {
|
|
9
|
+
return (observable: Observable<T>) => {
|
|
10
|
+
const loading = overlay.loading(msg);
|
|
11
|
+
return observable.pipe(
|
|
12
|
+
tap({
|
|
13
|
+
next: () => loading.dismiss(),
|
|
14
|
+
error: () => loading.dismiss(),
|
|
15
|
+
})
|
|
16
|
+
);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function withSaveLoading<T>() {
|
|
21
|
+
return withLoading<T>('正在保存');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function withDeleteLoading<T>() {
|
|
25
|
+
return withLoading<T>('正在删除');
|
|
26
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { BluetoothService } from '@opper/wx';
|
|
2
|
+
import { getSystemSetting } from '@tarojs/taro';
|
|
3
|
+
import { overlay } from './overlay';
|
|
4
|
+
import { vibrator } from './vibrator';
|
|
5
|
+
|
|
6
|
+
export type BlueToothDevice = WechatMiniprogram.BlueToothDevice;
|
|
7
|
+
|
|
8
|
+
export const bluetoothService = new BluetoothService();
|
|
9
|
+
|
|
10
|
+
export function checkBluetooth() {
|
|
11
|
+
const { bluetoothEnabled } = getSystemSetting();
|
|
12
|
+
|
|
13
|
+
if (!bluetoothEnabled) {
|
|
14
|
+
vibrator.short();
|
|
15
|
+
overlay.error('请先开启手机蓝牙');
|
|
16
|
+
}
|
|
17
|
+
// 对于没有 GPS ,使用网络定位的设备,locationEnabled 始终是 false
|
|
18
|
+
// else if (!locationEnabled) {
|
|
19
|
+
// overlay.error('请先开启手机定位');
|
|
20
|
+
// }
|
|
21
|
+
|
|
22
|
+
return bluetoothEnabled;
|
|
23
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type * as Taro from '@tarojs/taro';
|
|
2
|
+
import { hideLoading, showLoading, showModal, showToast } from '@tarojs/taro';
|
|
3
|
+
|
|
4
|
+
export class Overlay {
|
|
5
|
+
success(msg = '操作成功', duration = 1000) {
|
|
6
|
+
return showToast({
|
|
7
|
+
title: msg,
|
|
8
|
+
icon: 'success',
|
|
9
|
+
duration
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
error(msg = '操作失败', duration = 2000) {
|
|
14
|
+
return showToast({
|
|
15
|
+
title: msg,
|
|
16
|
+
icon: 'error' as any,
|
|
17
|
+
duration
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
tips(msg: string, duration = 2000) {
|
|
22
|
+
return showToast({
|
|
23
|
+
title: msg,
|
|
24
|
+
icon: 'none',
|
|
25
|
+
duration
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
loading(msg = '正在加载') {
|
|
30
|
+
showLoading({
|
|
31
|
+
title: msg,
|
|
32
|
+
mask: true
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
return {
|
|
36
|
+
dismiss: () => hideLoading()
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* 模态框,确认走 `then`,取消走 `catch`
|
|
42
|
+
* @param title
|
|
43
|
+
* @param content
|
|
44
|
+
* @param options
|
|
45
|
+
*/
|
|
46
|
+
async modal(title: string, content: string, options?: Taro.showModal.Option) {
|
|
47
|
+
const { confirm } = await showModal({
|
|
48
|
+
title,
|
|
49
|
+
content,
|
|
50
|
+
...options
|
|
51
|
+
});
|
|
52
|
+
if (!confirm) {
|
|
53
|
+
return Promise.reject();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
export const overlay = new Overlay();
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { HttpParams } from '@ngify/http';
|
|
2
|
+
import { EventChannel, getCurrentInstance, getCurrentPages, navigateBack, navigateTo, redirectTo, reLaunch } from '@tarojs/taro';
|
|
3
|
+
|
|
4
|
+
export class Router {
|
|
5
|
+
/**
|
|
6
|
+
* 当前页面的的事件通道
|
|
7
|
+
*/
|
|
8
|
+
get eventChannel() {
|
|
9
|
+
return getCurrentInstance().page?.getOpenerEventChannel?.() as EventChannel;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
get params() {
|
|
13
|
+
return getCurrentInstance().router?.params ?? {}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 重启某个页面
|
|
18
|
+
* @param page 页面名
|
|
19
|
+
* @param params 页面参数
|
|
20
|
+
*/
|
|
21
|
+
relaunch(page: string, params?: Record<string | number, any>): Promise<TaroGeneral.CallbackResult>;
|
|
22
|
+
/**
|
|
23
|
+
* 重启某个页面
|
|
24
|
+
* @param pkg 包名
|
|
25
|
+
* @param page 页面名
|
|
26
|
+
* @param params 页面参数
|
|
27
|
+
*/
|
|
28
|
+
relaunch(pkg: string, page: string, params?: Record<string | number, any>): Promise<TaroGeneral.CallbackResult>;
|
|
29
|
+
relaunch(pkgOrPage: string, pageOrParams?: string | Record<string | number, any>, params?: Record<string | number, any>) {
|
|
30
|
+
return reLaunch({ url: this.buildUrl(pkgOrPage, pageOrParams, params) });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* 重定向到某个页面
|
|
35
|
+
* @param page 页面名
|
|
36
|
+
* @param params 页面参数
|
|
37
|
+
*/
|
|
38
|
+
redirect(page: string, params?: Record<string | number, any>): Promise<TaroGeneral.CallbackResult>;
|
|
39
|
+
/**
|
|
40
|
+
* 重定向到某个页面
|
|
41
|
+
* @param pkg 包名
|
|
42
|
+
* @param page 页面名
|
|
43
|
+
* @param params 页面参数
|
|
44
|
+
*/
|
|
45
|
+
redirect(pkg: string, page: string, params?: Record<string | number, any>): Promise<TaroGeneral.CallbackResult>;
|
|
46
|
+
redirect(pkgOrPage: string, pageOrParams?: string | Record<string | number, any>, params?: Record<string | number, any>) {
|
|
47
|
+
return redirectTo({ url: this.buildUrl(pkgOrPage, pageOrParams, params) });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 导航到某个页面
|
|
52
|
+
* @param page
|
|
53
|
+
* @param params
|
|
54
|
+
* @param events
|
|
55
|
+
*/
|
|
56
|
+
navigate(page: string, params?: Record<string | number, any>, events?: { [event: string]: (...args: any) => void }): Promise<TaroGeneral.CallbackResult & { eventChannel: EventChannel }>;
|
|
57
|
+
/**
|
|
58
|
+
* 导航到某个页面
|
|
59
|
+
* @param pkg 包名
|
|
60
|
+
* @param page 页面名
|
|
61
|
+
* @param params 页面参数
|
|
62
|
+
* @param events
|
|
63
|
+
*/
|
|
64
|
+
navigate(pkg: string, page: string, params?: Record<string | number, any>, events?: { [event: string]: (...args: any) => void }): Promise<TaroGeneral.CallbackResult & { eventChannel: EventChannel }>;
|
|
65
|
+
navigate(pkgOrPage: string, pageOrParams?: string | Record<string | number, any>, paramsOrEvents?: Record<string | number, any>, events?: { [event: string]: (...args: any) => void }) {
|
|
66
|
+
// 分包页面
|
|
67
|
+
if (typeof pageOrParams === 'string') {
|
|
68
|
+
return navigateTo({ url: this.buildUrl(pkgOrPage, pageOrParams, paramsOrEvents), events }) as Promise<TaroGeneral.CallbackResult & { eventChannel: EventChannel }>;
|
|
69
|
+
} else {
|
|
70
|
+
return navigateTo({ url: this.buildUrl(pkgOrPage, pageOrParams), events: paramsOrEvents }) as Promise<TaroGeneral.CallbackResult & { eventChannel: EventChannel }>;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* 返回到某一页面
|
|
76
|
+
* @param delta 返回几页
|
|
77
|
+
*/
|
|
78
|
+
back(delta = 1) {
|
|
79
|
+
if (getCurrentPages().length > 1) {
|
|
80
|
+
return navigateBack({ delta });
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return this.redirect('index');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 构造完整的页面 URL
|
|
88
|
+
* @param page 页面名
|
|
89
|
+
* @param params 页面参数
|
|
90
|
+
*/
|
|
91
|
+
buildUrl(page: string, params?: Record<string | number, any>): string;
|
|
92
|
+
/**
|
|
93
|
+
* 构造完整的页面 URL
|
|
94
|
+
* @param pkg 包名,默认为主包
|
|
95
|
+
* @param page 页面名
|
|
96
|
+
* @param params 页面参数
|
|
97
|
+
*/
|
|
98
|
+
buildUrl(pkg: string, page: string, params?: Record<string | number, any>): string;
|
|
99
|
+
buildUrl(pkgOrPage: string, pageOrParams?: string | Record<string | number, any>, params?: Record<string | number, any>): string;
|
|
100
|
+
buildUrl(pkgOrPage: string, pageOrParams?: string | Record<string | number, any>, params?: Record<string | number, any>) {
|
|
101
|
+
let url: string, pkg: string | undefined, page: string;
|
|
102
|
+
// 分包页面
|
|
103
|
+
if (typeof pageOrParams === 'string') {
|
|
104
|
+
pkg = pkgOrPage;
|
|
105
|
+
page = pageOrParams;
|
|
106
|
+
} else {
|
|
107
|
+
page = pkgOrPage;
|
|
108
|
+
params = pageOrParams;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
url = `/pages/${page}/index`;
|
|
112
|
+
|
|
113
|
+
if (params) {
|
|
114
|
+
url += '?' + new HttpParams(params).toString();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (pkg) {
|
|
118
|
+
url = `/packages/${pkg}${url}`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return url;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export const router = new Router();
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type * as Taro from '@tarojs/taro';
|
|
2
|
+
import { vibrateLong, vibrateShort } from '@tarojs/taro';
|
|
3
|
+
|
|
4
|
+
export class Vibrator {
|
|
5
|
+
/**
|
|
6
|
+
* 短震
|
|
7
|
+
* @param type
|
|
8
|
+
*/
|
|
9
|
+
short(type: Taro.vibrateShort.Option['type'] = 'light') {
|
|
10
|
+
// @ts-expect-error: https://developers.weixin.qq.com/community/develop/doc/00046cbacd4df8f5ad2cf631651800
|
|
11
|
+
return vibrateShort({ type, style: type });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 长震
|
|
16
|
+
*/
|
|
17
|
+
long() {
|
|
18
|
+
return vibrateLong();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const vibrator = new Vibrator();
|
package/src/setup.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { amount } from './amount';
|
|
2
|
+
|
|
3
|
+
describe('amount.function', () => {
|
|
4
|
+
it('should be formatting number', () => {
|
|
5
|
+
expect(amount(12345)).toBe('12,345');
|
|
6
|
+
expect(amount(12345, true)).toBe('12,345.00');
|
|
7
|
+
expect(amount(12345.67)).toBe('12,345');
|
|
8
|
+
expect(amount(12345.67, true)).toBe('12,345.67');
|
|
9
|
+
});
|
|
10
|
+
});
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 将数字格式化成 1,234 这种带逗号的
|
|
3
|
+
*
|
|
4
|
+
* @param value
|
|
5
|
+
* @param accurate 精准模式,启用后会附加小数点
|
|
6
|
+
*/
|
|
7
|
+
export const amount = (value: number | string, accurate?: boolean) => {
|
|
8
|
+
const [integer, decimal] = (+value || 0).toFixed(2).split('.');
|
|
9
|
+
|
|
10
|
+
let str = integer
|
|
11
|
+
.split('')
|
|
12
|
+
.reverse()
|
|
13
|
+
.map((char, index) =>
|
|
14
|
+
char + (index > 0 && index % 3 === 0 ? ',' : '') // 每三个数字就加一逗号
|
|
15
|
+
)
|
|
16
|
+
.reverse()
|
|
17
|
+
.join('');
|
|
18
|
+
|
|
19
|
+
if (accurate) {
|
|
20
|
+
str += `.${decimal}`;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return str;
|
|
24
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { find } from './array';
|
|
2
|
+
|
|
3
|
+
describe('array.function', () => {
|
|
4
|
+
describe('find', () => {
|
|
5
|
+
it('应该可以从数组中找到目标元素', () => {
|
|
6
|
+
const element = { id: 1, fullName: 'one' };
|
|
7
|
+
const source = [element];
|
|
8
|
+
const result = find(source, 'id', 1);
|
|
9
|
+
|
|
10
|
+
expect(result).toEqual(element);
|
|
11
|
+
expect(result).toBe(element);
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it('在数组中找不到目标元素时应该返回 undefined', () => {
|
|
15
|
+
const element = { id: 1, fullName: 'one' };
|
|
16
|
+
const source = [element];
|
|
17
|
+
const result = find(source, 'id', 2);
|
|
18
|
+
|
|
19
|
+
expect(result).toBe(undefined);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
const MONTH_NAMES = [
|
|
2
|
+
'一月', '二月', '三月', '四月', '五月', '六月',
|
|
3
|
+
'七月', '八月', '九月', '十月', '十一月', '十二月'
|
|
4
|
+
];
|
|
5
|
+
|
|
6
|
+
const DAY_OF_WEEK_NAMES = [
|
|
7
|
+
'星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'
|
|
8
|
+
];
|
|
9
|
+
|
|
10
|
+
const twoDigitPad = (num: number) => num < 10 ? '0' + num : num.toString();
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 日期格式化
|
|
14
|
+
* - aaa: 上午/下午
|
|
15
|
+
* - EEEE: 星期几
|
|
16
|
+
* - MMMM: 月份全称
|
|
17
|
+
* @param timestamp
|
|
18
|
+
* @param fmt
|
|
19
|
+
*/
|
|
20
|
+
export const date = (timestamp: number, fmt = 'yyyy-MM-dd') => {
|
|
21
|
+
const date = new Date(timestamp);
|
|
22
|
+
const day = date.getDate();
|
|
23
|
+
const month = date.getMonth();
|
|
24
|
+
const year = date.getFullYear();
|
|
25
|
+
const hour = date.getHours();
|
|
26
|
+
const minute = date.getMinutes();
|
|
27
|
+
const second = date.getSeconds();
|
|
28
|
+
const miliseconds = date.getMilliseconds();
|
|
29
|
+
const h = hour % 12;
|
|
30
|
+
const hh = twoDigitPad(h);
|
|
31
|
+
const HH = twoDigitPad(hour);
|
|
32
|
+
const mm = twoDigitPad(minute);
|
|
33
|
+
const ss = twoDigitPad(second);
|
|
34
|
+
const aaa = hour < 12 ? '上午' : '下午';
|
|
35
|
+
const EEEE = DAY_OF_WEEK_NAMES[date.getDay()];
|
|
36
|
+
const EEE = EEEE;
|
|
37
|
+
const dd = twoDigitPad(day);
|
|
38
|
+
const M = month + 1;
|
|
39
|
+
const MM = twoDigitPad(M);
|
|
40
|
+
const MMMM = MONTH_NAMES[month];
|
|
41
|
+
const MMM = MMMM;
|
|
42
|
+
const yyyy = year + '';
|
|
43
|
+
const yy = yyyy.substring(2, 4);
|
|
44
|
+
|
|
45
|
+
fmt = fmt
|
|
46
|
+
.replace('hh', hh).replace('h', h.toString())
|
|
47
|
+
.replace('HH', HH).replace('H', hour.toString())
|
|
48
|
+
.replace('mm', mm).replace('m', minute.toString())
|
|
49
|
+
.replace('ss', ss).replace('s', second.toString())
|
|
50
|
+
.replace('S', miliseconds.toString())
|
|
51
|
+
.replace('dd', dd).replace('d', day.toString())
|
|
52
|
+
.replace('EEEE', EEEE).replace('EEE', EEE)
|
|
53
|
+
.replace('yyyy', yyyy)
|
|
54
|
+
.replace('yy', yy)
|
|
55
|
+
.replace('aaa', aaa);
|
|
56
|
+
|
|
57
|
+
if (fmt.includes('MMM')) {
|
|
58
|
+
fmt = fmt.replace('MMMM', MMMM).replace('MMM', MMM);
|
|
59
|
+
} else {
|
|
60
|
+
fmt = fmt.replace('MM', MM).replace('M', M.toString());
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return fmt;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 时间尺度
|
|
68
|
+
* @param timestamp
|
|
69
|
+
* @returns 将返回 x秒/分钟/小时/天/月 前/后
|
|
70
|
+
*/
|
|
71
|
+
export const relativeTime = (timestamp: number) => {
|
|
72
|
+
const now = Date.now();
|
|
73
|
+
const duration = Math.abs(now - timestamp);
|
|
74
|
+
const beforeOrAfter = now > timestamp ? '前' : '后';
|
|
75
|
+
|
|
76
|
+
if (duration <= 60000) {
|
|
77
|
+
const tmp = duration / 1000 | 0;
|
|
78
|
+
return tmp <= 900 ? '刚刚' : tmp + `秒${beforeOrAfter}`;
|
|
79
|
+
} else if (duration < 3600000) {
|
|
80
|
+
return (duration / 60000 | 0) + `分钟${beforeOrAfter}`;
|
|
81
|
+
} else if (duration < 86400000) {
|
|
82
|
+
return (duration / 3600000 | 0) + `小时${beforeOrAfter}`;
|
|
83
|
+
} else if (duration < 86400000 * 30) {
|
|
84
|
+
return (duration / 86400000 | 0) + `天${beforeOrAfter}`;
|
|
85
|
+
} else if (duration < 86400000 * 365) {
|
|
86
|
+
return (duration / (86400000 * 30) | 0) + `个月${beforeOrAfter}`;
|
|
87
|
+
} else {
|
|
88
|
+
return date(timestamp);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 定义一个服务,并提供 setter, getter 函数。
|
|
3
|
+
* 这对于需要先配置,后使用的 service 特别有用。
|
|
4
|
+
*
|
|
5
|
+
* @param factory 一个工厂函数,函数参数从返回的 setter 提供
|
|
6
|
+
*/
|
|
7
|
+
export function defineService<T extends (...args: any) => any>(factory: T) {
|
|
8
|
+
let service: ReturnType<T>;
|
|
9
|
+
|
|
10
|
+
function setter(...args: Parameters<T>) {
|
|
11
|
+
service = factory(...args as any);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function getter() {
|
|
15
|
+
return service;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return [setter, getter] as const;
|
|
19
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { getMenuButtonBoundingClientRect, getSystemInfoSync } from '@tarojs/taro';
|
|
2
|
+
import { once } from './helper';
|
|
3
|
+
|
|
4
|
+
export const getNavbarRect = once(() => {
|
|
5
|
+
const { statusBarHeight, windowWidth } = getSystemInfoSync();
|
|
6
|
+
const { height, width, left, top } = getMenuButtonBoundingClientRect();
|
|
7
|
+
const bottom = top - statusBarHeight!;
|
|
8
|
+
|
|
9
|
+
return {
|
|
10
|
+
height: statusBarHeight! + height + bottom * 2,
|
|
11
|
+
paddingTop: statusBarHeight! + bottom,
|
|
12
|
+
paddingBottom: bottom,
|
|
13
|
+
paddingLeft: windowWidth - width - left,
|
|
14
|
+
paddingRight: windowWidth - width - left,
|
|
15
|
+
buttonHeight: height,
|
|
16
|
+
};
|
|
17
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"forceConsistentCasingInFileNames": true,
|
|
5
|
+
"strict": true,
|
|
6
|
+
"noImplicitOverride": true,
|
|
7
|
+
"noImplicitReturns": true,
|
|
8
|
+
"noFallthroughCasesInSwitch": true,
|
|
9
|
+
"noPropertyAccessFromIndexSignature": true
|
|
10
|
+
},
|
|
11
|
+
"files": [],
|
|
12
|
+
"include": [],
|
|
13
|
+
"references": [
|
|
14
|
+
{
|
|
15
|
+
"path": "./tsconfig.lib.json"
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
"path": "./tsconfig.spec.json"
|
|
19
|
+
}
|
|
20
|
+
]
|
|
21
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "../../dist/out-tsc",
|
|
5
|
+
"declaration": true
|
|
6
|
+
},
|
|
7
|
+
"include": [
|
|
8
|
+
"src/**/*.ts"
|
|
9
|
+
],
|
|
10
|
+
"exclude": [
|
|
11
|
+
"vite.config.ts",
|
|
12
|
+
"vite.config.mts",
|
|
13
|
+
"vitest.config.ts",
|
|
14
|
+
"vitest.config.mts",
|
|
15
|
+
"src/**/*.test.ts",
|
|
16
|
+
"src/**/*.spec.ts",
|
|
17
|
+
"src/**/*.test.tsx",
|
|
18
|
+
"src/**/*.spec.tsx",
|
|
19
|
+
"src/**/*.test.js",
|
|
20
|
+
"src/**/*.spec.js",
|
|
21
|
+
"src/**/*.test.jsx",
|
|
22
|
+
"src/**/*.spec.jsx"
|
|
23
|
+
]
|
|
24
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"outDir": "../../dist/out-tsc",
|
|
5
|
+
"types": [
|
|
6
|
+
"vitest/globals",
|
|
7
|
+
"vitest/importMeta",
|
|
8
|
+
"vite/client",
|
|
9
|
+
"node",
|
|
10
|
+
"vitest"
|
|
11
|
+
]
|
|
12
|
+
},
|
|
13
|
+
"include": [
|
|
14
|
+
"vite.config.ts",
|
|
15
|
+
"vite.config.mts",
|
|
16
|
+
"vitest.config.ts",
|
|
17
|
+
"vitest.config.mts",
|
|
18
|
+
"src/**/*.test.ts",
|
|
19
|
+
"src/**/*.spec.ts",
|
|
20
|
+
"src/**/*.test.tsx",
|
|
21
|
+
"src/**/*.spec.tsx",
|
|
22
|
+
"src/**/*.test.js",
|
|
23
|
+
"src/**/*.spec.js",
|
|
24
|
+
"src/**/*.test.jsx",
|
|
25
|
+
"src/**/*.spec.jsx",
|
|
26
|
+
"src/**/*.d.ts"
|
|
27
|
+
]
|
|
28
|
+
}
|
package/typedoc.json
ADDED
package/vite.config.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { nxCopyAssetsPlugin } from '@nx/vite/plugins/nx-copy-assets.plugin';
|
|
2
|
+
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
|
|
3
|
+
import { defineConfig } from 'vite';
|
|
4
|
+
|
|
5
|
+
export default defineConfig({
|
|
6
|
+
root: __dirname,
|
|
7
|
+
cacheDir: '../../node_modules/.vite/core',
|
|
8
|
+
plugins: [nxViteTsPaths(), nxCopyAssetsPlugin(['*.md'])],
|
|
9
|
+
// Uncomment this if you are using workers.
|
|
10
|
+
// worker: {
|
|
11
|
+
// plugins: [ nxViteTsPaths() ],
|
|
12
|
+
// },
|
|
13
|
+
test: {
|
|
14
|
+
watch: false,
|
|
15
|
+
globals: true,
|
|
16
|
+
environment: 'node',
|
|
17
|
+
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
|
|
18
|
+
reporters: ['default'],
|
|
19
|
+
coverage: {
|
|
20
|
+
reportsDirectory: '../../coverage/core',
|
|
21
|
+
provider: 'v8',
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
});
|