index.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <!-- <picker mode="multiSelector"-->
  3. <!-- :value="multiIndex"-->
  4. <!-- :range="multiArray"-->
  5. <!-- @change="handleValueChange"-->
  6. <!-- @columnchange="handleColumnChange">-->
  7. <!-- <slot></slot>-->
  8. <!-- </picker>-->
  9. <picker
  10. mode="multiSelector"
  11. @columnchange="bindMultiPickerColumnChange"
  12. :value="multiIndex"
  13. range-key="title"
  14. :range="multiArray"
  15. >
  16. {{ multiStr }}
  17. </picker>
  18. </template>
  19. <script>
  20. /* eslint-disable */
  21. import { provinceList } from '@/api/basic';
  22. export default {
  23. props: {
  24. // defaultRegions: {
  25. // type: Array,
  26. // }
  27. defaultLevel: {
  28. type: Number,
  29. default: 3
  30. },
  31. addressData: {
  32. type: Object,
  33. default() {
  34. return {};
  35. }
  36. }
  37. },
  38. data() {
  39. return {
  40. multiArray: [],
  41. multiIndex: [0, 0, 0],
  42. multiStr: ''
  43. };
  44. },
  45. mounted() {
  46. this.$mHelper.log(this.addressData);
  47. this.getProvinceList();
  48. },
  49. methods: {
  50. async getProvinceList() {
  51. let provinceIndex = 0;
  52. let cityIndex = 0;
  53. let areaIndex = 0;
  54. let province_name = null;
  55. let city_name = null;
  56. let area_name = null;
  57. await this.$http.get(`${provinceList}`).then(async r => {
  58. this.multiArray[0] = r.data;
  59. if (this.addressData.province_id) {
  60. r.data.forEach((item, index) => {
  61. if (
  62. parseInt(item.id, 10) ===
  63. parseInt(this.addressData.province_id, 10)
  64. ) {
  65. provinceIndex = index;
  66. this.addressData.province_id = item.id;
  67. province_name = item.title;
  68. }
  69. });
  70. } else {
  71. this.addressData.province_id = this.multiArray[0][0].id;
  72. province_name = this.multiArray[0][0].title;
  73. }
  74. await this.$http
  75. .get(`${provinceList}`, {
  76. pid: this.addressData.province_id
  77. })
  78. .then(async r => {
  79. this.multiArray[1] = r.data;
  80. if (this.addressData.city_id) {
  81. r.data.forEach((item, index) => {
  82. if (
  83. parseInt(item.id, 10) ==
  84. parseInt(this.addressData.city_id, 10)
  85. ) {
  86. cityIndex = index;
  87. this.addressData.city_id = item.id;
  88. city_name = item.title;
  89. }
  90. });
  91. } else {
  92. this.addressData.city_id = this.multiArray[1][0].id;
  93. city_name = this.multiArray[1][0].title;
  94. }
  95. await this.$http
  96. .get(`${provinceList}`, {
  97. pid: this.addressData.city_id
  98. })
  99. .then(r => {
  100. this.multiArray[2] = r.data;
  101. if (this.addressData.area_id) {
  102. r.data.forEach((item, index) => {
  103. if (
  104. parseInt(item.id, 10) ==
  105. parseInt(this.addressData.area_id, 10)
  106. ) {
  107. areaIndex = index;
  108. this.addressData.area_id = item.id;
  109. area_name = item.title;
  110. }
  111. });
  112. } else {
  113. this.addressData.area_id = this.multiArray[2][0].id;
  114. area_name = this.multiArray[2][0].title;
  115. }
  116. this.multiIndex = [provinceIndex, cityIndex, areaIndex];
  117. this.multiStr = `${province_name}, ${city_name}, ${area_name}`;
  118. this.$emit('getRegions', this.addressData);
  119. });
  120. });
  121. });
  122. },
  123. async bindMultiPickerColumnChange(e) {
  124. this.multiIndex[e.detail.column] = e.detail.value;
  125. let provinceIndex = 0;
  126. let cityIndex = 0;
  127. let areaIndex = 0;
  128. let province_name = null;
  129. let city_name = null;
  130. let area_name = null;
  131. switch (e.detail.column) {
  132. case 0: //拖动第1列
  133. if (this.multiIndex[0] === this.multiIndex[0]) {
  134. this.addressData.province_id = this.multiArray[0][
  135. e.detail.value
  136. ].id;
  137. province_name = this.multiArray[0][e.detail.value].title;
  138. await this.$http
  139. .get(`${provinceList}`, {
  140. pid: this.multiArray[0][e.detail.value].id
  141. })
  142. .then(async r => {
  143. this.multiArray[1] = r.data;
  144. this.addressData.city_id = this.multiArray[1][0].id;
  145. city_name = this.multiArray[1][0].title;
  146. provinceIndex = e.detail.value;
  147. await this.$http
  148. .get(`${provinceList}`, {
  149. pid: this.multiArray[1][e.detail.column].id
  150. })
  151. .then(r => {
  152. this.multiArray[2] = r.data;
  153. this.addressData.area_id = this.multiArray[2][0].id;
  154. area_name = this.multiArray[2][0].title;
  155. this.multiIndex = [provinceIndex, 0, 0];
  156. this.multiStr = `${province_name}, ${city_name}, ${area_name}`;
  157. });
  158. });
  159. }
  160. break;
  161. case 1: //拖动第2列
  162. if (this.multiIndex[0] === this.multiIndex[0]) {
  163. if (this.multiIndex[1] === this.multiIndex[1]) {
  164. this.addressData.province_id = this.multiArray[0][
  165. this.multiIndex[0]
  166. ].id;
  167. province_name = this.multiArray[0][this.multiIndex[0]].title;
  168. this.addressData.city_id = this.multiArray[1][
  169. e.detail.value
  170. ].id;
  171. city_name = this.multiArray[1][e.detail.value].title;
  172. cityIndex = e.detail.value;
  173. await this.$http
  174. .get(`${provinceList}`, {
  175. pid: this.multiArray[1][e.detail.value].id
  176. })
  177. .then(r => {
  178. this.multiArray[2] = r.data;
  179. this.addressData.area_id = this.multiArray[2][0].id;
  180. area_name = this.multiArray[2][0].title;
  181. this.multiIndex = [this.multiIndex[0], cityIndex, 0];
  182. this.multiStr = `${province_name}, ${city_name}, ${area_name}`;
  183. });
  184. }
  185. }
  186. break;
  187. case 2: //拖动第3列
  188. if (this.multiIndex[0] === this.multiIndex[0]) {
  189. if (this.multiIndex[1] === this.multiIndex[1]) {
  190. if (this.multiIndex[2] === this.multiIndex[2]) {
  191. this.addressData.province_id = this.multiArray[0][
  192. this.multiIndex[0]
  193. ].id;
  194. province_name = this.multiArray[0][this.multiIndex[0]]
  195. .title;
  196. this.addressData.city_id = this.multiArray[1][
  197. this.multiIndex[1]
  198. ].id;
  199. city_name = this.multiArray[1][this.multiIndex[1]].title;
  200. this.addressData.area_id = this.multiArray[2][
  201. e.detail.value
  202. ].id;
  203. area_name = this.multiArray[2][e.detail.value].title;
  204. areaIndex = e.detail.value;
  205. this.multiIndex = [
  206. this.multiIndex[0],
  207. this.multiIndex[1],
  208. areaIndex
  209. ];
  210. this.multiStr = `${province_name}, ${city_name}, ${area_name}`;
  211. }
  212. }
  213. }
  214. break;
  215. }
  216. this.$emit('getRegions', this.addressData);
  217. }
  218. }
  219. };
  220. </script>