ScoreShop.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace addons\shopro\controller\app;
  3. use addons\shopro\controller\Common;
  4. use addons\shopro\service\goods\GoodsService;
  5. use app\admin\model\shopro\user\GoodsLog;
  6. class ScoreShop extends Common
  7. {
  8. protected $noNeedLogin = ['index', 'ids', 'detail'];
  9. protected $noNeedRight = ['*'];
  10. public function index()
  11. {
  12. $params = $this->request->param();
  13. $keyword = $params['keyword'] ?? '';
  14. $sort = $params['sort'] ?? 'weigh';
  15. $order = $params['order'] ?? 'desc';
  16. $service = new GoodsService(function ($goods) {
  17. $goods->score = $goods->score;
  18. return $goods;
  19. });
  20. $service->show()->score()->with(['all_score_sku_prices']); // 包含下架的积分规格
  21. if ($keyword) {
  22. $service->search($keyword);
  23. }
  24. if ($sort) {
  25. $service->order($sort, $order);
  26. }
  27. $goods = $service->paginate();
  28. $this->success('获取成功', $goods);
  29. }
  30. public function ids()
  31. {
  32. $params = $this->request->param();
  33. $ids = $params['ids'] ?? '';
  34. $service = new GoodsService(function ($goods) {
  35. $goods->score = $goods->score;
  36. return $goods;
  37. });
  38. $service->show()->score()->with(['all_score_sku_prices']);
  39. if ($ids) {
  40. $service->whereIds($ids);
  41. }
  42. $goods = $service->select();
  43. $this->success('获取成功', $goods);
  44. }
  45. public function detail()
  46. {
  47. $user = auth_user();
  48. $id = $this->request->param('id');
  49. $service = new GoodsService(function ($goods, $service) {
  50. $goods->score = $goods->score;
  51. $goods->service = $goods->service;
  52. $goods->skus = $goods->skus;
  53. return $goods;
  54. });
  55. $goods = $service->show()->score()->where('id', $id)->find();
  56. if (!$goods) {
  57. $this->error(__('No Results were found'));
  58. }
  59. // 添加浏览记录
  60. GoodsLog::addView($user, $goods);
  61. // 处理商品规格
  62. $skuPrices = $goods['new_sku_prices'];
  63. $content = $goods['content'];
  64. $goods = $goods->toArray();
  65. $goods['sku_prices'] = $skuPrices;
  66. $goods['content'] = $content;
  67. unset($goods['new_sku_prices']);
  68. $this->success('获取成功', $goods);
  69. }
  70. }