商城列表需要优化

This commit is contained in:
bobobobo
2025-12-24 22:21:24 +08:00
parent b67f9611c7
commit 334c0800fa
8 changed files with 202 additions and 64 deletions

18
api/mall.js Normal file
View File

@@ -0,0 +1,18 @@
import http from '@/utils/request'
/** 分类 */
export const getCategory = () => {
return http({
url: '/api/service/productCategory/list',
method: 'get'
})
}
/** 商品列表 */
export const getProductList = data => {
return http({
url: '/api/service/product/list',
method: 'get',
data
})
}

View File

@@ -0,0 +1,16 @@
import { storeToRefs } from 'pinia'
import { useUserStore } from '@/stores/user'
/**
* 统一提供响应式的用户信息和相关操作
*/
export const useAuthUser = () => {
const userStore = useUserStore()
// 响应式状态state & getters
const { userInfo } = storeToRefs(userStore)
return {
userInfo
}
}

View File

@@ -1,39 +1,65 @@
<script setup>
import { reactive } from 'vue'
import { reactive, ref } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { getCategory } from '@/api/mall'
// 顶部分类选项
const topNavOptions = [
{ name: '全部', value: '0' },
{ name: '休闲零食', value: '1' },
{ name: '中外名酒', value: '2' },
{ name: '家用洗漱', value: '3' },
{ name: '家电家具', value: '4' },
{ name: '电子产品', value: '5' },
{ name: '户外用品', value: '6' }
]
const topNavOptions = ref([])
const formData = reactive({
name: '',
type: '0'
})
const categoryList = async () => {
const res = await getCategory()
topNavOptions.value = res.data
console.log(res.data, '===22==')
}
const onTop = value => {
formData.type = value
}
onLoad(() => {
categoryList()
})
</script>
<template>
<view class="mall-list">
<view class="top-box">
<cb-search v-model="formData.name"></cb-search>
<view class="top-options">
<view
v-for="item in topNavOptions"
:key="item.value"
:class="{ active: item.value === formData.type }"
:class="{ active: item.id === formData.type }"
class="text"
@click="onTop(item.value)"
@click="onTop(item.id)"
>
{{ item.name }}
{{ item.categoryName }}
</view>
</view>
</view>
<!-- 商品卡片 -->
<view class="card-list">
<view v-for="item in 3" :key="item" class="card-item">
<image
src="https://wx1.sinaimg.cn/mw690/92eeb099gy1i29hl0ne80j21jk2bcash.jpg"
mode="scaleToFill"
class="imghead"
></image>
<text class="title">名称</text>
<view class="price">
<view class="num-box">
<text class="num"></text>
<text class="num">0.00</text>
</view>
<text class="buy">好评率99%</text>
</view>
<!-- 拼单数量 -->
<text class="bottom-name">拼单数量:12505</text>
</view>
</view>
</view>
@@ -41,10 +67,12 @@
<style lang="scss" scoped>
.mall-list {
.top-box {
padding: 24rpx;
.top-options {
overflow: hidden;
margin-top: 32rpx;
margin-bottom: 8rpx;
display: flex;
flex-direction: row;
white-space: nowrap; /* 重要:防止换行 */
@@ -75,4 +103,72 @@
}
}
}
.card-list {
background: #f9f9f9;
padding: 32rpx 24rpx;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
.card-item {
border-radius: 14rpx;
overflow: hidden;
margin-bottom: 18rpx;
display: flex;
flex-direction: column;
width: 344rpx;
background: #ffffff;
font-family: PingFang SC, PingFang SC;
font-style: normal;
text-transform: none;
.imghead {
width: 100%;
height: 288rpx;
}
.title {
font-weight: bold;
font-size: 28rpx;
color: #333333;
padding: 16rpx;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.price {
padding: 0 16rpx;
display: flex;
justify-content: space-between;
align-items: baseline;
.num-box {
display: flex;
align-items: baseline;
.num {
font-weight: 500;
font-size: 24rpx;
color: #eb1c26;
&:last-child {
margin-left: 2rpx;
font-weight: bold;
font-size: 32rpx;
color: #eb1c26;
}
}
}
.buy {
font-weight: 500;
font-size: 24rpx;
color: #00d993;
}
}
.bottom-name {
padding: 16rpx 16rpx 26rpx 16rpx;
font-weight: 500;
font-size: 24rpx;
color: #999999;
}
}
}
}
</style>

View File

@@ -1,5 +1,5 @@
<script setup>
import { useUserStore } from '@/stores/user'
import { useAuthUser } from '@/composables/useAuthUser'
const bottomList = [
{ name: '我的钱包', icon: 'wallet' },
@@ -10,10 +10,7 @@
{ name: '在线客服', icon: 'customer' },
{ name: '系统设置', icon: 'system' }
]
const { userInfo } = useUserStore()
console.log(userInfo)
const { userInfo } = useAuthUser()
</script>
<template>
@@ -26,8 +23,8 @@
class="avatar"
></image>
<view class="nickname">
<text class="name">{{ userInfo.userName }}</text>
<text class="name">ID:{{ userInfo.userId }}</text>
<text class="name">{{ userInfo?.userName || '' }}</text>
<text class="name">ID:{{ userInfo?.userId || '' }}</text>
</view>
</view>
<image

View File

@@ -1,8 +1,9 @@
<script setup>
import { useUserStore } from '@/stores/user'
const { userInfo } = useUserStore()
import { useAuthUser } from '@/composables/useAuthUser'
console.log(userInfo.userId, '====userInfo===')
const { userInfo } = useAuthUser()
console.log(userInfo.value, '====use22rInfo===')
</script>
<template>

View File

@@ -12,14 +12,17 @@ import { ref } from 'vue'
export const useUserStore = defineStore('user', () => {
const { clearToken } = useTokenStore()
const userInfo = ref(
getUserInfoData() ? JSON?.parse(getUserInfoData()) : {}
)
/** 用户信息对象 */
const userInfo = ref(JSON.parse(getUserInfoData()) || null)
/**
* 获取用户信息(可从缓存或接口)
*/
const fetchUserInfo = async () => {
// 示例:先尝试从本地缓存读取
// 尝试从本地缓存读取
const cachedToken = getToken()
const cachedUserInfo = getUserInfoData()
@@ -28,15 +31,14 @@ export const useUserStore = defineStore('user', () => {
return
}
const res = await getUserData()
await setUserInfo(res.data)
setUserInfo(res.data)
return
}
/**
* 设置用户信息
*/
const setUserInfo = async data => {
console.log('存储数据到userInfo==', data)
const setUserInfo = data => {
userInfo.value = data
// 同步到本地存储
setUserInfoData(data)
@@ -51,6 +53,12 @@ export const useUserStore = defineStore('user', () => {
removeUserInfoData()
}
/** 刷新用户信息(如用户信息被修改) */
const refreshUserInfo = async () => {
const res = await getUserData()
setUserInfo(res.data)
}
/**
* 更新部分用户信息(例如昵称、头像)
*/
@@ -61,7 +69,8 @@ export const useUserStore = defineStore('user', () => {
}
return {
userInfo: userInfo.value,
userInfo,
refreshUserInfo,
fetchUserInfo,
setUserInfo,
clearUserInfo,

View File

@@ -14,6 +14,7 @@ const request = options => {
method: 'GET',
data: {},
header: {
'deviceId': uni.getDeviceInfo().deviceId,
'Content-Type': 'application/json' // 默认请求内容类型
}
}