uview-pro 0.0.1

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.
Files changed (149) hide show
  1. package/changelog.md +6 -0
  2. package/components/u-action-sheet/u-action-sheet.vue +205 -0
  3. package/components/u-alert-tips/u-alert-tips.vue +241 -0
  4. package/components/u-avatar/u-avatar.vue +220 -0
  5. package/components/u-avatar-cropper/u-avatar-cropper.vue +329 -0
  6. package/components/u-avatar-cropper/weCropper.d.ts +54 -0
  7. package/components/u-avatar-cropper/weCropper.js +1267 -0
  8. package/components/u-avatar-cropper/weCropper.ts +1254 -0
  9. package/components/u-back-top/u-back-top.vue +156 -0
  10. package/components/u-badge/u-badge.vue +189 -0
  11. package/components/u-button/u-button.vue +562 -0
  12. package/components/u-calendar/u-calendar.vue +725 -0
  13. package/components/u-car-keyboard/u-car-keyboard.vue +236 -0
  14. package/components/u-card/u-card.vue +240 -0
  15. package/components/u-cell-group/u-cell-group.vue +56 -0
  16. package/components/u-cell-item/u-cell-item.vue +245 -0
  17. package/components/u-checkbox/u-checkbox.vue +310 -0
  18. package/components/u-checkbox-group/u-checkbox-group.vue +134 -0
  19. package/components/u-circle-progress/u-circle-progress.vue +210 -0
  20. package/components/u-col/u-col.vue +135 -0
  21. package/components/u-collapse/u-collapse.vue +82 -0
  22. package/components/u-collapse-item/u-collapse-item.vue +190 -0
  23. package/components/u-column-notice/u-column-notice.vue +264 -0
  24. package/components/u-count-down/u-count-down.vue +333 -0
  25. package/components/u-count-to/u-count-to.vue +297 -0
  26. package/components/u-divider/u-divider.vue +141 -0
  27. package/components/u-dropdown/u-dropdown.vue +311 -0
  28. package/components/u-dropdown-item/u-dropdown-item.vue +135 -0
  29. package/components/u-empty/u-empty.vue +111 -0
  30. package/components/u-field/u-field.vue +469 -0
  31. package/components/u-form/u-form.vue +162 -0
  32. package/components/u-form-item/u-form-item.vue +476 -0
  33. package/components/u-full-screen/u-full-screen.vue +80 -0
  34. package/components/u-gap/u-gap.vue +48 -0
  35. package/components/u-grid/u-grid.vue +101 -0
  36. package/components/u-grid-item/u-grid-item.vue +136 -0
  37. package/components/u-icon/u-icon.vue +389 -0
  38. package/components/u-image/types.ts +48 -0
  39. package/components/u-image/u-image.vue +218 -0
  40. package/components/u-index-anchor/u-index-anchor.vue +101 -0
  41. package/components/u-index-list/u-index-list.vue +376 -0
  42. package/components/u-input/u-input.vue +462 -0
  43. package/components/u-keyboard/u-keyboard.vue +188 -0
  44. package/components/u-lazy-load/u-lazy-load.vue +288 -0
  45. package/components/u-line/u-line.vue +71 -0
  46. package/components/u-line-progress/u-line-progress.vue +128 -0
  47. package/components/u-link/u-link.vue +87 -0
  48. package/components/u-loading/u-loading.vue +111 -0
  49. package/components/u-loadmore/u-loadmore.vue +205 -0
  50. package/components/u-mask/u-mask.vue +137 -0
  51. package/components/u-message-input/u-message-input.vue +315 -0
  52. package/components/u-modal/u-modal.vue +284 -0
  53. package/components/u-navbar/u-navbar.vue +314 -0
  54. package/components/u-no-network/image.ts +2 -0
  55. package/components/u-no-network/u-no-network.vue +311 -0
  56. package/components/u-notice-bar/u-notice-bar.vue +274 -0
  57. package/components/u-number-box/u-number-box.vue +344 -0
  58. package/components/u-number-keyboard/u-number-keyboard.vue +170 -0
  59. package/components/u-parse/libs/CssHandler.js +100 -0
  60. package/components/u-parse/libs/MpHtmlParser.js +580 -0
  61. package/components/u-parse/libs/config.js +80 -0
  62. package/components/u-parse/libs/handler.wxs +22 -0
  63. package/components/u-parse/libs/trees.vue +505 -0
  64. package/components/u-parse/u-parse.vue +645 -0
  65. package/components/u-picker/u-picker.vue +808 -0
  66. package/components/u-popup/u-popup.vue +404 -0
  67. package/components/u-radio/u-radio.vue +272 -0
  68. package/components/u-radio-group/u-radio-group.vue +116 -0
  69. package/components/u-rate/u-rate.vue +349 -0
  70. package/components/u-read-more/u-read-more.vue +199 -0
  71. package/components/u-row/u-row.vue +95 -0
  72. package/components/u-row-notice/u-row-notice.vue +273 -0
  73. package/components/u-search/u-search.vue +298 -0
  74. package/components/u-section/u-section.vue +175 -0
  75. package/components/u-select/u-select.vue +387 -0
  76. package/components/u-skeleton/u-skeleton.vue +230 -0
  77. package/components/u-slider/u-slider.vue +293 -0
  78. package/components/u-steps/u-steps.vue +200 -0
  79. package/components/u-sticky/u-sticky.vue +189 -0
  80. package/components/u-subsection/u-subsection.vue +388 -0
  81. package/components/u-swipe-action/u-swipe-action.vue +289 -0
  82. package/components/u-swiper/u-swiper.vue +305 -0
  83. package/components/u-switch/u-switch.vue +146 -0
  84. package/components/u-tabbar/u-tabbar.vue +347 -0
  85. package/components/u-table/u-table.vue +104 -0
  86. package/components/u-tabs/u-tabs.vue +322 -0
  87. package/components/u-tabs-swiper/u-tabs-swiper.vue +426 -0
  88. package/components/u-tag/u-tag.vue +270 -0
  89. package/components/u-td/u-td.vue +76 -0
  90. package/components/u-th/u-th.vue +70 -0
  91. package/components/u-time-line/u-time-line.vue +39 -0
  92. package/components/u-time-line-item/u-time-line-item.vue +88 -0
  93. package/components/u-toast/types.ts +4 -0
  94. package/components/u-toast/u-toast.vue +238 -0
  95. package/components/u-top-tips/u-top-tips.vue +118 -0
  96. package/components/u-tr/u-tr.vue +24 -0
  97. package/components/u-upload/u-upload.vue +600 -0
  98. package/components/u-verification-code/u-verification-code.vue +194 -0
  99. package/components/u-waterfall/u-waterfall.vue +186 -0
  100. package/iconfont.css +910 -0
  101. package/index.scss +23 -0
  102. package/index.ts +166 -0
  103. package/libs/config/config.ts +26 -0
  104. package/libs/config/zIndex.ts +37 -0
  105. package/libs/css/color.scss +155 -0
  106. package/libs/css/common.scss +176 -0
  107. package/libs/css/style.components.scss +7 -0
  108. package/libs/css/style.h5.scss +8 -0
  109. package/libs/css/style.mp.scss +72 -0
  110. package/libs/css/style.nvue.scss +3 -0
  111. package/libs/css/style.vue.scss +175 -0
  112. package/libs/function/$parent.ts +22 -0
  113. package/libs/function/addUnit.ts +13 -0
  114. package/libs/function/color.ts +37 -0
  115. package/libs/function/colorGradient.ts +123 -0
  116. package/libs/function/debounce.ts +28 -0
  117. package/libs/function/deepClone.ts +39 -0
  118. package/libs/function/deepMerge.ts +34 -0
  119. package/libs/function/getParent.ts +59 -0
  120. package/libs/function/getRect.ts +26 -0
  121. package/libs/function/guid.ts +42 -0
  122. package/libs/function/md5.ts +397 -0
  123. package/libs/function/parent.ts +21 -0
  124. package/libs/function/queryParams.ts +60 -0
  125. package/libs/function/random.ts +16 -0
  126. package/libs/function/randomArray.ts +11 -0
  127. package/libs/function/route.ts +118 -0
  128. package/libs/function/sys.ts +15 -0
  129. package/libs/function/test.ts +229 -0
  130. package/libs/function/throttle.ts +31 -0
  131. package/libs/function/timeFormat.ts +54 -0
  132. package/libs/function/timeFrom.ts +48 -0
  133. package/libs/function/toast.ts +14 -0
  134. package/libs/function/trim.ts +21 -0
  135. package/libs/function/type2icon.ts +36 -0
  136. package/libs/hooks/useEmitter.ts +77 -0
  137. package/libs/hooks/useParent.ts +29 -0
  138. package/libs/request/index.ts +237 -0
  139. package/libs/store/index.ts +88 -0
  140. package/libs/util/area.ts +1 -0
  141. package/libs/util/async-validator.js +1356 -0
  142. package/libs/util/city.ts +1 -0
  143. package/libs/util/emitter.ts +112 -0
  144. package/libs/util/mitt.ts +118 -0
  145. package/libs/util/parent.ts +20 -0
  146. package/libs/util/province.ts +1 -0
  147. package/package.json +98 -0
  148. package/readme.md +165 -0
  149. package/theme.scss +38 -0
@@ -0,0 +1,808 @@
1
+ <template>
2
+ <u-popup
3
+ :maskCloseAble="maskCloseAble"
4
+ mode="bottom"
5
+ :popup="false"
6
+ v-model="popupValue"
7
+ length="auto"
8
+ :safeAreaInsetBottom="safeAreaInsetBottom"
9
+ @close="close"
10
+ :z-index="uZIndex"
11
+ >
12
+ <view class="u-datetime-picker">
13
+ <view class="u-picker-header" @touchmove.stop.prevent="">
14
+ <view class="u-btn-picker u-btn-picker--tips" :style="{ color: cancelColor }" hover-class="u-opacity" :hover-stay-time="150" @tap="getResult('cancel')">{{
15
+ cancelText
16
+ }}</view>
17
+ <view class="u-picker__title">{{ title }}</view>
18
+ <view
19
+ class="u-btn-picker u-btn-picker--primary"
20
+ :style="{ color: moving ? cancelColor : confirmColor }"
21
+ hover-class="u-opacity"
22
+ :hover-stay-time="150"
23
+ @touchmove.stop=""
24
+ @tap.stop="getResult('confirm')"
25
+ >
26
+ {{ confirmText }}
27
+ </view>
28
+ </view>
29
+ <view class="u-picker-body">
30
+ <picker-view v-if="mode == 'region'" :value="valueArr" @change="change" class="u-picker-view" @pickstart="pickstart" @pickend="pickend">
31
+ <picker-view-column v-if="!reset && params.province">
32
+ <view class="u-column-item" v-for="(item, index) in provinces" :key="index">
33
+ <view class="u-line-1">{{ item.label }}</view>
34
+ </view>
35
+ </picker-view-column>
36
+ <picker-view-column v-if="!reset && params.city">
37
+ <view class="u-column-item" v-for="(item, index) in citysRef" :key="index">
38
+ <view class="u-line-1">{{ item.label }}</view>
39
+ </view>
40
+ </picker-view-column>
41
+ <picker-view-column v-if="!reset && params.area">
42
+ <view class="u-column-item" v-for="(item, index) in areasRef" :key="index">
43
+ <view class="u-line-1">{{ item.label }}</view>
44
+ </view>
45
+ </picker-view-column>
46
+ </picker-view>
47
+ <picker-view v-else-if="mode == 'time'" :value="valueArr" @change="change" class="u-picker-view" @pickstart="pickstart" @pickend="pickend">
48
+ <picker-view-column v-if="!reset && params.year">
49
+ <view class="u-column-item" v-for="(item, index) in years" :key="index">
50
+ {{ item }}
51
+ <text class="u-text" v-if="showTimeTag">年</text>
52
+ </view>
53
+ </picker-view-column>
54
+ <picker-view-column v-if="!reset && params.month">
55
+ <view class="u-column-item" v-for="(item, index) in months" :key="index">
56
+ {{ formatNumber(item) }}
57
+ <text class="u-text" v-if="showTimeTag">月</text>
58
+ </view>
59
+ </picker-view-column>
60
+ <picker-view-column v-if="!reset && params.day">
61
+ <view class="u-column-item" v-for="(item, index) in days" :key="index">
62
+ {{ formatNumber(item) }}
63
+ <text class="u-text" v-if="showTimeTag">日</text>
64
+ </view>
65
+ </picker-view-column>
66
+ <picker-view-column v-if="!reset && params.hour">
67
+ <view class="u-column-item" v-for="(item, index) in hours" :key="index">
68
+ {{ formatNumber(item) }}
69
+ <text class="u-text" v-if="showTimeTag">时</text>
70
+ </view>
71
+ </picker-view-column>
72
+ <picker-view-column v-if="!reset && params.minute">
73
+ <view class="u-column-item" v-for="(item, index) in minutes" :key="index">
74
+ {{ formatNumber(item) }}
75
+ <text class="u-text" v-if="showTimeTag">分</text>
76
+ </view>
77
+ </picker-view-column>
78
+ <picker-view-column v-if="!reset && params.second">
79
+ <view class="u-column-item" v-for="(item, index) in seconds" :key="index">
80
+ {{ formatNumber(item) }}
81
+ <text class="u-text" v-if="showTimeTag">秒</text>
82
+ </view>
83
+ </picker-view-column>
84
+ </picker-view>
85
+ <picker-view v-else-if="mode == 'selector'" :value="valueArr" @change="change" class="u-picker-view" @pickstart="pickstart" @pickend="pickend">
86
+ <picker-view-column v-if="!reset">
87
+ <view class="u-column-item" v-for="(item, index) in range" :key="index">
88
+ <view class="u-line-1">{{ getItemValue(item, 'selector') }}</view>
89
+ </view>
90
+ </picker-view-column>
91
+ </picker-view>
92
+ <picker-view v-else-if="mode == 'multiSelector'" :value="valueArr" @change="change" class="u-picker-view" @pickstart="pickstart" @pickend="pickend">
93
+ <picker-view-column v-if="!reset" v-for="(item, index) in range" :key="index">
94
+ <view class="u-column-item" v-for="(item1, index1) in item" :key="index1">
95
+ <view class="u-line-1">{{ getItemValue(item1, 'multiSelector') }}</view>
96
+ </view>
97
+ </picker-view-column>
98
+ </picker-view>
99
+ </view>
100
+ </view>
101
+ </u-popup>
102
+ </template>
103
+
104
+ <script setup lang="ts">
105
+ import { ref, computed, watch, onMounted, nextTick } from 'vue';
106
+ import provinces from '../../libs/util/province';
107
+ import citys from '../../libs/util/city';
108
+ import areas from '../../libs/util/area';
109
+
110
+ defineOptions({
111
+ name: 'u-picker'
112
+ });
113
+
114
+ /**
115
+ * picker picker弹出选择器
116
+ * @description 此选择器有两种弹出模式:一是时间模式,可以配置年,日,月,时,分,秒参数 二是地区模式,可以配置省,市,区参数
117
+ * @tutorial https://www.uviewui.com/components/picker.html
118
+ * @property {Object} params 需要显示的参数,见官网说明
119
+ * @property {String} mode 模式选择,region-地区类型,time-时间类型(默认time)
120
+ * @property {String Number} start-year 可选的开始年份,mode=time时有效(默认1950)
121
+ * @property {String Number} end-year 可选的结束年份,mode=time时有效(默认2050)
122
+ * @property {Boolean} safe-area-inset-bottom 是否开启底部安全区适配(默认false)
123
+ * @property {Boolean} show-time-tag 时间模式时,是否显示后面的年月日中文提示
124
+ * @property {String} cancel-color 取消按钮的颜色(默认#606266)
125
+ * @property {String} confirm-color 确认按钮的颜色(默认#2979ff)
126
+ * @property {String} default-time 默认选中的时间,mode=time时有效
127
+ * @property {String} confirm-text 确认按钮的文字
128
+ * @property {String} cancel-text 取消按钮的文字
129
+ * @property {String} default-region 默认选中的地区,中文形式,mode=region时有效
130
+ * @property {String} default-code 默认选中的地区,编号形式,mode=region时有效
131
+ * @property {Boolean} mask-close-able 是否允许通过点击遮罩关闭Picker(默认true)
132
+ * @property {String Number} z-index 弹出时的z-index值(默认1075)
133
+ * @property {Array} default-selector 数组形式,其中每一项表示选择了range对应项中的第几个
134
+ * @property {Array} range 自定义选择的数据,mode=selector或mode=multiSelector时有效
135
+ * @property {String} range-key 当range参数的元素为对象时,指定Object中的哪个key的值作为选择器显示内容
136
+ * @event {Function} confirm 点击确定按钮,返回当前选择的值
137
+ * @event {Function} cancel 点击取消按钮,返回当前选择的值
138
+ * @example <u-picker v-model="show" mode="time"></u-picker>
139
+ */
140
+ const props = defineProps({
141
+ /**
142
+ * picker中需要显示的参数
143
+ * @default { year: true, month: true, day: true, hour: false, minute: false, second: false, province: true, city: true, area: true, timestamp: true }
144
+ */
145
+ params: {
146
+ type: Object,
147
+ default: () => ({
148
+ year: true,
149
+ month: true,
150
+ day: true,
151
+ hour: false,
152
+ minute: false,
153
+ second: false,
154
+ province: true,
155
+ city: true,
156
+ area: true,
157
+ timestamp: true
158
+ })
159
+ },
160
+ /**
161
+ * 当mode=selector或者mode=multiSelector时,提供的数组
162
+ * @default []
163
+ */
164
+ range: {
165
+ type: Array,
166
+ default: () => []
167
+ },
168
+ /**
169
+ * 当mode=selector或者mode=multiSelector时,提供的默认选中的下标
170
+ * @default [0]
171
+ */
172
+ defaultSelector: {
173
+ type: Array,
174
+ default: () => [0]
175
+ },
176
+ /**
177
+ * 当 range 是一个 Array<Object> 时,通过 range-key 来指定 Object 中 key 的值作为选择器显示内容
178
+ * @default ''
179
+ */
180
+ rangeKey: {
181
+ type: String,
182
+ default: ''
183
+ },
184
+ /**
185
+ * 模式选择,region-地区类型,time-时间类型,selector-单列模式,multiSelector-多列模式
186
+ * @default 'time'
187
+ */
188
+ mode: {
189
+ type: String,
190
+ default: 'time'
191
+ },
192
+ /**
193
+ * 年份开始时间
194
+ * @default 1950
195
+ */
196
+ startYear: {
197
+ type: [String, Number],
198
+ default: 1950
199
+ },
200
+ /**
201
+ * 年份结束时间
202
+ * @default 2050
203
+ */
204
+ endYear: {
205
+ type: [String, Number],
206
+ default: 2050
207
+ },
208
+ /**
209
+ * "取消"按钮的颜色
210
+ * @default '#606266'
211
+ */
212
+ cancelColor: {
213
+ type: String,
214
+ default: '#606266'
215
+ },
216
+ /**
217
+ * "确定"按钮的颜色
218
+ * @default '#2979ff'
219
+ */
220
+ confirmColor: {
221
+ type: String,
222
+ default: '#2979ff'
223
+ },
224
+ /**
225
+ * 默认显示的时间,2025-07-02 || 2025-07-02 13:01:00 || 2025/07/02
226
+ * @default ''
227
+ */
228
+ defaultTime: {
229
+ type: String,
230
+ default: ''
231
+ },
232
+ /**
233
+ * 默认显示的地区,可传类似["河北省", "秦皇岛市", "北戴河区"]
234
+ * @default []
235
+ */
236
+ defaultRegion: {
237
+ type: Array,
238
+ default: () => []
239
+ },
240
+ /**
241
+ * 时间模式时,是否显示后面的年月日中文提示
242
+ * @default true
243
+ */
244
+ showTimeTag: {
245
+ type: Boolean,
246
+ default: true
247
+ },
248
+ /**
249
+ * 默认显示地区的编码,defaultRegion和areaCode同时存在,areaCode优先,可传类似["13", "1303", "130304"]
250
+ * @default []
251
+ */
252
+ areaCode: {
253
+ type: Array,
254
+ default: () => []
255
+ },
256
+ /**
257
+ * 是否开启底部安全区适配
258
+ * @default false
259
+ */
260
+ safeAreaInsetBottom: {
261
+ type: Boolean,
262
+ default: false
263
+ },
264
+ /**
265
+ * 是否允许通过点击遮罩关闭Picker
266
+ * @default true
267
+ */
268
+ maskCloseAble: {
269
+ type: Boolean,
270
+ default: true
271
+ },
272
+ /**
273
+ * 通过双向绑定控制组件的弹出与收起
274
+ * @default false
275
+ */
276
+ modelValue: {
277
+ type: Boolean,
278
+ default: false
279
+ },
280
+ /**
281
+ * 弹出的z-index值
282
+ * @default 0
283
+ */
284
+ zIndex: {
285
+ type: [String, Number],
286
+ default: 0
287
+ },
288
+ /**
289
+ * 顶部标题
290
+ * @default ''
291
+ */
292
+ title: {
293
+ type: String,
294
+ default: ''
295
+ },
296
+ /**
297
+ * 取消按钮的文字
298
+ * @default '取消'
299
+ */
300
+ cancelText: {
301
+ type: String,
302
+ default: '取消'
303
+ },
304
+ /**
305
+ * 确认按钮的文字
306
+ * @default '确认'
307
+ */
308
+ confirmText: {
309
+ type: String,
310
+ default: '确认'
311
+ }
312
+ });
313
+
314
+ const popupValue = computed({
315
+ get: () => props.modelValue,
316
+ set: (val: boolean) => emit('update:modelValue', val)
317
+ });
318
+
319
+ const emit = defineEmits(['update:modelValue', 'confirm', 'cancel', 'columnchange']);
320
+
321
+ // 主要数据
322
+ const years = ref<number[]>([]);
323
+ const months = ref<number[]>([]);
324
+ const days = ref<number[]>([]);
325
+ const hours = ref<number[]>([]);
326
+ const minutes = ref<number[]>([]);
327
+ const seconds = ref<number[]>([]);
328
+ const year = ref<number>(0);
329
+ const month = ref<number>(0);
330
+ const day = ref<number>(0);
331
+ const hour = ref<number>(0);
332
+ const minute = ref<number>(0);
333
+ const second = ref<number>(0);
334
+ const reset = ref(false);
335
+ const startDate = ref('');
336
+ const endDate = ref('');
337
+ const valueArr = ref<number[]>([]);
338
+ const provincesRef = ref<any[]>(provinces);
339
+ const citysRef = ref<any[]>(citys[0]);
340
+ const areasRef = ref<any[]>(areas[0][0]);
341
+ const province = ref<number>(0);
342
+ const city = ref<number>(0);
343
+ const area = ref<number>(0);
344
+ // 列是否还在滑动中,微信小程序如果在滑动中就点确定,结果可能不准确
345
+ const moving = ref(false);
346
+ const multiSelectorValue = ref<number[]>([]);
347
+
348
+ // 计算属性
349
+ // 引用这几个变量,是为了监听其变化
350
+ const propsChange = computed(() => `${props.mode}-${props.defaultTime}-${props.startYear}-${props.endYear}-${props.defaultRegion}-${props.areaCode}`);
351
+ // 引用这几个变量,是为了监听其变化
352
+ const regionChange = computed(() => `${province.value}-${city.value}`);
353
+
354
+ const yearAndMonth = computed(() => `${year.value}-${month.value}`);
355
+ // 如果用户有传递z-index值,优先使用
356
+ const uZIndex = computed(() => (props.zIndex ? props.zIndex : 1075));
357
+
358
+ // 监听
359
+ watch(propsChange, () => {
360
+ reset.value = true;
361
+ setTimeout(() => init(), 10);
362
+ });
363
+
364
+ // 如果地区发生变化,为了让picker联动起来,必须重置this.citys和this.areas
365
+ watch(regionChange, () => {
366
+ citysRef.value = citys[province.value];
367
+ areasRef.value = areas[province.value][city.value];
368
+ });
369
+
370
+ // watch监听月份的变化,实时变更日的天数,因为不同月份,天数不一样
371
+ // 一个月可能有30,31天,甚至闰年2月的29天,平年2月28天
372
+ watch(yearAndMonth, () => {
373
+ if (props.params.year) setDays();
374
+ });
375
+ watch(
376
+ () => props.modelValue,
377
+ n => {
378
+ if (n) {
379
+ reset.value = true;
380
+ setTimeout(() => init(), 10);
381
+ }
382
+ }
383
+ );
384
+
385
+ // 方法区
386
+ /**
387
+ * 标识滑动开始,只有微信小程序才有这样的事件
388
+ */
389
+ function pickstart() {
390
+ // #ifdef MP-WEIXIN
391
+ moving.value = true;
392
+ // #endif
393
+ }
394
+ /**
395
+ * 标识滑动结束
396
+ */
397
+ function pickend() {
398
+ // #ifdef MP-WEIXIN
399
+ moving.value = false;
400
+ // #endif
401
+ }
402
+ /**
403
+ * 对单列和多列形式的判断是否有传入变量的情况
404
+ * @param item 当前项
405
+ * @param mode 模式
406
+ */
407
+ function getItemValue(item: any, mode: string) {
408
+ // 目前(2020-05-25)uni-app对微信小程序编译有错误,导致v-if为false中的内容也执行,错误导致
409
+ // 单列模式或者多列模式中的getItemValue同时被执行,故在这里再加一层判断
410
+ if (props.mode == mode) {
411
+ return typeof item == 'object' ? item[props.rangeKey] : item;
412
+ }
413
+ }
414
+ /**
415
+ * 小于10前面补0,用于月份,日期,时分秒等
416
+ */
417
+ function formatNumber(num: number | string) {
418
+ return +num < 10 ? '0' + num : String(num);
419
+ }
420
+ /**
421
+ * 生成递进的数组
422
+ */
423
+ function generateArray(start: number, end: number) {
424
+ // 转为数值格式,否则用户给end-year等传递字符串值时,下面的end+1会导致字符串拼接,而不是相加
425
+ start = Number(start);
426
+ end = Number(end);
427
+ end = end > start ? end : start;
428
+ // 生成数组,获取其中的索引,并剪出来
429
+ return [...Array(end + 1).keys()].slice(start);
430
+ }
431
+ /**
432
+ * 获取数组中指定值的索引
433
+ */
434
+ function getIndex(arr: any[], val: any) {
435
+ let index = arr.indexOf(val);
436
+ // 如果index为-1(即找不到index值),~(-1)=-(-1)-1=0,导致条件不成立
437
+ return ~index ? index : 0;
438
+ }
439
+ /**
440
+ * 日期时间处理,初始化各时间字段
441
+ */
442
+ function initTimeValue() {
443
+ // 格式化时间,在IE浏览器(uni不存在此情况),无法识别日期间的"-"间隔符号
444
+ let fdate = props.defaultTime.replace(/\-/g, '/');
445
+ fdate = fdate && fdate.indexOf('/') == -1 ? `2020/01/01 ${fdate}` : fdate;
446
+ let time: Date;
447
+ if (fdate) time = new Date(fdate);
448
+ else time = new Date();
449
+ // 获取年日月时分秒
450
+ year.value = time.getFullYear();
451
+ month.value = Number(time.getMonth()) + 1;
452
+ day.value = time.getDate();
453
+ hour.value = time.getHours();
454
+ minute.value = time.getMinutes();
455
+ second.value = time.getSeconds();
456
+ }
457
+ /**
458
+ * 初始化picker各列数据
459
+ */
460
+ function init() {
461
+ valueArr.value = [];
462
+ reset.value = false;
463
+ if (props.mode == 'time') {
464
+ initTimeValue();
465
+ if (props.params.year) {
466
+ valueArr.value.push(0);
467
+ setYears();
468
+ }
469
+ if (props.params.month) {
470
+ valueArr.value.push(0);
471
+ setMonths();
472
+ }
473
+ if (props.params.day) {
474
+ valueArr.value.push(0);
475
+ setDays();
476
+ }
477
+ if (props.params.hour) {
478
+ valueArr.value.push(0);
479
+ setHours();
480
+ }
481
+ if (props.params.minute) {
482
+ valueArr.value.push(0);
483
+ setMinutes();
484
+ }
485
+ if (props.params.second) {
486
+ valueArr.value.push(0);
487
+ setSeconds();
488
+ }
489
+ } else if (props.mode == 'region') {
490
+ if (props.params.province) {
491
+ valueArr.value.push(0);
492
+ setProvinces();
493
+ }
494
+ if (props.params.city) {
495
+ valueArr.value.push(0);
496
+ setCitys();
497
+ }
498
+ if (props.params.area) {
499
+ valueArr.value.push(0);
500
+ setAreas();
501
+ }
502
+ } else if (props.mode == 'selector') {
503
+ valueArr.value = props.defaultSelector as number[];
504
+ } else if (props.mode == 'multiSelector') {
505
+ valueArr.value = props.defaultSelector as number[];
506
+ multiSelectorValue.value = props.defaultSelector as number[];
507
+ }
508
+ nextTick(() => {});
509
+ }
510
+ /**
511
+ * 设置年份列
512
+ */
513
+ function setYears() {
514
+ // 获取年份集合
515
+ years.value = generateArray(props.startYear as number, props.endYear as number);
516
+ // 设置this.valueArr某一项的值,是为了让picker预选中某一个值
517
+ valueArr.value.splice(valueArr.value.length - 1, 1, getIndex(years.value, year.value));
518
+ }
519
+ /**
520
+ * 设置月份列
521
+ */
522
+ function setMonths() {
523
+ months.value = generateArray(1, 12);
524
+ valueArr.value.splice(valueArr.value.length - 1, 1, getIndex(months.value, month.value));
525
+ }
526
+ /**
527
+ * 设置天数列
528
+ */
529
+ function setDays() {
530
+ let totalDays = new Date(year.value, month.value, 0).getDate();
531
+ days.value = generateArray(1, totalDays);
532
+ let index = 0;
533
+ // 这里不能使用类似setMonths()中的this.valueArr.splice(this.valueArr.length - 1, xxx)做法
534
+ // 因为this.month和this.year变化时,会触发watch中的this.setDays(),导致this.valueArr.length计算有误
535
+ if (props.params.year && props.params.month) index = 2;
536
+ else if (props.params.month) index = 1;
537
+ else if (props.params.year) index = 1;
538
+ else index = 0;
539
+ // 当月份变化时,会导致日期的天数也会变化,如果原来选的天数大于变化后的天数,则重置为变化后的最大值
540
+ // 比如原来选中3月31日,调整为2月后,日期变为最大29,这时如果day值继续为31显然不合理,于是将其置为29(picker-column从1开始)
541
+ if (day.value > days.value.length) day.value = days.value.length;
542
+ valueArr.value.splice(index, 1, getIndex(days.value, day.value));
543
+ }
544
+ /**
545
+ * 设置小时列
546
+ */
547
+ function setHours() {
548
+ hours.value = generateArray(0, 23);
549
+ valueArr.value.splice(valueArr.value.length - 1, 1, getIndex(hours.value, hour.value));
550
+ }
551
+ /**
552
+ * 设置分钟列
553
+ */
554
+ function setMinutes() {
555
+ minutes.value = generateArray(0, 59);
556
+ valueArr.value.splice(valueArr.value.length - 1, 1, getIndex(minutes.value, minute.value));
557
+ }
558
+ /**
559
+ * 设置秒数列
560
+ */
561
+ function setSeconds() {
562
+ seconds.value = generateArray(0, 59);
563
+ valueArr.value.splice(valueArr.value.length - 1, 1, getIndex(seconds.value, second.value));
564
+ }
565
+ /**
566
+ * 设置省份列
567
+ */
568
+ function setProvinces() {
569
+ // 判断是否需要province参数
570
+ if (!props.params.province) return;
571
+ let tmp: any = '';
572
+ let useCode = false;
573
+ // 如果同时配置了defaultRegion和areaCode,优先使用areaCode参数
574
+ if (props.areaCode.length) {
575
+ tmp = props.areaCode[0];
576
+ useCode = true;
577
+ } else if (props.defaultRegion.length) tmp = props.defaultRegion[0];
578
+ else tmp = 0;
579
+ // 历遍省份数组匹配
580
+ provinces.map((v: any, k: number) => {
581
+ if (useCode ? v.value == tmp : v.label == tmp) {
582
+ tmp = k;
583
+ }
584
+ });
585
+ province.value = tmp;
586
+ provincesRef.value = provinces;
587
+ // 设置默认省份的值
588
+ valueArr.value.splice(0, 1, province.value);
589
+ }
590
+ /**
591
+ * 设置城市列
592
+ */
593
+ function setCitys() {
594
+ if (!props.params.city) return;
595
+ let tmp: any = '';
596
+ let useCode = false;
597
+ if (props.areaCode.length) {
598
+ tmp = props.areaCode[1];
599
+ useCode = true;
600
+ } else if (props.defaultRegion.length) tmp = props.defaultRegion[1];
601
+ else tmp = 0;
602
+ // 历遍城市数组匹配
603
+ citys[province.value].map((v: any, k: number) => {
604
+ if (useCode ? v.value == tmp : v.label == tmp) {
605
+ tmp = k;
606
+ }
607
+ });
608
+ city.value = tmp;
609
+ citysRef.value = citys[province.value];
610
+ // 设置默认城市的值
611
+ valueArr.value.splice(1, 1, city.value);
612
+ }
613
+ /**
614
+ * 设置区县列
615
+ */
616
+ function setAreas() {
617
+ if (!props.params.area) return;
618
+ let tmp: any = '';
619
+ let useCode = false;
620
+ if (props.areaCode.length) {
621
+ tmp = props.areaCode[2];
622
+ useCode = true;
623
+ } else if (props.defaultRegion.length) tmp = props.defaultRegion[2];
624
+ else tmp = 0;
625
+ // 历遍区县数组匹配
626
+ areas[province.value][city.value].map((v: any, k: number) => {
627
+ if (useCode ? v.value == tmp : v.label == tmp) {
628
+ tmp = k;
629
+ }
630
+ });
631
+ area.value = tmp;
632
+ areasRef.value = areas[province.value][city.value];
633
+ // 设置默认区县的值
634
+ valueArr.value.splice(2, 1, area.value);
635
+ }
636
+ /**
637
+ * 关闭picker弹窗
638
+ */
639
+ function close() {
640
+ emit('update:modelValue', false);
641
+ }
642
+ /**
643
+ * 用户更改picker的列选项
644
+ * @param e 事件对象
645
+ */
646
+ function change(e: any) {
647
+ valueArr.value = e.detail.value;
648
+ let i = 0;
649
+ if (props.mode == 'time') {
650
+ // 这里使用i++,是因为valueArr数组的长度是不确定长度的,它根据params的值来配置长度
651
+ // 进入if规则,i会加1,保证了能获取准确的值
652
+ if (props.params.year) year.value = years.value[valueArr.value[i++]];
653
+ if (props.params.month) month.value = months.value[valueArr.value[i++]];
654
+ if (props.params.day) day.value = days.value[valueArr.value[i++]];
655
+ if (props.params.hour) hour.value = hours.value[valueArr.value[i++]];
656
+ if (props.params.minute) minute.value = minutes.value[valueArr.value[i++]];
657
+ if (props.params.second) second.value = seconds.value[valueArr.value[i++]];
658
+ } else if (props.mode == 'region') {
659
+ if (props.params.province) province.value = valueArr.value[i++];
660
+ if (props.params.city) city.value = valueArr.value[i++];
661
+ if (props.params.area) area.value = valueArr.value[i++];
662
+ } else if (props.mode == 'multiSelector') {
663
+ let index: number | null = null;
664
+ // 对比前后两个数组,寻找变更的是哪一列,如果某一个元素不同,即可判定该列发生了变化
665
+ (props.defaultSelector as number[]).map((val: number, idx: number) => {
666
+ if (val != e.detail.value[idx]) index = idx;
667
+ });
668
+ // 为了让用户对多列变化时,对动态设置其他列的变更
669
+ if (index != null) {
670
+ emit('columnchange', {
671
+ column: index,
672
+ index: e.detail.value[index]
673
+ });
674
+ }
675
+ }
676
+ }
677
+ /**
678
+ * 用户点击确定/取消按钮,获取结果
679
+ * @param event 事件名
680
+ */
681
+ function getResult(event: string | null = null) {
682
+ // #ifdef MP-WEIXIN
683
+ // 微信小程序滑动中不允许点击确定,防止取值不准确
684
+ if (moving.value) return;
685
+ // #endif
686
+ let result: any = {};
687
+ // 只返回用户在params中配置了为true的字段
688
+ if (props.mode == 'time') {
689
+ if (props.params.year) result.year = formatNumber(year.value || 0); // 年
690
+ if (props.params.month) result.month = formatNumber(month.value || 0); // 月
691
+ if (props.params.day) result.day = formatNumber(day.value || 0); // 日
692
+ if (props.params.hour) result.hour = formatNumber(hour.value || 0); // 时
693
+ if (props.params.minute) result.minute = formatNumber(minute.value || 0); // 分
694
+ if (props.params.second) result.second = formatNumber(second.value || 0); // 秒
695
+ if (props.params.timestamp) result.timestamp = getTimestamp(); // 时间戳
696
+ } else if (props.mode == 'region') {
697
+ // 地区模式,返回省市区对象
698
+ if (props.params.province) result.province = provinces[province.value];
699
+ if (props.params.city) result.city = citys[province.value][city.value];
700
+ if (props.params.area) result.area = areas[province.value][city.value][area.value];
701
+ } else if (props.mode == 'selector') {
702
+ // 单列选择器,直接返回选中下标数组
703
+ result = valueArr.value;
704
+ } else if (props.mode == 'multiSelector') {
705
+ // 多列选择器,直接返回选中下标数组
706
+ result = valueArr.value;
707
+ }
708
+ // 只允许 emit 已声明的事件类型
709
+ if (event && ['update:modelValue', 'confirm', 'cancel', 'columnchange'].includes(event)) emit(event as 'update:modelValue' | 'confirm' | 'cancel' | 'columnchange', result);
710
+ close();
711
+ }
712
+ /**
713
+ * 获取时间戳(秒)
714
+ * @returns {number} 时间戳
715
+ */
716
+ function getTimestamp() {
717
+ // yyyy-mm-dd为安卓写法,不支持iOS,需要使用"/"分隔,才能二者兼容
718
+ let time = year.value + '/' + month.value + '/' + day.value + ' ' + hour.value + ':' + minute.value + ':' + second.value;
719
+ return new Date(time).getTime() / 1000;
720
+ }
721
+
722
+ // 组件挂载时初始化
723
+ onMounted(() => {
724
+ init();
725
+ });
726
+ </script>
727
+
728
+ <style lang="scss" scoped>
729
+ @import '../../libs/css/style.components.scss';
730
+
731
+ .u-datetime-picker {
732
+ position: relative;
733
+ z-index: 999;
734
+ }
735
+
736
+ .u-picker-view {
737
+ height: 100%;
738
+ box-sizing: border-box;
739
+ }
740
+
741
+ .u-picker-header {
742
+ width: 100%;
743
+ height: 90rpx;
744
+ padding: 0 40rpx;
745
+ @include vue-flex;
746
+ justify-content: space-between;
747
+ align-items: center;
748
+ box-sizing: border-box;
749
+ font-size: 30rpx;
750
+ background: #fff;
751
+ position: relative;
752
+ }
753
+
754
+ .u-picker-header::after {
755
+ content: '';
756
+ position: absolute;
757
+ border-bottom: 1rpx solid #eaeef1;
758
+ -webkit-transform: scaleY(0.5);
759
+ transform: scaleY(0.5);
760
+ bottom: 0;
761
+ right: 0;
762
+ left: 0;
763
+ }
764
+
765
+ .u-picker__title {
766
+ color: $u-content-color;
767
+ }
768
+
769
+ .u-picker-body {
770
+ width: 100%;
771
+ height: 500rpx;
772
+ overflow: hidden;
773
+ background-color: #fff;
774
+ }
775
+
776
+ .u-column-item {
777
+ @include vue-flex;
778
+ align-items: center;
779
+ justify-content: center;
780
+ font-size: 32rpx;
781
+ color: $u-main-color;
782
+ padding: 0 8rpx;
783
+ }
784
+
785
+ .u-text {
786
+ font-size: 24rpx;
787
+ padding-left: 8rpx;
788
+ }
789
+
790
+ .u-btn-picker {
791
+ padding: 16rpx;
792
+ box-sizing: border-box;
793
+ text-align: center;
794
+ text-decoration: none;
795
+ }
796
+
797
+ .u-opacity {
798
+ opacity: 0.5;
799
+ }
800
+
801
+ .u-btn-picker--primary {
802
+ color: $u-type-primary;
803
+ }
804
+
805
+ .u-btn-picker--tips {
806
+ color: $u-tips-color;
807
+ }
808
+ </style>