guangchao.xu | 070005a | 2020-12-07 09:56:40 +0800 | [diff] [blame^] | 1 | <template> |
| 2 | <u-popup :maskCloseAble="maskCloseAble" mode="bottom" :popup="false" v-model="value" length="auto" :safeAreaInsetBottom="safeAreaInsetBottom" @close="close" :z-index="uZIndex"> |
| 3 | <view class="u-datetime-picker"> |
| 4 | <view class="u-picker-header" @touchmove.stop.prevent=""> |
| 5 | <view class="u-btn-picker u-btn-picker--tips" |
| 6 | :style="{ color: cancelColor }" |
| 7 | hover-class="u-opacity" |
| 8 | :hover-stay-time="150" |
| 9 | @tap="getResult('cancel')" |
| 10 | >{{cancelText}}</view> |
| 11 | <view class="u-picker__title">{{ title }}</view> |
| 12 | <view |
| 13 | class="u-btn-picker u-btn-picker--primary" |
| 14 | :style="{ color: moving ? cancelColor : confirmColor }" |
| 15 | hover-class="u-opacity" |
| 16 | :hover-stay-time="150" |
| 17 | @touchmove.stop="" |
| 18 | @tap.stop="getResult('confirm')" |
| 19 | > |
| 20 | {{confirmText}} |
| 21 | </view> |
| 22 | </view> |
| 23 | <view class="u-picker-body"> |
| 24 | <picker-view v-if="mode == 'region'" :value="valueArr" @change="change" class="u-picker-view" @pickstart="pickstart" @pickend="pickend"> |
| 25 | <picker-view-column v-if="!reset && params.province"> |
| 26 | <view class="u-column-item" v-for="(item, index) in provinces" :key="index"> |
| 27 | <view class="u-line-1">{{ item.label }}</view> |
| 28 | </view> |
| 29 | </picker-view-column> |
| 30 | <picker-view-column v-if="!reset && params.city"> |
| 31 | <view class="u-column-item" v-for="(item, index) in citys" :key="index"> |
| 32 | <view class="u-line-1">{{ item.label }}</view> |
| 33 | </view> |
| 34 | </picker-view-column> |
| 35 | <picker-view-column v-if="!reset && params.area"> |
| 36 | <view class="u-column-item" v-for="(item, index) in areas" :key="index"> |
| 37 | <view class="u-line-1">{{ item.label }}</view> |
| 38 | </view> |
| 39 | </picker-view-column> |
| 40 | </picker-view> |
| 41 | <picker-view v-else-if="mode == 'time'" :value="valueArr" @change="change" class="u-picker-view" @pickstart="pickstart" @pickend="pickend"> |
| 42 | <picker-view-column v-if="!reset && params.year"> |
| 43 | <view class="u-column-item" v-for="(item, index) in years" :key="index"> |
| 44 | {{ item }} |
| 45 | <text class="u-text" v-if="showTimeTag">年</text> |
| 46 | </view> |
| 47 | </picker-view-column> |
| 48 | <picker-view-column v-if="!reset && params.month"> |
| 49 | <view class="u-column-item" v-for="(item, index) in months" :key="index"> |
| 50 | {{ formatNumber(item) }} |
| 51 | <text class="u-text" v-if="showTimeTag">月</text> |
| 52 | </view> |
| 53 | </picker-view-column> |
| 54 | <picker-view-column v-if="!reset && params.day"> |
| 55 | <view class="u-column-item" v-for="(item, index) in days" :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.hour"> |
| 61 | <view class="u-column-item" v-for="(item, index) in hours" :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.minute"> |
| 67 | <view class="u-column-item" v-for="(item, index) in minutes" :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.second"> |
| 73 | <view class="u-column-item" v-for="(item, index) in seconds" :key="index"> |
| 74 | {{ formatNumber(item) }} |
| 75 | <text class="u-text" v-if="showTimeTag">秒</text> |
| 76 | </view> |
| 77 | </picker-view-column> |
| 78 | </picker-view> |
| 79 | <picker-view v-else-if="mode == 'selector'" :value="valueArr" @change="change" class="u-picker-view" @pickstart="pickstart" @pickend="pickend"> |
| 80 | <picker-view-column v-if="!reset"> |
| 81 | <view class="u-column-item" v-for="(item, index) in range" :key="index"> |
| 82 | <view class="u-line-1">{{ getItemValue(item, 'selector') }}</view> |
| 83 | </view> |
| 84 | </picker-view-column> |
| 85 | </picker-view> |
| 86 | <picker-view v-else-if="mode == 'multiSelector'" :value="valueArr" @change="change" class="u-picker-view" @pickstart="pickstart" @pickend="pickend"> |
| 87 | <picker-view-column v-if="!reset" v-for="(item, index) in range" :key="index"> |
| 88 | <view class="u-column-item" v-for="(item1, index1) in item" :key="index1"> |
| 89 | <view class="u-line-1">{{ getItemValue(item1, 'multiSelector') }}</view> |
| 90 | </view> |
| 91 | </picker-view-column> |
| 92 | </picker-view> |
| 93 | </view> |
| 94 | </view> |
| 95 | </u-popup> |
| 96 | </template> |
| 97 | |
| 98 | <script> |
| 99 | import provinces from '../../libs/util/province.js'; |
| 100 | import citys from '../../libs/util/city.js'; |
| 101 | import areas from '../../libs/util/area.js'; |
| 102 | |
| 103 | /** |
| 104 | * picker picker弹出选择器 |
| 105 | * @description 此选择器有两种弹出模式:一是时间模式,可以配置年,日,月,时,分,秒参数 二是地区模式,可以配置省,市,区参数 |
| 106 | * @tutorial https://www.uviewui.com/components/picker.html |
| 107 | * @property {Object} params 需要显示的参数,见官网说明 |
| 108 | * @property {String} mode 模式选择,region-地区类型,time-时间类型(默认time) |
| 109 | * @property {String Number} start-year 可选的开始年份,mode=time时有效(默认1950) |
| 110 | * @property {String Number} end-year 可选的结束年份,mode=time时有效(默认2050) |
| 111 | * @property {Boolean} safe-area-inset-bottom 是否开启底部安全区适配(默认false) |
| 112 | * @property {Boolean} show-time-tag 时间模式时,是否显示后面的年月日中文提示 |
| 113 | * @property {String} cancel-color 取消按钮的颜色(默认#606266) |
| 114 | * @property {String} confirm-color 确认按钮的颜色(默认#2979ff) |
| 115 | * @property {String} default-time 默认选中的时间,mode=time时有效 |
| 116 | * @property {String} confirm-text 确认按钮的文字 |
| 117 | * @property {String} cancel-text 取消按钮的文字 |
| 118 | * @property {String} default-region 默认选中的地区,中文形式,mode=region时有效 |
| 119 | * @property {String} default-code 默认选中的地区,编号形式,mode=region时有效 |
| 120 | * @property {Boolean} mask-close-able 是否允许通过点击遮罩关闭Picker(默认true) |
| 121 | * @property {String Number} z-index 弹出时的z-index值(默认1075) |
| 122 | * @property {Array} default-selector 数组形式,其中每一项表示选择了range对应项中的第几个 |
| 123 | * @property {Array} range 自定义选择的数据,mode=selector或mode=multiSelector时有效 |
| 124 | * @property {String} range-key 当range参数的元素为对象时,指定Object中的哪个key的值作为选择器显示内容 |
| 125 | * @event {Function} confirm 点击确定按钮,返回当前选择的值 |
| 126 | * @event {Function} cancel 点击取消按钮,返回当前选择的值 |
| 127 | * @example <u-picker v-model="show" mode="time"></u-picker> |
| 128 | */ |
| 129 | export default { |
| 130 | name: 'u-picker', |
| 131 | props: { |
| 132 | // picker中需要显示的参数 |
| 133 | params: { |
| 134 | type: Object, |
| 135 | default() { |
| 136 | return { |
| 137 | year: true, |
| 138 | month: true, |
| 139 | day: true, |
| 140 | hour: false, |
| 141 | minute: false, |
| 142 | second: false, |
| 143 | province: true, |
| 144 | city: true, |
| 145 | area: true, |
| 146 | timestamp: true, |
| 147 | }; |
| 148 | } |
| 149 | }, |
| 150 | // 当mode=selector或者mode=multiSelector时,提供的数组 |
| 151 | range: { |
| 152 | type: Array, |
| 153 | default() { |
| 154 | return []; |
| 155 | } |
| 156 | }, |
| 157 | // 当mode=selector或者mode=multiSelector时,提供的默认选中的下标 |
| 158 | defaultSelector: { |
| 159 | type: Array, |
| 160 | default() { |
| 161 | return [0]; |
| 162 | } |
| 163 | }, |
| 164 | // 当 range 是一个 Array<Object> 时,通过 range-key 来指定 Object 中 key 的值作为选择器显示内容 |
| 165 | rangeKey: { |
| 166 | type: String, |
| 167 | default: '' |
| 168 | }, |
| 169 | // 模式选择,region-地区类型,time-时间类型,selector-单列模式,multiSelector-多列模式 |
| 170 | mode: { |
| 171 | type: String, |
| 172 | default: 'time' |
| 173 | }, |
| 174 | // 年份开始时间 |
| 175 | startYear: { |
| 176 | type: [String, Number], |
| 177 | default: 1950 |
| 178 | }, |
| 179 | // 年份结束时间 |
| 180 | endYear: { |
| 181 | type: [String, Number], |
| 182 | default: 2050 |
| 183 | }, |
| 184 | // "取消"按钮的颜色 |
| 185 | cancelColor: { |
| 186 | type: String, |
| 187 | default: '#606266' |
| 188 | }, |
| 189 | // "确定"按钮的颜色 |
| 190 | confirmColor: { |
| 191 | type: String, |
| 192 | default: '#2979ff' |
| 193 | }, |
| 194 | // 默认显示的时间,2025-07-02 || 2025-07-02 13:01:00 || 2025/07/02 |
| 195 | defaultTime: { |
| 196 | type: String, |
| 197 | default: '' |
| 198 | }, |
| 199 | // 默认显示的地区,可传类似["河北省", "秦皇岛市", "北戴河区"] |
| 200 | defaultRegion: { |
| 201 | type: Array, |
| 202 | default() { |
| 203 | return []; |
| 204 | } |
| 205 | }, |
| 206 | // 时间模式时,是否显示后面的年月日中文提示 |
| 207 | showTimeTag: { |
| 208 | type: Boolean, |
| 209 | default: true |
| 210 | }, |
| 211 | // 默认显示地区的编码,defaultRegion和areaCode同时存在,areaCode优先,可传类似["13", "1303", "130304"] |
| 212 | areaCode: { |
| 213 | type: Array, |
| 214 | default() { |
| 215 | return []; |
| 216 | } |
| 217 | }, |
| 218 | safeAreaInsetBottom: { |
| 219 | type: Boolean, |
| 220 | default: false |
| 221 | }, |
| 222 | // 是否允许通过点击遮罩关闭Picker |
| 223 | maskCloseAble: { |
| 224 | type: Boolean, |
| 225 | default: true |
| 226 | }, |
| 227 | // 通过双向绑定控制组件的弹出与收起 |
| 228 | value: { |
| 229 | type: Boolean, |
| 230 | default: false |
| 231 | }, |
| 232 | // 弹出的z-index值 |
| 233 | zIndex: { |
| 234 | type: [String, Number], |
| 235 | default: 0 |
| 236 | }, |
| 237 | // 顶部标题 |
| 238 | title: { |
| 239 | type: String, |
| 240 | default: '' |
| 241 | }, |
| 242 | // 取消按钮的文字 |
| 243 | cancelText: { |
| 244 | type: String, |
| 245 | default: '取消' |
| 246 | }, |
| 247 | // 确认按钮的文字 |
| 248 | confirmText: { |
| 249 | type: String, |
| 250 | default: '确认' |
| 251 | } |
| 252 | }, |
| 253 | data() { |
| 254 | return { |
| 255 | years: [], |
| 256 | months: [], |
| 257 | days: [], |
| 258 | hours: [], |
| 259 | minutes: [], |
| 260 | seconds: [], |
| 261 | year: 0, |
| 262 | month: 0, |
| 263 | day: 0, |
| 264 | hour: 0, |
| 265 | minute: 0, |
| 266 | second: 0, |
| 267 | reset: false, |
| 268 | startDate: '', |
| 269 | endDate: '', |
| 270 | valueArr: [], |
| 271 | provinces: provinces, |
| 272 | citys: citys[0], |
| 273 | areas: areas[0][0], |
| 274 | province: 0, |
| 275 | city: 0, |
| 276 | area: 0, |
| 277 | moving: false // 列是否还在滑动中,微信小程序如果在滑动中就点确定,结果可能不准确 |
| 278 | }; |
| 279 | }, |
| 280 | mounted() { |
| 281 | this.init(); |
| 282 | }, |
| 283 | computed: { |
| 284 | propsChange() { |
| 285 | // 引用这几个变量,是为了监听其变化 |
| 286 | return `${this.mode}-${this.defaultTime}-${this.startYear}-${this.endYear}-${this.defaultRegion}-${this.areaCode}`; |
| 287 | }, |
| 288 | regionChange() { |
| 289 | // 引用这几个变量,是为了监听其变化 |
| 290 | return `${this.province}-${this.city}`; |
| 291 | }, |
| 292 | yearAndMonth() { |
| 293 | return `${this.year}-${this.month}`; |
| 294 | }, |
| 295 | uZIndex() { |
| 296 | // 如果用户有传递z-index值,优先使用 |
| 297 | return this.zIndex ? this.zIndex : this.$u.zIndex.popup; |
| 298 | } |
| 299 | }, |
| 300 | watch: { |
| 301 | propsChange() { |
| 302 | this.reset = true; |
| 303 | setTimeout(() => this.init(), 10); |
| 304 | }, |
| 305 | // 如果地区发生变化,为了让picker联动起来,必须重置this.citys和this.areas |
| 306 | regionChange(val) { |
| 307 | this.citys = citys[this.province]; |
| 308 | this.areas = areas[this.province][this.city]; |
| 309 | }, |
| 310 | // watch监听月份的变化,实时变更日的天数,因为不同月份,天数不一样 |
| 311 | // 一个月可能有30,31天,甚至闰年2月的29天,平年2月28天 |
| 312 | yearAndMonth(val) { |
| 313 | if (this.params.year) this.setDays(); |
| 314 | }, |
| 315 | // 微信和QQ小程序由于一些奇怪的原因(故同时对所有平台均初始化一遍),需要重新初始化才能显示正确的值 |
| 316 | value(n) { |
| 317 | if (n) { |
| 318 | this.reset = true; |
| 319 | setTimeout(() => this.init(), 10); |
| 320 | } |
| 321 | } |
| 322 | }, |
| 323 | methods: { |
| 324 | // 标识滑动开始,只有微信小程序才有这样的事件 |
| 325 | pickstart() { |
| 326 | // #ifdef MP-WEIXIN |
| 327 | this.moving = true; |
| 328 | // #endif |
| 329 | }, |
| 330 | // 标识滑动结束 |
| 331 | pickend() { |
| 332 | // #ifdef MP-WEIXIN |
| 333 | this.moving = false; |
| 334 | // #endif |
| 335 | }, |
| 336 | // 对单列和多列形式的判断是否有传入变量的情况 |
| 337 | getItemValue(item, mode) { |
| 338 | // 目前(2020-05-25)uni-app对微信小程序编译有错误,导致v-if为false中的内容也执行,错误导致 |
| 339 | // 单列模式或者多列模式中的getItemValue同时被执行,故在这里再加一层判断 |
| 340 | if (this.mode == mode) { |
| 341 | return typeof item == 'object' ? item[this.rangeKey] : item; |
| 342 | } |
| 343 | }, |
| 344 | // 小于10前面补0,用于月份,日期,时分秒等 |
| 345 | formatNumber(num) { |
| 346 | return +num < 10 ? '0' + num : String(num); |
| 347 | }, |
| 348 | // 生成递进的数组 |
| 349 | generateArray: function(start, end) { |
| 350 | // 转为数值格式,否则用户给end-year等传递字符串值时,下面的end+1会导致字符串拼接,而不是相加 |
| 351 | start = Number(start); |
| 352 | end = Number(end); |
| 353 | end = end > start ? end : start; |
| 354 | // 生成数组,获取其中的索引,并剪出来 |
| 355 | return [...Array(end + 1).keys()].slice(start); |
| 356 | }, |
| 357 | getIndex: function(arr, val) { |
| 358 | let index = arr.indexOf(val); |
| 359 | // 如果index为-1(即找不到index值),~(-1)=-(-1)-1=0,导致条件不成立 |
| 360 | return ~index ? index : 0; |
| 361 | }, |
| 362 | //日期时间处理 |
| 363 | initTimeValue() { |
| 364 | // 格式化时间,在IE浏览器(uni不存在此情况),无法识别日期间的"-"间隔符号 |
| 365 | let fdate = this.defaultTime.replace(/\-/g, '/'); |
| 366 | fdate = fdate && fdate.indexOf('/') == -1 ? `2020/01/01 ${fdate}` : fdate; |
| 367 | let time = null; |
| 368 | if (fdate) time = new Date(fdate); |
| 369 | else time = new Date(); |
| 370 | // 获取年日月时分秒 |
| 371 | this.year = time.getFullYear(); |
| 372 | this.month = Number(time.getMonth()) + 1; |
| 373 | this.day = time.getDate(); |
| 374 | this.hour = time.getHours(); |
| 375 | this.minute = time.getMinutes(); |
| 376 | this.second = time.getSeconds(); |
| 377 | }, |
| 378 | init() { |
| 379 | this.valueArr = []; |
| 380 | this.reset = false; |
| 381 | if (this.mode == 'time') { |
| 382 | this.initTimeValue(); |
| 383 | if (this.params.year) { |
| 384 | this.valueArr.push(0); |
| 385 | this.setYears(); |
| 386 | } |
| 387 | if (this.params.month) { |
| 388 | this.valueArr.push(0); |
| 389 | this.setMonths(); |
| 390 | } |
| 391 | if (this.params.day) { |
| 392 | this.valueArr.push(0); |
| 393 | this.setDays(); |
| 394 | } |
| 395 | if (this.params.hour) { |
| 396 | this.valueArr.push(0); |
| 397 | this.setHours(); |
| 398 | } |
| 399 | if (this.params.minute) { |
| 400 | this.valueArr.push(0); |
| 401 | this.setMinutes(); |
| 402 | } |
| 403 | if (this.params.second) { |
| 404 | this.valueArr.push(0); |
| 405 | this.setSeconds(); |
| 406 | } |
| 407 | } else if (this.mode == 'region') { |
| 408 | if (this.params.province) { |
| 409 | this.valueArr.push(0); |
| 410 | this.setProvinces(); |
| 411 | } |
| 412 | if (this.params.city) { |
| 413 | this.valueArr.push(0); |
| 414 | this.setCitys(); |
| 415 | } |
| 416 | if (this.params.area) { |
| 417 | this.valueArr.push(0); |
| 418 | this.setAreas(); |
| 419 | } |
| 420 | } else if (this.mode == 'selector') { |
| 421 | this.valueArr = this.defaultSelector; |
| 422 | } else if (this.mode == 'multiSelector') { |
| 423 | this.valueArr = this.defaultSelector; |
| 424 | this.multiSelectorValue = this.defaultSelector; |
| 425 | } |
| 426 | this.$forceUpdate(); |
| 427 | }, |
| 428 | // 设置picker的某一列值 |
| 429 | setYears() { |
| 430 | // 获取年份集合 |
| 431 | this.years = this.generateArray(this.startYear, this.endYear); |
| 432 | // 设置this.valueArr某一项的值,是为了让picker预选中某一个值 |
| 433 | this.valueArr.splice(this.valueArr.length - 1, 1, this.getIndex(this.years, this.year)); |
| 434 | }, |
| 435 | setMonths() { |
| 436 | this.months = this.generateArray(1, 12); |
| 437 | this.valueArr.splice(this.valueArr.length - 1, 1, this.getIndex(this.months, this.month)); |
| 438 | }, |
| 439 | setDays() { |
| 440 | let totalDays = new Date(this.year, this.month, 0).getDate(); |
| 441 | this.days = this.generateArray(1, totalDays); |
| 442 | let index = 0; |
| 443 | // 这里不能使用类似setMonths()中的this.valueArr.splice(this.valueArr.length - 1, xxx)做法 |
| 444 | // 因为this.month和this.year变化时,会触发watch中的this.setDays(),导致this.valueArr.length计算有误 |
| 445 | if (this.params.year && this.params.month) index = 2; |
| 446 | else if (this.params.month) index = 1; |
| 447 | else if (this.params.year) index = 1; |
| 448 | else index = 0; |
| 449 | // 当月份变化时,会导致日期的天数也会变化,如果原来选的天数大于变化后的天数,则重置为变化后的最大值 |
| 450 | // 比如原来选中3月31日,调整为2月后,日期变为最大29,这时如果day值继续为31显然不合理,于是将其置为29(picker-column从1开始) |
| 451 | if(this.day > this.days.length) this.day = this.days.length; |
| 452 | this.valueArr.splice(index, 1, this.getIndex(this.days, this.day)); |
| 453 | }, |
| 454 | setHours() { |
| 455 | this.hours = this.generateArray(0, 23); |
| 456 | this.valueArr.splice(this.valueArr.length - 1, 1, this.getIndex(this.hours, this.hour)); |
| 457 | }, |
| 458 | setMinutes() { |
| 459 | this.minutes = this.generateArray(0, 59); |
| 460 | this.valueArr.splice(this.valueArr.length - 1, 1, this.getIndex(this.minutes, this.minute)); |
| 461 | }, |
| 462 | setSeconds() { |
| 463 | this.seconds = this.generateArray(0, 59); |
| 464 | this.valueArr.splice(this.valueArr.length - 1, 1, this.getIndex(this.seconds, this.second)); |
| 465 | }, |
| 466 | setProvinces() { |
| 467 | // 判断是否需要province参数 |
| 468 | if (!this.params.province) return; |
| 469 | let tmp = ''; |
| 470 | let useCode = false; |
| 471 | // 如果同时配置了defaultRegion和areaCode,优先使用areaCode参数 |
| 472 | if (this.areaCode.length) { |
| 473 | tmp = this.areaCode[0]; |
| 474 | useCode = true; |
| 475 | } else if (this.defaultRegion.length) tmp = this.defaultRegion[0]; |
| 476 | else tmp = 0; |
| 477 | // 历遍省份数组匹配 |
| 478 | provinces.map((v, k) => { |
| 479 | if (useCode ? v.value == tmp : v.label == tmp) { |
| 480 | tmp = k; |
| 481 | } |
| 482 | }); |
| 483 | this.province = tmp; |
| 484 | this.provinces = provinces; |
| 485 | // 设置默认省份的值 |
| 486 | this.valueArr.splice(0, 1, this.province); |
| 487 | }, |
| 488 | setCitys() { |
| 489 | if (!this.params.city) return; |
| 490 | let tmp = ''; |
| 491 | let useCode = false; |
| 492 | if (this.areaCode.length) { |
| 493 | tmp = this.areaCode[1]; |
| 494 | useCode = true; |
| 495 | } else if (this.defaultRegion.length) tmp = this.defaultRegion[1]; |
| 496 | else tmp = 0; |
| 497 | citys[this.province].map((v, k) => { |
| 498 | if (useCode ? v.value == tmp : v.label == tmp) { |
| 499 | tmp = k; |
| 500 | } |
| 501 | }); |
| 502 | this.city = tmp; |
| 503 | this.citys = citys[this.province]; |
| 504 | this.valueArr.splice(1, 1, this.city); |
| 505 | }, |
| 506 | setAreas() { |
| 507 | if (!this.params.area) return; |
| 508 | let tmp = ''; |
| 509 | let useCode = false; |
| 510 | if (this.areaCode.length) { |
| 511 | tmp = this.areaCode[2]; |
| 512 | useCode = true; |
| 513 | } else if (this.defaultRegion.length) tmp = this.defaultRegion[2]; |
| 514 | else tmp = 0; |
| 515 | areas[this.province][this.city].map((v, k) => { |
| 516 | if (useCode ? v.value == tmp : v.label == tmp) { |
| 517 | tmp = k; |
| 518 | } |
| 519 | }); |
| 520 | this.area = tmp; |
| 521 | this.areas = areas[this.province][this.city]; |
| 522 | this.valueArr.splice(2, 1, this.area); |
| 523 | }, |
| 524 | close() { |
| 525 | this.$emit('input', false); |
| 526 | }, |
| 527 | // 用户更改picker的列选项 |
| 528 | change(e) { |
| 529 | this.valueArr = e.detail.value; |
| 530 | let i = 0; |
| 531 | if (this.mode == 'time') { |
| 532 | // 这里使用i++,是因为this.valueArr数组的长度是不确定长度的,它根据this.params的值来配置长度 |
| 533 | // 进入if规则,i会加1,保证了能获取准确的值 |
| 534 | if (this.params.year) this.year = this.years[this.valueArr[i++]]; |
| 535 | if (this.params.month) this.month = this.months[this.valueArr[i++]]; |
| 536 | if (this.params.day) this.day = this.days[this.valueArr[i++]]; |
| 537 | if (this.params.hour) this.hour = this.hours[this.valueArr[i++]]; |
| 538 | if (this.params.minute) this.minute = this.minutes[this.valueArr[i++]]; |
| 539 | if (this.params.second) this.second = this.seconds[this.valueArr[i++]]; |
| 540 | } else if (this.mode == 'region') { |
| 541 | if (this.params.province) this.province = this.valueArr[i++]; |
| 542 | if (this.params.city) this.city = this.valueArr[i++]; |
| 543 | if (this.params.area) this.area = this.valueArr[i++]; |
| 544 | } else if (this.mode == 'multiSelector') { |
| 545 | let index = null; |
| 546 | // 对比前后两个数组,寻找变更的是哪一列,如果某一个元素不同,即可判定该列发生了变化 |
| 547 | this.defaultSelector.map((val, idx) => { |
| 548 | if (val != e.detail.value[idx]) index = idx; |
| 549 | }); |
| 550 | // 为了让用户对多列变化时,对动态设置其他列的变更 |
| 551 | if (index != null) { |
| 552 | this.$emit('columnchange', { |
| 553 | column: index, |
| 554 | index: e.detail.value[index] |
| 555 | }); |
| 556 | } |
| 557 | } |
| 558 | }, |
| 559 | // 用户点击确定按钮 |
| 560 | getResult(event = null) { |
| 561 | // #ifdef MP-WEIXIN |
| 562 | if (this.moving) return; |
| 563 | // #endif |
| 564 | let result = {}; |
| 565 | // 只返回用户在this.params中配置了为true的字段 |
| 566 | if (this.mode == 'time') { |
| 567 | if (this.params.year) result.year = this.formatNumber(this.year || 0); |
| 568 | if (this.params.month) result.month = this.formatNumber(this.month || 0); |
| 569 | if (this.params.day) result.day = this.formatNumber(this.day || 0); |
| 570 | if (this.params.hour) result.hour = this.formatNumber(this.hour || 0); |
| 571 | if (this.params.minute) result.minute = this.formatNumber(this.minute || 0); |
| 572 | if (this.params.second) result.second = this.formatNumber(this.second || 0); |
| 573 | if (this.params.timestamp) result.timestamp = this.getTimestamp(); |
| 574 | } else if (this.mode == 'region') { |
| 575 | if (this.params.province) result.province = provinces[this.province]; |
| 576 | if (this.params.city) result.city = citys[this.province][this.city]; |
| 577 | if (this.params.area) result.area = areas[this.province][this.city][this.area]; |
| 578 | } else if (this.mode == 'selector') { |
| 579 | result = this.valueArr; |
| 580 | } else if (this.mode == 'multiSelector') { |
| 581 | result = this.valueArr; |
| 582 | } |
| 583 | if (event) this.$emit(event, result); |
| 584 | this.close(); |
| 585 | }, |
| 586 | // 获取时间戳 |
| 587 | getTimestamp() { |
| 588 | // yyyy-mm-dd为安卓写法,不支持iOS,需要使用"/"分隔,才能二者兼容 |
| 589 | let time = this.year + '/' + this.month + '/' + this.day + ' ' + this.hour + ':' + this.minute + ':' + this.second; |
| 590 | return new Date(time).getTime() / 1000; |
| 591 | } |
| 592 | } |
| 593 | }; |
| 594 | </script> |
| 595 | |
| 596 | <style lang="scss" scoped> |
| 597 | @import '../../libs/css/style.components.scss'; |
| 598 | |
| 599 | .u-datetime-picker { |
| 600 | position: relative; |
| 601 | z-index: 999; |
| 602 | } |
| 603 | |
| 604 | .u-picker-view { |
| 605 | height: 100%; |
| 606 | box-sizing: border-box; |
| 607 | } |
| 608 | |
| 609 | .u-picker-header { |
| 610 | width: 100%; |
| 611 | height: 90rpx; |
| 612 | padding: 0 40rpx; |
| 613 | @include vue-flex; |
| 614 | justify-content: space-between; |
| 615 | align-items: center; |
| 616 | box-sizing: border-box; |
| 617 | font-size: 30rpx; |
| 618 | background: #fff; |
| 619 | position: relative; |
| 620 | } |
| 621 | |
| 622 | .u-picker-header::after { |
| 623 | content: ''; |
| 624 | position: absolute; |
| 625 | border-bottom: 1rpx solid #eaeef1; |
| 626 | -webkit-transform: scaleY(0.5); |
| 627 | transform: scaleY(0.5); |
| 628 | bottom: 0; |
| 629 | right: 0; |
| 630 | left: 0; |
| 631 | } |
| 632 | |
| 633 | .u-picker__title { |
| 634 | color: $u-content-color; |
| 635 | } |
| 636 | |
| 637 | .u-picker-body { |
| 638 | width: 100%; |
| 639 | height: 500rpx; |
| 640 | overflow: hidden; |
| 641 | background-color: #fff; |
| 642 | } |
| 643 | |
| 644 | .u-column-item { |
| 645 | @include vue-flex; |
| 646 | align-items: center; |
| 647 | justify-content: center; |
| 648 | font-size: 32rpx; |
| 649 | color: $u-main-color; |
| 650 | padding: 0 8rpx; |
| 651 | } |
| 652 | |
| 653 | .u-text { |
| 654 | font-size: 24rpx; |
| 655 | padding-left: 8rpx; |
| 656 | } |
| 657 | |
| 658 | .u-btn-picker { |
| 659 | padding: 16rpx; |
| 660 | box-sizing: border-box; |
| 661 | text-align: center; |
| 662 | text-decoration: none; |
| 663 | } |
| 664 | |
| 665 | .u-opacity { |
| 666 | opacity: 0.5; |
| 667 | } |
| 668 | |
| 669 | .u-btn-picker--primary { |
| 670 | color: $u-type-primary; |
| 671 | } |
| 672 | |
| 673 | .u-btn-picker--tips { |
| 674 | color: $u-tips-color; |
| 675 | } |
| 676 | </style> |