cc0b9c137cd3a52d75b4e1aa03deec0c4b8bc4f0.svn-base 1.6 KB

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