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.

217 lines
5.8 KiB

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