租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
6.6 KiB

6 months ago
  1. <template>
  2. <uv-popup ref="popup" mode="bottom" :popup="false" :mask="true" :closeable="true" :safe-area-inset-bottom="true"
  3. close-icon-color="#ffffff" :z-index="uZIndex" :maskCloseAble="maskCloseAble" @close="close">
  4. <uv-tabs v-if="value" lineColor="#a0cd63" :list="genTabsList" :scrollable="true" :current="tabsIndex" @change="tabsChange" ref="tabs"></uv-tabs>
  5. <view class="area-box">
  6. <view class="u-flex" :class="{ 'change':isChange }">
  7. <view class="area-item">
  8. <view class="u-padding-10 u-bg-gray" style="height: 100%;">
  9. <scroll-view :scroll-y="true" style="height: 100%">
  10. <uv-cell-group>
  11. <uv-cell v-for="(item,index) in provinces" :title="item.label" :arrow="false" :name="index" :key="index"
  12. @click="provinceChange">
  13. <template v-slot:right-icon>
  14. <uv-icon v-if="isChooseP&&province===index" size="34" color="#a0cd63" name="checkbox-mark"></uv-icon>
  15. </template>
  16. </uv-cell>
  17. </uv-cell-group>
  18. </scroll-view>
  19. </view>
  20. </view>
  21. <view class="area-item">
  22. <view class="u-padding-10 u-bg-gray" style="height: 100%;">
  23. <scroll-view :scroll-y="true" style="height: 100%">
  24. <uv-cell-group v-if="isChooseP">
  25. <uv-cell v-for="(item,index) in citys" :title="item.label" :arrow="false" :name="index" :key="index"
  26. @click="cityChange">
  27. <template v-slot:right-icon>
  28. <uv-icon v-if="isChooseC&&city===index" size="34" color="#a0cd63" name="checkbox-mark"></uv-icon>
  29. </template>
  30. </uv-cell>
  31. </uv-cell-group>
  32. </scroll-view>
  33. </view>
  34. </view>
  35. <view class="area-item">
  36. <view class="u-padding-10 u-bg-gray" style="height: 100%;">
  37. <scroll-view :scroll-y="true" style="height: 100%">
  38. <uv-cell-group v-if="isChooseC">
  39. <uv-cell v-for="(item,index) in areas" :title="item.label" :arrow="false" :name="index" :key="index"
  40. @click="areaChange">
  41. <template v-slot:right-icon>
  42. <uv-icon v-if="isChooseA&&area===index" size="34" color="#a0cd63" name="checkbox-mark"></uv-icon>
  43. </template>
  44. </uv-cell>
  45. </uv-cell-group>
  46. </scroll-view>
  47. </view>
  48. </view>
  49. </view>
  50. </view>
  51. </uv-popup>
  52. </template>
  53. <script>
  54. import provinces from "./utils/province.js";
  55. import citys from "./utils/city.js";
  56. import areas from "./utils/area.js";
  57. /**
  58. * city-select 省市区级联选择器
  59. * @property {String Number} z-index 弹出时的z-index值默认1075
  60. * @property {Boolean} mask-close-able 是否允许通过点击遮罩关闭Picker默认true
  61. * @property {String} default-region 默认选中的地区中文形式
  62. * @property {String} default-code 默认选中的地区编号形式
  63. */
  64. export default {
  65. props: {
  66. // 通过双向绑定控制组件的弹出与收起
  67. value: {
  68. type: Boolean,
  69. default: true
  70. },
  71. // 默认显示的地区,可传类似["河北省", "秦皇岛市", "北戴河区"]
  72. defaultRegion: {
  73. type: Array,
  74. default () {
  75. return [];
  76. }
  77. },
  78. // 默认显示地区的编码,defaultRegion和areaCode同时存在,areaCode优先,可传类似["13", "1303", "130304"]
  79. areaCode: {
  80. type: Array,
  81. default () {
  82. return [];
  83. }
  84. },
  85. // 是否允许通过点击遮罩关闭Picker
  86. maskCloseAble: {
  87. type: Boolean,
  88. default: true
  89. },
  90. // 弹出的z-index值
  91. zIndex: {
  92. type: [String, Number],
  93. default: 0
  94. }
  95. },
  96. data() {
  97. return {
  98. cityValue: "",
  99. isChooseP: false, //是否已经选择了省
  100. province: 0, //省级下标
  101. provinces: provinces,
  102. isChooseC: false, //是否已经选择了市
  103. city: 0, //市级下标
  104. citys: citys[0],
  105. isChooseA: false, //是否已经选择了区
  106. area: 0, //区级下标
  107. areas: areas[0][0],
  108. tabsIndex: 0,
  109. }
  110. },
  111. mounted() {
  112. this.init();
  113. },
  114. computed: {
  115. isChange() {
  116. return this.tabsIndex > 1;
  117. },
  118. genTabsList() {
  119. let tabsList = [{
  120. name: "请选择"
  121. }];
  122. if (this.isChooseP) {
  123. tabsList[0]['name'] = this.provinces[this.province]['label'];
  124. tabsList[1] = {
  125. name: "请选择"
  126. };
  127. }
  128. if (this.isChooseC) {
  129. tabsList[1]['name'] = this.citys[this.city]['label'];
  130. tabsList[2] = {
  131. name: "请选择"
  132. };
  133. }
  134. if (this.isChooseA) {
  135. tabsList[2]['name'] = this.areas[this.area]['label'];
  136. }
  137. return tabsList;
  138. },
  139. uZIndex() {
  140. // 如果用户有传递z-index值,优先使用
  141. return this.zIndex ? this.zIndex : 99;
  142. }
  143. },
  144. methods: {
  145. open(){
  146. this.$refs.popup.open();
  147. },
  148. init() {
  149. if (this.areaCode.length == 3) {
  150. this.setProvince("", this.areaCode[0]);
  151. this.setCity("", this.areaCode[1]);
  152. this.setArea("", this.areaCode[2]);
  153. } else if (this.defaultRegion.length == 3) {
  154. this.setProvince(this.defaultRegion[0], "");
  155. this.setCity(this.defaultRegion[1], "");
  156. this.setArea(this.defaultRegion[2], "");
  157. };
  158. },
  159. setProvince(label = "", value = "") {
  160. this.provinces.map((v, k) => {
  161. if (value ? v.value == value : v.label == label) {
  162. this.provinceChange(k);
  163. }
  164. })
  165. },
  166. setCity(label = "", value = "") {
  167. this.citys.map((v, k) => {
  168. if (value ? v.value == value : v.label == label) {
  169. this.cityChange(k);
  170. }
  171. })
  172. },
  173. setArea(label = "", value = "") {
  174. this.areas.map((v, k) => {
  175. if (value ? v.value == value : v.label == label) {
  176. this.isChooseA = true;
  177. this.area = k;
  178. }
  179. })
  180. },
  181. close() {
  182. this.$refs.popup.close();
  183. },
  184. tabsChange(index) {
  185. this.tabsIndex = index;
  186. },
  187. provinceChange(param) {
  188. this.isChooseP = true;
  189. this.isChooseC = false;
  190. this.isChooseA = false;
  191. this.province = param.name;
  192. this.citys = citys[param.name];
  193. this.tabsIndex = 1;
  194. },
  195. cityChange(param) {
  196. this.isChooseC = true;
  197. this.isChooseA = false;
  198. this.city = param.name;
  199. this.areas = areas[this.province][param.name];
  200. this.tabsIndex = 2;
  201. },
  202. areaChange(param) {
  203. this.isChooseA = true;
  204. this.area = param.name;
  205. let result = {};
  206. result.province = this.provinces[this.province];
  207. result.city = this.citys[this.city];
  208. result.area = this.areas[this.area];
  209. this.$emit('city-change', result);
  210. this.close();
  211. }
  212. }
  213. }
  214. </script>
  215. <style lang="scss">
  216. .area-box {
  217. width: 100%;
  218. overflow: hidden;
  219. height: 800rpx;
  220. .u-flex {
  221. display: flex;
  222. }
  223. .u-padding-10 {
  224. padding: 10rpx;
  225. }
  226. >view {
  227. width: 150%;
  228. transition: transform 0.3s ease-in-out 0s;
  229. transform: translateX(0);
  230. &.change {
  231. transform: translateX(-33.3333333%);
  232. }
  233. }
  234. .area-item {
  235. width: 33.3333333%;
  236. height: 800rpx;
  237. }
  238. }
  239. </style>