Diy.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api;
  12. use app\common\repositories\system\diy\DiyRepository;
  13. use app\common\repositories\community\CommunityRepository;
  14. use app\common\repositories\store\broadcast\BroadcastRoomRepository;
  15. use app\common\repositories\store\coupon\StoreCouponRepository;
  16. use app\common\repositories\store\order\StoreOrderRepository;
  17. use app\common\repositories\store\product\ProductAssistRepository;
  18. use app\common\repositories\store\product\ProductGroupRepository;
  19. use app\common\repositories\store\product\ProductPresellRepository;
  20. use app\common\repositories\store\product\ProductRepository;
  21. use app\common\repositories\store\product\SpuRepository;
  22. use app\common\repositories\store\StoreCategoryRepository;
  23. use app\common\repositories\store\StoreSeckillActiveRepository;
  24. use app\common\repositories\store\StoreSeckillTimeRepository;
  25. use app\common\repositories\system\merchant\MerchantRepository;
  26. use crmeb\basic\BaseController;
  27. use think\App;
  28. use think\facade\Cache;
  29. class Diy extends BaseController
  30. {
  31. protected $unique;
  32. protected $diyId;
  33. public function __construct(App $app)
  34. {
  35. parent::__construct($app);
  36. $this->unique = trim((string)$this->request->param('unique'));
  37. if ($this->unique) {
  38. $params = $this->request->get();
  39. unset($params['diy_id'], $params['unique']);
  40. $params['_url'] = $this->request->pathinfo();
  41. ksort($params);
  42. $this->unique = md5($this->unique . json_encode($params));
  43. }
  44. $this->diyId = ((int)$this->request->param('diy_id')) ?: 0;
  45. }
  46. protected function cache($fn)
  47. {
  48. if (!$this->unique || !$this->diyId) {
  49. return $fn();
  50. }
  51. if(!env('APP_DEBUG', false)) {
  52. $res = Cache::get('diy.' . $this->diyId . '.' . $this->unique);
  53. if ($res) return json_decode($res, true);
  54. }
  55. $res = $fn();
  56. Cache::set('diy.' . $this->diyId . '.' . $this->unique, json_encode($res), $res['ttl'] ?? 1500 + random_int(30, 100));
  57. return $res;
  58. }
  59. /**
  60. * 首页diy需要的秒杀列表
  61. * @param ProductRepository $productRepository
  62. * @param StoreSeckillTimeRepository $storeSeckillTimeRepository
  63. * FerryZhao 2024/4/24
  64. */
  65. public function seckill(ProductRepository $productRepository, StoreSeckillTimeRepository $storeSeckillTimeRepository)
  66. {
  67. $mer_id = $this->request->param('mer_id','');
  68. $limit = $this->request->param('limit',10);
  69. $currentHour = date('H', time());
  70. $storeSeckillTimeInfo = $storeSeckillTimeRepository->getSearch([])->where([
  71. ['start_time','<=',$currentHour],
  72. ['end_time','>',$currentHour],
  73. ])->find();
  74. if(empty($storeSeckillTimeInfo)){
  75. return app('json')->success(['list'=>[]]);
  76. }
  77. $stopTime = date('Y-m-d',time()).' '.($storeSeckillTimeInfo['end_time'].':00:00');
  78. $ttl = (strtotime($stopTime) - time() - 30) > 0 ?: 5;
  79. $data = $this->cache(function() use($productRepository,$storeSeckillTimeRepository,$limit,
  80. $storeSeckillTimeInfo,$stopTime,$mer_id) {
  81. $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';
  82. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  83. $storeSeckillActiveRepository = app()->make(StoreSeckillActiveRepository::class);
  84. $activeIds = $storeSeckillActiveRepository->getSearch([])->where(['active_status'=>1])->whereFindInSet("seckill_time_ids",$storeSeckillTimeInfo['seckill_time_id'])->column('seckill_active_id');
  85. $where['star'] = '';
  86. if ($mer_id) $where['mer_id'] = $mer_id;
  87. $query = $productRepository->seckillSearch($where)->with(['seckillActive']);
  88. return $query->whereIn('active_id',$activeIds)->limit($limit)->setOption('field', [])->field($field)->select()
  89. ->each(function ($item) use ($storeOrderRepository,$storeSeckillTimeInfo,$stopTime) {
  90. $item['sales'] = $storeOrderRepository->seckillOrderCounut($item['active_id'],$item['product_id']);
  91. $item['stop'] = strtotime($stopTime);
  92. $item['skill_status'] = $item['seckillActive']['active_status'];
  93. });
  94. });
  95. $list = getThumbWaterImage($data, ['image'], 'mid');
  96. return app('json')->success(['list'=>$list,'stop'=> strtotime($stopTime),'ttl'=>$ttl]);
  97. }
  98. /**
  99. * DIY 预售商品列表
  100. * @param ProductPresellRepository $productPresellRepository
  101. * @return \think\response\Json
  102. * @author Qinii
  103. * @day 2023/8/15
  104. */
  105. public function presell(ProductPresellRepository $repository)
  106. {
  107. $limit = $this->request->param('limit',10);
  108. $data = $this->cache(function() use($repository,$limit) {
  109. $where = $repository->actionShow();
  110. $where['type'] = 100;
  111. $where['star'] = '';
  112. $where['mer_id'] = $this->request->param('mer_id','');
  113. $list = $repository->search($where)->with(['product' => function($query){
  114. $query->field('product_id,image,store_name');
  115. }])->limit($limit)->select();
  116. if ($list) $data['list'] = $list->toArray();
  117. return $data;
  118. });
  119. return app('json')->success($data);
  120. }
  121. /**
  122. * DIY助力商品列表
  123. * @param ProductAssistRepository $repository
  124. * @return \think\response\Json
  125. * @author Qinii
  126. * @day 2023/8/15
  127. */
  128. public function assist(ProductAssistRepository $repository)
  129. {
  130. $limit = $this->request->param('limit',10);
  131. $data = $this->cache(function() use($repository,$limit) {
  132. $where = $repository->assistShow();
  133. $where['star'] = '';
  134. $where['mer_id'] = $this->request->param('mer_id','');
  135. $list = $repository->search($where)->with([
  136. 'assistSku',
  137. 'product' => function($query){
  138. $query->field('product_id,image,store_name');
  139. },
  140. ])->append(['user_count'])->limit($limit)->select();
  141. if ($list) $data['list'] = $list->toArray();
  142. return $data;
  143. });
  144. return app('json')->success($data);
  145. }
  146. /**
  147. * DIY拼团商品列表
  148. * @param ProductGroupRepository $repository
  149. * @return \think\response\Json
  150. * @author Qinii
  151. * @day 2023/8/15
  152. */
  153. public function group(ProductGroupRepository $repository)
  154. {
  155. $limit = $this->request->param('limit',10);
  156. $data = $this->cache(function() use($repository,$limit) {
  157. $where = $repository->actionShow();
  158. $where['order'] = '';
  159. $where['mer_id'] = $this->request->param('mer_id','');
  160. $list = $repository->search($where)->with([
  161. 'product' => function($query){
  162. $query->field('product_id,store_name,image,price,sales,unit_name');
  163. },
  164. ])->limit($limit)->select();
  165. if ($list) $data['list'] = $list->toArray();
  166. return $data;
  167. });
  168. return app('json')->success($data);
  169. }
  170. /**
  171. * DIY商品列表
  172. * @param SpuRepository $repository
  173. * @return \think\response\Json
  174. * @author Qinii
  175. * @day 2023/8/15
  176. */
  177. public function spu(SpuRepository $repository)
  178. {
  179. $data = $this->cache(function() use($repository) {
  180. $where = $this->request->params(['cate_pid','product_ids','mer_id','mer_cate_id', 'order','latitude','longitude']);
  181. $limit = (int)$this->request->param('limit',10);
  182. $where['spu_status'] = 1;
  183. $where['mer_status'] = 1;
  184. $where['not_type'] = [20];
  185. $where['is_gift_bag'] = 0;
  186. $where['product_type'] = 0;
  187. $list = $repository->search($where)->with(['merchant'=> function($query){
  188. $query->with(['typeName','categoryName'])->field('mer_id,category_id,type_id,mer_avatar,mer_name,is_trader,long,lat');
  189. },'issetCoupon'])->limit($limit)->select();
  190. if ($list) $data['list'] = $list->toArray();
  191. // 计算距离
  192. if((isset($where['latitude']) && !empty($where['latitude'])) && (isset($where['longitude']) && !empty($where['longitude']))) {
  193. foreach ($data['list'] as &$item) {
  194. [$item['distance'], $item['distanceM']] = $this->distance($where, $item['merchant']['lat'], $item['merchant']['long']);
  195. }
  196. }
  197. // 根据距离排序
  198. if($where['order'] == 'range_asc') {
  199. usort($data['list'], function($a, $b) {
  200. return $a['distanceM'] > $b['distanceM'];
  201. });
  202. }
  203. $data['list'] = getThumbWaterImage($data['list'], ['image'], 'mid');
  204. return $data;
  205. });
  206. return app('json')->success($data);
  207. }
  208. /**
  209. * 计算距离
  210. *
  211. * @param array $params
  212. * @param string $merLat
  213. * @param string $merLong
  214. * @return void
  215. */
  216. public function distance(array $params, string $merLat, string $merLong)
  217. {
  218. if (!$merLat || !$merLong) {
  219. return false;
  220. }
  221. $distance = $distanceM = getDistance($params['latitude'], $params['longitude'], $merLat, $merLong);
  222. if ($distance < 0.9) {
  223. $distance = max(bcmul($distance, 1000, 0), 1).'m';
  224. if ($distance == '1m') {$distance = '100m以内';}
  225. } else {
  226. $distance.= 'km';
  227. }
  228. return [$distance, $distanceM];
  229. }
  230. /**
  231. * DIY社区文章列表
  232. * @param CommunityRepository $repository
  233. * @return \think\response\Json
  234. * @author Qinii
  235. * @day 2023/8/15
  236. */
  237. public function community(CommunityRepository $repository)
  238. {
  239. $limit = $this->request->param('limit',10);
  240. $data = $this->cache(function() use($repository,$limit) {
  241. $where = $repository::IS_SHOW_WHERE;
  242. $list = $repository->search($where)->with([
  243. 'author' => function($query) {
  244. $query->field('uid,real_name,status,avatar,nickname,count_start');
  245. },
  246. ])->limit($limit)->select();
  247. if ($list) $data['list'] = $list->toArray();
  248. return $data;
  249. });
  250. return app('json')->success($data);
  251. }
  252. /**
  253. * DIY店铺推荐列表
  254. * @param MerchantRepository $repository
  255. * @return \think\response\Json
  256. * @author Qinii
  257. * @day 2023/8/15
  258. */
  259. public function store(MerchantRepository $repository)
  260. {
  261. $limit = $this->request->param('limit',10);
  262. $data = $this->cache(function() use($repository,$limit) {
  263. $where = $this->request->params(['order','latitude','longitude']);
  264. $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';
  265. $where['is_best'] = 1;
  266. $where['status'] = 1;
  267. $where['mer_state'] = 1;
  268. $where['is_del'] = 0;
  269. $list = $repository->search($where)->with(['type_name'])->setOption('field', [])->field($field)->limit((int)$limit)->select()->append(['all_recommend','mer_type_name']);
  270. if ($list) $data['list'] = $list->toArray();
  271. // 计算距离
  272. if((isset($where['latitude']) && !empty($where['latitude'])) && (isset($where['longitude']) && !empty($where['longitude']))) {
  273. foreach ($data['list'] as &$item) {
  274. [$item['distance'], $item['distanceM']] = $this->distance($where, $item['lat'], $item['long']);
  275. }
  276. }
  277. // 根据距离排序
  278. if($where['order'] == 'range_asc') {
  279. usort($data['list'], function($a, $b) {
  280. return $a['distanceM'] > $b['distanceM'];
  281. });
  282. }
  283. return $data;
  284. });
  285. return app('json')->success($data);
  286. }
  287. /**
  288. * DIY 优惠券列表
  289. * @param StoreCouponRepository $repository
  290. * @return \think\response\Json
  291. * @author Qinii
  292. * @day 2023/8/15
  293. */
  294. public function coupon(StoreCouponRepository $repository)
  295. {
  296. $limit = $this->request->param('limit',10);
  297. $data = $this->cache(function() use($repository,$limit) {
  298. $uid = 0;
  299. if ($this->request->isLogin()) $uid = $this->request->uid();
  300. $where['send_type'] = 0;
  301. $where['mer_id'] = $this->request->param('mer_id','');
  302. $with = [];
  303. if ($uid)
  304. $with['issue'] = function ($query) use ($uid) {
  305. $query->where('uid', $uid);
  306. };
  307. $baseQuery = $repository->validCouponQueryWithMerchant($where, $uid)->with($with);
  308. $list = $baseQuery->setOption('field',[])->field('C.*')->limit($limit)->select();
  309. if ($list) $data['list'] = $list->toArray();
  310. return $data;
  311. });
  312. return app('json')->success($data);
  313. }
  314. /**
  315. * DIY二级分类
  316. * @param StoreCategoryRepository $repository
  317. * @return \think\response\Json
  318. * @author Qinii
  319. * @day 2023/8/16
  320. */
  321. public function category(StoreCategoryRepository $repository)
  322. {
  323. $data = $this->cache(function() use($repository) {
  324. $data = app()->make(StoreCategoryRepository::class)->getTwoLevel();
  325. return $data;
  326. });
  327. return app('json')->success($data);
  328. }
  329. /**
  330. * 小程序直播接口
  331. * @param BroadcastRoomRepository $repository
  332. * @return \think\response\Json
  333. * @author Qinii
  334. * @day 2023/8/16
  335. */
  336. public function broadcast(BroadcastRoomRepository $repository)
  337. {
  338. $limit = $this->request->param('limit',10);
  339. $data = $this->cache(function() use($repository,$limit) {
  340. $where = $this->request->params(['mer_id']);
  341. $where['show_tag'] = 1;
  342. $list = $repository->search($where)->where('room_id', '>', 0)
  343. ->whereNotIn('live_status', [107])->limit($limit)
  344. ->with([
  345. 'broadcast' => function($query) {
  346. $query->where('on_sale',1);
  347. $query->with('goods');
  348. }
  349. ])
  350. ->order('star DESC, sort DESC, create_time DESC')->select();
  351. // 对查询结果中的每个用户,格式化其直播开始时间
  352. foreach ($list as $item) {
  353. $item->show_time = date('m/d H:i', strtotime($item->start_time));
  354. }
  355. if ($list) $data['list'] = $list->toArray();
  356. return $data;
  357. });
  358. return app('json')->success($data);
  359. }
  360. /**
  361. * DIY 热门排行列表
  362. * @param SpuRepository $repository
  363. * @return \think\response\Json
  364. * @author Qinii
  365. * @day 2023/8/16
  366. */
  367. public function hot_top(SpuRepository $repository)
  368. {
  369. $data = $this->cache(function() use($repository) {
  370. $cateId = $this->request->param('cate_pid',0);
  371. $cateId = is_array($cateId) ?:explode(',',$cateId);
  372. $cateId = array_unique($cateId);
  373. $count = count($cateId);
  374. if ($count > 3){
  375. $cateId = array_slice($cateId,0,3);
  376. } else if ($count < 3) {
  377. $limit = 3 - count($cateId);
  378. $_cateId = app()->make(StoreCategoryRepository::class)->getSearch([
  379. 'level' => systemConfig('hot_ranking_lv') ?:0,
  380. 'mer_id' => 0,
  381. 'is_show' => 1,
  382. 'type' => 0
  383. ])->limit($limit)->order('sort DESC,create_time DESC')->column('store_category_id');
  384. $cateId = array_merge($cateId,$_cateId);
  385. }
  386. $data = [];
  387. $storeCategoryRepository = app()->make(StoreCategoryRepository::class);
  388. foreach ($cateId as $cate_id) {
  389. $list = $repository->getHotRanking($cate_id ?: 0,3);
  390. $cate = $storeCategoryRepository->get($cate_id);
  391. $data[] = [
  392. 'cate_id' => $cate['store_category_id'] ?? 0,
  393. 'cate_name' => $cate['cate_name'] ?? '总榜',
  394. 'list' => $list,
  395. ];
  396. }
  397. return $data;
  398. });
  399. return app('json')->success($data);
  400. }
  401. public function productDetail(DiyRepository $repository)
  402. {
  403. $key = env('APP_KEY').'_sys.get_sys_product_detail';
  404. $data = Cache::remember($key,function(){
  405. $res = app()->make(DiyRepository::class)->getProductDetail();
  406. $data['value'] = $res['product_detail_diy'];
  407. return $data;
  408. }, 60);
  409. if (is_null($data['value'])) return app('json')->fail('暂无数据');
  410. return app('json')->encode($data);
  411. }
  412. }