addressList.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <view class="addressList">
  3. <van-cell-group>
  4. <van-field :value="addressInfo.receiver" required clearable label="姓名" placeholder="请输入姓名"
  5. @change="onChange1" />
  6. <van-field :value="addressInfo.mobile" label="电话" placeholder="请输入电话号码" required @change="onChange2" />
  7. <van-field :value="fieldValue" is-link readonly label="地区" placeholder="请选择所在地区" @tap="onClick" />
  8. <van-popup :show="show" round position="bottom">
  9. <van-cascader v-if="show" :field-names="fieldNames" :value="cascaderValue" title="请选择所在地区"
  10. :options="options" @close="onClose" @finish="onFinish" />
  11. </van-popup>
  12. <van-field :value="addressInfo.addr" placeholder="街道门牌楼层房间号等信息" @change="onChange3" />
  13. <view class="switch">
  14. <text>是否是默认地址</text>
  15. <van-switch :checked="checked" @change="onChange4" disabled />
  16. </view>
  17. </van-cell-group>
  18. <view class="btn1">
  19. <van-button type="danger" round size="large" @click="onSave" v-if="addressInfo.addrId == 0">保 存</van-button>
  20. <van-button type="danger" round size="large" @click="onSave" v-if="addressInfo.addrId != 0">修 改</van-button>
  21. </view>
  22. <van-button type="default" round size="large" v-if="addressInfo.addrId != 0" @click="delMain">删 除</van-button>
  23. </view>
  24. </template>
  25. <script>
  26. import {
  27. addAddr,
  28. listByPid,
  29. updateAddr,
  30. deleteAddress
  31. } from '@/api/address.js'
  32. import addressData from '@/json/areas.json'
  33. export default {
  34. data() {
  35. return {
  36. checked: false,
  37. addressInfo: {
  38. addrId: 0,
  39. receiver: "",
  40. addr: "",
  41. postCode: "",
  42. mobile: "",
  43. provinceId: 0,
  44. cityId: 0,
  45. areaId: 0,
  46. province: "",
  47. city: "",
  48. area: "",
  49. commonAddr: null,
  50. },
  51. show: false,
  52. fieldNames: {
  53. text: 'label',
  54. value: 'value',
  55. children: 'children',
  56. },
  57. options: [],
  58. fieldValue: '',
  59. cascaderValue: '',
  60. }
  61. },
  62. onLoad(options) {
  63. if (options) {
  64. this.addressInfo = JSON.parse(options.data);
  65. this.checked = this.addressInfo.commonAddr == 1 ? true : false;
  66. }
  67. },
  68. onShow() {
  69. this.options = addressData;
  70. },
  71. methods: {
  72. onClick() {
  73. this.show = true;
  74. },
  75. onClose() {
  76. this.show = false;
  77. },
  78. onFinish(e) {
  79. const selectedOptions = e.detail.selectedOptions;
  80. const value = e.detail.value;
  81. const fieldValue = selectedOptions.map((option) => option.text || option.label).join('/');
  82. const newId = selectedOptions.map(obj => obj[Object.keys(obj)[0]]);
  83. this.addressInfo.provinceId = newId[0];
  84. this.addressInfo.cityId = newId[1];
  85. this.addressInfo.areaId = newId[2];
  86. const newValue = selectedOptions.map(obj => obj[Object.keys(obj)[1]]);
  87. this.addressInfo.province = newValue[0];
  88. this.addressInfo.city = newValue[1];
  89. this.addressInfo.area = newValue[2];
  90. this.fieldValue = fieldValue;
  91. this.cascaderValue = value;
  92. },
  93. onChange1(event) {
  94. this.addressInfo.receiver = event.detail;
  95. },
  96. onChange2(event) {
  97. this.addressInfo.mobile = event.detail;
  98. },
  99. onChange3(event) {
  100. this.addressInfo.addr = event.detail;
  101. },
  102. onChange4(event) {
  103. this.checked = event.detail;
  104. this.addressInfo.commonAddr = this.checked === true ? 1 : 0;
  105. },
  106. delMain() {
  107. if (this.addressInfo.commonAddr == 1) {
  108. uni.showToast({
  109. title: "默认地址不能删除",
  110. icon: "exception"
  111. })
  112. } else {
  113. deleteAddress(this.addressInfo.addrId).then(res => {
  114. uni.navigateBack({
  115. url: '/pages/address/address'
  116. })
  117. })
  118. }
  119. },
  120. // 保存 or 修改
  121. async onSave() {
  122. const province = await listByPid({
  123. pid: 0
  124. })
  125. province.map(async (result) => {
  126. if (this.addressInfo.province === result.areaName) {
  127. this.addressInfo.provinceId = result.areaId
  128. const city = await listByPid({
  129. pid: result.areaId
  130. })
  131. city.map(async (result) => {
  132. if (this.addressInfo.city === result.areaName) {
  133. this.addressInfo.cityId = result.areaId
  134. const city = await listByPid({
  135. pid: result.areaId
  136. })
  137. city.map(async (result) => {
  138. if (this.addressInfo.area === result
  139. .areaName) {
  140. this.addressInfo.areaId = result.areaId
  141. const postData = {
  142. receiver: this.addressInfo
  143. .receiver,
  144. addr: this.addressInfo.addr,
  145. mobile: this.addressInfo.mobile,
  146. province: this.addressInfo
  147. .province,
  148. provinceId: this.addressInfo
  149. .provinceId,
  150. city: this.addressInfo.city,
  151. cityId: this.addressInfo.cityId,
  152. area: this.addressInfo.area,
  153. areaId: this.addressInfo.areaId,
  154. commonAddr: this.addressInfo
  155. .commonAddr
  156. }
  157. if (this.addressInfo.addrId) {
  158. postData.addrId = this.addressInfo
  159. .addrId
  160. updateAddr(postData).then(() => {
  161. uni.navigateBack({
  162. url: "/pages/address/address"
  163. })
  164. })
  165. } else {
  166. addAddr(postData).then(() => {
  167. uni.navigateBack({
  168. url: "/pages/address/address"
  169. })
  170. })
  171. }
  172. this.addressInfo = {}
  173. }
  174. })
  175. }
  176. })
  177. }
  178. })
  179. }
  180. }
  181. }
  182. </script>
  183. <style lang="scss">
  184. .addressList {
  185. padding: 0 25rpx;
  186. .switch {
  187. width: 90%;
  188. height: 90rpx;
  189. border-bottom: 1px solid #eee;
  190. display: flex;
  191. justify-content: space-between;
  192. align-items: center;
  193. }
  194. .btn1 {
  195. margin: 30rpx 0;
  196. }
  197. }
  198. </style>