cc-selectDity.vue 8.6 KB

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