mpvueCityPicker.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. <template>
  2. <div class="mpvue-picker">
  3. <div :class="{'pickerMask':showPicker}" @click="maskClick" catchtouchmove="true"></div>
  4. <div class="mpvue-picker-content " :class="{'mpvue-picker-view-show':showPicker}">
  5. <div class="mpvue-picker__hd" catchtouchmove="true">
  6. <div class="mpvue-picker__action" @click="pickerCancel">取消</div>
  7. <div class="mpvue-picker__action" :style="{color:themeColor}" @click="pickerConfirm">确定</div>
  8. </div>
  9. <picker-view indicator-style="height: 40px;" class="mpvue-picker-view" :value="pickerValue" @change="pickerChange">
  10. <block>
  11. <picker-view-column>
  12. <div class="picker-item" v-for="(item,index) in provinceDataList" :key="index">{{item.label}}</div>
  13. </picker-view-column>
  14. <picker-view-column>
  15. <div class="picker-item" v-for="(item,index) in cityDataList" :key="index">{{item.label}}</div>
  16. </picker-view-column>
  17. <picker-view-column>
  18. <div class="picker-item" v-for="(item,index) in areaDataList" :key="index">{{item.label}}</div>
  19. </picker-view-column>
  20. </block>
  21. </picker-view>
  22. </div>
  23. </div>
  24. </template>
  25. <script>
  26. import {provinceData,cityData,areaData,cityInfoList,setCityData} from './city-data/province.js';
  27. import * as loginApi from '@/apis/login.js'
  28. //import cityData from './city-data/city.js';
  29. //import areaData from './city-data/area.js';
  30. //let provinceData =[];
  31. //let cityData = [];
  32. //let areaData = [];
  33. let _self ;
  34. export default {
  35. data() {
  36. return {
  37. pickerValue: [0, 0, 0],
  38. provinceDataList: [],
  39. cityDataList: [],
  40. areaDataList: [],
  41. /* 是否显示控件 */
  42. showPicker: false,
  43. };
  44. },
  45. created() {
  46. _self = this;
  47. if(provinceData.length<=0){
  48. loginApi.getCityList().then(function(data){
  49. //console.log('获取城市成功'+JSON.stringify(data));
  50. if(data.result && data.data){
  51. setCityData(data.data);
  52. //_self.$refs.mpvuebirtydayCityPicker.setCityData(data.data);
  53. //console.log('province data '+JSON.stringify(provinceData))
  54. }
  55. _self.init()
  56. },function(err){
  57. console.log('获取城市列表错误'+JSON.stringify(err))
  58. })
  59. }else
  60. {
  61. this.init()
  62. }
  63. //console.log('mpvueCityPicker created')
  64. },
  65. props: {
  66. AreaListDefault:{
  67. type: Array,
  68. default(){
  69. return []
  70. }
  71. },
  72. /* 默认值 */
  73. pickerValueDefault: {
  74. type: Array,
  75. default(){
  76. return [0, 0, 0]
  77. }
  78. },
  79. /* 主题色 */
  80. themeColor: String
  81. },
  82. watch:{
  83. pickerValueDefault(){
  84. this.init();
  85. }
  86. },
  87. methods: {
  88. init() {
  89. //console.log('省'+JSON.stringify(provinceData));
  90. //console.log('市'+JSON.stringify(cityData));
  91. //console.log('区'+JSON.stringify(areaData));
  92. //console.log('mpvueCityPicker init')
  93. if(this.pickerValueDefault.length == 0)
  94. this.pickerValueDefault = [0,0,0];
  95. this.handPickValueDefault(); // 对 pickerValueDefault 做兼容处理
  96. this.provinceDataList = provinceData;
  97. //console.log('provinces'+JSON.stringify(this.provinceDataList))
  98. //console.log('省'+JSON.stringify(this.pickerValueDefault))
  99. this.cityDataList = cityData[this.pickerValueDefault[0]];
  100. this.areaDataList = areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]];
  101. //console.log('citys'+JSON.stringify(this.cityDataList))
  102. //console.log('area'+JSON.stringify(this.areaDataList))
  103. this.pickerValue = this.pickerValueDefault;
  104. },
  105. getProvinceDataListLength(){
  106. return this.provinceDataList.length;
  107. },
  108. getCityDataListLength(){
  109. return this.cityDataList.length;
  110. },
  111. getAreaDataListLength(){
  112. return this.areaDataList.length;
  113. },
  114. show() {
  115. setTimeout(() => {
  116. this.showPicker = true;
  117. }, 0);
  118. },
  119. maskClick() {
  120. this.pickerCancel();
  121. },
  122. pickerCancel() {
  123. this.showPicker = false;
  124. this._$emit('onCancel');
  125. },
  126. pickerConfirm(e) {
  127. this.showPicker = false;
  128. this._$emit('onConfirm');
  129. },
  130. showPickerView() {
  131. this.showPicker = true;
  132. },
  133. handPickValueDefault() {
  134. //console.log('mpvueCityPicker handPickValueDefault')
  135. //console.log('默认树'+JSON.stringify(this.pickerValueDefault))
  136. //console.log('省'+JSON.stringify(this.pickerValueDefault))
  137. if (this.pickerValueDefault !== [0, 0, 0]) {
  138. if (this.pickerValueDefault[0] > provinceData.length - 1) {
  139. this.pickerValueDefault[0] = provinceData.length - 1;
  140. console.log('省索引'+JSON.stringify(this.pickerValueDefault[0]))
  141. }
  142. // console.log('handPickValueDefault 1'+JSON.stringify(cityData));
  143. // console.log('pickerValueDefault[1]' +JSON.stringify(this.pickerValueDefault));
  144. if(this.pickerValueDefault.length>1)
  145. {
  146. if (this.pickerValueDefault[1] > cityData[this.pickerValueDefault[0]].length - 1) {
  147. this.pickerValueDefault[1] = cityData[this.pickerValueDefault[0]].length - 1;
  148. console.log('市索引'+JSON.stringify(this.pickerValueDefault[1]))
  149. }
  150. }
  151. //console.log('cityData[this.pickerValueDefault[0]].length' +JSON.stringify(cityData[this.pickerValueDefault[0]].length));
  152. //console.log('handPickValueDefault 2');
  153. if(this.pickerValueDefault.length>2)
  154. {
  155. if (this.pickerValueDefault[2] > areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]].length - 1) {
  156. this.pickerValueDefault[2] = areaData[this.pickerValueDefault[0]][this.pickerValueDefault[1]].length - 1;
  157. console.log('区索引'+JSON.stringify(this.pickerValueDefault[2]))
  158. }
  159. }
  160. //console.log('handPickValueDefault 3');
  161. }
  162. },
  163. pickerChange(e) {
  164. let changePickerValue = e.mp.detail.value;
  165. if (this.pickerValue[0] !== changePickerValue[0]) {
  166. // 第一级发生滚动
  167. this.cityDataList = cityData[changePickerValue[0]];
  168. this.areaDataList = areaData[changePickerValue[0]][0];
  169. changePickerValue[1] = 0;
  170. changePickerValue[2] = 0;
  171. } else if (this.pickerValue[1] !== changePickerValue[1]) {
  172. // 第二级滚动
  173. this.areaDataList =
  174. areaData[changePickerValue[0]][changePickerValue[1]];
  175. changePickerValue[2] = 0;
  176. }
  177. this.pickerValue = changePickerValue;
  178. this._$emit('onChange');
  179. },
  180. _$emit(emitName) {
  181. let pickObj = {
  182. label: this._getLabel(),
  183. value: this.pickerValue,
  184. cityCode: this._getCityCode()
  185. };
  186. this.$emit(emitName, pickObj);
  187. },
  188. _getLabel() {
  189. let pcikerLabel =
  190. this.provinceDataList[this.pickerValue[0]].label +
  191. '-' +
  192. this.cityDataList[this.pickerValue[1]].label +
  193. '-' +
  194. this.areaDataList[this.pickerValue[2]].label;
  195. return pcikerLabel;
  196. },
  197. _getCityCode() {
  198. return this.areaDataList[this.pickerValue[2]].value;
  199. }
  200. }
  201. };
  202. </script>
  203. <style>
  204. .pickerMask {
  205. position: fixed;
  206. z-index: 1000;
  207. top: 0;
  208. right: 0;
  209. left: 0;
  210. bottom: 0;
  211. background: rgba(0, 0, 0, 0.6);
  212. }
  213. .mpvue-picker-content {
  214. position: fixed;
  215. bottom: 0;
  216. left: 0;
  217. width: 100%;
  218. transition: all 0.3s ease;
  219. transform: translateY(100%);
  220. z-index: 3000;
  221. }
  222. .mpvue-picker-view-show {
  223. transform: translateY(0);
  224. }
  225. .mpvue-picker__hd {
  226. display: flex;
  227. padding: 9px 15px;
  228. background-color: #fff;
  229. position: relative;
  230. text-align: center;
  231. font-size: 17px;
  232. }
  233. .mpvue-picker__hd:after {
  234. content: ' ';
  235. position: absolute;
  236. left: 0;
  237. bottom: 0;
  238. right: 0;
  239. height: 1px;
  240. border-bottom: 1px solid #e5e5e5;
  241. color: #e5e5e5;
  242. transform-origin: 0 100%;
  243. transform: scaleY(0.5);
  244. }
  245. .mpvue-picker__action {
  246. display: block;
  247. flex: 1;
  248. color: #1aad19;
  249. }
  250. .mpvue-picker__action:first-child {
  251. text-align: left;
  252. color: #888;
  253. }
  254. .mpvue-picker__action:last-child {
  255. text-align: right;
  256. }
  257. .picker-item {
  258. text-align: center;
  259. line-height: 40px;
  260. text-overflow: ellipsis;
  261. white-space: nowrap;
  262. font-size: 16px;
  263. }
  264. .mpvue-picker-view {
  265. position: relative;
  266. bottom: 0;
  267. left: 0;
  268. width: 100%;
  269. height: 238px;
  270. background-color: rgba(255, 255, 255, 1);
  271. }
  272. </style>