index.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // components/w-area/index.ts
  2. Component({
  3. /**
  4. * 组件的属性列表
  5. */
  6. properties: {
  7. show: {
  8. type: Boolean,
  9. value: false,
  10. observer: "_show",
  11. },
  12. /** 选中的数据 */
  13. value: {
  14. type: Object as any,
  15. value: {},
  16. observer: "_value",
  17. },
  18. /**
  19. * 示例
  20. * [{
  21. * id: xxx,
  22. * code: xxx,
  23. * name: xxx,
  24. * area: [{
  25. * id: xxx,
  26. * code: xxx,
  27. * name: xxx,
  28. * area: []
  29. * }]
  30. * }]
  31. */
  32. columns: {
  33. type: Array as any,
  34. value: [],
  35. observer: "_columns",
  36. },
  37. zIndex: {
  38. type: Number,
  39. value: 100,
  40. },
  41. // 数据分隔符
  42. separator: {
  43. type: String,
  44. value: "/",
  45. },
  46. /**
  47. * 可见的选项个数
  48. */
  49. visibleItemCount: {
  50. type: Number,
  51. value: 6,
  52. },
  53. itemHeight: {
  54. type: Number,
  55. value: 46,
  56. },
  57. },
  58. /**
  59. * 组件的初始数据
  60. */
  61. data: {
  62. innerShow: false,
  63. showAfterLeave: false,
  64. areaList: [] as any,
  65. cacheArea: [] as { cityCode: string; shiftCityCode: string }[], // 临时存储的对应关系
  66. provinceCode: null as any, // 省
  67. cityCode: null, // 市
  68. regionCode: null, // 区
  69. },
  70. /**
  71. * 组件的方法列表
  72. */
  73. methods: {
  74. submitArea(event: any) {
  75. const selectedOptions: any = event.detail.values || [];
  76. const provinceCode = selectedOptions[0].code || null;
  77. const provinceName = selectedOptions[0].name || "";
  78. const cityCode = selectedOptions[1].code || null;
  79. const cityName = selectedOptions[1].name || "";
  80. const regionCode = selectedOptions[2]?.code || null;
  81. const regionName = selectedOptions[2]?.name || "";
  82. const separator = this.data.separator;
  83. const formatAfterCityCode = this.formateCityCode(regionCode, cityCode);
  84. const codes: any = {
  85. provinceCode,
  86. cityCode: formatAfterCityCode,
  87. regionCode,
  88. };
  89. this.setData({
  90. show: false,
  91. value: codes,
  92. });
  93. this.triggerEvent("input", codes);
  94. this.triggerEvent("confirm", {
  95. ...codes,
  96. name:
  97. (provinceName || "") +
  98. (cityName ? separator + cityName : "") +
  99. (regionName ? separator + regionName : ""),
  100. });
  101. },
  102. onClose() {
  103. this.triggerEvent("cancel");
  104. },
  105. onAfterLeave() {
  106. this.setData({
  107. showAfterLeave: true,
  108. });
  109. },
  110. onBeforeEnter() {
  111. this.setData({
  112. showAfterLeave: false,
  113. });
  114. },
  115. formateArea(area: any[]) {
  116. const province_list: { [_: string]: string } = {};
  117. const city_list: { [_: string]: string } = {};
  118. const county_list: { [_: string]: string } = {};
  119. area.forEach((item: any) => {
  120. province_list[item.code] = item.name;
  121. });
  122. area.forEach((item: any) => {
  123. item.areas &&
  124. item.areas.forEach((city: any, index: number) => {
  125. let code = city.code + "";
  126. // 某些数据不标准 这里需要转换一下
  127. if (code[4] !== "0" || code[5] !== "0") {
  128. // 现在把区域的数据改为市的
  129. const newCode =
  130. code.substring(0, 2) +
  131. (index < 10
  132. ? `a${index}`
  133. : index < 20
  134. ? `b${index - 10}`
  135. : index < 30
  136. ? `c${index - 20}`
  137. : `d${index - 30}`) +
  138. "00";
  139. this.data.cacheArea.push({
  140. cityCode: code,
  141. shiftCityCode: newCode,
  142. });
  143. code = newCode;
  144. }
  145. city_list[code] = city.name;
  146. });
  147. });
  148. area.forEach((item: any) => {
  149. item.areas &&
  150. item.areas.forEach((city: any) => {
  151. city.areas &&
  152. city.areas.forEach((county: any) => {
  153. county_list[county.code] = county.name;
  154. });
  155. });
  156. });
  157. return {
  158. province_list,
  159. city_list,
  160. county_list,
  161. };
  162. },
  163. // 转换
  164. formateCityCode(
  165. regionCode: string | null,
  166. cityCode: string | null,
  167. reverse?: boolean
  168. ) {
  169. if (!regionCode && cityCode) {
  170. const cityCodeObj = this.data.cacheArea.find((item: any) => {
  171. return item[reverse ? "cityCode" : "shiftCityCode"] == cityCode;
  172. });
  173. return cityCodeObj
  174. ? cityCodeObj[reverse ? "shiftCityCode" : "cityCode"]
  175. : "";
  176. }
  177. return cityCode;
  178. },
  179. formatValue() {
  180. const value = this.data.value;
  181. if(!value || value && !value.provinceCode) return
  182. this.setData({
  183. provinceCode: value.provinceCode || null,
  184. cityCode: (this.formateCityCode(value.regionCode, value.cityCode, true) ||
  185. null) as any,
  186. regionCode: value.regionCode,
  187. });
  188. },
  189. _show() {
  190. this.setData({
  191. innerShow: this.data.show,
  192. });
  193. },
  194. _value() {
  195. this.formatValue()
  196. },
  197. // 数组初始化,为了处理只有两层数据
  198. _columns() {
  199. if (this.data.columns.length <= 0) return;
  200. const areaList = this.formateArea(this.data.columns);
  201. this.setData({
  202. areaList,
  203. }, () => {
  204. this.formatValue()
  205. });
  206. },
  207. },
  208. });