cc-selectDityBak.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <template>
  2. <view>
  3. <!--自定义地址选择器-->
  4. <view class="cc_area_mask" v-show="show == true"></view>
  5. <view :class="'cc_area_view ' + (show ? 'show':'hide')">
  6. <view class="cc_area_view_btns">
  7. <text class="cc_area_view_btn_cancle" @tap="handleNYZAreaCancle">取消</text>
  8. <text class="cc_area_view_btn_title" style="color: #393939;font-size: 32upx;">地区选择</text>
  9. <text class="cc_area_view_btn_sure" @tap="handleNYZAreaSelect" :data-province="province"
  10. :data-city="city" :data-area="area" style="color: #4284e5;">确定</text>
  11. </view>
  12. <picker-view class="cc_area_pick_view" indicator-style="height: 35px;" @change="handleNYZAreaChange"
  13. :value="value">
  14. <picker-view-column>
  15. <view v-for="(item, index) in provinces" :key="index" class="cc_area_colum_view">{{item}}</view>
  16. </picker-view-column>
  17. <picker-view-column>
  18. <view v-for="(item, index) in citys" :key="index" class="cc_area_colum_view">{{item}}</view>
  19. </picker-view-column>
  20. <picker-view-column>
  21. <view v-for="(item, index) in areas" :key="index" class="cc_area_colum_view">{{item}}</view>
  22. </picker-view-column>
  23. </picker-view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. import {
  29. getProvinces,
  30. getMyCity,
  31. getAreas,
  32. getAreasCode
  33. } from "./area.js"
  34. let index = [0, 0, 0];
  35. let provinces = getProvinces();
  36. let citys = getMyCity(index[0]);
  37. let areas = getMyCity(index[0], index[1]);
  38. export default {
  39. mixins: [{
  40. methods: {
  41. setData: function(obj, callback) {
  42. let that = this;
  43. const handleData = (tepData, tepKey, afterKey) => {
  44. tepKey = tepKey.split('.');
  45. tepKey.forEach(item => {
  46. if (tepData[item] === null || tepData[item] === undefined) {
  47. let reg = /^[0-9]+$/;
  48. tepData[item] = reg.test(afterKey) ? [] : {};
  49. tepData = tepData[item];
  50. } else {
  51. tepData = tepData[item];
  52. }
  53. });
  54. return tepData;
  55. };
  56. const isFn = function(value) {
  57. return typeof value == 'function' || false;
  58. };
  59. Object.keys(obj).forEach(function(key) {
  60. let val = obj[key];
  61. key = key.replace(/\]/g, '').replace(/\[/g, '.');
  62. let front, after;
  63. let index_after = key.lastIndexOf('.');
  64. if (index_after != -1) {
  65. after = key.slice(index_after + 1);
  66. front = handleData(that, key.slice(0, index_after), after);
  67. } else {
  68. after = key;
  69. front = that;
  70. }
  71. if (front.$data && front.$data[after] === undefined) {
  72. Object.defineProperty(front, after, {
  73. get() {
  74. return front.$data[after];
  75. },
  76. set(newValue) {
  77. front.$data[after] = newValue;
  78. that.$forceUpdate();
  79. },
  80. enumerable: true,
  81. configurable: true
  82. });
  83. // #ifndef VUE3
  84. that.$set(front, after, val);
  85. // #endif
  86. // #ifdef VUE3
  87. Reflect.set(front, after, val);
  88. // #endif
  89. } else {
  90. // #ifndef VUE3
  91. that.$set(front, after, val);
  92. // #endif
  93. // #ifdef VUE3
  94. Reflect.set(front, after, val);
  95. // #endif
  96. }
  97. });
  98. isFn(callback) && this.$nextTick(callback);
  99. }
  100. }
  101. }],
  102. data() {
  103. return {
  104. provinces: getProvinces(),
  105. citys: getMyCity(index[0]),
  106. areas: getAreas(index[0], index[1]),
  107. value: [0, 0, 0]
  108. };
  109. },
  110. components: {},
  111. props: {
  112. // 省
  113. province: {
  114. //控制area_select显示隐藏
  115. type: String,
  116. default: ''
  117. },
  118. // 市
  119. city: {
  120. //控制area_select显示隐藏
  121. type: String,
  122. default: ''
  123. },
  124. // 区
  125. area: {
  126. //控制area_select显示隐藏
  127. type: String,
  128. default: ''
  129. },
  130. show: {
  131. //控制area_select显示隐藏
  132. type: Boolean,
  133. default: false
  134. },
  135. maskShow: {
  136. //是否显示蒙层
  137. type: Boolean,
  138. default: true
  139. }
  140. },
  141. watch: {
  142. province() {
  143. this.init();
  144. },
  145. city() {
  146. this.init();
  147. },
  148. area() {
  149. this.init();
  150. }
  151. },
  152. mounted() {
  153. let provinceIndex = this.provinces.indexOf(this.province);
  154. this.citys = getMyCity(provinceIndex);
  155. let cityIndex = this.citys.indexOf(this.city);
  156. this.areas = getAreas(provinceIndex, cityIndex);
  157. let areaIndex = this.areas.indexOf(this.area);
  158. // 设置选择序列
  159. this.value = [provinceIndex, cityIndex, areaIndex];
  160. let areaCode = getAreasCode(provinceIndex, cityIndex, areaIndex);
  161. //console.log(areaCode)
  162. //console.log("this.value = " + JSON.stringify(this.value));
  163. },
  164. methods: {
  165. init() {
  166. //console.log(this.area)
  167. let provinceIndex = this.provinces.indexOf(this.province);
  168. this.citys = getMyCity(provinceIndex);
  169. let cityIndex = this.citys.indexOf(this.city);
  170. this.areas = getAreas(provinceIndex, cityIndex);
  171. let areaIndex = this.areas.indexOf(this.area);
  172. //获取地区编码
  173. let areaCode = getAreasCode(provinceIndex, cityIndex, areaIndex);
  174. // 设置选择序列
  175. this.value = [provinceIndex, cityIndex, areaIndex];
  176. },
  177. handleNYZAreaChange: function(e) {
  178. var that = this;
  179. var value = e.detail.value;
  180. // 更新 index
  181. index = value;
  182. // 获取对应的城市和区域数据
  183. let selectCitys = getMyCity(index[0]);
  184. let selectAreas = getAreas(index[0], index[1]);
  185. // 触发 setData 更新数据
  186. that.setData({
  187. citys: selectCitys,
  188. areas: selectAreas,
  189. value: index
  190. });
  191. let areaCode = getAreasCode(index[0], index[1], index[2]);
  192. // 触发事件
  193. that.$emit("changeClick", provinces[index[0]], selectCitys[index[1]], selectAreas[index[2]], areaCode);
  194. },
  195. /**
  196. * 确定按钮的点击事件
  197. */
  198. handleNYZAreaSelect: function(e) {
  199. var myEventDetail = e;
  200. var myEventOption = {};
  201. this.$emit('sureSelectArea', {
  202. detail: myEventDetail
  203. }, myEventOption);
  204. // 复原初始状态
  205. index = [0, 0, 0];
  206. },
  207. handleNYZAreaCancle: function(e) {
  208. var that = this;
  209. this.$emit('hideShow', {
  210. detail: false
  211. });
  212. // 复原初始状态
  213. index = [0, 0, 0];
  214. }
  215. },
  216. /**
  217. * 取消按钮的点击事件
  218. */
  219. handleNYZAreaCancle: function(e) {
  220. var that = this;
  221. //console.log("e:" + JSON.stringify(e));
  222. this.$emit('hideShow', {
  223. detail: false
  224. });
  225. // 复原初始状态
  226. index = [0, 0, 0];
  227. }
  228. }
  229. </script>
  230. <style scoped lang="scss">
  231. .cc_area_view {
  232. width: 100%;
  233. position: fixed;
  234. bottom: -1000px;
  235. left: 0px;
  236. background-color: #fff;
  237. z-index: 21;
  238. transition: all 0.3s;
  239. }
  240. .cc_area_pick_view {
  241. height: 400px;
  242. width: 100%;
  243. }
  244. .cc_area_colum_view {
  245. line-height: 35px;
  246. text-align: center;
  247. font-size: 28upx;
  248. }
  249. .hide {
  250. bottom: -1000upx;
  251. transition: all 0.3s;
  252. }
  253. .show {
  254. bottom: 0upx;
  255. transition: all 0.3s;
  256. }
  257. .cc_area_view_btns {
  258. background-color: #fff;
  259. border-bottom: 1px solid #eeeeee;
  260. font-size: 30upx;
  261. padding: 18upx 0upx;
  262. display: flex;
  263. justify-content: space-between;
  264. }
  265. .cc_area_view_btns>text {
  266. display: inline-block;
  267. word-spacing: 4upx;
  268. letter-spacing: 4upx;
  269. }
  270. .cc_area_view_btn_cancle {
  271. color: #939393;
  272. padding-right: 20upx;
  273. padding-left: 25upx;
  274. }
  275. .cc_area_view_btn_sure {
  276. float: right;
  277. padding-left: 20upx;
  278. padding-right: 25upx;
  279. }
  280. .cc_area_mask {
  281. width: 100%;
  282. height: 100vh;
  283. background-color: rgba(28, 28, 28, 0.6);
  284. position: absolute;
  285. top: 0upx;
  286. left: 0upx;
  287. z-index: 20;
  288. }
  289. </style>