123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319 |
- <?php
- namespace addons\unishop\controller;
- use addons\unishop\extend\Hashids;
- use addons\unishop\model\Config;
- use addons\unishop\model\Evaluate;
- use addons\unishop\model\Favorite;
- use addons\unishop\model\Product as productModel;
- use addons\unishop\model\Coupon;
- use think\Exception;
- use think\Db;
- /**
- * 商品
- */
- class Product extends Base
- {
- protected $noNeedLogin = ['detail', 'lists'];
- protected $frequently = ['detail', 'lists'];
- /**
- * @ApiTitle (产品详情)
- * @ApiSummary (产品详情)
- * @ApiMethod (GET)
- * @ApiHeaders (name=cookie, type=string, required=false, description="用户会话的cookie")
- * @ApiParams (name="id", type="string", required=true, description="商品id")
- * @ApiReturn ({"code":1,"msg":"","data":{}})
- *
- * @ApiReturnParams (name="category_id", type="integer", description="分类id")
- * @ApiReturnParams (name="title", type="string", description="商品名称")
- * @ApiReturnParams (name="image", type="string", description="商品图片")
- * @ApiReturnParams (name="images_text", type="array", description="商品图片组")
- * @ApiReturnParams (name="desc", type="string", description="商品详情")
- * @ApiReturnParams (name="sales", type="integer", description="销量")
- * @ApiReturnParams (name="sales_price", type="string", description="销售价钱")
- * @ApiReturnParams (name="market_price", type="string", description="市场价钱")
- * @ApiReturnParams (name="product_id", type="string", description="商品id")
- * @ApiReturnParams (name="stock", type="integer", description="库存")
- * @ApiReturnParams (name="look", type="integer", description="观看量")
- * @ApiReturnParams (name="use_spec", type="integer", description="是否使用规格")
- * @ApiReturnParams (name="server", type="string", description="支持的服务")
- * @ApiReturnParams (name="favorite", type="integer", description="是否已收藏")
- * @ApiReturnParams (name="evaluate_data", type="json", description="{count:'评论数据',avg:'好评平均值'}")
- * @ApiReturnParams (name="coupon", type="array", description="可用优惠券")
- * @ApiReturnParams (name="cart_num", type="integer", description="购物车数量")
- * @ApiReturnParams (name="spec_list", type="array", description="规格键值数据")
- * @ApiReturnParams (name="spec_table_list", type="array", description="规格值数据")
- *
- */
- public function detail()
- {
- $productId = input('id');
- $productId = \addons\unishop\extend\Hashids::decodeHex($productId);
- try {
- $productModel = new productModel();
- $data = $productModel->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);
- }
- }
|