su-region-picker.vue 6.6 KB

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