index.js 5.1 KB

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