| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439 |
- <?php
- // +----------------------------------------------------------------------
- // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
- // +----------------------------------------------------------------------
- // | Author: CRMEB Team <admin@crmeb.com>
- // +----------------------------------------------------------------------
- namespace app\controller\api;
- use app\common\repositories\system\diy\DiyRepository;
- use app\common\repositories\community\CommunityRepository;
- use app\common\repositories\store\broadcast\BroadcastRoomRepository;
- use app\common\repositories\store\coupon\StoreCouponRepository;
- use app\common\repositories\store\order\StoreOrderRepository;
- use app\common\repositories\store\product\ProductAssistRepository;
- use app\common\repositories\store\product\ProductGroupRepository;
- use app\common\repositories\store\product\ProductPresellRepository;
- use app\common\repositories\store\product\ProductRepository;
- use app\common\repositories\store\product\SpuRepository;
- use app\common\repositories\store\StoreCategoryRepository;
- use app\common\repositories\store\StoreSeckillActiveRepository;
- use app\common\repositories\store\StoreSeckillTimeRepository;
- use app\common\repositories\system\merchant\MerchantRepository;
- use crmeb\basic\BaseController;
- use think\App;
- use think\facade\Cache;
- class Diy extends BaseController
- {
- protected $unique;
- protected $diyId;
- public function __construct(App $app)
- {
- parent::__construct($app);
- $this->unique = trim((string)$this->request->param('unique'));
- if ($this->unique) {
- $params = $this->request->get();
- unset($params['diy_id'], $params['unique']);
- $params['_url'] = $this->request->pathinfo();
- ksort($params);
- $this->unique = md5($this->unique . json_encode($params));
- }
- $this->diyId = ((int)$this->request->param('diy_id')) ?: 0;
- }
- protected function cache($fn)
- {
- if (!$this->unique || !$this->diyId) {
- return $fn();
- }
- if(!env('APP_DEBUG', false)) {
- $res = Cache::get('diy.' . $this->diyId . '.' . $this->unique);
- if ($res) return json_decode($res, true);
- }
- $res = $fn();
- Cache::set('diy.' . $this->diyId . '.' . $this->unique, json_encode($res), $res['ttl'] ?? 1500 + random_int(30, 100));
- return $res;
- }
- /**
- * 首页diy需要的秒杀列表
- * @param ProductRepository $productRepository
- * @param StoreSeckillTimeRepository $storeSeckillTimeRepository
- * FerryZhao 2024/4/24
- */
- public function seckill(ProductRepository $productRepository, StoreSeckillTimeRepository $storeSeckillTimeRepository)
- {
- $mer_id = $this->request->param('mer_id','');
- $limit = $this->request->param('limit',10);
- $currentHour = date('H', time());
- $storeSeckillTimeInfo = $storeSeckillTimeRepository->getSearch([])->where([
- ['start_time','<=',$currentHour],
- ['end_time','>',$currentHour],
- ])->find();
- if(empty($storeSeckillTimeInfo)){
- return app('json')->success(['list'=>[]]);
- }
- $stopTime = date('Y-m-d',time()).' '.($storeSeckillTimeInfo['end_time'].':00:00');
- $ttl = (strtotime($stopTime) - time() - 30) > 0 ?: 5;
- $data = $this->cache(function() use($productRepository,$storeSeckillTimeRepository,$limit,
- $storeSeckillTimeInfo,$stopTime,$mer_id) {
- $field = 'Product.product_id,Product.active_id,Product.mer_id,is_new,U.keyword,brand_id,U.image,U.product_type,U.store_name,U.sort,U.rank,star,rate,reply_count,sales,U.price,cost,Product.ot_price,stock,extension_type,care_count,unit_name,U.create_time';
- $storeOrderRepository = app()->make(StoreOrderRepository::class);
- $storeSeckillActiveRepository = app()->make(StoreSeckillActiveRepository::class);
- $activeIds = $storeSeckillActiveRepository->getSearch([])->where(['active_status'=>1])->whereFindInSet("seckill_time_ids",$storeSeckillTimeInfo['seckill_time_id'])->column('seckill_active_id');
- $where['star'] = '';
- if ($mer_id) $where['mer_id'] = $mer_id;
- $query = $productRepository->seckillSearch($where)->with(['seckillActive']);
- return $query->whereIn('active_id',$activeIds)->limit($limit)->setOption('field', [])->field($field)->select()
- ->each(function ($item) use ($storeOrderRepository,$storeSeckillTimeInfo,$stopTime) {
- $item['sales'] = $storeOrderRepository->seckillOrderCounut($item['active_id'],$item['product_id']);
- $item['stop'] = strtotime($stopTime);
- $item['skill_status'] = $item['seckillActive']['active_status'];
- });
- });
- $list = getThumbWaterImage($data, ['image'], 'mid');
- return app('json')->success(['list'=>$list,'stop'=> strtotime($stopTime),'ttl'=>$ttl]);
- }
- /**
- * DIY 预售商品列表
- * @param ProductPresellRepository $productPresellRepository
- * @return \think\response\Json
- * @author Qinii
- * @day 2023/8/15
- */
- public function presell(ProductPresellRepository $repository)
- {
- $limit = $this->request->param('limit',10);
- $data = $this->cache(function() use($repository,$limit) {
- $where = $repository->actionShow();
- $where['type'] = 100;
- $where['star'] = '';
- $where['mer_id'] = $this->request->param('mer_id','');
- $list = $repository->search($where)->with(['product' => function($query){
- $query->field('product_id,image,store_name');
- }])->limit($limit)->select();
- if ($list) $data['list'] = $list->toArray();
- return $data;
- });
- return app('json')->success($data);
- }
- /**
- * DIY助力商品列表
- * @param ProductAssistRepository $repository
- * @return \think\response\Json
- * @author Qinii
- * @day 2023/8/15
- */
- public function assist(ProductAssistRepository $repository)
- {
- $limit = $this->request->param('limit',10);
- $data = $this->cache(function() use($repository,$limit) {
- $where = $repository->assistShow();
- $where['star'] = '';
- $where['mer_id'] = $this->request->param('mer_id','');
- $list = $repository->search($where)->with([
- 'assistSku',
- 'product' => function($query){
- $query->field('product_id,image,store_name');
- },
- ])->append(['user_count'])->limit($limit)->select();
- if ($list) $data['list'] = $list->toArray();
- return $data;
- });
- return app('json')->success($data);
- }
- /**
- * DIY拼团商品列表
- * @param ProductGroupRepository $repository
- * @return \think\response\Json
- * @author Qinii
- * @day 2023/8/15
- */
- public function group(ProductGroupRepository $repository)
- {
- $limit = $this->request->param('limit',10);
- $data = $this->cache(function() use($repository,$limit) {
- $where = $repository->actionShow();
- $where['order'] = '';
- $where['mer_id'] = $this->request->param('mer_id','');
- $list = $repository->search($where)->with([
- 'product' => function($query){
- $query->field('product_id,store_name,image,price,sales,unit_name');
- },
- ])->limit($limit)->select();
- if ($list) $data['list'] = $list->toArray();
- return $data;
- });
- return app('json')->success($data);
- }
- /**
- * DIY商品列表
- * @param SpuRepository $repository
- * @return \think\response\Json
- * @author Qinii
- * @day 2023/8/15
- */
- public function spu(SpuRepository $repository)
- {
- $data = $this->cache(function() use($repository) {
- $where = $this->request->params(['cate_pid','product_ids','mer_id','mer_cate_id', 'order','latitude','longitude']);
- $limit = (int)$this->request->param('limit',10);
- $where['spu_status'] = 1;
- $where['mer_status'] = 1;
- $where['not_type'] = [20];
- $where['is_gift_bag'] = 0;
- $where['product_type'] = 0;
- $list = $repository->search($where)->with(['merchant'=> function($query){
- $query->with(['typeName','categoryName'])->field('mer_id,category_id,type_id,mer_avatar,mer_name,is_trader,long,lat');
- },'issetCoupon'])->limit($limit)->select();
- if ($list) $data['list'] = $list->toArray();
- // 计算距离
- if((isset($where['latitude']) && !empty($where['latitude'])) && (isset($where['longitude']) && !empty($where['longitude']))) {
- foreach ($data['list'] as &$item) {
- [$item['distance'], $item['distanceM']] = $this->distance($where, $item['merchant']['lat'], $item['merchant']['long']);
- }
- }
- // 根据距离排序
- if($where['order'] == 'range_asc') {
- usort($data['list'], function($a, $b) {
- return $a['distanceM'] > $b['distanceM'];
- });
- }
- $data['list'] = getThumbWaterImage($data['list'], ['image'], 'mid');
- return $data;
- });
- return app('json')->success($data);
- }
- /**
- * 计算距离
- *
- * @param array $params
- * @param string $merLat
- * @param string $merLong
- * @return void
- */
- public function distance(array $params, string $merLat, string $merLong)
- {
- if (!$merLat || !$merLong) {
- return false;
- }
- $distance = $distanceM = getDistance($params['latitude'], $params['longitude'], $merLat, $merLong);
- if ($distance < 0.9) {
- $distance = max(bcmul($distance, 1000, 0), 1).'m';
- if ($distance == '1m') {$distance = '100m以内';}
- } else {
- $distance.= 'km';
- }
- return [$distance, $distanceM];
- }
- /**
- * DIY社区文章列表
- * @param CommunityRepository $repository
- * @return \think\response\Json
- * @author Qinii
- * @day 2023/8/15
- */
- public function community(CommunityRepository $repository)
- {
- $limit = $this->request->param('limit',10);
- $data = $this->cache(function() use($repository,$limit) {
- $where = $repository::IS_SHOW_WHERE;
- $list = $repository->search($where)->with([
- 'author' => function($query) {
- $query->field('uid,real_name,status,avatar,nickname,count_start');
- },
- ])->limit($limit)->select();
- if ($list) $data['list'] = $list->toArray();
- return $data;
- });
- return app('json')->success($data);
- }
- /**
- * DIY店铺推荐列表
- * @param MerchantRepository $repository
- * @return \think\response\Json
- * @author Qinii
- * @day 2023/8/15
- */
- public function store(MerchantRepository $repository)
- {
- $limit = $this->request->param('limit',10);
- $data = $this->cache(function() use($repository,$limit) {
- $where = $this->request->params(['order','latitude','longitude']);
- $field = 'mer_id,care_count,is_trader,type_id,mer_banner,mini_banner,mer_name, mark,mer_avatar,product_score,service_score,postage_score,sales,status,is_best,create_time,long,lat,is_margin';
- $where['is_best'] = 1;
- $where['status'] = 1;
- $where['mer_state'] = 1;
- $where['is_del'] = 0;
- $list = $repository->search($where)->with(['type_name'])->setOption('field', [])->field($field)->limit((int)$limit)->select()->append(['all_recommend','mer_type_name']);
- if ($list) $data['list'] = $list->toArray();
- // 计算距离
- if((isset($where['latitude']) && !empty($where['latitude'])) && (isset($where['longitude']) && !empty($where['longitude']))) {
- foreach ($data['list'] as &$item) {
- [$item['distance'], $item['distanceM']] = $this->distance($where, $item['lat'], $item['long']);
- }
- }
- // 根据距离排序
- if($where['order'] == 'range_asc') {
- usort($data['list'], function($a, $b) {
- return $a['distanceM'] > $b['distanceM'];
- });
- }
- return $data;
- });
- return app('json')->success($data);
- }
- /**
- * DIY 优惠券列表
- * @param StoreCouponRepository $repository
- * @return \think\response\Json
- * @author Qinii
- * @day 2023/8/15
- */
- public function coupon(StoreCouponRepository $repository)
- {
- $limit = $this->request->param('limit',10);
- $data = $this->cache(function() use($repository,$limit) {
- $uid = 0;
- if ($this->request->isLogin()) $uid = $this->request->uid();
- $where['send_type'] = 0;
- $where['mer_id'] = $this->request->param('mer_id','');
- $with = [];
- if ($uid)
- $with['issue'] = function ($query) use ($uid) {
- $query->where('uid', $uid);
- };
- $baseQuery = $repository->validCouponQueryWithMerchant($where, $uid)->with($with);
- $list = $baseQuery->setOption('field',[])->field('C.*')->limit($limit)->select();
- if ($list) $data['list'] = $list->toArray();
- return $data;
- });
- return app('json')->success($data);
- }
- /**
- * DIY二级分类
- * @param StoreCategoryRepository $repository
- * @return \think\response\Json
- * @author Qinii
- * @day 2023/8/16
- */
- public function category(StoreCategoryRepository $repository)
- {
- $data = $this->cache(function() use($repository) {
- $data = app()->make(StoreCategoryRepository::class)->getTwoLevel();
- return $data;
- });
- return app('json')->success($data);
- }
- /**
- * 小程序直播接口
- * @param BroadcastRoomRepository $repository
- * @return \think\response\Json
- * @author Qinii
- * @day 2023/8/16
- */
- public function broadcast(BroadcastRoomRepository $repository)
- {
- $limit = $this->request->param('limit',10);
- $data = $this->cache(function() use($repository,$limit) {
- $where = $this->request->params(['mer_id']);
- $where['show_tag'] = 1;
- $list = $repository->search($where)->where('room_id', '>', 0)
- ->whereNotIn('live_status', [107])->limit($limit)
- ->with([
- 'broadcast' => function($query) {
- $query->where('on_sale',1);
- $query->with('goods');
- }
- ])
- ->order('star DESC, sort DESC, create_time DESC')->select();
- // 对查询结果中的每个用户,格式化其直播开始时间
- foreach ($list as $item) {
- $item->show_time = date('m/d H:i', strtotime($item->start_time));
- }
- if ($list) $data['list'] = $list->toArray();
- return $data;
- });
- return app('json')->success($data);
- }
- /**
- * DIY 热门排行列表
- * @param SpuRepository $repository
- * @return \think\response\Json
- * @author Qinii
- * @day 2023/8/16
- */
- public function hot_top(SpuRepository $repository)
- {
- $data = $this->cache(function() use($repository) {
- $cateId = $this->request->param('cate_pid',0);
- $cateId = is_array($cateId) ?:explode(',',$cateId);
- $cateId = array_unique($cateId);
- $count = count($cateId);
- if ($count > 3){
- $cateId = array_slice($cateId,0,3);
- } else if ($count < 3) {
- $limit = 3 - count($cateId);
- $_cateId = app()->make(StoreCategoryRepository::class)->getSearch([
- 'level' => systemConfig('hot_ranking_lv') ?:0,
- 'mer_id' => 0,
- 'is_show' => 1,
- 'type' => 0
- ])->limit($limit)->order('sort DESC,create_time DESC')->column('store_category_id');
- $cateId = array_merge($cateId,$_cateId);
- }
- $data = [];
- $storeCategoryRepository = app()->make(StoreCategoryRepository::class);
- foreach ($cateId as $cate_id) {
- $list = $repository->getHotRanking($cate_id ?: 0,3);
- $cate = $storeCategoryRepository->get($cate_id);
- $data[] = [
- 'cate_id' => $cate['store_category_id'] ?? 0,
- 'cate_name' => $cate['cate_name'] ?? '总榜',
- 'list' => $list,
- ];
- }
- return $data;
- });
- return app('json')->success($data);
- }
- public function productDetail(DiyRepository $repository)
- {
- $key = env('APP_KEY').'_sys.get_sys_product_detail';
- $data = Cache::remember($key,function(){
- $res = app()->make(DiyRepository::class)->getProductDetail();
- $data['value'] = $res['product_detail_diy'];
- return $data;
- }, 60);
- if (is_null($data['value'])) return app('json')->fail('暂无数据');
- return app('json')->encode($data);
- }
- }
|