Area.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import REGION_DATA from 'china-area-data';
  2. /**
  3. * Area 属性all的类型
  4. */
  5. interface PlainPca {
  6. id: string;
  7. text: string;
  8. pid: string;
  9. index: Number;
  10. }
  11. /**
  12. * 省市区工具类 -解决列表省市区组件的翻译问题
  13. */
  14. class Area {
  15. all: PlainPca[];
  16. /**
  17. * 构造器
  18. * @param express
  19. */
  20. constructor(pcaa?) {
  21. if (!pcaa) {
  22. pcaa = REGION_DATA;
  23. }
  24. let arr: PlainPca[] = [];
  25. const province = pcaa['86'];
  26. Object.keys(province).map((key) => {
  27. arr.push({ id: key, text: province[key], pid: '86', index: 1 });
  28. const city = pcaa[key];
  29. Object.keys(city).map((key2) => {
  30. arr.push({ id: key2, text: city[key2], pid: key, index: 2 });
  31. const qu = pcaa[key2];
  32. if (qu) {
  33. Object.keys(qu).map((key3) => {
  34. arr.push({ id: key3, text: qu[key3], pid: key2, index: 3 });
  35. });
  36. }
  37. });
  38. });
  39. this.all = arr;
  40. }
  41. get pca() {
  42. return this.all;
  43. }
  44. getCode(text) {
  45. if (!text || text.length == 0) {
  46. return '';
  47. }
  48. for (let item of this.all) {
  49. if (item.text === text) {
  50. return item.id;
  51. }
  52. }
  53. }
  54. getText(code) {
  55. if (!code || code.length == 0) {
  56. return '';
  57. }
  58. let arr = [];
  59. this.getAreaBycode(code, arr, 3);
  60. return arr.join('/');
  61. }
  62. getRealCode(code) {
  63. let arr = [];
  64. this.getPcode(code, arr, 3);
  65. return arr;
  66. }
  67. getPcode(id, arr, index) {
  68. for (let item of this.all) {
  69. if (item.id === id && item.index == index) {
  70. arr.unshift(id);
  71. if (item.pid != '86') {
  72. this.getPcode(item.pid, arr, --index);
  73. }
  74. }
  75. }
  76. }
  77. getAreaBycode(code, arr, index) {
  78. for (let item of this.all) {
  79. if (item.id === code && item.index == index) {
  80. arr.unshift(item.text);
  81. if (item.pid != '86') {
  82. this.getAreaBycode(item.pid, arr, --index);
  83. }
  84. }
  85. }
  86. }
  87. }
  88. const jeecgAreaData = new Area();
  89. // 根据code找文本
  90. const getAreaTextByCode = function (code) {
  91. //update-begin-author:liusq---date:20220531--for: 判断code是否是多code逗号分割的字符串,是的话,获取最后一位的code ---
  92. if (code && code.includes(',')) {
  93. code = code.substr(code.lastIndexOf(',') + 1);
  94. }
  95. //update-end-author:liusq---date:20220531--for: 判断code是否是多code逗号分割的字符串,是的话,获取最后一位的code ---
  96. return jeecgAreaData.getText(code);
  97. };
  98. export { getAreaTextByCode };