Compare commits

...

3 Commits

Author SHA1 Message Date
877469ab43 优化 2024-04-14 20:18:44 +08:00
13fd77f13b 优化 2024-04-14 17:02:49 +08:00
f2f7fcde48 购物车逻辑调整 2024-04-14 16:43:43 +08:00
14 changed files with 130 additions and 41 deletions

View File

@@ -1,8 +1,20 @@
<script setup lang='ts'>
import { mpUpdate } from '@/utils';
import { mpUpdate, setReferrerUserId, setRegisterStoreId } from '@/utils';
onLaunch((options) => {
console.log('App Launch options ', options);
if(options?.query) {
//保存登录邀请员工id
if(options.query.referrerUserId) {
setReferrerUserId(options.query.referrerUserId);
}
//保存注册门店id
if(options.query.storeId) {
setRegisterStoreId(options.query.storeId);
}
}
// #ifdef MP
mpUpdate();
// #endif

View File

@@ -4,8 +4,10 @@
<view class='c-flex-row' style='align-items: flex-start'>
<image class='goods-image' :src='goodsDetailBean?.images||defaultImage' />
<view class='c-flex-column' style='flex: 1'>
<text class='goods-name'>{{ goodsDetailBean?.name }}</text>
<text class='goods-price'>{{ flashPrice > 0 ? `${flashPrice}` : `${goodsDetailBean?.price}` || 0 }}</text>
<text class='goods-name'>{{ goodsDetailBean?.name || '未知' }}</text>
<text class='goods-price'>{{ flashPrice > 0 ? `${flashPrice}` : `${goodsDetailBean?.consumePrice || 0}` || 0
}}
</text>
</view>
<image class='close-image' :src='assetsUrl("ic_close.png")' @click.stop='close' />
</view>
@@ -58,6 +60,10 @@ import { assetsUrl, defaultImage } from '@/utils/assets';
import { showToast } from '@/utils';
import { getGoodsDetail } from '@/api/goods';
import { GoodsBean, StockBean } from '@/api/goods/types';
import { useUserStore } from '@/store';
const userStore = useUserStore();
const { userInfo } = storeToRefs(userStore);
const props = defineProps({
flashPrice: {
@@ -78,7 +84,9 @@ const show = async (goodsId: string, fn: Function) => {
callback = fn;
goodsDetailBean.value = await getGoodsDetail(goodsId);
if(goodsDetailBean.value?.stocks?.length == 0) {
goodsDetailBean.value.consumePrice = Number((goodsDetailBean.value?.price * ((userInfo?.value?.levelEntity?.discount || 100) / 100)).toFixed(2));
if((goodsDetailBean.value?.stocks?.length || 0) <= 0) {
showToast('暂无库存');
return;
}
@@ -103,7 +111,8 @@ const show = async (goodsId: string, fn: Function) => {
}
if(skuSizeList) {
// skuSizeList.value![2].existingNumber = 2;
//skuSizeList.value![0].existingNumber = 2;
//skuSizeList.value![2].existingNumber = 2;
}
};

View File

@@ -120,6 +120,7 @@ onLoad((e) => {
onShow(async () => {
if(isLogin()) {
// userStore.getProfile();
if(userInfo.value.maOpenId) {
if(getCompanyId() === '') {
switchCompany();

View File

@@ -31,7 +31,7 @@
<view class='shopping-cart' @click.stop='goPath("/pages/mall/subs/shoppingcart/index")'>
<image :src='assetsUrl("ic_shopping_cart.png")' />
<text v-if='shoppingCartList.length>0'>{{ shoppingCartList?.length }}</text>
<text v-if='totalCount>0'>{{ totalCount }}</text>
</view>
</view>
<view class='content' v-else style='align-items: center;justify-content:center;'>
@@ -52,7 +52,7 @@ import { useUserStore } from '@/store';
const userStore = useUserStore();
const { companyConfigInfo } = storeToRefs(userStore);
const shoppingCartStore = useShoppingCartStore();
const { shoppingCartList } = storeToRefs(shoppingCartStore);
const { totalCount } = storeToRefs(shoppingCartStore);
const skuDialogRef = ref();
const categoryList = ref<CategoryBean[]>([]);
@@ -66,13 +66,12 @@ onLoad(() => {
return;
}
console.log('companyConfigInfo.value', companyConfigInfo.value);
userStore.$onAction(({ name, after }) => {
after(() => {
//更新用户信息
if(name === 'setUserInfo') {
fetchCategoryList();
shoppingCartStore.resetData();
}
});
});
@@ -127,7 +126,16 @@ const getRandomFloatInRange = (a: number, b: number) => {
const addShoppingCart = (goodsBean: GoodsBean) => {
skuDialogRef.value.show(goodsBean.goodsId, (e: GoodsBean) => {
//重新选择sku后判断当前购物中是否存在相同sku的商品
const existsIndex = shoppingCartStore.getSameGoodsIndex(e?.id || '', e.checkedStock.colorId, e.checkedStock.sizeId);
//不存在则更新当前商品sku
if(existsIndex < 0) {
shoppingCartStore.save(e);
}
//如果已存在,则更新已存在商品数量,并删除当前商品
else {
shoppingCartStore.updateCount(existsIndex, e.checkedStock.count);
}
showToast('添加购物车成功');
});
};

View File

@@ -42,7 +42,7 @@
</view>
</view>
<view class='recommend-view c-flex-column' v-if='recommendList?.length>0'>
<view class='recommend-view c-flex-column' v-if='(recommendList?.length||0)>0'>
<text>浏览此商品的客户还浏览了</text>
<scroll-view scroll-x>
<view style='display: inline-block' v-for='(item, index) in recommendList'
@@ -78,7 +78,7 @@
<view class='small-button-item'>
<view class='shoppingcart-count' @click.stop='goPath("/pages/mall/subs/shoppingcart/index")'>
<image :src='assetsUrl("ic_goods_shoppingcart.png")' />
<text v-if='shoppingCartList.length>0'>{{ shoppingCartList.length }}</text>
<text v-if='totalCount>0'>{{ totalCount }}</text>
</view>
<text>购物车</text>
</view>
@@ -102,7 +102,7 @@ import { GoodsBean } from '@/api/goods/types';
import useShoppingCartStore from '@/store/modules/shoppingcart';
const shoppingCartStore = useShoppingCartStore();
const { shoppingCartList } = storeToRefs(shoppingCartStore);
const { totalCount } = storeToRefs(shoppingCartStore);
const goodsBean = ref<GoodsBean>();
const recommendList = ref<GoodsBean[]>();
@@ -161,8 +161,9 @@ const addShoppingCart = () => {
const placeOrder = () => {
showSkuDialog((e: GoodsBean) => {
console.log(e);
const orderBean = {
totalPrice: e.price * e.checkedStock.count,
totalPrice: (e.consumePrice * e.checkedStock.count).toFixed(2),
orderGoods: [e]
};
goPath(`/pages/mall/subs/order/order-confirm?orderBean=${encodeURIComponent(JSON.stringify(orderBean))}`);

View File

@@ -124,7 +124,7 @@
import { assetsUrl, defaultImage } from '@/utils/assets';
import PaymentDialog from '@/components/payment-dialog.vue';
import CouponDialog from '@/pages/mall/subs/components/coupon-dialog.vue';
import { goPath, parseParameter, sortASCII } from '@/utils';
import { goPath, parseParameter, showToast, sortASCII } from '@/utils';
import { hexMD5 } from '@/utils/common/md5';
import { getPaymentList, orderCreate, overPayment } from '@/api/order';
import { CouponBean } from '@/api/user/types';
@@ -198,6 +198,12 @@ const navigateTo = (e: any) => {
};
const createOrder = async () => {
//邮寄
if(tabIndex.value == 1 && !deliveryAddress.value.addrid) {
showToast('请选择收货地址');
return;
}
const params = {
// 'discount': 0,
// 'freePrice': 0,
@@ -217,7 +223,7 @@ const createOrder = async () => {
'stockId': item.checkedStock.stockId,
'originPrice': item.price,
// 'consumePrice': item.price * ((userInfo.value?.levelEntity?.discount || 0) / 100),
'consumePrice': item.price,
'consumePrice': item.consumePrice,
'discount': userInfo.value?.levelEntity?.discount || 0
// 'discountOriginPrice': 0,
// 'produceIntegral': 0,

View File

@@ -6,7 +6,7 @@
</text>
</view>
<view class='card-view' v-if='shoppingCartList.length>0'>
<view class='card-view' v-if='totalCount>0'>
<scroll-view>
<view class='c-flex-column' v-for='(item, index) in shoppingCartList' :key='index'>
<view class='c-flex-row'>
@@ -72,7 +72,7 @@ const isEditMode = ref(false);
const checkedAll = ref(false);
const shoppingCartStore = useShoppingCartStore();
const { shoppingCartList } = storeToRefs(shoppingCartStore);
const { shoppingCartList, totalCount } = storeToRefs(shoppingCartStore);
const skuDialogRef = ref();
const temporaryGoodsBean = ref<GoodsBean>();
@@ -85,7 +85,7 @@ watch(checkedAll, (newValue) => {
});
const totalPayPrice = computed(() => {
return shoppingCartList.value.filter(res => res.checked).reduce((a, b) => a + b.price * b.checkedStock.count, 0);
return shoppingCartList.value.filter(res => res.checked).reduce((a, b) => a + b.consumePrice * b.checkedStock.count, 0);
});
const deleteShoppingCart = () => {

View File

@@ -10,7 +10,7 @@
<view class='right-content accent-text-color' :class='{"right-content-disabled": item?.status==1}'>
<text>{{ item?.name }}</text>
<text>有效期至{{ dayjs(item?.startTime).format('YYYY-MM-DD') }}</text>
<text class='btn-text' :class='{"btn-text-disabled": item?.status==1}'>
<text class='btn-text' :class='{"btn-text-disabled": item?.status==1}' @click.stop='goPath("/pages/mall/index")'>
立即使用
</text>
</view>
@@ -24,6 +24,7 @@ import { assetsUrl } from '@/utils/assets';
import dayjs from 'dayjs';
import { PropType } from 'vue';
import { CouponBean } from '@/api/user/types';
import { goPath } from '@/utils';
defineProps({
item: Object as PropType<CouponBean>

View File

@@ -4,7 +4,7 @@
<image class='goods-image' :src='item?.orderGoods[0].images||defaultImage' />
<view class='c-flex-column' style='flex: 1;'>
<view class='c-flex-row'>
<text class='goods-name'>{{ item?.orderGoods[0].goodsName }}</text>
<text class='goods-name'>{{ item?.orderGoods[0].goodsName||'未知' }}</text>
<text class='status'>
<text v-if='item?.payStatus == 2'>已支付</text>
<text v-else-if='isPending(item)'>待支付</text>

View File

@@ -38,7 +38,7 @@
<image class='goods-image' :src='item.stockStock.images||defaultImage' />
<view class='c-flex-column' style='flex: 1;'>
<view class='c-flex-row'>
<text class='goods-name'>{{ item.goodsName }}</text>
<text class='goods-name'>{{ item.goodsName || '未知' }}</text>
</view>
<text style='color: #999999;margin-top: 30rpx'>
{{ item.goodsCode }}
@@ -92,6 +92,12 @@
<text style='color: #5B96FB;margin-left: 10rpx'>复制</text>
</text>
<text>下单时间{{ orderBean?.order?.createTime }}</text>
<text>
支付状态
<text v-if='orderBean?.order?.payStatus == 2' style='color: #F32B2B'>已支付</text>
<text v-else-if='isPending(orderBean?.order)' style='color: #F32B2B'>待支付</text>
<text v-else style='color: #999999'>已过期</text>
</text>
</view>
<view class='bottom-action-view c-flex-row' v-if='isPending(orderBean?.order)'>
@@ -404,8 +410,8 @@ const payment = () => {
color: #333333;
}
text:nth-of-type(1) {
margin-bottom: 12rpx;
text:nth-of-type(3) {
margin-bottom: 0rpx;
}
}

View File

@@ -4,18 +4,19 @@ import { getCompanyId } from '@/utils';
const useShoppingCartStore = defineStore('shoppingCart', {
state: (): { shoppingCartList: GoodsBean[] } => ({
shoppingCartList: []
shoppingCartList: uni.getStorageSync(`shoppingCart_${getCompanyId()}`) == '' ? [] : uni.getStorageSync(`shoppingCart_${getCompanyId()}`) as GoodsBean[]
}),
persist: {
// 修改存储中使用的键名称,默认为当前 Store的 id
key: 'shoppingCartState_' + getCompanyId(),
key: 'shoppingCartState',
storage: {
setItem(key, value) {
uni.setStorageSync(key, value); // [!code warning]
uni.setStorageSync(key, value || []); // [!code warning]
uni.setStorageSync(`shoppingCart_${getCompanyId()}`, JSON.parse(value).shoppingCartList);
},
getItem(key) {
return uni.getStorageSync(key); // [!code warning]
return uni.getStorageSync(uni.getStorageSync(`shoppingCart_${getCompanyId()}`)) || []; // [!code warning]
}
},
// 部分持久化状态的点符号路径数组,[]意味着没有状态被持久化(默认为undefined持久化整个状态)
@@ -23,16 +24,16 @@ const useShoppingCartStore = defineStore('shoppingCart', {
},
getters: {
getShoppingCartList(): any[] {
getShoppingCartList(): GoodsBean[] {
return this.shoppingCartList;
},
getTotalCount(): number {
totalCount(): number {
return this.shoppingCartList.length;
},
getSameGoodsIndex: (state) => (goodsId: string, colorId: string, sizeId: string) => {
return state.shoppingCartList.findIndex(res => res.id === goodsId && res.checkedStock.colorId === colorId && res.checkedStock.sizeId === sizeId);
return state.shoppingCartList.findIndex(res => res.id === goodsId && res.checkedStock.colorId === colorId && res.checkedStock.sizeId === sizeId) || -1;
}
},
@@ -57,6 +58,10 @@ const useShoppingCartStore = defineStore('shoppingCart', {
this.shoppingCartList = this.shoppingCartList.filter(res => res.checked == undefined || res.checked === false);
},
resetData() {
this.shoppingCartList = uni.getStorageSync(`shoppingCart_${getCompanyId()}`);
},
clearAll() {
this.shoppingCartList = [];
}

View File

@@ -1,7 +1,7 @@
import { defineStore } from 'pinia';
import type { providerType, UserBean } from './types';
import { getTerminal, getUserProfile, login, logout as userLogout, register } from '@/api/user/index';
import { clearToken, setCompanyId, setToken } from '@/utils/auth';
import { clearToken, getReferrerUserId, getRegisterStoreId, setCompanyId, setToken } from '@/utils/auth';
import type { LoginResult, RegisterParams, TerminalBean } from '@/api/user/types';
import { getCompanyInfo } from '@/api/company';
@@ -43,8 +43,8 @@ const useUserStore = defineStore('user', {
},
getters: {
// getUserInfo(state: UserState): UserState {
// return state.address;
// getUserInfo(state: UserBean): UserBean {
// return { state };
// }
},
@@ -112,7 +112,8 @@ const useUserStore = defineStore('user', {
const res = await login({
code: result.code,
userInfo: userInfo,
referrerUserId: '1727303781559697409'
// referrerUserId: '1727303781559697409'
referrerUserId: getReferrerUserId()
});
if(res.user == undefined || res.user.id === null) {
@@ -126,7 +127,8 @@ const useUserStore = defineStore('user', {
birthday: res.user.birthday,
companyId: res.user.companyId,
creatorId: res.user.creatorId,
gender: res.user.gender
gender: res.user.gender,
storeId: getRegisterStoreId()
};
setToken(res.token);
const registerResult = await this.userRegister(registerForm);

View File

@@ -1,5 +1,8 @@
const TokenKey = 'accessToken';
const CompanyIdKey = 'companyId';
const ReferrerUserIdKey = 'referrerUserId';
const RegisterStoreIdKey = 'storeId';
const TokenPrefix = 'Bearer ';
function isLogin() {
@@ -22,9 +25,37 @@ function setCompanyId(companyId: string) {
uni.setStorageSync(CompanyIdKey, companyId);
}
function getReferrerUserId() {
return uni.getStorageSync(ReferrerUserIdKey);
}
function setReferrerUserId(referrerUserId: string) {
uni.setStorageSync(ReferrerUserIdKey, referrerUserId);
}
function setRegisterStoreId(storeId: string) {
uni.setStorageSync(RegisterStoreIdKey, storeId);
}
function getRegisterStoreId() {
return uni.getStorageSync(RegisterStoreIdKey);
}
function clearToken() {
uni.removeStorageSync(TokenKey);
uni.removeStorageSync(CompanyIdKey);
}
export { TokenPrefix, isLogin, getToken, setToken, getCompanyId, setCompanyId, clearToken };
export {
TokenPrefix,
isLogin,
getToken,
setToken,
getCompanyId,
setCompanyId,
getReferrerUserId,
setReferrerUserId,
getRegisterStoreId,
setRegisterStoreId,
clearToken
};

View File

@@ -51,10 +51,17 @@ export function showToast(title: string, { icon, duration, complete }: ToastOpti
}
export function goPath(path: string) {
if(path.includes('home/index') || path.includes('mall/index') || path.includes('qrcode/index') || path.includes('mine/index')) {
uni.switchTab({
url: path
}).then(r => {
});
} else {
uni.navigateTo({
url: path
}).then(r => {
});
}
}
export function goLogin() {