| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- namespace app\api\controller;
- use think\Db;
- use think\Loader ;
- use app\common\controller\Api;
- use app\common\model\ProductLists;
- use app\common\model\ProductTransfer;
- use app\common\model\ProductsModel;
- use app\common\model\ProductPopular;
- use app\api\logic\OrderLogic;
- class Product extends Api
- {
- protected array $noNeedLogin = ['getPopularList'];
- protected string $lan = '';
- public function _initialize()
- {
- parent::_initialize();
- $this->lan = $this->request->getLan();
- }
- /**
- * 热销列表
- */
- public function getPopularList(ProductsModel $productsModel, ProductPopular $productPopular, ProductLists $productLists)
- {
- $item = $productsModel::getProductTypeAll($this->lan);
- $resp = array();
- foreach ($item as $kk =>$val) {
- $list= $productLists->where('type_id', $kk)->field('id,thum as img_url,'.$this->lan.'_name as name')->select();
- $i = 0;
- $proArr = array();
- foreach ($list as &$item) {
- $pro =$productPopular
- ->field('id,product_id,price,cost_price,stock,(num+init_num) as num,start_time,end_time,status')
- ->where('product_id', $item['id'])
- ->where('status', '<', $productPopular::Stop)
- ->order('start_time')
- ->find();
- if(!empty($pro)){
- $proArr[$i] = $item;
- $proArr[$i]['pro'] = $pro;
- $i+=1;
- }
- }
- $resp[] = ['type_id'=>$kk,'title'=>$val, 'product'=>$proArr];
- }
- $this->success('', $resp);
- }
- /**
- * 寄售转让列表
- * sort: 0 默认排序 1价格从高到低 2价格从低到高
- * type_id 分类
- * key_val 商品名称搜索
- */
- public function getTransferList(ProductTransfer $productTransfer, OrderLogic $orderLogic)
- {
- $sort = $this->request->post('sort/d', '');
- $type_id = $this->request->post('type_id/s', '');
- $key_val = $this->request->post('key_val/s', '');
- $order = 'b.weigh desc';
- if($sort == 1) $order = 'price desc, a.product_id DESC';
- if($sort == 2) $order = 'price ASC, a.product_id ASC';
- $map['a.status'] = $productTransfer::Normal;
- if(!empty($type_id)) $map['b.type_id'] = $type_id;
- if(!empty($key_val)) $map['b.'.$this->lan.'_name'] = ['like', '%'.$key_val.'%'];
-
- $list = $productTransfer
- ->alias('a')
- ->join("product_list b", "a.product_id = b.id", "left")
- ->group('a.product_id')
- ->field('a.id,'.'b.'.$this->lan.'_name as name,b.thum as img_url,min(a.price) as price,a.product_id,b.type_id,b.issue_num as issue')
- ->where($map)
- ->order($order)
- ->paginate($this->pageSize);
- $list = $this->getProductOrderList($list, $this->auth->id, true, $orderLogic);
- $this->success('', $list);
- }
-
- /**
- * 寄售转让收藏列表
- * sort: 0 默认排序 1价格从高到低 2价格从低到高
- * type_id 分类
- * key_val 商品名称搜索
- */
- public function getTransferCollectList(ProductTransfer $productTransfer, OrderLogic $orderLogic)
- {
- $sort = $this->request->post('sort/d', '');
- $type_id = $this->request->post('type_id/s', '');
- $key_val = $this->request->post('key_val/s', '');
- $order = 'b.weigh desc';
- if($sort == 1) $order = 'price DESC, a.product_id DESC';
- if($sort == 2) $order = 'price ASC, a.product_id ASC';
- $map['a.status'] = $productTransfer::Normal;
- if(!empty($type_id)) $map['b.type_id'] = $type_id;
- if(!empty($key_val)) $map['b.'.$this->lan.'_name'] = ['like', '%'.$key_val.'%'];
- $list = $productTransfer->alias('a')
- ->join("product_list b", "a.product_id = b.id", "left")
- ->join("user_collect c", "c.product_id = a.product_id and c.user_id = {$this->auth->id}")
- ->group('a.product_id')
- ->field('a.id,'.'b.'.$this->lan.'_name as name,b.thum as img_url,min(a.price) as price,a.product_id,b.type_id,b.issue_num as issue')
- ->where('a.status', $productTransfer::Normal)
- ->where($map)
- ->order($order)
- ->paginate($this->pageSize);
- //统计
- $list = $this->getProductOrderList($list, $this->auth->id, false, $orderLogic);
- $this->success('', $list);
- }
-
- /**
- * 寄售转让详情
- * sort: 0 默认排序 1价格从高到低 2价格从低到高
- * type_id 分类
- * key_val 商品名称搜索
- */
- public function getTransferDetail(ProductLists $productLists, OrderLogic $orderLogic)
- {
-
- $ids = $this->request->post('ids/d', '');
- $list = $productLists->with('producttransfer,collect')
- ->where('product_list.id', $ids)
- ->where('product_list.status', $productLists::Normal)
- ->field('id,'.$this->lan .'_name as name,issue_num as issue,thum,details,min_buying_token as min_buy_price,max_buying_token as max_buy_price')
- ->find();
- if(empty($list)) $this->error('数据不存在');
-
- $list->circulation = $orderLogic::getProductCirculation($list->id); //流通: 所有用户的持有量
- $this->success('', $list);
- }
-
- //获取持有产品订单列表
- public function getHoldProductOrderList(OrderLogic $orderLogic)
- {
- $productId = $this->request->post('product_id/d', '');
- if(empty($productId)) $this->error('参数错误');
- $list = $orderLogic::getUserProductOrder($this->auth->id, $productId, $this->lan);
- $this->success('', $list);
- }
- private static function getProductOrderList(object $list, int $uid, bool $isCollect, OrderLogic $orderLogic): object
- {
- if($list){
- $userCollect = Loader::model('UserCollect');
- foreach ($list as &$item) {
- if($isCollect) $item['collect'] = $userCollect::where('user_id', $uid)->where('product_id', $item->product_id)->count();
- $item['circulation'] = $orderLogic::getProductCirculation($item->product_id); //流通: 所有用户的持有量
- }
- }
- return $list;
- }
-
- //获取分类
- public function getPopularType()
- {
- return $this->success('', ProductsModel::getProductTypeById($this->lan));
- }
- }
|