| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace app\api\controller\v1;
- use app\BaseController;
- use app\model\api\Product;
- use app\model\system\Category as CategoryModel;
- use app\model\system\ProductAttr;
- use app\Request;
- use library\services\UtilService;
- use library\services\QrcodeService;
- use think\facade\Db;
- class Index extends BaseController
- {
- /**
- * 获取产品分类
- */
- public function getCategory(){
- $category = new CategoryModel();
- $menus = $category->getArMenu('',0,1);
- return app('json')->success($menus);
- }
- /**
- * 获取产品标签
- */
- public function getKeyword($cid){
- $data = Product::where('is_del', 0)->where('is_show', 1)->where('cate_id',$cid)->where('keyword','<>',"")->group('keyword')->column('keyword');
- $keyword = [];
- foreach ($data as $k => $v) {
- $arr = explode('、',$v);
- $keyword = array_merge($keyword, $arr);
- }
- $keyword = array_unique($keyword);
- return app('json')->success($keyword);
- }
- /**
- * 获取产品列表
- */
- public function productList(Request $request){
- $data = UtilService::getMore([
- ['cid', 0],
- ['sort', ''],
- ['keyword', ''],
- ['page', 1],
- ['limit', 24]
- ], $request);
- $list = Product::getProductList($data);
- foreach ($list as $k => $v) {
- if($v['price'] == 0) {
- $list[$k]['price'] = round($v['ot_price'] * 1.15, 2);
- }
- $list[$k]['market_price'] = round($list[$k]['price'] * 1.3, 2);
- $score = Db::name("product_score")->where('product_id', $v['id'])->find();
- if($score) {
- $attr = json_decode($score['attribute'], true);
- $max = $attr[2]['taobao'];
- if($attr[2]['t1688'] > $max)
- $max = $attr[2]['t1688'];
- if($attr[2]['pdd'] > $max)
- $max = $attr[2]['pdd'];
- if($attr[2]['dy'] > $max)
- $max = $attr[2]['dy'];
- if($attr[2]['ks'] > $max)
- $max = $attr[2]['ks'];
- if($attr[2]['other'] > $max)
- $max = $attr[2]['other'];
- if($max)
- $list[$k]['market_price'] = $max;
- }
- if($v['label'] != ""){
- $list[$k]['label'] = explode('、',$v['label']);
- }else{
- $list[$k]['label'] = [];
- }
- $list[$k]['saler'] = random_int(1, 100);
- }
- return app('json')->success($list);
- }
- /**
- * 商品详情
- * @param Request $request
- * @param $id
- * @param int $type
- * @return mixed
- */
- public function productDetail(Request $request, $id)
- {
- if (!$id || !($storeInfo = Product::getValidProduct($id))) return app('json')->fail('商品不存在或已下架');
- $storeInfo['slider_image'] = json_decode($storeInfo['slider_image'], true);
- $storeInfo['detail_image'] = json_decode($storeInfo['detail_image'], true);
- $storeInfo['code_base'] = QrcodeService::getWechatQrcodePath($id . '_product_detail_wap.jpg', '/h5/pages/product/detail?id=' . $id);
- //替换windows服务器下正反斜杠问题导致图片无法显示
- $storeInfo['description'] = preg_replace_callback('#<img.*?src="([^"]*)"[^>]*>#i', function ($imagsSrc) {
- return isset($imagsSrc[1]) && isset($imagsSrc[0]) ? str_replace($imagsSrc[1], str_replace('\\', '/', $imagsSrc[1]), $imagsSrc[0]) : '';
- }, $storeInfo['description']);
- if($storeInfo['price'] == 0){
- $storeInfo['price'] = round($storeInfo['ot_price'] * 1.15, 2);
- }
- $storeInfo['market_price'] = round($storeInfo['price'] * 1.3, 2);
- $score = Db::name("product_score")->where('product_id', $id)->find();
- if($score) {
- $attr = json_decode($score['attribute'], true);
- $max = $attr[2]['taobao'];
- if($attr[2]['t1688'] > $max)
- $max = $attr[2]['t1688'];
- if($attr[2]['pdd'] > $max)
- $max = $attr[2]['pdd'];
- if($attr[2]['dy'] > $max)
- $max = $attr[2]['dy'];
- if($attr[2]['ks'] > $max)
- $max = $attr[2]['ks'];
- if($attr[2]['other'] > $max)
- $max = $attr[2]['other'];
- if($max)
- $storeInfo['market_price'] = $max;
- }
- $data['storeInfo'] = $storeInfo;
- list($productAttr, $productValue, $sku) = ProductAttr::getProductAttrDetail($id, 0, 0);
- $data['productAttr'] = $productAttr;
- $prices = array_column($productValue, 'price');
- array_multisort($prices, SORT_ASC, SORT_NUMERIC, $productValue);
- $keys = array_keys($productValue);
- $productValue = array_combine($keys, $productValue);
- $data['productValue'] = $productValue;
- $data['sku'] = $sku;
- $data['priceName'] = 0;
- $data['good_list'] = Product::getGoodList(18, 'image,store_name,price,id,ot_price,stock,IFNULL(sales,0) + IFNULL(ficti,0) as fsales,unit_name');
- return app('json')->successful($data);
- }
- }
|