where(['id' => $productId])->find(); if (!$data) { $this->error(__('Goods not exist')); } if ($data['switch'] == productModel::SWITCH_OFF) { $this->error(__('Goods are off the shelves')); } // 真实浏览量加一 $data->real_look++; $data->look++; $data->save(); //服务 /*$server = explode(',', $data->server); $configServer = json_decode(Config::getByName('server')['value'],true); $serverValue = []; foreach ($server as $k => $v) { if (isset($configServer[$v])) { $serverValue[] = $configServer[$v]; } } $data->server = count($serverValue) ? implode(' · ', $serverValue) : '';*/ // 默认没有收藏 // $data->favorite = false; // 评价 /*$data['evaluate_data'] = (new Evaluate)->where(['product_id' => $productId]) ->field('COUNT(*) as count, IFNULL(CEIL(AVG(rate)/5*100),0) as avg') ->find();*/ //优惠券 /*$data->coupon = (new Coupon)->where('endtime', '>', time()) ->where(['switch' => Coupon::SWITCH_ON])->order('weigh DESC')->select();*/ $data->coupon = []; if($this->auth->isLogin()){ $data->coupon = Db::name('unishop_coupon_user')->alias('cu') ->field(['cu.id as cu_id','c.id','c.title','c.least','c.value','c.starttime','c.endtime']) ->join('unishop_coupon c','cu.coupon_id = c.id','LEFT') ->where('cu.user_id',$this->auth->id) ->where('cu.status',0) ->where('c.deletetime',NULL) ->where('c.switch',Coupon::SWITCH_ON) ->where('c.starttime','<',time()) ->where('c.endtime','>',time()) ->select(); } // 是否已收藏 /*if ($this->auth->id) { $data->favorite = (new Favorite)->where(['user_id' => $this->auth->id, 'product_id' => $productId])->count(); }*/ // 购物车数量 // $data->cart_num = (new \addons\unishop\model\Cart)->where(['user_id' => $this->auth->id])->count(); // 评价信息 /*$evaluate = (new Evaluate)->alias('e') ->join('user u', 'e.user_id = u.id') ->where(['e.product_id' => $productId, 'toptime' => ['>', Evaluate::TOP_OFF]]) ->field('u.username,u.avatar,e.*') ->order(['toptime' => 'desc', 'createtime' => 'desc'])->select(); if ($evaluate) { $data->evaluate_list = collection($evaluate)->append(['createtime_text'])->toArray(); }*/ $data = $data->append(['images_text', 'spec_list', 'spec_table_list'])->toArray(); } catch (Exception $e) { $this->error($e->getMessage()); } $this->success('', $data); } /** * @ApiTitle (产品列表) * @ApiSummary (产品列表) * @ApiMethod (GET) * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie") * @ApiParams (name="sid", type="integer", required=false, description="二级分类id") * @ApiParams (name="fid", type="integer", required=true, description="一级分类id") * @ApiParams (name="page", type="integer", required=true, description="页面") * @ApiParams (name="pagesize", type="integer", required=true, description="每页数量") * @ApiParams (name="by", type="string", required=true, description="排序字段") * @ApiParams (name="desc", type="string", required=true, description="排序desc,asc") * @ApiReturn ({"code":1,"msg":"","data":[]}) * @ApiReturnParams (name="title", type="string", description="商品名称") * @ApiReturnParams (name="image", type="string", description="商品图片") * @ApiReturnParams (name="sales", type="integer", description="销量") * @ApiReturnParams (name="sales_price", type="string", description="销售价钱") * @ApiReturnParams (name="product_id", type="string", description="商品id") */ public function lists() { $page = input('page', 1); $pagesize = input('pagesize', 20); $by = input('by', 'weigh'); $desc = input('desc', 'desc'); // $sid = input('sid'); // 二级分类Id $fid = input('fid'); // 一级分类Id $productModel = new productModel(); /*if ($fid && !$sid) { $categoryModel = new \addons\unishop\model\Category(); $sArr = $categoryModel->where('pid', $fid)->field('id')->select(); $sArr = array_column($sArr, 'id'); array_push($sArr, $fid); $productModel->where('category_id', 'in', $sArr); } else { $sid && $productModel->where(['category_id' => $sid]); }*/ $fid && $productModel->where(['category_id' => $fid]); $result = $productModel ->where(['switch' => productModel::SWITCH_ON]) ->page($page, $pagesize) ->order($by, $desc) ->field('id,title,info,image,sales_price,use_spec') ->select(); if ($result) { $result = collection($result)->toArray(); } else { $result = []; } $this->success('', $result); } /** * @ApiTitle (收藏/取消) * @ApiSummary (收藏/取消) * @ApiMethod (GET) * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie") * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e") * @ApiParams (name="id", type="string", required=true, description="商品id") * @ApiReturn ({"code":1,"msg":"","data":true}) */ public function favorite() { $id = input('id', 0); $id = \addons\unishop\extend\Hashids::decodeHex($id); $user_id = $this->auth->id; $favoriteModel = Favorite::get(function ($query) use ($id, $user_id) { $query->where(['user_id' => $user_id, 'product_id' => $id]); }); if ($favoriteModel) { Favorite::destroy($favoriteModel->id); } else { $product = productModel::withTrashed()->where(['id' => $id, 'switch' => productModel::SWITCH_ON])->find(); if (!$product) { $this->error('参数错误'); } $favoriteModel = new Favorite(); $favoriteModel->user_id = $user_id; $favoriteModel->product_id = $id; $product = $product->getData(); $data['image'] = $product['image']; $data['market_price'] = $product['market_price']; $data['product_id'] = Hashids::encodeHex($product['id']); $data['sales_price'] = $product['sales_price']; $data['title'] = $product['title']; $favoriteModel->snapshot = json_encode($data); $favoriteModel->save(); } $this->success('', true); } /** * @ApiTitle (收藏列表) * @ApiSummary (收藏列表) * @ApiMethod (GET) * @ApiHeaders (name=token, type=string, required=true, description="用户登录的Token", sample="a2e3cc70-d2d1-41e6-9c14-f1d774ee5e1e") * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie") * @ApiParams (name="page", type="integer", required=true, description="页面") * @ApiParams (name="pagesize", type="integer", required=true, description="每页数量") * @ApiReturn ({"code":1,"msg":"","data":[]}) * * @ApiReturnParams (name="id", type="integer", description="收藏id") * @ApiReturnParams (name="user_id", type="integer", description="用户id") * @ApiReturnParams (name="product.image", type="string", description="商品图片") * @ApiReturnParams (name="product.title", type="string", description="商品名称") * @ApiReturnParams (name="product.sales_price", type="string", description="商品销售价钱") * @ApiReturnParams (name="product.market_price", type="string", description="商品市场价钱") * @ApiReturnParams (name="product.product_id", type="string", description="商品id") * @ApiReturnParams (name="status", type="integer", description="是否还有效") * */ public function favoriteList() { $page = input('page', 1); $pageSize = input('pagesize', 20); $list = (new Favorite)->where(['user_id' => $this->auth->id])->with(['product'])->page($page, $pageSize)->select(); $list = collection($list)->toArray(); foreach ($list as &$item) { if (!empty($item['product'])) { $item['status'] = 1; } else { $item['status'] = 0; $item['product'] = json_decode($item['snapshot'],true); $image = $item['product']['image']; $item['product']['image'] = Config::getImagesFullUrl($image); } unset($item['snapshot']); } $this->success('', $list); } /** * @ApiTitle (商品评论) * @ApiSummary (商品评论) * @ApiMethod (GET) * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie") * @ApiParams (name="product_id", type="string", required=true, description="商品id") * @ApiParams (name="page", type="integer", required=true, description="页面") * @ApiParams (name="pagesize", type="integer", required=true, description="每页数量") * @ApiReturn ({"code":1,"msg":"","data":[]}) * * @ApiReturnParams (name="id", type="integer", description="评论id") * @ApiReturnParams (name="username", type="string", description="评论人名称") * @ApiReturnParams (name="avatar", type="string", description="评论人头像") * @ApiReturnParams (name="product_id", type="string", description="商品id") * @ApiReturnParams (name="comment", type="string", description="评论内容") * @ApiReturnParams (name="rate", type="integer", description="星星") * @ApiReturnParams (name="order_id", type="string", description="订单id") * @ApiReturnParams (name="spec", type="string", description="评论的规格") * @ApiReturnParams (name="anonymous", type="integer", description="是否匿名") * @ApiReturnParams (name="toptime", type="integer", description="是否置顶") * @ApiReturnParams (name="createtime_text", type="string", description="评论时间") * */ public function evaluate() { $page = input('page', 1); $pageSize = input('pagesize', 20); $productId = input('product_id'); $productId = \addons\unishop\extend\Hashids::decodeHex($productId); // 评价信息 $evaluate = (new Evaluate)->alias('e') ->join('user u', 'e.user_id = u.id') ->where(['e.product_id' => $productId]) ->field('u.username,u.avatar,e.*') ->order(['toptime' => 'desc', 'createtime' => 'desc']) ->page($page, $pageSize) ->select(); if ($evaluate) { $evaluate = collection($evaluate)->append(['createtime_text'])->toArray(); } $this->success('', $evaluate); } }