@tzk-design-vue2/shared 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.
package/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # @tzk-design-vue2/shared
2
+
3
+ 共享资源包 - TZK Design Vue2 组件库
4
+
5
+ ## 说明
6
+
7
+ 这是 TZK Design Vue2 组件库的共享资源包,包含:
8
+
9
+ - 🎨 通用样式变量
10
+ - 🛠️ 工具函数
11
+ - 📦 公共类型定义
12
+ - 🔧 辅助方法
13
+
14
+ ## 安装
15
+
16
+ ```bash
17
+ npm install @tzk-design-vue2/shared
18
+ # 或
19
+ yarn add @tzk-design-vue2/shared
20
+ # 或
21
+ pnpm add @tzk-design-vue2/shared
22
+ ```
23
+
24
+ ## 使用
25
+
26
+ ### 样式变量
27
+
28
+ ```less
29
+ // 在你的 Less 文件中导入
30
+ @import '@tzk-design-vue2/shared/styles/variables.less';
31
+
32
+ .my-component {
33
+ color: @primary-color;
34
+ border-radius: @border-radius-base;
35
+ }
36
+ ```
37
+
38
+ ### 工具函数
39
+
40
+ ```javascript
41
+ import { utils } from '@tzk-design-vue2/shared';
42
+
43
+ // 使用工具函数
44
+ const result = utils.someFunction();
45
+ ```
46
+
47
+ ## 内容
48
+
49
+ ### 样式资源
50
+
51
+ - `styles/variables.less` - Less 变量
52
+ - `styles/mixins.less` - Less 混入
53
+ - `styles/common.less` - 通用样式
54
+
55
+ ### 工具函数
56
+
57
+ - 类型判断
58
+ - 数据处理
59
+ - DOM 操作
60
+ - 其他辅助函数
61
+
62
+ ## 注意事项
63
+
64
+ > [!IMPORTANT]
65
+ > 此包通常作为其他组件包的依赖使用,一般不需要单独安装。
66
+ > 如果使用完整的 `@tzk-design-vue2/ui` 包,会自动包含此依赖。
67
+
68
+ ## 开源协议
69
+
70
+ MIT
@@ -0,0 +1,234 @@
1
+ /**
2
+ * API 适配器
3
+ * 统一不同平台的 API 差异
4
+ */
5
+
6
+ /**
7
+ * 显示 Toast
8
+ * @param {Object} options - 配置项
9
+ */
10
+ export function showToast(options) {
11
+ const { title, icon = 'none', duration = 2000, mask = false } = options;
12
+
13
+ // #ifdef MP-WEIXIN
14
+ wx.showToast({ title, icon, duration, mask });
15
+ // #endif
16
+
17
+ // #ifdef MP-ALIPAY
18
+ my.showToast({ content: title, type: icon, duration, mask });
19
+ // #endif
20
+
21
+ // #ifndef MP-WEIXIN || MP-ALIPAY
22
+ uni.showToast({ title, icon, duration, mask });
23
+ // #endif
24
+ }
25
+
26
+ /**
27
+ * 显示 Loading
28
+ * @param {Object} options - 配置项
29
+ */
30
+ export function showLoading(options) {
31
+ const { title = '加载中...', mask = true } = options;
32
+
33
+ // #ifdef MP-WEIXIN
34
+ wx.showLoading({ title, mask });
35
+ // #endif
36
+
37
+ // #ifdef MP-ALIPAY
38
+ my.showLoading({ content: title, delay: 0 });
39
+ // #endif
40
+
41
+ // #ifndef MP-WEIXIN || MP-ALIPAY
42
+ uni.showLoading({ title, mask });
43
+ // #endif
44
+ }
45
+
46
+ /**
47
+ * 隐藏 Loading
48
+ */
49
+ export function hideLoading() {
50
+ // #ifdef MP-WEIXIN
51
+ wx.hideLoading();
52
+ // #endif
53
+
54
+ // #ifdef MP-ALIPAY
55
+ my.hideLoading();
56
+ // #endif
57
+
58
+ // #ifndef MP-WEIXIN || MP-ALIPAY
59
+ uni.hideLoading();
60
+ // #endif
61
+ }
62
+
63
+ /**
64
+ * 显示模态对话框
65
+ * @param {Object} options - 配置项
66
+ * @returns {Promise}
67
+ */
68
+ export function showModal(options) {
69
+ const {
70
+ title = '提示',
71
+ content = '',
72
+ showCancel = true,
73
+ cancelText = '取消',
74
+ confirmText = '确定'
75
+ } = options;
76
+
77
+ return new Promise((resolve, reject) => {
78
+ // #ifdef MP-WEIXIN
79
+ wx.showModal({
80
+ title,
81
+ content,
82
+ showCancel,
83
+ cancelText,
84
+ confirmText,
85
+ success: res => resolve(res),
86
+ fail: err => reject(err)
87
+ });
88
+ // #endif
89
+
90
+ // #ifdef MP-ALIPAY
91
+ my.confirm({
92
+ title,
93
+ content,
94
+ confirmButtonText: confirmText,
95
+ cancelButtonText: cancelText,
96
+ success: res => resolve({ confirm: res.confirm }),
97
+ fail: err => reject(err)
98
+ });
99
+ // #endif
100
+
101
+ // #ifndef MP-WEIXIN || MP-ALIPAY
102
+ uni.showModal({
103
+ title,
104
+ content,
105
+ showCancel,
106
+ cancelText,
107
+ confirmText,
108
+ success: res => resolve(res),
109
+ fail: err => reject(err)
110
+ });
111
+ // #endif
112
+ });
113
+ }
114
+
115
+ /**
116
+ * 设置导航栏标题
117
+ * @param {string} title - 标题
118
+ */
119
+ export function setNavigationBarTitle(title) {
120
+ // #ifdef MP-WEIXIN
121
+ wx.setNavigationBarTitle({ title });
122
+ // #endif
123
+
124
+ // #ifdef MP-ALIPAY
125
+ my.setNavigationBar({ title });
126
+ // #endif
127
+
128
+ // #ifndef MP-WEIXIN || MP-ALIPAY
129
+ uni.setNavigationBarTitle({ title });
130
+ // #endif
131
+ }
132
+
133
+ /**
134
+ * 获取存储数据
135
+ * @param {string} key - 键名
136
+ * @returns {Promise}
137
+ */
138
+ export function getStorage(key) {
139
+ return new Promise((resolve, reject) => {
140
+ // #ifdef MP-WEIXIN
141
+ wx.getStorage({
142
+ key,
143
+ success: res => resolve(res.data),
144
+ fail: err => reject(err)
145
+ });
146
+ // #endif
147
+
148
+ // #ifdef MP-ALIPAY
149
+ my.getStorage({
150
+ key,
151
+ success: res => resolve(res.data),
152
+ fail: err => reject(err)
153
+ });
154
+ // #endif
155
+
156
+ // #ifndef MP-WEIXIN || MP-ALIPAY
157
+ uni.getStorage({
158
+ key,
159
+ success: res => resolve(res.data),
160
+ fail: err => reject(err)
161
+ });
162
+ // #endif
163
+ });
164
+ }
165
+
166
+ /**
167
+ * 设置存储数据
168
+ * @param {string} key - 键名
169
+ * @param {*} data - 数据
170
+ * @returns {Promise}
171
+ */
172
+ export function setStorage(key, data) {
173
+ return new Promise((resolve, reject) => {
174
+ // #ifdef MP-WEIXIN
175
+ wx.setStorage({
176
+ key,
177
+ data,
178
+ success: () => resolve(),
179
+ fail: err => reject(err)
180
+ });
181
+ // #endif
182
+
183
+ // #ifdef MP-ALIPAY
184
+ my.setStorage({
185
+ key,
186
+ data,
187
+ success: () => resolve(),
188
+ fail: err => reject(err)
189
+ });
190
+ // #endif
191
+
192
+ // #ifndef MP-WEIXIN || MP-ALIPAY
193
+ uni.setStorage({
194
+ key,
195
+ data,
196
+ success: () => resolve(),
197
+ fail: err => reject(err)
198
+ });
199
+ // #endif
200
+ });
201
+ }
202
+
203
+ /**
204
+ * 移除存储数据
205
+ * @param {string} key - 键名
206
+ * @returns {Promise}
207
+ */
208
+ export function removeStorage(key) {
209
+ return new Promise((resolve, reject) => {
210
+ // #ifdef MP-WEIXIN
211
+ wx.removeStorage({
212
+ key,
213
+ success: () => resolve(),
214
+ fail: err => reject(err)
215
+ });
216
+ // #endif
217
+
218
+ // #ifdef MP-ALIPAY
219
+ my.removeStorage({
220
+ key,
221
+ success: () => resolve(),
222
+ fail: err => reject(err)
223
+ });
224
+ // #endif
225
+
226
+ // #ifndef MP-WEIXIN || MP-ALIPAY
227
+ uni.removeStorage({
228
+ key,
229
+ success: () => resolve(),
230
+ fail: err => reject(err)
231
+ });
232
+ // #endif
233
+ });
234
+ }
@@ -0,0 +1,86 @@
1
+ /**
2
+ * 事件适配器
3
+ * 统一不同平台的事件处理
4
+ */
5
+
6
+ /**
7
+ * 获取事件详情
8
+ * @param {Event} event - 事件对象
9
+ * @returns {Object}
10
+ */
11
+ export function getEventDetail(event) {
12
+ if (!event) return {};
13
+
14
+ // 微信小程序
15
+ if (event.detail) {
16
+ return event.detail;
17
+ }
18
+
19
+ // 支付宝小程序
20
+ if (event.target && event.target.dataset) {
21
+ return event.target.dataset;
22
+ }
23
+
24
+ return {};
25
+ }
26
+
27
+ /**
28
+ * 阻止事件冒泡
29
+ * @param {Event} event - 事件对象
30
+ */
31
+ export function stopPropagation(event) {
32
+ if (!event) return;
33
+
34
+ if (event.stopPropagation) {
35
+ event.stopPropagation();
36
+ }
37
+
38
+ // 小程序
39
+ if (event.detail && event.detail.stopPropagation) {
40
+ event.detail.stopPropagation();
41
+ }
42
+ }
43
+
44
+ /**
45
+ * 阻止默认行为
46
+ * @param {Event} event - 事件对象
47
+ */
48
+ export function preventDefault(event) {
49
+ if (!event) return;
50
+
51
+ if (event.preventDefault) {
52
+ event.preventDefault();
53
+ }
54
+
55
+ // 小程序
56
+ if (event.detail && event.detail.preventDefault) {
57
+ event.detail.preventDefault();
58
+ }
59
+ }
60
+
61
+ /**
62
+ * 获取触摸点坐标
63
+ * @param {Event} event - 事件对象
64
+ * @returns {Object}
65
+ */
66
+ export function getTouchPoint(event) {
67
+ if (!event) return { x: 0, y: 0 };
68
+
69
+ // H5
70
+ if (event.touches && event.touches[0]) {
71
+ return {
72
+ x: event.touches[0].clientX,
73
+ y: event.touches[0].clientY
74
+ };
75
+ }
76
+
77
+ // 小程序
78
+ if (event.changedTouches && event.changedTouches[0]) {
79
+ return {
80
+ x: event.changedTouches[0].x || event.changedTouches[0].clientX,
81
+ y: event.changedTouches[0].y || event.changedTouches[0].clientY
82
+ };
83
+ }
84
+
85
+ return { x: 0, y: 0 };
86
+ }
@@ -0,0 +1,89 @@
1
+ /**
2
+ * 样式适配器
3
+ */
4
+
5
+ import { getSystemInfo } from '../utils/platform';
6
+
7
+ /**
8
+ * 添加单位
9
+ * @param {number|string} value - 值
10
+ * @param {string} unit - 单位
11
+ * @returns {string}
12
+ */
13
+ export function addUnit(value, unit = 'rpx') {
14
+ if (value === undefined || value === null || value === '') {
15
+ return '';
16
+ }
17
+
18
+ if (typeof value === 'number') {
19
+ return `${value}${unit}`;
20
+ }
21
+
22
+ if (typeof value === 'string') {
23
+ // 如果已经有单位,直接返回
24
+ if (/^\d+(\.\d+)?(px|rpx|rem|em|%|vh|vw)$/.test(value)) {
25
+ return value;
26
+ }
27
+ // 如果是纯数字字符串,添加单位
28
+ if (/^\d+(\.\d+)?$/.test(value)) {
29
+ return `${value}${unit}`;
30
+ }
31
+ }
32
+
33
+ return value;
34
+ }
35
+
36
+ /**
37
+ * 获取样式对象
38
+ * @param {Object} styles - 样式对象
39
+ * @returns {string}
40
+ */
41
+ export function getStyle(styles) {
42
+ if (!styles || typeof styles !== 'object') {
43
+ return '';
44
+ }
45
+
46
+ return Object.keys(styles)
47
+ .map(key => {
48
+ const value = styles[key];
49
+ const cssKey = key.replace(/([A-Z])/g, '-$1').toLowerCase();
50
+ return `${cssKey}: ${value}`;
51
+ })
52
+ .join('; ');
53
+ }
54
+
55
+ /**
56
+ * 获取安全区域
57
+ * @returns {Object}
58
+ */
59
+ export function getSafeArea() {
60
+ const systemInfo = getSystemInfo();
61
+ const { safeArea, safeAreaInsets, screenHeight, statusBarHeight } = systemInfo;
62
+
63
+ // 支付宝小程序
64
+ if (safeAreaInsets) {
65
+ return {
66
+ top: safeAreaInsets.top || 0,
67
+ bottom: safeAreaInsets.bottom || 0,
68
+ left: safeAreaInsets.left || 0,
69
+ right: safeAreaInsets.right || 0
70
+ };
71
+ }
72
+
73
+ // 微信小程序
74
+ if (safeArea) {
75
+ return {
76
+ top: safeArea.top || statusBarHeight || 0,
77
+ bottom: screenHeight - safeArea.bottom || 0,
78
+ left: safeArea.left || 0,
79
+ right: safeArea.right || 0
80
+ };
81
+ }
82
+
83
+ return {
84
+ top: statusBarHeight || 0,
85
+ bottom: 0,
86
+ left: 0,
87
+ right: 0
88
+ };
89
+ }
@@ -0,0 +1,36 @@
1
+ /**
2
+ * 错误处理配置
3
+ */
4
+
5
+ export default {
6
+ // 是否启用错误上报
7
+ enableReport: false,
8
+
9
+ // 错误上报地址
10
+ reportUrl: '',
11
+
12
+ // 错误过滤规则
13
+ filterRules: [
14
+ // 示例:过滤特定错误
15
+ // errorLog => errorLog.error.message.includes('Script error')
16
+ ],
17
+
18
+ // 错误采样率 (0-1)
19
+ sampleRate: 1,
20
+
21
+ // 开发环境行为
22
+ development: {
23
+ // 显示错误详情
24
+ showErrorDetail: true,
25
+ // 显示错误提示组件
26
+ showErrorBoundary: true
27
+ },
28
+
29
+ // 生产环境行为
30
+ production: {
31
+ // 显示错误详情
32
+ showErrorDetail: false,
33
+ // 显示错误提示组件
34
+ showErrorBoundary: true
35
+ }
36
+ };
package/index.js ADDED
@@ -0,0 +1,32 @@
1
+ /**
2
+ * 共享资源包入口文件
3
+ */
4
+
5
+ // 样式
6
+ import './styles/index.less';
7
+
8
+ // 工具函数
9
+ export * from './utils/type';
10
+ export * from './utils/format';
11
+ export * from './utils/performance';
12
+ export * from './utils/dom';
13
+ export * from './utils/platform';
14
+ export * from './utils/error-handler';
15
+ export * from './utils/warn';
16
+
17
+ // Mixins
18
+ export { default as componentMixin } from './mixins/component';
19
+ export { default as eventMixin } from './mixins/event';
20
+ export { default as formMixin } from './mixins/form';
21
+ export { default as errorBoundaryMixin } from './mixins/error-boundary';
22
+
23
+ // 适配器
24
+ export * from './adapters/api';
25
+ export * from './adapters/style';
26
+ export * from './adapters/event';
27
+
28
+ // Store
29
+ export { default as store } from './store';
30
+
31
+ // 配置
32
+ export { default as errorConfig } from './config/error';
@@ -0,0 +1,44 @@
1
+ /**
2
+ * 组件基础 Mixin
3
+ */
4
+
5
+ export default {
6
+ data() {
7
+ return {
8
+ // 组件名称前缀
9
+ componentPrefix: 'tzk'
10
+ };
11
+ },
12
+
13
+ methods: {
14
+ /**
15
+ * 生成 BEM 类名
16
+ * @param {string} block - 块名
17
+ * @param {string} element - 元素名
18
+ * @param {string} modifier - 修饰符
19
+ * @returns {string}
20
+ */
21
+ bem(block, element, modifier) {
22
+ let className = `${this.componentPrefix}-${block}`;
23
+
24
+ if (element) {
25
+ className += `__${element}`;
26
+ }
27
+
28
+ if (modifier) {
29
+ className += `--${modifier}`;
30
+ }
31
+
32
+ return className;
33
+ },
34
+
35
+ /**
36
+ * 触发自定义事件
37
+ * @param {string} eventName - 事件名
38
+ * @param {*} detail - 事件数据
39
+ */
40
+ emitEvent(eventName, detail) {
41
+ this.$emit(eventName, detail);
42
+ }
43
+ }
44
+ };
@@ -0,0 +1,15 @@
1
+ /**
2
+ * 错误边界 Mixin
3
+ */
4
+
5
+ import { handleError } from '../utils/error-handler';
6
+
7
+ export default {
8
+ errorCaptured(err, vm, info) {
9
+ // 捕获子组件错误
10
+ handleError(err, vm, info);
11
+
12
+ // 阻止错误继续向上传播
13
+ return false;
14
+ }
15
+ };
@@ -0,0 +1,44 @@
1
+ /**
2
+ * 事件处理 Mixin
3
+ */
4
+
5
+ export default {
6
+ methods: {
7
+ /**
8
+ * 绑定事件
9
+ * @param {string} event - 事件名
10
+ * @param {Function} handler - 事件处理函数
11
+ * @param {Object} options - 事件选项
12
+ */
13
+ bindEvent(event, handler, options = {}) {
14
+ if (!this._eventHandlers) {
15
+ this._eventHandlers = new Map();
16
+ }
17
+
18
+ this._eventHandlers.set(event, { handler, options });
19
+ },
20
+
21
+ /**
22
+ * 解绑事件
23
+ * @param {string} event - 事件名
24
+ */
25
+ unbindEvent(event) {
26
+ if (this._eventHandlers) {
27
+ this._eventHandlers.delete(event);
28
+ }
29
+ },
30
+
31
+ /**
32
+ * 解绑所有事件
33
+ */
34
+ unbindAllEvents() {
35
+ if (this._eventHandlers) {
36
+ this._eventHandlers.clear();
37
+ }
38
+ }
39
+ },
40
+
41
+ beforeDestroy() {
42
+ this.unbindAllEvents();
43
+ }
44
+ };