123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace addons\shopro\controller\goods;
- use addons\shopro\controller\Common;
- use addons\shopro\service\goods\GoodsService;
- use app\admin\model\shopro\user\GoodsLog;
- use app\admin\model\shopro\activity\Activity;
- class Goods extends Common
- {
- protected $noNeedLogin = ['*'];
- protected $noNeedRight = ['*'];
- public function index()
- {
- $params = $this->request->param();
- $keyword = $params['keyword'] ?? '';
- $ids = $params['ids'] ?? '';
- $category_id = $params['category_id'] ?? '';
- $is_category_deep = $params['is_category_deep'] ?? true;
- $sort = $params['sort'] ?? 'weigh';
- $order = $params['order'] ?? 'desc';
- $service = new GoodsService(function ($goods) {
- $goods->activities = $goods->activities;
- $goods->promos = $goods->promos;
- return $goods;
- });
-
- $service->up();
- if ($keyword) {
- $service->search($keyword);
- }
- if ($ids) {
- $service->whereIds($ids);
- }
- if ($category_id) {
- $service->category($category_id, $is_category_deep);
- }
- if ($sort) {
- $service->order($sort, $order);
- }
- $goods = $service->select_autopage();
- $goods = collection($goods)->toArray();
- $goods = list_domain_image($goods,['image']);
- $this->success('获取成功', $goods);
- }
-
- public function ids()
- {
- $params = $this->request->param();
- $ids = $params['ids'] ?? '';
- $service = new GoodsService(function ($goods) {
- $goods->activities = $goods->activities;
- $goods->promos = $goods->promos;
- return $goods;
- });
- $service->show()->with(['max_sku_price' => function ($query) {
- $query->where('status', 'up');
- }]);
- if ($ids) {
- $service->whereIds($ids);
- }
- $goods = $service->select();
- $this->success('获取成功', $goods);
- }
- public function detail()
- {
- $user = auth_user();
- $id = $this->request->param('id');
- $activity_id = $this->request->param('activity_id');
-
- session('goods-activity_id:' . $id, $activity_id);
- $service = new GoodsService(function ($goods, $service) use ($activity_id) {
- $goods->service = $goods->service;
- $goods->skus = $goods->skus;
-
- if (!$activity_id) {
- $goods->activities = $goods->activities;
- $goods->promos = $goods->promos;
- } else {
- $goods->activity = $goods->activity;
- $goods->original_goods_price = $goods->original_goods_price;
- }
- return $goods;
- });
- $goods = $service->field('*,price as pricemin')->show()->activity($activity_id)
- ->with(
- [
-
- 'favorite'])
- ->where('id', $id)->find();
- if (!$goods) {
- $this->error(__('No Results were found'));
- }
-
- GoodsLog::addView($user, $goods);
-
- $skuPrices = $goods['new_sku_prices'];
- $content = $goods['content'];
- $goods = $goods->toArray();
- $goods['image'] = localpath_to_netpath($goods['image']);
- $goods['images'] = array_domain_image($goods['images']);
- $skuPrices = list_domain_image($skuPrices->toArray(),['image']);
- $goods['sku_prices'] = $skuPrices;
- $goods['content'] = $content;
- unset($goods['new_sku_prices']);
- $this->success('获取成功', $goods);
- }
-
- public function activity()
- {
- $activity_id = $this->request->param('activity_id');
- $need_buyers = $this->request->param('need_buyers', 0);
- $activity = Activity::where('id', $activity_id)->find();
- if (!$activity) {
- $this->error(__('No Results were found'));
- }
- $goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
-
- foreach ($goodsIds as $id) {
- session('goods-activity_id:' . $id, $activity_id);
- }
- $service = new GoodsService(function ($goods) use ($need_buyers) {
- if ($need_buyers) {
- $goods->buyers = $goods->buyers;
- }
- $goods->activity = $goods->activity;
- return $goods;
- });
- $goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->order('weigh', 'desc')->select();
- $goods = collection($goods)->toArray();
- foreach ($goods as &$gd) {
- unset($gd['new_sku_prices'], $gd['activity']);
- }
- $this->success('获取成功', $goods);
- }
-
- public function activityList()
- {
- $activity_id = $this->request->param('activity_id');
- $activity = Activity::where('id', $activity_id)->find();
- if (!$activity) {
- $this->error(__('No Results were found'));
- }
- $goodsIds = $activity->goods_ids ? explode(',', $activity->goods_ids) : [];
-
- foreach ($goodsIds as $id) {
- session('goods-activity_id:' . $id, $activity_id);
- }
- $service = new GoodsService(function ($goods) {
- $goods->promos = $goods->promos;
- return $goods;
- });
- $goods = $service->activity($activity_id)->whereIds($goodsIds)->show()->order('weigh', 'desc')->paginate();
- $this->success('获取成功', $goods);
- }
- }
|