Blame view

ant-design-vue-jeecg/src/components/_util/Area.js 1.62 KB
1
import Vue from 'vue'
2
3
4
5
6
7
8
9
/**
 * 省市区
 */
export default class Area {
  /**
   * 构造器
   * @param express
   */
10
  constructor(pcaa) {
11
12
13
    if(!pcaa){
      pcaa = Vue.prototype.$Jpcaa;
    }
14
15
16
    let arr = []
    const province = pcaa['86']
    Object.keys(province).map(key=>{
17
      arr.push({id:key, text:province[key], pid:'86', index:1});
18
19
      const city = pcaa[key];
      Object.keys(city).map(key2=>{
20
        arr.push({id:key2, text:city[key2], pid:key, index:2});
21
        const qu = pcaa[key2];
22
23
24
25
26
        if(qu){
          Object.keys(qu).map(key3=>{
            arr.push({id:key3, text:qu[key3], pid:key2, index:3});
          })
        }
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
      })
    })
    this.all = arr;
  }

  get pca(){
    return this.all;
  }

  getCode(text){
    if(!text || text.length==0){
      return ''
    }
    for(let item of this.all){
      if(item.text === text){
        return item.id;
      }
    }
  }

  getText(code){
    if(!code || code.length==0){
      return ''
    }
    let arr = []
52
    this.getAreaBycode(code, arr, 3);
53
54
55
56
57
    return arr.join('/')
  }

  getRealCode(code){
    let arr = []
58
    this.getPcode(code, arr, 3)
59
60
61
    return arr;
  }
62
  getPcode(id, arr, index){
63
    for(let item of this.all){
64
      if(item.id === id && item.index == index){
65
66
        arr.unshift(id)
        if(item.pid != '86'){
67
          this.getPcode(item.pid, arr, --index)
68
69
70
71
72
        }
      }
    }
  }
73
  getAreaBycode(code, arr, index){
74
    for(let item of this.all){
75
      if(item.id === code && item.index == index){
76
        arr.unshift(item.text);
77
78
79
80
        if(item.pid != '86'){
          this.getAreaBycode(item.pid, arr, --index)
        }
81
82
83
84
85
      }
    }
  }

}