index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. const {
  2. getProvince,
  3. getCity,
  4. getArea,
  5. getStreet
  6. } = require("../../utils/api/api");
  7. const {
  8. throttle
  9. } = require("../../utils/throttle/throttle");
  10. // components/area-picker/index.js
  11. Component({
  12. options: {
  13. lifetimes: true
  14. },
  15. /**
  16. * 组件的属性列表
  17. */
  18. properties: {
  19. content: {
  20. type: String,
  21. default: null
  22. },
  23. hasStreet: {
  24. type: Boolean,
  25. default: false
  26. },
  27. title: {
  28. type: String,
  29. default: null
  30. }
  31. },
  32. /**
  33. * 组件的初始数据
  34. */
  35. data: {
  36. show: false,
  37. valueIndex: [],
  38. result: [],
  39. result_show: null,
  40. addressList: []
  41. },
  42. /**
  43. * 组件的方法列表
  44. */
  45. methods: {
  46. showPopup() {
  47. this.setData({
  48. show: true
  49. })
  50. },
  51. change(event) {
  52. // console.info('detail-->', event.detail)
  53. let column = event.detail.index;
  54. this.getAddressList(column, event.detail.picker.getColumnIndex(column), null, event.detail.picker)
  55. },
  56. onConfirm(e) {
  57. let { valueIndex, result, result_show } = this.data
  58. valueIndex = e.detail.index
  59. result = e.detail.value.map(item => {
  60. return item.name
  61. })
  62. result_show = result.join("-");
  63. this.setData({
  64. show: false,
  65. valueIndex,
  66. result_show
  67. })
  68. this.triggerEvent("change", result_show);
  69. },
  70. onCancel() {
  71. this.setData({ show: false })
  72. },
  73. //获取省市区街道列表并赋值
  74. getAddressList: throttle(async function(column = -1, index = 0, value, picker = null) {
  75. let { addressList, valueIndex, result, result_show } = this.data;
  76. if (column <= -1) {
  77. await getProvince().then(provinceList => {
  78. addressList[0] = { values: provinceList.data, key: '省' }
  79. if (value) {
  80. index = provinceList.data.findIndex(item => {
  81. return item.name == value[0];
  82. });
  83. index = index > -1 ? index : 0;
  84. } else {
  85. index = 0;
  86. }
  87. });
  88. }
  89. if (column <= 0) {
  90. valueIndex[0] = index
  91. result[0] = addressList[0].values[index].name;
  92. await getCity(addressList[0].values[index || 0].areaCodeId).then(
  93. cityList => {
  94. addressList[1] = { values: cityList.data, key: '市' }
  95. if(!!picker) {
  96. picker.setColumnValues(++column, cityList.data)
  97. }
  98. if (value) {
  99. index = cityList.data.findIndex(item => {
  100. return item.name == value[1];
  101. });
  102. index = index > -1 ? index : 0;
  103. } else {
  104. index = 0;
  105. }
  106. }
  107. );
  108. }
  109. if (column <= 1) {
  110. valueIndex[1] = index;
  111. result[1] = addressList[1].values[index].name;
  112. await getArea(addressList[1].values[index || 0].areaCodeId).then(
  113. areaList => {
  114. addressList[2] = { values: areaList.data, key: '区' }
  115. if(!!picker) {
  116. picker.setColumnValues(++column, areaList.data)
  117. }
  118. if (value) {
  119. index = areaList.data.findIndex(item => {
  120. return item.name == value[2];
  121. });
  122. index = index > -1 ? index : 0;
  123. } else {
  124. index = 0;
  125. }
  126. }
  127. );
  128. }
  129. if (column <= 2) {
  130. valueIndex[2] = index;
  131. result[2] = addressList[2].values[index].name;
  132. if (this.data.hasStreet) {
  133. await getStreet(addressList[2].values[index || 0].areaCodeId).then(
  134. streetList => {
  135. addressList[3] = { values: streetList.data, key: '街道' }
  136. if(!!picker) {
  137. picker.setColumnValues(++column, streetList.data)
  138. }
  139. if (value) {
  140. index = streetList.data.findIndex(item => {
  141. return item.name == value[3];
  142. });
  143. index = index > -1 ? index : 0;
  144. } else {
  145. index = 0;
  146. }
  147. }
  148. );
  149. }
  150. }
  151. if (column <= 3 && this.data.hasStreet) {
  152. valueIndex[3] = index;
  153. result[3] =
  154. addressList[3].length > 0
  155. ? addressList[3][index].name
  156. : "暂无街道";
  157. }
  158. if (column <= -1 && !value) {
  159. result = [];
  160. result_show = null;
  161. }
  162. // result_show = result.join("-") || null;
  163. this.setData({
  164. addressList,
  165. valueIndex,
  166. result_show
  167. });
  168. // this.triggerEvent("change", result_show)
  169. }, 300, false)
  170. },
  171. lifetimes: {
  172. // 组件初始化时
  173. attached: async function() {
  174. let value = this.data.content;
  175. value = value ? value.split("-") : null;
  176. this.getAddressList(-1, 0, value);
  177. }
  178. }
  179. });