fix: 优化提交订单页面js警告,充值订单跳转bug以及其他bug。refactor: 重构地址选择

This commit is contained in:
黄少君
2024-03-05 12:16:58 +08:00
parent 3fc1284094
commit a9533e2d7f
18 changed files with 553 additions and 671 deletions

View File

@ -25,7 +25,7 @@ onPageScroll(() => {
const {getParams, push, goBack} = useRouter()
const {toast, loading, hideLoading} = useInterface();
const mainStore = useMainStore();
const {selectAddress, defaultAddress, address, integralName} = storeToRefs(mainStore);
const {integralName} = storeToRefs(mainStore);
const campaignType = ref(0)
const zeroPrice = ref(false) // 本单支付金额是否为0
/**
@ -34,20 +34,19 @@ const zeroPrice = ref(false) // 本单支付金额是否为0
*/
async function calculateOrder() {
orderDetail.value = await orderConfirm({
cartId: unref(routerParams).cartId,
orderType: unref(routerParams).orderType,
cartId: unref(routerParams)?.cartId,
orderType: unref(routerParams)?.orderType,
addressId: unref(selectAddress)?.id || undefined,
shippingType: addressTabSelect.value, // 默认快递 1快递2门店
shippingType: addressTabSelect.value, // 默认快递 1快递 2门店
storeId: shopSelect.value?.id,
useIntegral: useIntegral.value.length > 0,
couponId: currentCouponId.value
})
console.log(orderDetail.value)
// 余额为0 自动走余额支付
if (orderDetail.value.priceGroup.totalPrice <= 0) {
payType.value = PayType["1"]
zeroPrice.value = true
toast({title: '支付金额为0.00,自动选择余额支付', icon: 'none', duration: 3000})
toast?.({title: '支付金额为0.00,自动选择余额支付', icon: 'none', duration: 3000})
} else {
zeroPrice.value = false
}
@ -59,13 +58,13 @@ const currentCouponId = ref(undefined)
const selectCouponFn = async (coupon) => {
currentCouponId.value = coupon.couponId
await calculateOrder()
selectCouponRef.value.close()
selectCouponRef.value?.close()
}
const showCouponSelect = (index) => {
if (index === 0) {
selectCouponRef.value.open()
selectCouponRef.value?.open()
}
}
@ -86,29 +85,25 @@ function changeAddressTab(tab) {
* 去选择地址
*/
function gotoSelectAddress() {
push({url: '/pages/address/address'}, {data: {type: 'select', cartId: unref(routerParams).cartId}})
push?.({url: '/pages/address/address'}, {
data: {
type: 'select', cartId: unref(routerParams)?.cartId
}
})
}
/**
* 获取默认选择的地址
*/
async function doGetSelectAddress() {
// store内部没有地址就先去获取地址
if (unref(address).length <= 0) {
await mainStore.getAddressList(1)
}
// 获取完地址还是空
if (unref(address).length <= 0) {
return
}
// 没有默认地址设置默认地址
if (!selectAddress.value || !selectAddress.value?.id) {
defaultAddress.value && defaultAddress.value.id ? mainStore.setSelectAddress(defaultAddress.value.id) : mainStore.setSelectAddress(unref(address)[0].id)
doGetInitConfirmOrder()
if (!selectAddress.value && orderDetail.value?.addressInfo) {
selectAddress.value = orderDetail.value?.addressInfo
await doGetInitConfirmOrder()
}
}
const shopSelect = ref(null) // 当前选择的店铺
const selectAddress = ref(null) // 当前选择的地址
// 注册事件监听
emitter.on('selectShop', (res) => {
@ -116,7 +111,9 @@ emitter.on('selectShop', (res) => {
shopSelect.value = res
doGetInitConfirmOrder()
})
emitter.on('selectAddress', async () => {
emitter.on('selectAddress', async (item) => {
// item
selectAddress.value = item
// 处理门店选择逻辑
await doGetSelectAddress()
await doGetInitConfirmOrder()
@ -126,7 +123,7 @@ emitter.on('selectAddress', async () => {
* 选择店铺
*/
function selectShop() {
push({url: '/pages/submitOrder/shopSelect'}, {data: {shopSelect: shopSelect.value}})
push?.({url: '/pages/submitOrder/shopSelect'}, {data: {shopSelect: shopSelect.value}})
}
// =============================== 订单信息相关 ======================================================
@ -155,8 +152,8 @@ const orderDetail = ref({
*/
async function doGetInitConfirmOrder() {
await calculateOrder()
cartIds.value = unref(routerParams).cartId
currentCouponId.value = orderDetail.value.priceGroup.couponId
cartIds.value = unref(routerParams)?.cartId
currentCouponId.value = orderDetail.value.priceGroup?.couponId
flag.value = true
}
@ -177,11 +174,11 @@ const subLoading = ref(false) // 加载中
*/
async function handleConfirm() {
if (addressTabSelect.value === 1 && (!unref(selectAddress) || !unref(selectAddress).id)) {
toast({title: '请先选择地址'})
toast?.({title: '请先选择地址'})
return
}
if (addressTabSelect.value === 2 && !shopSelect.value) {
toast({title: '请先选择门店'})
toast?.({title: '请先选择门店'})
return
}
subLoading.value = true
@ -190,18 +187,18 @@ async function handleConfirm() {
// 去拉取支付
await doPayment({type: payType.value, payInfo, isGroup: routerParams.value.campaignType === 1})
// #ifndef H5
push({url: '/pages/payStatus/index'}, {data: {campaignType: campaignType.value}, type: 'redirectTo'})
push?.({url: '/pages/payStatus/index'}, {data: {campaignType: campaignType.value}, type: 'redirectTo'})
// #endif
// 处理微信内h5
// #ifdef H5
if (h5InWeChat()) {
push({url: '/pages/payStatus/index'}, {data: {campaignType: campaignType.value}, type: 'redirectTo'})
push?.({url: '/pages/payStatus/index'}, {data: {campaignType: campaignType.value}, type: 'redirectTo'})
}
// #endif
} catch (e) {
console.error(e)
toast({title: '支付失败'})
push({url: '/pages/payStatus/index'}, {data: {campaignType: campaignType.value}, type: 'redirectTo'})
toast?.({title: '支付失败'})
push?.({url: '/pages/payStatus/index'}, {data: {campaignType: campaignType.value}, type: 'redirectTo'})
} finally {
subLoading.value = false
mainStore.cartId = null
@ -213,11 +210,11 @@ async function handleConfirm() {
*/
async function doCreateServiceOrder() {
try {
loading({title: '订单创建中...'})
loading?.({title: '订单创建中...'})
const data = {
key: unref(orderDetail).orderKey,
addressId: unref(selectAddress) && unref(selectAddress).id,
storeId: unref(shopSelect) && unref(shopSelect).id,
key: unref(orderDetail)?.orderKey,
addressId: unref(selectAddress)?.id,
storeId: unref(shopSelect)?.id,
bargainId: 0,
combinationId: 0,
couponId: currentCouponId.value,
@ -235,7 +232,7 @@ async function doCreateServiceOrder() {
const res = await orderCreate(data);
return res.result
} finally {
hideLoading()
hideLoading?.()
}
}
@ -245,15 +242,15 @@ async function doCreateServiceOrder() {
*/
function setActivityData(data) {
// 处理活动数据 路由参数 orderType 1普通下单 2活动下单
if (routerParams.value.orderType !== 2) return
if (routerParams.value?.orderType !== 2) return
// 活动商品
data.campaignType = routerParams.value.campaignType // 1拼团 2秒杀 3砍价
campaignType.value = routerParams.value.campaignType
data.campaignDetailId = routerParams.value.campaignDetailId // 活动营销ID
data.campaignType = routerParams.value?.campaignType // 1拼团 2秒杀 3砍价
campaignType.value = routerParams.value?.campaignType
data.campaignDetailId = routerParams.value?.campaignDetailId // 活动营销ID
// 拼团 路由参数 campaignType 1拼团活动
if (routerParams.value.campaignType !== 1) return;
data.teamworkType = routerParams.value.teamworkType // 1发起拼团 2加入
data.teamworkType === 2 ? data.teamworkId = routerParams.value.teamworkId : void (0) // 加入拼团的id
if (routerParams.value?.campaignType !== 1) return;
data.teamworkType = routerParams.value?.teamworkType // 1发起拼团 2加入
data.teamworkType === 2 ? data.teamworkId = routerParams.value?.teamworkId : void (0) // 加入拼团的id
}
@ -267,7 +264,7 @@ const cartIds = ref('')
*/
function checkRouterParam(params) {
if (!params.cartId) {
toast({title: '路由参数错误'})
toast?.({title: '路由参数错误'})
return
}
routerParams.value = params
@ -276,7 +273,7 @@ function checkRouterParam(params) {
onLoad(async options => {
try {
const params = getParams(options)
const params = getParams?.(options)
await checkRouterParam(params)
} catch (e) {
console.error(e)
@ -318,8 +315,8 @@ onShow(async () => {
<!-- address info -->
<template v-if="addressTabSelect === 1">
<view
v-if="selectAddress"
class="address-row flex flex-ai__center flex-jc__sb"
v-if="address.length>0"
@click="gotoSelectAddress"
>
<view
@ -351,7 +348,7 @@ onShow(async () => {
v-else
@click="gotoSelectAddress"
>
点击添加地址
点击选择地址
<image
class="arrow-icon"
:src="nextIcon"