From 2c864ce7705d14f134632f3df18b9ab32ae2fe1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BF=A0=E6=9E=97?= <170083662@qq.com> Date: Thu, 14 Aug 2025 01:06:44 +0800 Subject: [PATCH] =?UTF-8?q?refactor(shop):=20=E6=9B=B4=E6=96=B0=20API?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E5=92=8C=E9=A1=B5=E9=9D=A2=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改了多个文件中的 API调用路径 - 优化了部分组件的显示逻辑 - 添加了优惠券相关功能 -调整了环境变量配置 --- config/env.ts | 4 +- src/api/layout/index.ts | 4 +- src/api/shop/shopUserCoupon/model/index.ts | 6 ++ src/components/CouponStats.tsx | 2 +- src/pages/cart/cart.tsx | 10 +- src/pages/index/BestSellers.tsx | 108 +++++++++++++-------- src/pages/index/Header.tsx | 4 +- src/pages/index/index.tsx | 4 +- src/pages/order/order.scss | 4 + src/pages/user/components/UserCard.tsx | 3 +- src/pages/user/components/UserCell.tsx | 55 +++++------ src/user/coupon/index.tsx | 23 ++--- 12 files changed, 138 insertions(+), 89 deletions(-) create mode 100644 src/pages/order/order.scss diff --git a/config/env.ts b/config/env.ts index 44ddb26..1a51159 100644 --- a/config/env.ts +++ b/config/env.ts @@ -2,13 +2,13 @@ export const ENV_CONFIG = { // 开发环境 development: { - API_BASE_URL: 'http://127.0.0.1:9200/api', + API_BASE_URL: 'https://cms-api.websoft.top/api', APP_NAME: '开发环境', DEBUG: 'true', }, // 生产环境 production: { - API_BASE_URL: 'https://cms-api.websoft.top/api', + API_BASE_URL: 'https://cms-api.s209.websoft.top/api', APP_NAME: '时里院子市集', DEBUG: 'false', }, diff --git a/src/api/layout/index.ts b/src/api/layout/index.ts index c172c91..56538be 100644 --- a/src/api/layout/index.ts +++ b/src/api/layout/index.ts @@ -8,9 +8,9 @@ import {SERVER_API_URL} from "@/utils/server"; /** * 获取网站信息 */ -export async function getSiteInfo() { +export async function getShopInfo() { const res = await request.get>( - '/cms/cms-website/getSiteInfo' + '/shop/getShopInfo' ); if (res.code === 0 && res.data) { return res.data; diff --git a/src/api/shop/shopUserCoupon/model/index.ts b/src/api/shop/shopUserCoupon/model/index.ts index b1ab95d..144d04a 100644 --- a/src/api/shop/shopUserCoupon/model/index.ts +++ b/src/api/shop/shopUserCoupon/model/index.ts @@ -32,6 +32,8 @@ export interface ShopUserCoupon { endTime?: string; // 使用状态(0未使用 1已使用 2已过期) status?: number; + // 是否过期, 0否, 1是 + isExpire?: number; // 使用时间 useTime?: string; // 使用订单ID @@ -57,5 +59,9 @@ export interface ShopUserCoupon { */ export interface ShopUserCouponParam extends PageParam { id?: number; + status?: number; + isExpire?: number; + sortBy?: string; + sortOrder?: string; keywords?: string; } diff --git a/src/components/CouponStats.tsx b/src/components/CouponStats.tsx index 11285fc..c30fe45 100644 --- a/src/components/CouponStats.tsx +++ b/src/components/CouponStats.tsx @@ -24,7 +24,7 @@ const CouponStats: React.FC = ({ } return ( - + 优惠券统计 diff --git a/src/pages/cart/cart.tsx b/src/pages/cart/cart.tsx index 07ba471..1b98b77 100644 --- a/src/pages/cart/cart.tsx +++ b/src/pages/cart/cart.tsx @@ -1,5 +1,5 @@ import {useEffect, useState} from "react"; -import Taro, {useShareAppMessage, useShareTimeline} from '@tarojs/taro'; +import Taro, {useShareAppMessage, useShareTimeline, useDidShow} from '@tarojs/taro'; import { NavBar, Checkbox, @@ -25,7 +25,8 @@ function Cart() { cartCount, updateQuantity, removeFromCart, - clearCart + clearCart, + loadCartFromStorage } = useCart(); // InputNumber 主题配置 @@ -56,6 +57,11 @@ function Cart() { }; }); + // 页面显示时刷新购物车数据 + useDidShow(() => { + loadCartFromStorage(); + }); + useEffect(() => { Taro.getSystemInfo({ success: (res) => { diff --git a/src/pages/index/BestSellers.tsx b/src/pages/index/BestSellers.tsx index 5ba552a..8e14c48 100644 --- a/src/pages/index/BestSellers.tsx +++ b/src/pages/index/BestSellers.tsx @@ -1,6 +1,7 @@ import {useEffect, useState} from "react"; import {Image} from '@nutui/nutui-react-taro' import {Share} from '@nutui/icons-react-taro' +import {View, Text} from '@tarojs/components'; import Taro, {useShareAppMessage, useShareTimeline} from "@tarojs/taro"; import {ShopGoods} from "@/api/shop/shopGoods/model"; import {pageShopGoods} from "@/api/shop/shopGoods"; @@ -17,6 +18,42 @@ const BestSellers = () => { }) } + // 处理分享点击 + const handleShare = (item: ShopGoods) => { + setGoods(item); + + // 显示分享选项菜单 + Taro.showActionSheet({ + itemList: ['分享给好友', '分享到朋友圈'], + success: (res) => { + if (res.tapIndex === 0) { + // 分享给好友 - 触发转发 + Taro.showShareMenu({ + withShareTicket: true, + success: () => { + // 提示用户点击右上角分享 + Taro.showToast({ + title: '请点击右上角分享给好友', + icon: 'none', + duration: 2000 + }); + } + }); + } else if (res.tapIndex === 1) { + // 分享到朋友圈 + Taro.showToast({ + title: '请点击右上角分享到朋友圈', + icon: 'none', + duration: 2000 + }); + } + }, + fail: (err) => { + console.log('显示分享菜单失败', err); + } + }); + } + useEffect(() => { reload() }, []) @@ -57,52 +94,47 @@ const BestSellers = () => { return ( <> -
-
+ + {list?.map((item, index) => { return ( -
+ Taro.navigateTo({url: '/shop/goodsDetail/index?id=' + item.goodsId})}/> -
-
-
{item.name}
-
- {item.comments} - 已售 {item.sales} -
-
-
- - {item.price} -
-
-
- - {/* Taro.navigateTo({url: '/shop/goodsDetail/index?id=' + item.goodsId})}/>*/} -
-
Taro.navigateTo({url: '/shop/goodsDetail/index?id=' + item.goodsId})}>购买 -
-
-
-
-
-
+ +
+
+ Taro.navigateTo({url: '/shop/goodsDetail/index?id=' + item.goodsId})}>购买 + + + + + + ) })} -
-
+
+
) } diff --git a/src/pages/index/Header.tsx b/src/pages/index/Header.tsx index 980c3e8..280fd52 100644 --- a/src/pages/index/Header.tsx +++ b/src/pages/index/Header.tsx @@ -3,7 +3,7 @@ import Taro from '@tarojs/taro'; import {Button, Space} from '@nutui/nutui-react-taro' import {TriangleDown} from '@nutui/icons-react-taro' import {Popup, Avatar, NavBar} from '@nutui/nutui-react-taro' -import {getSiteInfo, getUserInfo, getWxOpenId} from "@/api/layout"; +import {getShopInfo, getUserInfo, getWxOpenId} from "@/api/layout"; import {TenantId} from "@/config/app"; import {getOrganization} from "@/api/system/organization"; import {myUserVerify} from "@/api/system/userVerify"; @@ -26,7 +26,7 @@ const Header = (props: any) => { }, }) // 获取站点信息 - getSiteInfo().then((data) => { + getShopInfo().then((data) => { setConfig(data); console.log(userInfo) }) diff --git a/src/pages/index/index.tsx b/src/pages/index/index.tsx index bb5d525..e8fc754 100644 --- a/src/pages/index/index.tsx +++ b/src/pages/index/index.tsx @@ -3,7 +3,7 @@ import BestSellers from './BestSellers'; import Taro from '@tarojs/taro'; import {useShareAppMessage, useShareTimeline} from "@tarojs/taro" import {useEffect, useState} from "react"; -import {getSiteInfo} from "@/api/layout"; +import {getShopInfo} from "@/api/layout"; import {Sticky} from '@nutui/nutui-react-taro' import Menu from "./Menu"; import Banner from "./Banner"; @@ -84,7 +84,7 @@ function Home() { useEffect(() => { // 获取站点信息 - getSiteInfo().then(() => { + getShopInfo().then(() => { }) // Taro.getSetting:获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限。 diff --git a/src/pages/order/order.scss b/src/pages/order/order.scss new file mode 100644 index 0000000..bec4fb5 --- /dev/null +++ b/src/pages/order/order.scss @@ -0,0 +1,4 @@ +// 订单页面样式 +.order-page { + // 订单相关样式 +} diff --git a/src/pages/user/components/UserCard.tsx b/src/pages/user/components/UserCard.tsx index d4ff6b9..0148c30 100644 --- a/src/pages/user/components/UserCard.tsx +++ b/src/pages/user/components/UserCard.tsx @@ -237,8 +237,7 @@ function UserCard() { 礼品卡 {giftCount} -
navTo('/user/points/points', true)}> +
积分 {pointsCount}
diff --git a/src/pages/user/components/UserCell.tsx b/src/pages/user/components/UserCell.tsx index 2a363d1..7890234 100644 --- a/src/pages/user/components/UserCell.tsx +++ b/src/pages/user/components/UserCell.tsx @@ -1,6 +1,7 @@ import {Cell} from '@nutui/nutui-react-taro' import navTo from "@/utils/common"; import Taro from '@tarojs/taro' +import {View,Text} from '@tarojs/components' import {ArrowRight, ShieldCheck, LogisticsError, Location, Reward, Tips, Ask} from '@nutui/icons-react-taro' const UserCell = () => { @@ -25,25 +26,25 @@ const UserCell = () => { return ( <> -
+ navTo('/dealer/index', true)}> + navTo('/dealer/index', true)}> - 开通会员 - 享优惠 -
+ 开通会员 + 享优惠 + } extra={} /> - 我的服务 -
+ + 我的服务 + }> { display: 'none' }} title={ -
+ - 我的钱包 -
+ 我的钱包 +
} align="center" extra={} @@ -65,10 +66,10 @@ const UserCell = () => { + - 收货地址 - + 收货地址 + } align="center" extra={} @@ -79,10 +80,10 @@ const UserCell = () => { + - 实名认证 - + 实名认证 + } align="center" extra={} @@ -93,10 +94,10 @@ const UserCell = () => { + - 常见问题 - + 常见问题 + } align="center" extra={} @@ -107,10 +108,10 @@ const UserCell = () => { + - 关于我们 - + 关于我们 + } align="center" extra={} @@ -120,9 +121,9 @@ const UserCell = () => { /> - 账号管理 - + + 账号管理 + }> { onClick={onLogout} /> - + ) } diff --git a/src/user/coupon/index.tsx b/src/user/coupon/index.tsx index ec0ed8b..e34bb52 100644 --- a/src/user/coupon/index.tsx +++ b/src/user/coupon/index.tsx @@ -3,8 +3,8 @@ import Taro, {useDidShow} from '@tarojs/taro' import {Button, Empty, ConfigProvider, SearchBar, InfiniteLoading, Loading, PullToRefresh, Tabs, TabPane} from '@nutui/nutui-react-taro' import {Plus, Filter} from '@nutui/icons-react-taro' import {View} from '@tarojs/components' -import {ShopCoupon} from "@/api/shop/shopCoupon/model"; -import {pageShopCoupon} from "@/api/shop/shopCoupon"; +import {ShopUserCoupon} from "@/api/shop/shopUserCoupon/model"; +import {pageShopUserCoupon} from "@/api/shop/shopUserCoupon"; import CouponList from "@/components/CouponList"; import CouponStats from "@/components/CouponStats"; import CouponGuide from "@/components/CouponGuide"; @@ -14,7 +14,7 @@ import {CouponCardProps} from "@/components/CouponCard"; import dayjs from "dayjs"; const CouponManage = () => { - const [list, setList] = useState([]) + const [list, setList] = useState([]) const [loading, setLoading] = useState(false) const [hasMore, setHasMore] = useState(true) const [searchValue, setSearchValue] = useState('') @@ -63,7 +63,7 @@ const CouponManage = () => { try { const currentPage = isRefresh ? 1 : page const statusFilter = getStatusFilter() - const res = await pageShopCoupon({ + const res = await pageShopUserCoupon({ page: currentPage, limit: 10, keywords: searchValue, @@ -127,7 +127,7 @@ const CouponManage = () => { } // 转换优惠券数据为CouponCard组件所需格式 - const transformCouponData = (coupon: ShopCoupon): CouponCardProps => { + const transformCouponData = (coupon: ShopUserCoupon): CouponCardProps => { // 判断优惠券状态 let status: 0 | 1 | 2 = 0 // 默认未使用 if (coupon.isExpire === 1) { @@ -176,7 +176,7 @@ const CouponManage = () => { } // 使用优惠券 - const handleUseCoupon = (coupon: ShopCoupon) => { + const handleUseCoupon = (coupon: ShopUserCoupon) => { Taro.showModal({ title: '使用优惠券', content: `确定要使用"${coupon.name}"吗?`, @@ -201,7 +201,7 @@ const CouponManage = () => { } // 显示优惠券详情 - const showCouponDetail = (coupon: ShopCoupon) => { + const showCouponDetail = (coupon: ShopUserCoupon) => { // 跳转到优惠券详情页 Taro.navigateTo({ url: `/user/coupon/detail?id=${coupon.id}` @@ -213,9 +213,9 @@ const CouponManage = () => { try { // 并行获取各状态的优惠券数量 const [availableRes, usedRes, expiredRes] = await Promise.all([ - pageShopCoupon({ page: 1, limit: 1, status: 0, isExpire: 0 }), - pageShopCoupon({ page: 1, limit: 1, status: 1 }), - pageShopCoupon({ page: 1, limit: 1, isExpire: 1 }) + pageShopUserCoupon({ page: 1, limit: 1, status: 0, isExpire: 0 }), + pageShopUserCoupon({ page: 1, limit: 1, status: 1 }), + pageShopUserCoupon({ page: 1, limit: 1, isExpire: 1 }) ]) setStats({ @@ -248,7 +248,7 @@ const CouponManage = () => { const checkExpiringSoonCoupons = async () => { try { // 获取即将过期的优惠券(3天内过期) - const res = await pageShopCoupon({ + const res = await pageShopUserCoupon({ page: 1, limit: 50, status: 0, // 未使用 @@ -277,6 +277,7 @@ const CouponManage = () => { .sort((a, b) => a.daysLeft - b.daysLeft) if (expiringSoon.length > 0) { + // @ts-ignore setExpiringSoonCoupons(expiringSoon) // 延迟显示提醒,避免与页面加载冲突 setTimeout(() => {