supplyAreaDebug.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // 供货区域调试工具
  2. // 用于调试供货区域选择和回显的问题
  3. /**
  4. * 调试级联选择器的数据结构
  5. */
  6. export function debugCascaderData(data: any[], title: string = '级联数据') {
  7. console.log(`=== ${title} ===`);
  8. console.log('数据总数:', data.length);
  9. if (data.length > 0) {
  10. const firstItem = data[0];
  11. console.log('第一项结构:', {
  12. areaCode: firstItem.areaCode,
  13. areaName: firstItem.areaName,
  14. level: firstItem.level,
  15. hasChildren: !!firstItem.children,
  16. childrenCount: firstItem.children?.length || 0
  17. });
  18. if (firstItem.children && firstItem.children.length > 0) {
  19. const firstChild = firstItem.children[0];
  20. console.log('第一个子项结构:', {
  21. areaCode: firstChild.areaCode,
  22. areaName: firstChild.areaName,
  23. level: firstChild.level,
  24. parentCode: firstChild.parentCode
  25. });
  26. }
  27. }
  28. }
  29. /**
  30. * 调试选中路径的格式
  31. */
  32. export function debugSelectedPaths(paths: any[], title: string = '选中路径') {
  33. console.log(`=== ${title} ===`);
  34. console.log('路径总数:', paths.length);
  35. paths.forEach((path, index) => {
  36. if (Array.isArray(path)) {
  37. console.log(`路径 ${index + 1}:`, {
  38. length: path.length,
  39. provinceCode: path[0],
  40. cityCode: path[1],
  41. path: path
  42. });
  43. } else {
  44. console.log(`路径 ${index + 1} (非数组):`, path);
  45. }
  46. });
  47. }
  48. /**
  49. * 调试保存的区域数据
  50. */
  51. export function debugSavedAreaData(data: any[], title: string = '保存的区域数据') {
  52. console.log(`=== ${title} ===`);
  53. console.log('数据总数:', data.length);
  54. const provinces = data.filter(item => item.level === 1 || item.level === '1');
  55. const cities = data.filter(item => item.level === 2 || item.level === '2');
  56. console.log('省份数量:', provinces.length);
  57. console.log('城市数量:', cities.length);
  58. provinces.forEach(province => {
  59. const provinceCities = cities.filter(city => city.parentCode === province.areaCode);
  60. console.log(`${province.areaName} (${province.areaCode}):`,
  61. provinceCities.map(city => `${city.areaName}(${city.areaCode})`).join(', ') || '无城市'
  62. );
  63. });
  64. }
  65. /**
  66. * 验证级联选择器配置
  67. */
  68. export function validateCascaderProps(props: any) {
  69. console.log('=== 级联选择器配置验证 ===');
  70. console.log('配置:', props);
  71. const requiredProps = ['value', 'label', 'children'];
  72. const missingProps = requiredProps.filter(prop => !props[prop]);
  73. if (missingProps.length > 0) {
  74. console.error('缺少必要配置:', missingProps);
  75. } else {
  76. console.log('✓ 配置完整');
  77. }
  78. if (props.multiple) {
  79. console.log('✓ 多选模式');
  80. }
  81. if (props.checkStrictly) {
  82. console.log('⚠️ 严格模式 - 父子不关联');
  83. } else {
  84. console.log('✓ 关联模式 - 父子关联');
  85. }
  86. }
  87. /**
  88. * 模拟级联选择器的选择过程
  89. */
  90. export function simulateSelection(
  91. supplyAreaOptions: any[],
  92. targetProvinces: string[],
  93. targetCities: { [provinceCode: string]: string[] }
  94. ) {
  95. console.log('=== 模拟选择过程 ===');
  96. console.log('目标省份:', targetProvinces);
  97. console.log('目标城市:', targetCities);
  98. const selectedPaths: any[] = [];
  99. targetProvinces.forEach(provinceName => {
  100. // 查找省份
  101. const province = supplyAreaOptions.find(p => p.areaName === provinceName);
  102. if (!province) {
  103. console.error(`未找到省份: ${provinceName}`);
  104. return;
  105. }
  106. const provinceCode = province.areaCode;
  107. const cities = targetCities[provinceName] || [];
  108. if (cities.length === 0) {
  109. // 只选择省份
  110. selectedPaths.push([provinceCode]);
  111. console.log(`选择省份: ${provinceName} (${provinceCode})`);
  112. } else {
  113. // 选择省份下的城市
  114. cities.forEach(cityName => {
  115. const city = province.children?.find((c: any) => c.areaName === cityName);
  116. if (city) {
  117. selectedPaths.push([provinceCode, city.areaCode]);
  118. console.log(`选择城市: ${provinceName} > ${cityName} (${provinceCode}, ${city.areaCode})`);
  119. } else {
  120. console.error(`未找到城市: ${provinceName} > ${cityName}`);
  121. }
  122. });
  123. }
  124. });
  125. console.log('生成的选择路径:', selectedPaths);
  126. return selectedPaths;
  127. }
  128. /**
  129. * 完整的调试流程
  130. */
  131. export function debugSupplyAreaFlow(
  132. supplyAreaOptions: any[],
  133. savedAreaData: any[],
  134. selectedPaths: any[]
  135. ) {
  136. console.log('🔍 === 供货区域完整调试 ===');
  137. // 1. 调试数据源
  138. debugCascaderData(supplyAreaOptions, '供货区域选项数据');
  139. // 2. 调试保存的数据
  140. debugSavedAreaData(savedAreaData, '后端保存的数据');
  141. // 3. 调试选中路径
  142. debugSelectedPaths(selectedPaths, '当前选中路径');
  143. // 4. 验证配置
  144. validateCascaderProps({
  145. multiple: true,
  146. checkStrictly: false,
  147. value: 'areaCode',
  148. label: 'areaName',
  149. children: 'children',
  150. emitPath: true
  151. });
  152. console.log('🔍 === 调试完成 ===');
  153. }
  154. // 在开发环境下暴露到全局
  155. if (import.meta.env.DEV) {
  156. (window as any).debugSupplyArea = {
  157. debugCascaderData,
  158. debugSelectedPaths,
  159. debugSavedAreaData,
  160. validateCascaderProps,
  161. simulateSelection,
  162. debugSupplyAreaFlow
  163. };
  164. }