su-region-picker.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <template>
  2. <su-popup :show="show" @close="onCancel" round="20">
  3. <view class="ui-region-picker">
  4. <su-toolbar
  5. :cancelColor="cancelColor"
  6. :confirmColor="confirmColor"
  7. :cancelText="cancelText"
  8. :confirmText="confirmText"
  9. title="选择区域"
  10. @cancel="onCancel"
  11. @confirm="onConfirm('confirm')"
  12. ></su-toolbar>
  13. <view class="ui-picker-body">
  14. <picker-view
  15. :value="state.currentIndex"
  16. @change="change"
  17. class="ui-picker-view"
  18. @pickstart="pickstart"
  19. @pickend="pickend"
  20. >
  21. <picker-view-column>
  22. <view class="ui-column-item" v-for="province in provinceList" :key="province.id">
  23. <view :style="getSizeByNameLength(province.name)">{{ province.name }}</view>
  24. </view>
  25. </picker-view-column>
  26. <picker-view-column>
  27. <view class="ui-column-item" v-for="city in cityList" :key="city.id">
  28. <view :style="getSizeByNameLength(city.name)">{{ city.name }}</view>
  29. </view>
  30. </picker-view-column>
  31. <picker-view-column>
  32. <view class="ui-column-item" v-for="district in districtList" :key="district.id">
  33. <view :style="getSizeByNameLength(district.name)">{{ district.name }}</view>
  34. </view>
  35. </picker-view-column>
  36. </picker-view>
  37. </view>
  38. </view>
  39. </su-popup>
  40. </template>
  41. <script setup>
  42. /**
  43. * picker picker弹出选择器
  44. * @property {Object} params 需要显示的参
  45. * @property {Boolean} safe-area-inset-bottom 是否开启底部安全区适配(默认false)
  46. * @property {Boolean} show-time-tag 时间模式时,是否显示后面的年月日中文提示
  47. * @property {String} cancel-color 取消按钮的颜色
  48. * @property {String} confirm-color 确认按钮的颜色
  49. * @property {String} confirm-text 确认按钮的文字
  50. * @property {String} cancel-text 取消按钮的文字
  51. * @property {String} default-region 默认选中的地区,
  52. * @property {String} default-code 默认选中的地区
  53. * @property {Boolean} mask-close-able 是否允许通过点击遮罩关闭Picker(默认true)
  54. * @property {String Number} z-index 弹出时的z-index值(默认1075)
  55. * @property {Array} default-selector 数组形式,其中每一项表示选择了range对应项中的第几个
  56. * @property {String} range-key 当range参数的元素为对象时,指定Object中的哪个key的值作为选择器显示内容
  57. * @event {Function} confirm 点击确定按钮,返回当前选择的值
  58. * @event {Function} cancel 点击取消按钮,返回当前选择的值
  59. */
  60. import { computed, reactive } from 'vue';
  61. const props = defineProps({
  62. show: {
  63. type: Boolean,
  64. default: false,
  65. },
  66. // "取消"按钮的颜色
  67. cancelColor: {
  68. type: String,
  69. default: '#6666',
  70. },
  71. // "确定"按钮的颜色
  72. confirmColor: {
  73. type: String,
  74. default: 'var(--ui-BG-Main)',
  75. },
  76. // 取消按钮的文字
  77. cancelText: {
  78. type: String,
  79. default: '取消',
  80. },
  81. // 确认按钮的文字
  82. confirmText: {
  83. type: String,
  84. default: '确认',
  85. },
  86. });
  87. const areaData = uni.getStorageSync('areaData');
  88. const getSizeByNameLength = (name) => {
  89. let length = name.length;
  90. if (length <= 7) return '';
  91. if (length < 9) {
  92. return 'font-size:28rpx';
  93. } else {
  94. return 'font-size: 24rpx';
  95. }
  96. };
  97. const state = reactive({
  98. currentIndex: [0, 0, 0],
  99. moving: false, // 列是否还在滑动中,微信小程序如果在滑动中就点确定,结果可能不准确
  100. });
  101. const emits = defineEmits(['confirm', 'cancel', 'change']);
  102. const provinceList = areaData;
  103. const cityList = computed(() => {
  104. return areaData[state.currentIndex[0]].children;
  105. });
  106. const districtList = computed(() => {
  107. return cityList.value[state.currentIndex[1]]?.children;
  108. });
  109. // 标识滑动开始,只有微信小程序才有这样的事件
  110. const pickstart = () => {
  111. // #ifdef MP-WEIXIN
  112. state.moving = true;
  113. // #endif
  114. };
  115. // 标识滑动结束
  116. const pickend = () => {
  117. // #ifdef MP-WEIXIN
  118. state.moving = false;
  119. // #endif
  120. };
  121. const init = () => {};
  122. const onCancel = () => {
  123. emits('cancel');
  124. };
  125. // 用户更改picker的列选项
  126. const change = (e) => {
  127. if (
  128. state.currentIndex[0] === e.detail.value[0] &&
  129. state.currentIndex[1] === e.detail.value[1]
  130. ) {
  131. // 不更改省市区列表
  132. state.currentIndex[2] = e.detail.value[2];
  133. return;
  134. } else {
  135. // 更改省市区列表
  136. if (state.currentIndex[0] !== e.detail.value[0]) {
  137. e.detail.value[1] = 0;
  138. }
  139. e.detail.value[2] = 0;
  140. state.currentIndex = e.detail.value;
  141. }
  142. emits('change', state.currentIndex);
  143. };
  144. // 用户点击确定按钮
  145. const onConfirm = (event = null) => {
  146. // #ifdef MP-WEIXIN
  147. if (state.moving) return;
  148. // #endif
  149. let index = state.currentIndex;
  150. let province = provinceList[index[0]];
  151. let city = cityList.value[index[1]];
  152. let district = districtList.value[index[2]];
  153. let result = {
  154. province_name: province.name,
  155. province_id: province.id,
  156. city_name: city.name,
  157. city_id: city.id,
  158. district_name: district.name,
  159. district_id: district.id,
  160. };
  161. if (event) emits(event, result);
  162. };
  163. </script>
  164. <style lang="scss" scoped>
  165. .ui-region-picker {
  166. position: relative;
  167. z-index: 999;
  168. }
  169. .ui-picker-view {
  170. height: 100%;
  171. box-sizing: border-box;
  172. }
  173. .ui-picker-header {
  174. width: 100%;
  175. height: 90rpx;
  176. padding: 0 40rpx;
  177. display: flex;
  178. justify-content: space-between;
  179. align-items: center;
  180. box-sizing: border-box;
  181. font-size: 30rpx;
  182. background: #fff;
  183. position: relative;
  184. }
  185. .ui-picker-header::after {
  186. content: '';
  187. position: absolute;
  188. border-bottom: 1rpx solid #eaeef1;
  189. -webkit-transform: scaleY(0.5);
  190. transform: scaleY(0.5);
  191. bottom: 0;
  192. right: 0;
  193. left: 0;
  194. }
  195. .ui-picker__title {
  196. color: #333;
  197. }
  198. .ui-picker-body {
  199. width: 100%;
  200. height: 500rpx;
  201. overflow: hidden;
  202. background-color: #fff;
  203. }
  204. .ui-column-item {
  205. display: flex;
  206. align-items: center;
  207. justify-content: center;
  208. font-size: 32rpx;
  209. color: #333;
  210. padding: 0 8rpx;
  211. }
  212. .ui-btn-picker {
  213. padding: 16rpx;
  214. box-sizing: border-box;
  215. text-align: center;
  216. text-decoration: none;
  217. }
  218. .ui-opacity {
  219. opacity: 0.5;
  220. }
  221. .ui-btn-picker--primary {
  222. color: blue;
  223. }
  224. .ui-btn-picker--tips {
  225. color: red;
  226. }
  227. </style>