Blame view

ant-design-vue-jeecg/src/components/_util/Area.js 1.67 KB
肖超群 authored
1
import Vue from 'vue'
肖超群 authored
2
肖超群 authored
3
4
5
6
7
8
9
10
11
/**
 * 省市区
 */
export default class Area {
  /**
   * 构造器
   * @param express
   */
  constructor(pcaa) {
肖超群 authored
12
    if (!pcaa) {
肖超群 authored
13
14
15
16
      pcaa = Vue.prototype.$Jpcaa;
    }
    let arr = []
    const province = pcaa['86']
肖超群 authored
17
18
    Object.keys(province).map(key => {
      arr.push({id: key, text: province[key], pid: '86', index: 1});
肖超群 authored
19
      const city = pcaa[key];
肖超群 authored
20
21
      Object.keys(city).map(key2 => {
        arr.push({id: key2, text: city[key2], pid: key, index: 2});
肖超群 authored
22
        const qu = pcaa[key2];
肖超群 authored
23
24
25
        if (qu) {
          Object.keys(qu).map(key3 => {
            arr.push({id: key3, text: qu[key3], pid: key2, index: 3});
肖超群 authored
26
27
28
29
30
31
32
          })
        }
      })
    })
    this.all = arr;
  }
肖超群 authored
33
  get pca() {
肖超群 authored
34
35
36
    return this.all;
  }
肖超群 authored
37
38
  getCode(text) {
    if (!text || text.length == 0) {
肖超群 authored
39
40
      return ''
    }
肖超群 authored
41
42
    for (let item of this.all) {
      if (item.text === text) {
肖超群 authored
43
44
45
46
47
        return item.id;
      }
    }
  }
肖超群 authored
48
49
  getText(code) {
    if (!code || code.length == 0) {
肖超群 authored
50
51
52
53
54
55
56
      return ''
    }
    let arr = []
    this.getAreaBycode(code, arr, 3);
    return arr.join('/')
  }
肖超群 authored
57
  getRealCode(code) {
肖超群 authored
58
59
60
61
62
    let arr = []
    this.getPcode(code, arr, 3)
    return arr;
  }
肖超群 authored
63
64
65
  getPcode(id, arr, index) {
    for (let item of this.all) {
      if (item.id === id && item.index == index) {
肖超群 authored
66
        arr.unshift(id)
肖超群 authored
67
        if (item.pid != '86') {
肖超群 authored
68
69
70
71
72
73
          this.getPcode(item.pid, arr, --index)
        }
      }
    }
  }
肖超群 authored
74
75
76
  getAreaBycode(code, arr, index) {
    for (let item of this.all) {
      if (item.id === code && item.index == index) {
肖超群 authored
77
        arr.unshift(item.text);
肖超群 authored
78
        if (item.pid != '86') {
肖超群 authored
79
80
81
82
83
84
85
86
          this.getAreaBycode(item.pid, arr, --index)
        }

      }
    }
  }

}