field('id,product_name as title,cover_image as image,price,stock,sales') ->select()->toArray(); return $this->renderSuccess('', $bannerList); } public function getProductList() { $page = intval($this->request->param('page', 1)); $limit = intval($this->request->param('limit', 10)); $title = $this->request->param('title', ''); $productData = FFProducts::where('is_hot', '=', 0); if ($title) { $productData->where('product_name', 'like', '%' . $title . '%'); } $list = $productData-> field('id,product_name as title,cover_image as image,price,stock,sales') ->order(['is_new' => 'desc', 'id' => 'desc']) ->page($page, $limit) ->select()->toArray(); return $this->renderSuccess('', $list); } public function getProductDetail() { $id = $this->request->param('id', 0); $productDetail = FFProducts::where('id', '=', $id) ->field('id,product_name as title,cover_image as image,price,stock,sales,product_desc,detail_image1,detail_image2,detail_image3,detail_image4,detail_html,is_hot,is_new,is_recommend')->find(); if (!$productDetail) { return $this->renderError('商品不存在'); } $productDetail['detail_image'] = [ $productDetail['detail_image1'], $productDetail['detail_image2'], $productDetail['detail_image3'], $productDetail['detail_image4'] ]; $productDetail['detail_image'] = array_filter($productDetail['detail_image'], function ($value) { return !empty($value); }); unset( $productDetail['detail_image1'], $productDetail['detail_image2'], $productDetail['detail_image3'], $productDetail['detail_image4'] ); if ($this->getUserId() > 0) { $productDetail['is_favorite'] = FFFavorites::isFavorite($this->getUserId(), $id); } return $this->renderSuccess('', $productDetail); } public function PayByOrder() { $user_id = $this->getUserId(); if (!$user_id) { return $this->renderError('请先登录'); } $productId = $this->request->param('product_id', 0); $productDetail = FFProducts::where('id', '=', $productId)->find(); if (!$productDetail) { return $this->renderError('商品不存在'); } if ($productDetail['sales'] <= 0) { return $this->renderError('库存不足'); } // 查询默认地址 $address = UserAddress::where('user_id', $user_id) ->where('is_default', 1) ->where('is_deleted', 0) ->field('id,receiver_name,receiver_phone,detailed_address,is_default') ->find(); if (!$address) { // 如果没有默认地址,返回最新添加的一条 $address = UserAddress::where('user_id', $user_id) ->where('is_deleted', 0) ->field('id,receiver_name,receiver_phone,detailed_address,is_default') ->order('id', 'desc') ->find(); } $whe = 'user_id=' . $user_id . ' and status=0 and man_price<=' . $productDetail['price']; $coupon = CouponReceive::where($whe) ->field('id,title,man_price,price,end_time,status') ->select()->toArray(); $order = [ 'product_id' => $productId, 'title' => $productDetail['product_name'], 'image' => $productDetail['cover_image'], 'price' => $productDetail['price'], 'coupon' => $coupon, 'default_address' => $address, ]; return $this->renderSuccess('', $order); } /** * 获取用户收藏商品列表 * @return \think\Response */ public function getFavoriteList() { $user_id = $this->getUserId(); if (!$user_id) { return $this->renderError('请先登录'); } $page = intval($this->request->param('page', 1)); $limit = intval($this->request->param('limit', 10)); // 使用模型静态方法获取收藏列表 $favoriteList = FFFavorites::getUserFavoriteList($user_id, $page, $limit); // 格式化数据 $result = []; foreach ($favoriteList as $item) { if (isset($item['product']) && $item['product']) { $result[] = [ 'id' => $item['product']['id'], 'title' => $item['product']['product_name'], 'image' => $item['product']['cover_image'], 'price' => $item['product']['price'], 'stock' => $item['product']['stock'], 'sales' => $item['product']['sales'], 'favorite_time' => $item['create_time'] ]; } } return $this->renderSuccess('', $result); } /** * 添加商品收藏 * @return \think\Response */ public function addFavorite() { $user_id = $this->getUserId(); if (!$user_id) { return $this->renderError('请先登录'); } $product_id = intval($this->request->param('product_id', 0)); if (!$product_id) { return $this->renderError('商品ID不能为空'); } // 检查商品是否存在 $product = FFProducts::where('id', $product_id)->find(); if (!$product) { return $this->renderError('商品不存在'); } // 使用模型静态方法添加收藏 $result = FFFavorites::addFavorite($user_id, $product_id); return $this->renderSuccess('收藏成功'); } /** * 取消商品收藏 * @return \think\Response */ public function cancelFavorite() { $user_id = $this->getUserId(); if (!$user_id) { return $this->renderError('请先登录'); } $product_id = intval($this->request->param('product_id', 0)); if (!$product_id) { return $this->renderError('商品ID不能为空'); } // 使用模型静态方法取消收藏 $result = FFFavorites::cancelFavorite($user_id, $product_id); if ($result) { return $this->renderSuccess('取消收藏成功'); } else { return $this->renderError('取消收藏失败'); } } /** * 批量取消收藏 * @return \think\Response */ public function batchCancelFavorite() { $user_id = $this->getUserId(); if (!$user_id) { return $this->renderError('请先登录'); } $product_ids = $this->request->param('product_ids'); if (empty($product_ids)) { return $this->renderError('商品ID不能为空'); } // 转换为数组 if (!is_array($product_ids)) { $product_ids = explode(',', $product_ids); } // 批量取消收藏 $result = FFFavorites::batchCancelFavorite($user_id, $product_ids); if ($result) { return $this->renderSuccess('批量取消收藏成功'); } else { return $this->renderError('批量取消收藏失败'); } } /** * 检查商品是否已收藏 * @return \think\Response */ public function checkIsFavorite() { $user_id = $this->getUserId(); if (!$user_id) { return $this->renderError('请先登录'); } $product_id = intval($this->request->param('product_id', 0)); if (!$product_id) { return $this->renderError('商品ID不能为空'); } // 检查是否已收藏 $isFavorite = FFFavorites::isFavorite($user_id, $product_id); return $this->renderSuccess('', ['is_favorite' => $isFavorite ? true : false]); } }