Blame view

src/components/SimpleAddress/SimpleAddress.vue 12.9 KB
936893552   BigBoss   新增地址页面
1
  <template>
7d6833f2c   范牧   地址列表
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    <view
      class="simple-address"
      v-if="showPopup"
      @touchmove.stop.prevent="clear"
    >
      <!-- 遮罩层 -->
      <view
        class="simple-address-mask"
        @touchmove.stop.prevent="clear"
        v-if="maskClick"
        :class="[ani + '-mask', animation ? 'mask-ani' : '']"
        :style="{
            'background-color': maskBgColor
          }"
        @tap="hideMask(true)"
      ></view>
936893552   BigBoss   新增地址页面
18
  
7d6833f2c   范牧   地址列表
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
      <view
        class="simple-address-content simple-address--fixed"
        :class="[type, ani + '-content', animation ? 'content-ani' : '']"
      >
        <view class="simple-address__header">
          <view
            class="simple-address__header-btn-box"
            @click="pickerCancel"
          >
            <text
              class="simple-address__header-text"
              :style="{ color: cancelColor, fontSize: btnFontSize }"
            >取消</text>
          </view>
          <view
            class="simple-address__header-btn-box"
            @click="pickerConfirm"
          >
            <text
              class="simple-address__header-text"
              :style="{ color: confirmColor || themeColor, fontSize: btnFontSize }"
            >确定</text>
          </view>
        </view>
        <view class="simple-address__box">
          <picker-view
            indicator-style="height: 70rpx;"
            class="simple-address-view"
            :value="pickerValue"
            @change="pickerChange"
          >
            <picker-view-column>
              <!-- #ifndef APP-NVUE -->
              <view
                class="picker-item"
                :style="{ fontSize: fontSize }"
                v-for="(item, index) in provinceDataList"
                :key="index"
              >{{ item.label }}</view>
              <!-- #endif -->
              <!-- #ifdef APP-NVUE -->
              <text
                class="picker-item"
                :style="{ fontSize: fontSize }"
                v-for="(item, index) in provinceDataList"
                :key="index"
              >{{ item.label }}</text>
              <!-- #endif -->
            </picker-view-column>
            <picker-view-column>
              <!-- #ifndef APP-NVUE -->
              <view
                class="picker-item"
                :style="{ fontSize: fontSize }"
                v-for="(item, index) in cityDataList"
                :key="index"
              >{{ item.label }}</view>
              <!-- #endif -->
              <!-- #ifdef APP-NVUE -->
              <text
                class="picker-item"
                :style="{ fontSize: fontSize }"
                v-for="(item, index) in cityDataList"
                :key="index"
              >{{ item.label }}</text>
              <!-- #endif -->
            </picker-view-column>
            <picker-view-column>
              <!-- #ifndef APP-NVUE -->
              <view
                class="picker-item"
                :style="{ fontSize: fontSize }"
                v-for="(item, index) in areaDataList"
                :key="index"
              >{{ item.label }}</view>
              <!-- #endif -->
              <!-- #ifdef APP-NVUE -->
              <text
                class="picker-item"
                :style="{ fontSize: fontSize }"
                v-for="(item, index) in areaDataList"
                :key="index"
              >{{ item.label }}</text>
              <!-- #endif -->
            </picker-view-column>
          </picker-view>
        </view>
      </view>
    </view>
936893552   BigBoss   新增地址页面
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  </template>
  
  <script>
  /**
   * Simple-addres  地址联动组件
   * @description 三级地址联动,支持(app)nvue、小程序、H5
   * @tutorial https://ext.dcloud.net.cn/plugin?id=1084
   * @property {String} animation 是否开启动画
   * @property {String} type = [bottom] 弹出层类型,暂时只支持底部弹出
   * @property {Boolean} maskClick = [true | false] 是否允许点击遮罩层关闭
   * @property {Boolean} show = [true | false]  显示或隐藏地址组件
   * @property {String} maskBgColor 遮罩层背景颜色
   * @property {String} cancelColor 取消按钮颜色,默认为:#1aad19
   * @property {String} confirmColor 确认按钮颜色,默认为:themeColor
   * @property {String} themeColor 主题颜色,后续会废弃该配置,建议使用`cancelColor`或`confirmColor`
   * @property {String} btnFontSize 取消、确认按钮字体大小,默认为`uni.scss里的 $uni-font-size-base `
   * @property {String} fontSize picker-item字体大小,默认为:28rpx
   * @property {Array} pickerValueDefault 默认值,可以通过function queryIndex 获取
   * @property {Function} queryIndex 根据自定义信息返回对应的index
   * @property {Function} open 打开
   * @example  <simple-address ref="simpleAddress" :pickerValueDefault="cityPickerValueDefault" @onConfirm="onConfirm" themeColor='#007AFF'></simple-address>
   */
  
7d6833f2c   范牧   地址列表
131
132
133
  import provinceData from './cityData/province.js'
  import cityData from './cityData/city.js'
  import areaData from './cityData/area.js'
936893552   BigBoss   新增地址页面
134
  export default {
7d6833f2c   范牧   地址列表
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
    name: 'simpleAddress',
    props: {
      mode: {
        // 地址类型
        // default 则代表老版本根据index索引获取数据
        //
        type: String,
        default: 'default'
      },
      // 开启动画
      animation: {
        type: Boolean,
        default: true
      },
      /* 弹出层类型,可选值;
       *bottom:底部弹出层
       */
      type: {
        type: String,
        default: 'bottom'
      },
      // maskClick
      maskClick: {
        type: Boolean,
        default: true
      },
      show: {
        type: Boolean,
        default: true
      },
      maskBgColor: {
        type: String,
        default: 'rgba(0, 0, 0, 0.4)' // 背景颜色 rgba(0, 0, 0, 0.4) 为空则调用 uni.scss
      },
      themeColor: {
        type: String,
        default: '' // 确认按钮颜色(向下兼容)
      },
      cancelColor: {
        type: String,
        default: '' // 取消按钮颜色
      },
      confirmColor: {
        type: String,
        default: '' // 确认按钮颜色
      },
      fontSize: {
        type: String,
        default: '28rpx' // picker-item字体大小
      },
      btnFontSize: {
        type: String,
        default: '' // 按钮的字体大小
      },
      /* 默认值 */
      pickerValueDefault: {
        type: Array,
        default () {
          return [0, 0, 0]
        }
      }
    },
    data () {
      return {
        ani: '',
        showPopup: false,
        pickerValue: [0, 0, 0],
        provinceDataList: [],
        cityDataList: [],
        areaDataList: []
      }
    },
    watch: {
      show (newValue) {
        if (newValue) {
          this.open()
        } else {
          this.close()
        }
      },
      pickerValueDefault () {
        this.init()
      }
    },
    created () {
      this.init()
    },
    methods: {
      init () {
        this.handPickValueDefault() // 对 pickerValueDefault 做兼容处理
        this.provinceDataList = provinceData
        this.cityDataList = cityData[this.pickerValueDefault[0]]
        this.areaDataList = areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]]
        this.pickerValue = this.pickerValueDefault
      },
      handPickValueDefault () {
        if (this.pickerValueDefault !== [0, 0, 0]) {
          if (this.pickerValueDefault[0] > provinceData.length - 1) {
            this.pickerValueDefault[0] = provinceData.length - 1
          }
          if (this.pickerValueDefault[1] > cityData[this.pickerValueDefault[0]].length - 1) {
            this.pickerValueDefault[1] = cityData[this.pickerValueDefault[0]].length - 1
          }
          if (this.pickerValueDefault[2] > areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]].length - 1) {
            this.pickerValueDefault[2] = areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]].length - 1
          }
        }
      },
      pickerChange (e) {
        const changePickerValue = e.detail.value
        if (this.pickerValue[0] !== changePickerValue[0]) {
          // 第一级发生滚动
          this.cityDataList = cityData[changePickerValue[0]]
          this.areaDataList = areaData[changePickerValue[0]][0]
          changePickerValue[1] = 0
          changePickerValue[2] = 0
        } else if (this.pickerValue[1] !== changePickerValue[1]) {
          // 第二级滚动
          this.areaDataList = areaData[changePickerValue[0]][changePickerValue[1]]
          changePickerValue[2] = 0
        }
        this.pickerValue = changePickerValue
        this._$emit('onChange')
      },
      _$emit (emitName) {
        const pickObj = {
          label: this._getLabel(),
          value: this.pickerValue,
          cityCode: this._getCityCode(),
          areaCode: this._getAreaCode(),
          provinceCode: this._getProvinceCode(),
          labelArr: this._getLabel().split('-')
        }
        this.$emit(emitName, pickObj)
      },
      _getLabel () {
        const pcikerLabel = this.provinceDataList[this.pickerValue[0]].label + '-' + this.cityDataList[this.pickerValue[1]].label + '-' + this.areaDataList[this.pickerValue[2]].label
        return pcikerLabel
      },
      _getCityCode () {
        return this.cityDataList[this.pickerValue[1]].value
      },
      _getProvinceCode () {
        return this.provinceDataList[this.pickerValue[0]].value
      },
      _getAreaCode () {
        return this.areaDataList[this.pickerValue[2]].value
      },
      queryIndex (params = [], type = 'value') {
        // params = [ 11 ,1101,110101 ];
        // 1.获取省份的index
        const provinceIndex = provinceData.findIndex(res => res[type] === params[0])
        const cityIndex = cityData[provinceIndex].findIndex(res => res[type] === params[1])
        const areaIndex = areaData[provinceIndex][cityIndex].findIndex(res => res[type] === params[2])
        return {
          index: [provinceIndex, cityIndex, areaIndex],
          data: {
            province: provinceData[provinceIndex],
            city: cityData[provinceIndex][cityIndex],
            area: areaData[provinceIndex][cityIndex][areaIndex]
          }
        }
      },
      clear () {},
      hideMask () {
        this._$emit('onCancel')
        this.close()
      },
      pickerCancel () {
        this._$emit('onCancel')
        this.close()
      },
      pickerConfirm () {
        this._$emit('onConfirm')
        this.close()
      },
      open () {
        this.showPopup = true
        this.$nextTick(() => {
          setTimeout(() => {
            this.ani = 'simple-' + this.type
          }, 100)
        })
      },
      close (type) {
        if (!this.maskClick && type) return
        this.ani = ''
        this.$nextTick(() => {
          setTimeout(() => {
            this.showPopup = false
          }, 300)
        })
      }
    }
  }
936893552   BigBoss   新增地址页面
330
331
332
333
  </script>
  
  <style lang="scss" scoped>
  .simple-address {
7d6833f2c   范牧   地址列表
334
335
336
337
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: column;
936893552   BigBoss   新增地址页面
338
339
340
  }
  
  .simple-address-mask {
7d6833f2c   范牧   地址列表
341
342
343
344
345
    position: fixed;
    bottom: 0;
    top: 0;
    left: 0;
    right: 0;
936893552   BigBoss   新增地址页面
346
  
7d6833f2c   范牧   地址列表
347
348
349
350
351
352
    transition-property: opacity;
    transition-duration: 0.3s;
    opacity: 0;
    /* #ifndef APP-NVUE */
    z-index: 99;
    /* #endif */
936893552   BigBoss   新增地址页面
353
354
355
  }
  
  .mask-ani {
7d6833f2c   范牧   地址列表
356
357
    transition-property: opacity;
    transition-duration: 0.2s;
936893552   BigBoss   新增地址页面
358
359
360
  }
  
  .simple-bottom-mask {
7d6833f2c   范牧   地址列表
361
    opacity: 1;
936893552   BigBoss   新增地址页面
362
363
364
  }
  
  .simple-center-mask {
7d6833f2c   范牧   地址列表
365
    opacity: 1;
936893552   BigBoss   新增地址页面
366
367
368
  }
  
  .simple-address--fixed {
7d6833f2c   范牧   地址列表
369
370
371
372
373
374
375
376
377
378
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    transition-property: transform;
    transition-duration: 0.3s;
    transform: translateY(460rpx);
    /* #ifndef APP-NVUE */
    z-index: 99;
    /* #endif */
936893552   BigBoss   新增地址页面
379
380
381
  }
  
  .simple-address-content {
7d6833f2c   范牧   地址列表
382
    background-color: #ffffff;
936893552   BigBoss   新增地址页面
383
384
385
  }
  
  .simple-content-bottom {
7d6833f2c   范牧   地址列表
386
387
388
389
    bottom: 0;
    left: 0;
    right: 0;
    transform: translateY(500rpx);
936893552   BigBoss   新增地址页面
390
391
392
  }
  
  .content-ani {
7d6833f2c   范牧   地址列表
393
394
    transition-property: transform, opacity;
    transition-duration: 0.2s;
936893552   BigBoss   新增地址页面
395
396
397
  }
  
  .simple-bottom-content {
7d6833f2c   范牧   地址列表
398
    transform: translateY(0);
936893552   BigBoss   新增地址页面
399
400
401
  }
  
  .simple-center-content {
7d6833f2c   范牧   地址列表
402
403
    transform: scale(1);
    opacity: 1;
936893552   BigBoss   新增地址页面
404
405
406
  }
  
  .simple-address__header {
7d6833f2c   范牧   地址列表
407
408
409
410
411
412
413
414
415
416
    position: relative;
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: space-between;
    border-bottom-color: #f2f2f2;
    border-bottom-style: solid;
    border-bottom-width: 1rpx;
936893552   BigBoss   新增地址页面
417
418
419
  }
  
  .simple-address--fixed-top {
7d6833f2c   范牧   地址列表
420
421
422
423
424
425
426
427
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: row;
    justify-content: space-between;
    border-top-color: $uni-border-color;
    border-top-style: solid;
    border-top-width: 1rpx;
936893552   BigBoss   新增地址页面
428
429
430
  }
  
  .simple-address__header-btn-box {
7d6833f2c   范牧   地址列表
431
432
433
434
435
436
437
    /* #ifndef APP-NVUE */
    display: flex;
    /* #endif */
    flex-direction: row;
    align-items: center;
    justify-content: center;
    height: 70rpx;
936893552   BigBoss   新增地址页面
438
439
440
  }
  
  .simple-address__header-text {
7d6833f2c   范牧   地址列表
441
442
443
444
445
446
    text-align: center;
    font-size: $uni-font-size-base;
    color: #1aad19;
    line-height: 70rpx;
    padding-left: 40rpx;
    padding-right: 40rpx;
936893552   BigBoss   新增地址页面
447
448
449
  }
  
  .simple-address__box {
7d6833f2c   范牧   地址列表
450
    position: relative;
936893552   BigBoss   新增地址页面
451
452
453
  }
  
  .simple-address-view {
7d6833f2c   范牧   地址列表
454
455
456
457
458
459
460
461
462
463
464
    position: relative;
    bottom: 0;
    left: 0;
    /* #ifndef APP-NVUE */
    width: 100%;
    /* #endif */
    /* #ifdef APP-NVUE */
    width: 750rpx;
    /* #endif */
    height: 408rpx;
    background-color: rgba(255, 255, 255, 1);
936893552   BigBoss   新增地址页面
465
466
467
  }
  
  .picker-item {
7d6833f2c   范牧   地址列表
468
469
470
471
    text-align: center;
    line-height: 70rpx;
    text-overflow: ellipsis;
    font-size: 28rpx;
936893552   BigBoss   新增地址页面
472
  }
9ff2df1bf   范牧   文件名称规范
473
  </style>