Fix(购物车&订单):修复地址选择之后事件分发错误问题

This commit is contained in:
lifizer
2024-12-04 12:03:32 +08:00
parent c0a09ad449
commit c5c9c9146b
6 changed files with 223 additions and 274 deletions
+28 -20
View File
@@ -399,21 +399,23 @@ export default {
},
created() {
this.getCart()
},
onLoad() {
this.getAddress()
},
onShow() {
this.isEdit = false
uni.$on('chooseAddress', (res) => {
console.log(res, '地址修改了!!!!!!!!!!!!!!!!!!!!');
this.getAddress(res)
})
if (!this.isLoad) {
return
}
this.closeAttr()
this.getCart()
// 选择地址
const cartChooseAddress = uni.getStorageSync('cartChooseAddress') || {}
if (cartChooseAddress.id) {
this.addressInfo = cartChooseAddress
uni.setStorageSync('cartChooseAddress', {})
return
}
this.getAddress()
},
onHide() {
this.pageToHideHandle()
@@ -434,7 +436,7 @@ export default {
this.$yrouter.push({
path: "/pages/user/address/AddressManagement/index",
query: {
choosMode: 1
choosMode: 2
}
});
},
@@ -442,22 +444,28 @@ export default {
this.$yrouter.push({
path: "/pages/user/address/AddAddress/index",
query: {
choosMode: 1
choosMode: 2
}
});
},
getAddress(details) {
if(details) {
this.addressInfo = details;
this.$forceUpdate()
}
if(!details) {
getAddressList({page: 1, limit: 9999}).then(res => {
this.addressList = res.data
this.addressInfo = res.data.find(e => e.isDefault == 1) || res.data[0] && res.data[0] || null
});
}
getAddress() {
getAddressList({page: 1, limit: 9999}).then(res => {
const { data = [] } = res
this.addressList = data
if (!this.addressInfo.id) {
this.addressInfo = data.find(e => e.isDefault == 1) || (data && data[0]) || null
} else {
let isExist = false
data.map(item => {
if (item.id === this.addressInfo.id) {
isExist = true
}
})
if (!isExist) {
this.addressInfo = data.find(e => e.isDefault == 1) || (data && data[0]) || null
}
}
})
},
clearCart() {
const ids = this.list.map(l => {
+39 -56
View File
@@ -246,9 +246,7 @@ export default {
delayId: null,
pageKeyId: Object.freeze('userOrderIndex'),
keyword: '',
_orderId: '',
addressId: '',
currentRoute: ''
_orderId: ''
};
},
components: {
@@ -258,60 +256,45 @@ export default {
},
computed: mapGetters(["userInfo"]),
onShow() {
this.type = this.type || parseInt(this.$yroute.query.type) || 0;
this.changeType(this.type);
this.getOrderData();
this.getOrderListReq();
uni.$on('chooseAddress', (res) => {
this.addressId = res.id
})
const pages = getCurrentPages() // 获取栈实例
this.currentRoute = pages[pages.length - 1].route;
},
onHide() {
this.orderList = [];
this.page = 1;
this.limit = 20;
this.loaded = false;
this.loading = false;
},
watch: {
addressId(val) {
const pages = getCurrentPages() // 获取栈实例
let currentRoute = pages[pages.length - 2].route;
if(val && this.currentRoute === currentRoute) {
const params = {
addressId: this.addressId,
orderId: this._orderId
const _this = this
const chooseAddress = uni.getStorageSync('chooseAddress') || {}
const addressId = chooseAddress.id || ''
// 监听到选择收货地址
if (addressId) {
uni.setStorageSync('chooseAddress', {})
uni.showModal({
title:'确定修改地址吗?',
content: '',
showCancel: true,
cancelText: '取消',
confirmText: '确认',
success:(success)=>{
if(success.confirm) {
uni.showLoading()
editAddress({
addressId,
orderId: _this._orderId
}).then(res => {
uni.showToast({
icon: 'success',
title: '修改成功!'
})
_this.changeType()
}).finally(() => {
uni.hideLoading()
})
}
},
cancel: () => {
_this.changeType()
},
complete:(complete)=>{
_this._orderId = ''
}
setTimeout(() => {
uni.showModal({
title:'确定修改地址吗?',
content: '',
showCancel: true,
cancelText: '取消',
confirmText: '确认',
success:(success)=>{
if(success.confirm) {
uni.showLoading()
editAddress(params).then(res => {
uni.showToast({
icon: 'success',
title: '修改成功!'
})
}).finally(() => {
uni.hideLoading()
})
}
},
complete:(complete)=>{
this.addressId = ''
this._orderId = ''
},
})
}, 1000)
}
})
} else {
_this.type = _this.type || parseInt(_this.$yroute.query.type) || 0;
_this.changeType(_this.type);
}
},
methods: {
+35 -41
View File
@@ -465,7 +465,6 @@ export default {
mixins: [pageListenMixins],
data: function () {
return {
addressId: null,
showExtend: false,
delayId: '',
showMore: false,
@@ -498,47 +497,42 @@ export default {
...mapGetters(["userInfo"])
},
onShow() {
this.id = this.$yroute.query.id;
this.getDetail();
uni.$on('chooseAddress', (res) => {
this.addressId = res.id
})
},
watch: {
addressId(val) {
if(val) {
const params = {
addressId: this.addressId,
orderId: this.orderInfo.orderId
}
setTimeout(() => {
uni.showModal({
title:'确定修改地址吗?',
content: '',
showCancel: true,
cancelText: '取消',
confirmText: '确认',
success:(success)=>{
if(success.confirm) {
uni.showLoading()
editAddress(params).then(res => {
uni.showToast({
icon: 'success',
title: '修改成功!'
})
this.getDetail();
}).finally(() => {
uni.hideLoading()
})
}
},
complete:(complete)=>{
const _this = this
const chooseAddress = uni.getStorageSync('chooseAddress') || {}
const addressId = chooseAddress.id || ''
// 监听到选择收货地址
if (addressId) {
uni.setStorageSync('chooseAddress', {})
uni.showModal({
title:'确定修改地址吗?',
content: '',
showCancel: true,
cancelText: '取消',
confirmText: '确认',
success:(success)=>{
if(success.confirm) {
uni.showLoading()
editAddress({
addressId,
orderId: _this.orderInfo.orderId
}).then(res => {
uni.showToast({
icon: 'success',
title: '修改成功!'
})
_this.getDetail()
}).finally(() => {
uni.hideLoading()
this.addressId = ''
},
})
}, 1000)
}
})
}
},
complete:(complete)=>{
uni.hideLoading()
}
})
} else {
_this.id = _this.$yroute.query.id;
_this.getDetail();
}
},
// inject: ["app"],
+31 -34
View File
@@ -388,49 +388,46 @@ export default {
this.computedPrice('couponId')
}
},
onLoad: function () {
let that = this;
uni.$on('chooseAddress', (res) => {
console.log('监听到选择收货地址:' + JSON.stringify(res));
setTimeout(() => {
uni.showModal({
title:'确定修改地址吗?',
content: '',
showCancel: true,
cancelText: '取消',
confirmText: '确认',
success:(success)=>{
if(success.confirm) {
that.addressInfo = res;
//地址切换也要重新计算一下
that.computedPrice('onLoad chooseAddress');
}
},
complete:(complete)=>{
uni.hideLoading()
},
})
})
},1000)
onLoad() {
const that = this
if (that.$yroute.query.pinkid !== undefined) {
that.pinkId = that.$yroute.query.pinkid;
that.pinkId = that.$yroute.query.pinkid
}
if (that.$yroute.query.id !== undefined) {
that.cartid = that.$yroute.query.id;
that.cartid = that.$yroute.query.id
console.log(that.cartid)
}
if (that.$yroute.query.lotteryRecordId !== undefined) {
that.lotteryRecordId = that.$yroute.query.lotteryRecordId;
that.lotteryRecordId = that.$yroute.query.lotteryRecordId
}
that.getCartInfo();
that.getCartInfo()
},
onUnload: function () {
console.log('关闭监听选择收货地址');
uni.$off('chooseAddress');
},
onShow: function () {
onShow() {
const _this = this
//获取用户信息
this.$store.dispatch("getUser", true);
_this.$store.dispatch("getUser", true)
const chooseAddress = uni.getStorageSync('chooseAddress') || {}
// 监听到选择收货地址
if (chooseAddress.id) {
uni.showModal({
title:'确定修改地址吗?',
content: '',
showCancel: true,
cancelText: '取消',
confirmText: '确认',
success: (res) => {
if(res.confirm) {
_this.addressInfo = chooseAddress
// 地址切换也要重新计算一下
_this.computedPrice('onLoad chooseAddress')
}
},
complete: () => {
uni.hideLoading()
uni.setStorageSync('chooseAddress', {})
}
})
}
},
methods: {
isNullOrEmpty,
+18 -2
View File
@@ -91,6 +91,8 @@ export default {
return {
district: [],
id: 0,
// 是否选择地址模式:0-不是,1-来源订单创建页面,2-来源购物车
choosMode: 0,
userAddress: {
realName: '',
phone: '',
@@ -104,6 +106,15 @@ export default {
addressInput: ''
};
},
onLoad(e) {
// 是否选择模式
const choosMode = this.$yroute.query.choosMode || 0
if (choosMode) {
this.choosMode = parseInt(choosMode)
} else {
this.choosMode = 0
}
},
mounted: function () {
const id = this.$yroute.query.id
this.id = id
@@ -382,7 +393,7 @@ export default {
duration: 2000
});
}
if(that.$yroute.query.choosMode == 1) {
if (that.choosMode) {
const item = {
id: res.data.id,
realName: name,
@@ -394,7 +405,12 @@ export default {
is_default: isDefault ? true : false,
post_code: ""
}
uni.$emit('chooseAddress', item)
if(that.choosMode === 1) {
uni.setStorageSync('chooseAddress', item)
}
if(that.choosMode === 2) {
uni.setStorageSync('cartChooseAddress', item)
}
}
that.$yrouter.back();
});
+72 -121
View File
@@ -1,9 +1,8 @@
<template>
<view
class="address-management"
:class="addressList.length < 1 && page > 1 ? 'on' : ''"
ref="container"
ref="container"
:class="addressList.length < 1 && page > 1 ? 'on' : ''"
class="address-management"
>
<view style="margin-bottom: 12rpx;" class="line" v-if="addressList.length > 0">
<!-- <image :src="webUrl+'/20230304135025917856.png'"/> -->
@@ -25,7 +24,7 @@
}}{{ item.detail }}
</view>
</view>
<view v-if="!chooseMode" class="operation acea-row row-between-wrapper">
<view v-if="!choosMode" class="operation acea-row row-between-wrapper">
<view class="select-btn">
<view class="checkbox-wrapper">
<checkbox-group @change.stop="radioChange(item.id)">
@@ -65,22 +64,13 @@
<text class="iconfont icon-tianjiadizhi"></text>
添加新地址
</view>
<!--<view class="addressBnt wxbnt" v-if="isWechat" @click="getAddress">-->
<!--<text class="iconfont icon-weixin2"></text>导入微信地址-->
<!--</view>-->
</view>
</view>
</template>
<style scoped lang="less">
.address-management.on {
background-color: #fff;
height: 100vh;
}
</style>
<script type="text/babel">
import {getAddressDefaultSet, getAddressList, getAddressRemove} from "@/api/user";
import Loading from "@/components/Loading";
import {isWeixin} from "@/utils";
import {getAddressDefaultSet, getAddressList, getAddressRemove} from "@/api/user"
import Loading from "@/components/Loading"
import {isWeixin} from "@/utils"
import { pageListenMixins } from '@/mixins/pageListenMixins'
export default {
@@ -93,85 +83,92 @@ export default {
webUrl: this.$VUE_APP_RESOURCES_URL,
page: 1,
limit: 20,
chooseMode: false,//是否选择地址模式
// 是否选择地址模式:0-不是,1-来源订单创建页面,2-来源购物车
choosMode: 0,
addressList: [],
loadTitle: "",
loading: false,
loadend: false,
isWechat: isWeixin(),
pageKeyId: Object.freeze('userAddressIndex')
};
}
},
mounted: function () {
this.AddressList();
mounted() {
this.AddressList()
},
onReachBottom() {
!this.loading && this.AddressList();
!this.loading && this.AddressList()
},
onLoad: function (e) {
//是否选择模式
if (this.$yroute.query.choosMode != undefined) {
this.chooseMode = true;
onLoad(e) {
// 是否选择模式
const choosMode = this.$yroute.query.choosMode || 0
if (choosMode) {
this.choosMode = parseInt(choosMode)
uni.setNavigationBarTitle({
title: '选择收货地址'
})
} else {
this.chooseMode = false;
this.choosMode = 0
}
},
onShow: function () {
this.refresh();
onShow() {
this.refresh()
},
methods: {
tapAddress: function (item) {
if (this.chooseMode) {
//通知前一界面修改地址
uni.$emit('chooseAddress', item)
//关闭当前页面
this.$yrouter.back();
tapAddress(item) {
// 来源订单创建页面
if (this.choosMode === 1) {
uni.setStorageSync('chooseAddress', item)
this.$yrouter.back()
}
// 来源购物车
if (this.choosMode === 2) {
uni.setStorageSync('cartChooseAddress', item)
this.$yrouter.back()
}
},
refresh: function () {
this.addressList = [];
this.page = 1;
this.loadend = false;
this.AddressList();
refresh() {
this.addressList = []
this.page = 1
this.loadend = false
this.AddressList()
},
/**
* 获取地址列表
*
*/
AddressList: function () {
let that = this;
if (that.loading) return; //阻止下次请求(false可以进行请求)
if (that.loadend) return; //阻止结束当前请求(false可以进行请求);
that.loading = true;
AddressList() {
const that = this
// 阻止下次请求(false可以进行请求)
if (that.loading) return
// 阻止结束当前请求(false可以进行请求)
if (that.loadend) return
that.loading = true
getAddressList({page: that.page, limit: that.limit}).then(res => {
that.loading = false;
//apply();js将一个数组插入另一个数组;
that.addressList.push.apply(that.addressList, res.data);
that.loadend = res.data.length < that.limit; //判断所有数据是否加载完成
that.page = that.page + 1;
});
that.loading = false
// apply();js将一个数组插入另一个数组
that.addressList.push.apply(that.addressList, res.data)
// 判断所有数据是否加载完成
that.loadend = res.data.length < that.limit
that.page = that.page + 1
})
},
/**
* 编辑地址
*/
editAddress: function (index) {
editAddress(index) {
this.$yrouter.push({
path: "/pages/user/address/AddAddress/index",
query: {id: this.addressList[index].id}
});
})
},
/**
* 删除地址
*/
delAddress: function (index) {
let that = this;
let address = this.addressList[index];
let id = address.id;
delAddress(index) {
const that = this
const address = this.addressList[index]
const id = address.id
uni.showModal({
title: '确认删除此收货地址?',
content: '',
@@ -179,7 +176,6 @@ export default {
cancelText: '取消',
confirmText: '确认',
success: res => {
if (res.confirm) {
getAddressRemove(id).then(function () {
uni.showToast({
@@ -187,84 +183,40 @@ export default {
icon: "success",
duration: 2000,
complete: () => {
that.addressList.splice(index, 1);
that.$set(that, "addressList", that.addressList);
that.addressList.splice(index, 1)
that.$set(that, "addressList", that.addressList)
}
});
});
})
})
}
},
fail: () => {
},
complete: () => {
}
});
})
},
/**
* 设置默认地址
*/
radioChange: function (id) {
radioChange(id) {
getAddressDefaultSet(id).then(res => {
this.refresh();
uni.showToast({title: res.msg, icon: "none", duration: 2000});
});
this.refresh()
uni.showToast({title: res.msg, icon: "none", duration: 2000})
})
},
/**
* 新增地址
*/
addAddress: function () {
addAddress() {
this.$yrouter.push({
path: "/pages/user/address/AddAddress/index"
});
},
getAddress() {
// openAddress().then(userInfo => {
// uni.showLoading({ title: "加载中" });
// postAddress({
// real_name: userInfo.userName,
// phone: userInfo.telNumber,
// address: {
// province: userInfo.provinceName,
// city: userInfo.cityName,
// district: userInfo.countryName
// },
// detail: userInfo.detailInfo,
// post_code: userInfo.postalCode,
// wx_export: 1
// })
// .then(() => {
// this.page = 1;
// this.loading = false;
// this.loadend = false;
// this.addressList = [];
// this.AddressList();
// uni.hideLoading();
// uni.showToast({
// title: "添加成功",
// icon: 'success',
// duration: 2000
// });
// })
// .catch(err => {
// uni.hideLoading();
// uni.showToast({
// title: err.msg || err.response.data.msg|| err.response.data.message,
// icon: 'none',
// duration: 2000
// });
// });
// });
})
}
}
};
}
</script>
<style scoped lang="less">
.address-management.on {
background-color: #fff;
height: 100vh;
}
.address-management .item {
box-shadow: 0px 6rpx 6rpx rgba(0, 0, 0, 0.1);
opacity: 1;
@@ -272,5 +224,4 @@ export default {
margin-left: 32rpx;
margin-right: 32rpx;
}
</style>