环卫车小程序前端代码
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.

235 lines
6.4 KiB

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