add-or-update.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <template>
  2. <view class="page">
  3. <view class="form">
  4. <u-form :model="form" ref="form" label-width="180rpx">
  5. <u-form-item label="收货人">
  6. <u-input v-model="form.name" placeholder="请输入收货人姓名" />
  7. </u-form-item>
  8. <u-form-item label="联系方式">
  9. <u-input v-model="form.mobile" placeholder="请输入联系方式" />
  10. </u-form-item>
  11. <u-form-item label="所在地区">
  12. <uni-data-picker v-model="selAddressCodes" :localdata="dataList" placeholder="请选择所在地区"
  13. popup-title="请选择所在地区" @change="cityChange"></uni-data-picker>
  14. </u-form-item>
  15. <u-form-item label="详细地址">
  16. <u-input v-model="form.detailAddress" placeholder="请输入详细地址" />
  17. </u-form-item>
  18. <u-form-item :border-bottom="false" label="设为默认地址">
  19. <u-switch slot="right" v-model="form.defaultFlag" :active-value="1" :inactive-value="0"
  20. :active-color="appThemeColor"></u-switch>
  21. </u-form-item>
  22. </u-form>
  23. </view>
  24. <view class="btn">
  25. <u-button type="primary" shape="circle" @click="submit">
  26. <u-icon name="plus"></u-icon>
  27. <text>保存地址</text>
  28. </u-button>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. import CityPicker from '@/pages-mine/components/city-picker.vue';
  34. export default {
  35. components: {
  36. CityPicker
  37. },
  38. data() {
  39. return {
  40. // 标题
  41. title: '收货地址',
  42. appThemeColor: this.$appTheme.appThemeColor,
  43. // 表单
  44. form: {
  45. id: null,
  46. "provinceId": '',
  47. "cityId": '',
  48. "districtId": '',
  49. "detailAddress": "",
  50. "name": "",
  51. "mobile": "",
  52. "defaultFlag": 0
  53. },
  54. // 省市区
  55. selAddressCodes: [],
  56. dataList: [],
  57. };
  58. },
  59. onLoad(ops) {
  60. if (ops.id) {
  61. this.form.id = ops.id;
  62. this.form.type = 'edit';
  63. this.title = '修改地址';
  64. this.getAddressDetail(ops.id);
  65. } else {
  66. this.form.id = null;
  67. this.form.type = 'add';
  68. this.title = '新建地址';
  69. }
  70. uni.setNavigationBarTitle({
  71. title: this.title
  72. })
  73. uni.$u.http.get('/token/getAllDistrict').then(res => {
  74. if (res.code == 200) {
  75. this.dataList = res.data
  76. }
  77. })
  78. },
  79. methods: {
  80. // 省市区选择回调
  81. cityChange(e) {
  82. let {
  83. value
  84. } = e.detail
  85. if (!value.length) return
  86. this.form.provinceId = value[0].value;
  87. this.form.cityId = value[1].value
  88. this.form.districtId = value[2].value || ''
  89. },
  90. // 提交表单
  91. submit() {
  92. let path = this.form.id ? '/token/user/address/update' : '/token/user/address/add'
  93. uni.$u.http.post(path, this.form).then(res => {
  94. if (res.code == 200) {
  95. uni.showToast('操作成功');
  96. uni.navigateBack();
  97. }
  98. })
  99. },
  100. // 编辑
  101. getAddressDetail(id) {
  102. uni.showLoading();
  103. uni.$u.http.get(`/token/user/address/getDetail/${id}`).then((res) => {
  104. if (res.code == 200) {
  105. this.form = res.data
  106. this.selAddressCodes = [res.data.provinceId, res.data.cityId, res.data.districtId]
  107. }
  108. }).finally(() => {
  109. uni.hideLoading();
  110. })
  111. },
  112. }
  113. };
  114. </script>
  115. <style lang="scss" scoped>
  116. .form {
  117. background-color: $app-theme-bg-color;
  118. padding: 30rpx;
  119. }
  120. .btn {
  121. padding: 60rpx 30rpx;
  122. }
  123. </style>