From 8fbdec3738c1ed1963d47e0589809c2dce42d9a5 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Fri, 9 Jun 2023 20:43:37 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=95=86=E5=93=81=E8=AF=A6=E6=83=85?= =?UTF-8?q?=E3=80=91=201.=20=E6=8E=A5=E5=85=A5=E6=94=B6=E8=97=8F=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/product/favorite.js | 22 +++++++++ pages/goods_details/index.vue | 86 +++++++++++++++++++++-------------- 2 files changed, 74 insertions(+), 34 deletions(-) create mode 100644 api/product/favorite.js diff --git a/api/product/favorite.js b/api/product/favorite.js new file mode 100644 index 00000000..33f40e23 --- /dev/null +++ b/api/product/favorite.js @@ -0,0 +1,22 @@ +import request from "@/utils/request.js"; + +// 检查是否收藏过商品 +export function isFavoriteExists(spuId) { + return request.get('app-api/product/favorite/exits', { + spuId + }); +} + +// 添加商品收藏 +export function createFavorite(spuId) { + return request.post('app-api/product/favorite/create', { + spuId + }); +} + +// 取消商品收藏 +export function deleteFavorite(spuId) { + return request.delete('app-api/product/favorite/delete', { + spuId + }); +} diff --git a/pages/goods_details/index.vue b/pages/goods_details/index.vue index 601bb3b4..615502d4 100644 --- a/pages/goods_details/index.vue +++ b/pages/goods_details/index.vue @@ -306,12 +306,14 @@ import home from '@/components/home'; import parser from "@/components/jyf-parser/jyf-parser"; import * as ProductSpuApi from '@/api/product/spu.js'; + import * as ProductFavoriteApi from '@/api/product/favorite.js'; import * as TradeCartApi from '@/api/trade/cart.js'; import * as Util from '@/utils/util.js'; import * as ProductUtil from '@/utils/product.js'; // #ifdef MP import { base64src } from '@/utils/base64src.js' import { getQrcode } from '@/api/api.js'; + import {createFavorite, deleteFavorite} from "../../api/product/favorite"; // #endif const app = getApp(); export default { @@ -352,7 +354,7 @@ reply: [], // 评论列表 replyChance: 0, // TODO 芋艿:评论相关,待接入 - // ========== 收藏相关的变量 ① TODO ========== + // ========== 收藏相关的变量 ========== userCollect: false, // ========== 优惠劵相关的变量 ② TODO ========== @@ -418,7 +420,8 @@ if (newV === true) { that.getCouponList(); that.getCartCount(); - } + that.isFavoriteExists(); + } }, deep: true } @@ -525,7 +528,6 @@ let skus = res.data.skus; this.$set(this, 'productInfo', productInfo); this.$set(this, 'spu', spu); - this.$set(this, 'userCollect', res.data.userCollect); // TODO 芋艿:需要改造下,异步加载收藏状态 this.$set(this.attr, 'properties', ProductUtil.convertProductPropertyList(skus)); this.$set(this, 'skuMap', ProductUtil.convertProductSkuMap(skus)); // 设置分销相关变量 // TODO 芋艿:待接入 @@ -542,6 +544,7 @@ // 登录情况下,获得购物车、分享等信息 if (this.isLogin) { this.getCartCount(); + this.isFavoriteExists(); // #ifdef H5 this.make(); this.ShareInfo(); @@ -768,6 +771,52 @@ }); }, + // ========== 评价相关的方法 TODO ========== + + getProductReplyList: function() { + getReplyProduct(this.id).then(res => { + this.reply = res.data.productReply ? [res.data.productReply] : []; + }) + }, + getProductReplyCount: function() { + let that = this; + getReplyConfig(that.id).then(res => { + that.$set(that, 'replyChance', res.data.replyChance * 100); + that.$set(that, 'replyCount', res.data.sumCount); + }); + }, + + // ========== 收藏相关方法 ========== + /** + * 获得是否收藏 + */ + isFavoriteExists: function() { + ProductFavoriteApi.isFavoriteExists(this.id).then(res => { + this.userCollect = res.data; + }); + }, + /** + * 收藏 / 取消商品 + */ + setCollect: function() { + if (!this.isLogin) { + toLogin(); + return; + } + + // 情况一:取消收藏 + if (this.userCollect) { + ProductFavoriteApi.deleteFavorite(this.id).then(res => { + this.$set(this, 'userCollect', false); + }) + // 情况二:添加收藏 + } else { + ProductFavoriteApi.createFavorite(this.id).then(res => { + this.$set(this, 'userCollect', true); + }) + } + }, + // === TODO 芋艿:未处理 ==== kefuClick() { @@ -868,18 +917,6 @@ }) }); }, - getProductReplyList: function() { - getReplyProduct(this.id).then(res => { - this.reply = res.data.productReply ? [res.data.productReply] : []; - }) - }, - getProductReplyCount: function() { - let that = this; - getReplyConfig(that.id).then(res => { - that.$set(that, 'replyChance', res.data.replyChance * 100); - that.$set(that, 'replyCount', res.data.sumCount); - }); - }, /** * 获取优惠券 */ @@ -911,25 +948,6 @@ that.$set(that.coupon, 'list', that.coupon.list); that.$set(that.coupon, 'coupon', false); }, - /** - * 收藏商品 - */ - setCollect: function() { - let that = this; - if (this.isLogin === false) { - toLogin(); - } else { - if (this.userCollect) { - collectDel(this.productInfo.id).then(res => { - that.$set(that, 'userCollect', !that.userCollect); - }) - } else { - collectAdd(this.productInfo.id).then(res => { - that.$set(that, 'userCollect', !that.userCollect); - }) - } - } - }, /** * 打开优惠券插件 */