index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const { getProvince, getCity, getArea, getStreet } = require("../../utils/api/api")
  2. // components/area-picker/index.js
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. content: {
  9. type: String,
  10. default: null
  11. },
  12. hasStreet: {
  13. type: Boolean,
  14. default: false
  15. }
  16. },
  17. /**
  18. * 组件的初始数据
  19. */
  20. data: {
  21. addressList: [],
  22. valueIndex: [],
  23. result: [],
  24. result_show: null,
  25. },
  26. /**
  27. * 组件的方法列表
  28. */
  29. methods: {
  30. //获取省市区街道列表并赋值
  31. async getAddressList(column = -1, index = 0, value) {
  32. let {addressList, valueIndex, result, result_show} = this.data;
  33. if(column <= -1) {
  34. await getProvince().then(provinceList => {
  35. addressList[0] = provinceList.data;
  36. if(value) {
  37. index = provinceList.data.findIndex((item) => {
  38. return item.name == value[0]
  39. });
  40. index = (index>-1)?index:0
  41. } else {
  42. index = 0
  43. }
  44. });
  45. };
  46. if(column <= 0) {
  47. valueIndex[0] = index;
  48. result[0] = addressList[0][index].name
  49. await getCity(addressList[0][(index || 0)].areaCodeId).then(cityList => {
  50. addressList[1] = cityList.data;
  51. if(value) {
  52. index = cityList.data.findIndex((item) => {
  53. return item.name == value[1]
  54. });
  55. index = (index>-1)?index:0
  56. } else {
  57. index = 0
  58. }
  59. })
  60. }
  61. if(column <= 1) {
  62. valueIndex[1] = index;
  63. result[1] = addressList[1][index].name
  64. await getArea(addressList[1][(index || 0)].areaCodeId).then(areaList => {
  65. addressList[2] = areaList.data;
  66. if(value) {
  67. index = areaList.data.findIndex((item) => {
  68. return item.name == value[2]
  69. });
  70. index = (index>-1)?index:0
  71. } else {
  72. index = 0
  73. }
  74. })
  75. }
  76. if(column <= 2) {
  77. valueIndex[2] = index;
  78. result[2] = addressList[2][index].name
  79. if(this.data.hasStreet) {
  80. await getStreet(addressList[2][(index || 0)].areaCodeId).then(streetList => {
  81. addressList[3] = streetList.data
  82. if(value) {
  83. index = streetList.data.findIndex((item) => {
  84. return item.name == value[3]
  85. });
  86. index = (index>-1)?index:0
  87. } else {
  88. index = 0
  89. }
  90. })
  91. }
  92. }
  93. if(column <= 3 && this.data.hasStreet) {
  94. valueIndex[3] = index;
  95. result[3] = addressList[3].length > 0 ? addressList[3][index].name : '暂无街道'
  96. };
  97. if(column <= -1 && !value) {
  98. result = [];
  99. result_show = null
  100. }
  101. result_show = result.join('-') || null;
  102. this.setData({ addressList, valueIndex, result_show });
  103. this.triggerEvent("value", result_show)
  104. },
  105. //改变列时
  106. changeColumn(e) {
  107. let column = e.detail.column;
  108. let index = e.detail.value;
  109. this.getAddressList(column, index)
  110. },
  111. // 点击确定时
  112. changeValue(e) {
  113. let { valueIndex, result, result_show } = this.data;
  114. valueIndex = e.detail.value;
  115. valueIndex.forEach((item,index) => {
  116. if(this.data.addressList[index][item]){
  117. result[index] = this.data.addressList[index][item].name
  118. }
  119. });
  120. result_show = result.join('-');
  121. this.setData({ valueIndex, result_show });
  122. this.triggerEvent("value", result_show)
  123. },
  124. },
  125. lifetimes: {
  126. // 组件初始化时
  127. attached: async function() {
  128. let value = this.data.content;
  129. value = value?value.split('-'):null
  130. this.getAddressList(-1, 0, value)
  131. },
  132. }
  133. })