handle.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. import address from '../data/address';
  2. export default {
  3. data(){
  4. return {
  5. data:{
  6. province:[],
  7. city:[],
  8. area:[],
  9. },
  10. province:[],
  11. city:[],
  12. area:[],
  13. vValue:[]
  14. }
  15. },
  16. watch:{
  17. value:{
  18. handler:function (value) {
  19. return this.setValue(value);
  20. },
  21. immediate:true
  22. }
  23. },
  24. methods:{
  25. open:function(){
  26. if (this.storeValueTarget && this.storeValueTarget.join('') !== this.vValue.join('')) {
  27. this.vValue = this.storeValueTarget;
  28. this.changeLevel('0',0);
  29. }
  30. },
  31. /* 点击确认触发 */
  32. confirm:function(){
  33. let code = '0';
  34. let cCode = [];
  35. let cAddress = [];
  36. this.vValue.map((item,index)=>{
  37. code = this.getCode(index,code);
  38. if (address[code]&&address[code][item]) {
  39. cAddress.push(address[code][item].name);
  40. code = address[code][item].code;
  41. cCode.push(code);
  42. }
  43. });
  44. this.storeValue = cAddress.join('');
  45. this.storeValueTarget = [...this.vValue];
  46. if (this.$attrs.input) {
  47. this.$emit('input',cAddress);
  48. }
  49. if (this.$attrs.onChange) {
  50. this.$emit('change',{
  51. postcode: code,
  52. code: cCode,
  53. value: cAddress
  54. });
  55. }
  56. },
  57. /* 更改展示数据 */
  58. changeLevel:function(code = '0',level=0){
  59. let parentCode = code;
  60. let nowCode = '';
  61. for (let i=level;i<this.use.length;i++) {
  62. nowCode = this.getCode(i,parentCode);
  63. if (address[nowCode]) {
  64. this.data[this.use[i]] = address[nowCode] || [];
  65. if (address[nowCode][this.vValue[i] || 0]){
  66. parentCode = address[nowCode][this.vValue[i] || 0].code || '';
  67. }else {
  68. parentCode = '';
  69. }
  70. } else {
  71. this.data[this.use[i]] = [];
  72. }
  73. }
  74. },
  75. /* 获取默认选择的地址 */
  76. getAddressValue:function (value) {
  77. let newValue = [];
  78. let key = 0;
  79. let parentItem = address;
  80. for(let i=0;i<this.use.length;i++) {
  81. if (value[i]) {
  82. key = i===0?this.use[i]:parentItem.code+':'+this.use[i];
  83. newValue[i] = address[key].indexOf(value[i]);
  84. if (newValue[i] < 0) newValue[i] = 0;
  85. } else {
  86. newValue[i] = 0;
  87. }
  88. if (i === 0) {
  89. parentItem = address['0:0'][newValue[i]];
  90. } else {
  91. parentItem = address[this.getCode(i,parentItem.code)][newValue[i]];
  92. }
  93. }
  94. return newValue;
  95. },
  96. changeColumn:function (key,index,value) {
  97. let newValue = [...this.vValue];
  98. newValue[index] = value;
  99. let item ={
  100. code:'0'
  101. };
  102. let change = false;
  103. let storageI = 0;
  104. let oldValue = [...this.vValue];
  105. for (let i=0;i<this.use.length;i++) {
  106. if (change) {
  107. newValue[i] = 0;
  108. }else {
  109. item = this.getItem(this.getCode(i,item.code),newValue[i]);
  110. }
  111. if (oldValue[i] !== newValue[i]) {
  112. if (!change) {
  113. storageI = i;
  114. }
  115. change = true;
  116. }
  117. }
  118. if (change) {
  119. this.vValue = newValue;
  120. this.changeLevel(item.code,storageI+1);
  121. this.confirm();
  122. }
  123. },
  124. getCode:function(level,code){
  125. return level+':'+code;
  126. },
  127. getItem:function (code='0:0',index) {
  128. if (address[code]) {
  129. return address[code][index];
  130. } else {
  131. return {
  132. code:-1
  133. };
  134. }
  135. },
  136. setValue:function (value) {
  137. let storeValue = value.join('');
  138. if (this.storeValue !== storeValue) {
  139. this.vValue = this.getAddressValue(value);
  140. this.storeValue = storeValue;
  141. this.changeLevel('0',0);
  142. this.confirm();
  143. }
  144. }
  145. },
  146. created(){
  147. this.open();
  148. console.log(this);
  149. }
  150. }