mpvueCityPicker.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. export default {
  27. data() {
  28. return {
  29. pickerValue: [0, 0, 0],
  30. provinceDataList: [],
  31. cityDataList: [],
  32. areaDataList: [],
  33. showPicker: false,
  34. pickerValueDefault:[0,0,0]
  35. };
  36. },
  37. async created() {
  38. // 微信小程序不需要这行 ,H5需要
  39. // #ifndef MP
  40. this.pickerValue = await this.handPickValueDefault();
  41. this._$emit('onConfirm');
  42. // #endif
  43. },
  44. props: {
  45. /* 默认值 */
  46. // pickerValueDefault: {
  47. // type: Array,
  48. // default: [0, 0, 0]
  49. // },
  50. /* 主题色 */
  51. themeColor: {
  52. type: String,
  53. default: '#1aad19'
  54. }
  55. },
  56. methods: {
  57. async creat(pickerValueDefault) {
  58. this.pickerValueDefault = pickerValueDefault;
  59. this.pickerValue = await this.handPickValueDefault();
  60. this._$emit('onConfirm');
  61. },
  62. show() {
  63. setTimeout(() => {
  64. this.showPicker = true;
  65. }, 0);
  66. },
  67. maskClick() {
  68. this.pickerCancel();
  69. },
  70. pickerCancel() {
  71. this.showPicker = false;
  72. this._$emit('onCancel');
  73. },
  74. pickerConfirm(e) {
  75. this.showPicker = false;
  76. this._$emit('onConfirm');
  77. },
  78. showPickerView() {
  79. this.showPicker = true;
  80. },
  81. async handPickValueDefault() {
  82. let tempPickerValue = this.pickerValueDefault;
  83. console.log(tempPickerValue);
  84. this.provinceDataList = await this.$api.request('/address/area?pid=0');
  85. this.cityDataList = await this.$api.request('/address/area?pid=' + (tempPickerValue[0] != 0 ? tempPickerValue[0] : this.provinceDataList[0].id));
  86. this.areaDataList = await this.$api.request('/address/area?pid=' + (tempPickerValue[1] != 0 ? tempPickerValue[1] : this.cityDataList[0].id));
  87. for (let i in this.provinceDataList) {
  88. if (this.provinceDataList[i].id == tempPickerValue[0]) {
  89. tempPickerValue[0] = i;
  90. break;
  91. }
  92. }
  93. for (let i in this.cityDataList) {
  94. if (this.cityDataList[i].id == tempPickerValue[1]) {
  95. tempPickerValue[1] = i;
  96. break;
  97. }
  98. }
  99. for (let i in this.areaDataList) {
  100. if (this.areaDataList[i].id == tempPickerValue[2]) {
  101. tempPickerValue[2] = i;
  102. break;
  103. }
  104. }
  105. return tempPickerValue;
  106. },
  107. async pickerChange(e) {
  108. let changePickerValue = e.mp.detail.value;
  109. if (this.pickerValue[0] !== changePickerValue[0]) {
  110. // 第一级发生滚动
  111. // this.cityDataList = cityData[changePickerValue[0]];
  112. // this.areaDataList = areaData[changePickerValue[0]][0];
  113. let provinceId = this.provinceDataList[changePickerValue[0]].id;
  114. this.cityDataList = await this.$api.request('/address/area?pid=' + provinceId);
  115. this.areaDataList = await this.$api.request('/address/area?pid=' + this.cityDataList[0].id);
  116. changePickerValue[1] = 0;
  117. changePickerValue[2] = 0;
  118. } else if (this.pickerValue[1] !== changePickerValue[1]) {
  119. // 第二级滚动
  120. // this.areaDataList =
  121. // areaData[changePickerValue[0]][changePickerValue[1]];
  122. let cityId = this.cityDataList[changePickerValue[1]].id;
  123. this.areaDataList = await this.$api.request('/address/area?pid=' + cityId);
  124. changePickerValue[2] = 0;
  125. }
  126. this.pickerValue = changePickerValue;
  127. this._$emit('onChange');
  128. },
  129. _$emit(emitName) {
  130. let pickObj = {
  131. label: this._getLabel(),
  132. value: this._getAreaId(),
  133. cityCode: this._getCityCode()
  134. };
  135. this.$emit(emitName, pickObj);
  136. },
  137. _getLabel() {
  138. let pcikerLabel =
  139. this.provinceDataList[this.pickerValue[0]].label +
  140. '-' +
  141. this.cityDataList[this.pickerValue[1]].label +
  142. '-' +
  143. this.areaDataList[this.pickerValue[2]].label;
  144. return pcikerLabel;
  145. },
  146. _getCityCode() {
  147. return this.areaDataList[this.pickerValue[2]].value;
  148. },
  149. _getAreaId() {
  150. let areaId = [
  151. this.provinceDataList[this.pickerValue[0]].id,
  152. this.cityDataList[this.pickerValue[1]].id,
  153. this.areaDataList[this.pickerValue[2]].id
  154. ];
  155. return areaId;
  156. }
  157. }
  158. };
  159. </script>
  160. <style>
  161. .pickerMask {
  162. position: fixed;
  163. z-index: 1000;
  164. top: 0;
  165. right: 0;
  166. left: 0;
  167. bottom: 0;
  168. background: rgba(0, 0, 0, 0.6);
  169. }
  170. .mpvue-picker-content {
  171. position: fixed;
  172. bottom: 0;
  173. left: 0;
  174. width: 100%;
  175. transition: all 0.3s ease;
  176. transform: translateY(100%);
  177. z-index: 3000;
  178. }
  179. .mpvue-picker-view-show {
  180. transform: translateY(0);
  181. }
  182. .mpvue-picker__hd {
  183. display: flex;
  184. padding: 9px 15px;
  185. background-color: #fff;
  186. position: relative;
  187. text-align: center;
  188. font-size: 17px;
  189. }
  190. .mpvue-picker__hd:after {
  191. content: " ";
  192. position: absolute;
  193. left: 0;
  194. bottom: 0;
  195. right: 0;
  196. height: 1px;
  197. border-bottom: 1px solid #e5e5e5;
  198. color: #e5e5e5;
  199. transform-origin: 0 100%;
  200. transform: scaleY(0.5);
  201. }
  202. .mpvue-picker__action {
  203. display: block;
  204. flex: 1;
  205. color: #1aad19;
  206. }
  207. .mpvue-picker__action:first-child {
  208. text-align: left;
  209. color: #888;
  210. }
  211. .mpvue-picker__action:last-child {
  212. text-align: right;
  213. }
  214. .picker-item {
  215. text-align: center;
  216. line-height: 40px;
  217. text-overflow: ellipsis;
  218. white-space: nowrap;
  219. }
  220. .mpvue-picker-view {
  221. position: relative;
  222. bottom: 0;
  223. left: 0;
  224. width: 100%;
  225. height: 238px;
  226. background-color: rgba(255, 255, 255, 1);
  227. }
  228. </style>