view.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <template>
  2. <div class="addressView" style="padding-top: 46px">
  3. <van-nav-bar
  4. class="van-nav-bar-my-fixed"
  5. :title="title"
  6. fixed
  7. left-arrow
  8. @click-left="onClickLeft"
  9. />
  10. <van-address-edit
  11. v-loading="loading"
  12. :area-list="areaList"
  13. :show-postal="false"
  14. :show-delete="false"
  15. :tel-validator="validator"
  16. :address-info="addressInfo"
  17. :show-set-default="false"
  18. :show-search-result="false"
  19. :search-result="[]"
  20. :area-columns-placeholder="['请选择', '请选择', '请选择']"
  21. default-tag-text="默认"
  22. @save="onSave"
  23. />
  24. </div>
  25. </template>
  26. <script>
  27. import asyncRequest from "@/apis/address/index";
  28. import resToken from "@/mixins/resToken";
  29. import { areaList } from "@vant/area-data";
  30. export default {
  31. mixins: [resToken],
  32. data() {
  33. return {
  34. // /^1[3|4|5|6|7|8|9][0-9]\d{8}$/.test(s)
  35. id: "0",
  36. title: "",
  37. areaList: areaList,
  38. addressInfo: {
  39. id: "",
  40. name: "",
  41. tel: "",
  42. addressDetail: "",
  43. areaCode: "",
  44. },
  45. loading: false,
  46. isDefault: false,
  47. };
  48. },
  49. async created() {
  50. if (this.$route.query.id) {
  51. this.title = "编辑地址";
  52. this.id = this.$route.query.id;
  53. await this.initData();
  54. } else {
  55. this.title = "新建地址";
  56. this.id = "0";
  57. }
  58. },
  59. methods: {
  60. validator(e) {
  61. let myreg1 = /^[1][3,4,5,7,8][0-9]{9}$/;
  62. let myreg2 = /^[2][3,4,5,7,8][0-9]{6}$/;
  63. if (!myreg1.test(e) && !myreg2.test(e)) {
  64. return false;
  65. } else {
  66. return true;
  67. }
  68. },
  69. onClickLeft() {
  70. window.history.back(-1);
  71. },
  72. async onSave(e) {
  73. if (!this.loading) {
  74. this.loading = true;
  75. let code = e.areaCode;
  76. let model = {
  77. contector: e.name,
  78. mobile: e.tel,
  79. provice: code.substr(0, 2) + "0000",
  80. city: code.substr(0, 4) + "00",
  81. area: e.areaCode,
  82. addr: e.addressDetail,
  83. };
  84. let res = {};
  85. if (this.id === "0") {
  86. res = await asyncRequest.add(model);
  87. } else {
  88. model.id = this.id;
  89. res = await asyncRequest.update(model);
  90. }
  91. this.loading = false;
  92. if (res && res.code == 0) {
  93. this.show_title(`${this.title}成功!`);
  94. setTimeout(() => {
  95. this.onClickLeft();
  96. }, 1500);
  97. } else if (res && res.code >= 100 && res.code <= 104) {
  98. await this.logout();
  99. } else {
  100. this.show_title(res.msg);
  101. }
  102. }
  103. },
  104. async initData() {
  105. if (!this.loading) {
  106. this.loading = true;
  107. const res = await asyncRequest.detail({ id: this.id });
  108. this.loading = false;
  109. if (res && res.code === 0 && res.data) {
  110. let formData = res.data;
  111. this.addressInfo = {
  112. id: formData.id,
  113. name: formData.contector,
  114. tel: formData.mobile,
  115. addressDetail: formData.addr,
  116. areaCode: formData.area,
  117. };
  118. } else if (res && res.code >= 100 && res.code <= 104) {
  119. await this.logout();
  120. } else {
  121. this.show_title(res.msg);
  122. }
  123. }
  124. },
  125. },
  126. };
  127. </script>
  128. <style lang="scss" scoped>
  129. .addressView {
  130. position: relative;
  131. width: 100%;
  132. height: 100%;
  133. box-sizing: border-box;
  134. background: rgb(230, 230, 220)!important;
  135. .van-address-edit__buttons {
  136. button {
  137. float: right;
  138. width: 42.5%;
  139. &:first-child {
  140. margin: 0 5% 0 2.5%;
  141. background: linear-gradient(0deg, #ff6680 0%, #ffd490 100%);
  142. border: 0;
  143. }
  144. &:last-child {
  145. margin: 0 2.5% 0 5%;
  146. border-color: rgb(255, 131, 39);
  147. color: rgb(255, 131, 39);
  148. }
  149. }
  150. }
  151. .van-address-edit__buttons::after,
  152. .van-address-edit__buttons::before {
  153. content: "";
  154. display: block;
  155. clear: both;
  156. }
  157. //新增地址栏和顶部的间距
  158. .van-address-edit {
  159. // margin-top: 15px;
  160. }
  161. }
  162. </style>