Goods.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace addons\shop\controller;
  3. use addons\shop\model\Category;
  4. use addons\shop\model\Collect;
  5. use addons\shop\model\Goods as GoodsModel;
  6. use addons\shop\model\CouponCondition;
  7. use addons\shop\model\UserCoupon;
  8. use addons\shop\model\Coupon;
  9. use addons\shop\model\SkuSpec;
  10. use addons\shop\model\Guarantee;
  11. use addons\shop\model\AttributeValue;
  12. use think\Config;
  13. /**
  14. * 详情控制器
  15. * Class Goods
  16. * @package addons\shop\controller
  17. */
  18. class Goods extends Base
  19. {
  20. protected $noNeedLogin = '*';
  21. protected $noNeedRight = '*';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. }
  26. /**
  27. * 商品详情页
  28. */
  29. public function index()
  30. {
  31. $id = $this->request->param('id/d');
  32. // 判断是否跳转移动端
  33. $this->checkredirect('goods/detail', ['id' => $id]);
  34. if (!$id) {
  35. $this->error('参数错误');
  36. }
  37. $row = (new GoodsModel())->with([
  38. 'Sku',
  39. 'Comment' => function ($query) {
  40. $query->where('status', 'normal')->field('id,goods_id,content,star,user_id,images,createtime')->with([
  41. 'User' => function ($u) {
  42. $u->field('id,nickname,avatar');
  43. }
  44. ]);
  45. }
  46. ])->where('status', '<>', 'hidden')->where('id', $id)->find();
  47. if (!$row) {
  48. $this->error('未找到该商品');
  49. }
  50. $row->setInc('views');
  51. $category = Category::get($row['category_id']);
  52. //收藏
  53. if ($this->auth->isLogin()) {
  54. $row->is_collect = !!(Collect::where('user_id', $this->auth->id)->where('goods_id', $id)->where('status', 1)->find());
  55. } else {
  56. $row->is_collect = false;
  57. }
  58. $row->sku_spec = SkuSpec::getGoodsSkuSpec($id);
  59. //$row->visible(explode(',', 'id,title,price,marketprice,sales,views,image,content,images,sku_spec,sku,comment,is_collect'));
  60. $sku_spec = collection($row->sku_spec)->toArray();
  61. array_multisort(array_column($sku_spec, 'id'), SORT_ASC, $sku_spec);
  62. $row->sku_spec = $sku_spec;
  63. //print_r(collection($sku)->toArray());exit;
  64. $priceList = [];
  65. foreach ($row->sku as $index => $item) {
  66. $priceList[$item['sku_id']] = $item;
  67. }
  68. //评论列表
  69. $commentList = $row->comment()->relation([
  70. 'reply' => function ($query) {
  71. $query->with([
  72. 'manage' => function ($u) {
  73. $u->field('id,nickname');
  74. }
  75. ]);
  76. }
  77. ])->where('status', 'normal')->paginate([
  78. 'path' => 'javascript:load_comment_list([PAGE]);'
  79. ]);
  80. foreach ($commentList as &$item) {
  81. if ($item['user']) {
  82. $item['user']['avatar'] = cdnurl($item['user']['avatar'], true);
  83. }
  84. }
  85. unset($item);
  86. //服务保障
  87. $guarantee = [];
  88. if ($row->guarantee_ids) {
  89. $guarantee = Guarantee::where('id', 'IN', $row->guarantee_ids)->where('status', 'normal')->select();
  90. }
  91. $this->view->assign('guarantee', $guarantee);
  92. //属性
  93. $attributes = AttributeValue::getAttributeList($row->attribute_ids);
  94. $this->view->assign('attributes', $attributes);
  95. //优惠券
  96. $conditions = CouponCondition::getGoodsCondition($id, $row->category_id, $row->brand_id);
  97. $sql = "condition_ids IS NULL OR condition_ids=''";
  98. foreach ($conditions as $key => $item) {
  99. $sql .= " OR FIND_IN_SET('{$item['id']}',condition_ids)";
  100. }
  101. $couponList = Coupon::where($sql)
  102. ->where('is_open', 1)
  103. ->where('is_private', 'no')
  104. ->where('endtime', '>', time())
  105. ->limit(3)
  106. ->select();
  107. //已经登录,渲染已领的优惠券
  108. $coupon_ids = [];
  109. if ($this->auth->isLogin()) {
  110. $coupon_ids = UserCoupon::where('user_id', $this->auth->id)->column('coupon_id');
  111. }
  112. foreach ($couponList as $key => &$item) {
  113. Coupon::render($item, $coupon_ids);
  114. }
  115. $this->view->assign('couponList', $couponList);
  116. $this->view->assign('commentList', $commentList);
  117. $this->view->assign('priceList', $priceList);
  118. $this->view->assign('__category__', $category);
  119. $this->view->assign('__goods__', $row);
  120. Config::set('shop.title', isset($row['seotitle']) && $row['seotitle'] ? $row['seotitle'] : $row['title']);
  121. Config::set('shop.keywords', $row['keywords']);
  122. Config::set('shop.description', $row['description']);
  123. Config::set('shop.image', cdnurl($row['image'], true));
  124. return $this->view->fetch('/show');
  125. }
  126. /**
  127. * 获取评论列表
  128. */
  129. public function get_comment_list()
  130. {
  131. $id = $this->request->post("id/d");
  132. $page = $this->request->post("page/d", 1);
  133. if (!$id || !$page) {
  134. $this->error("参数不正确");
  135. }
  136. $row = GoodsModel::get($id);
  137. if (!$row) {
  138. $this->error('未找到指定的商品');
  139. }
  140. //评论列表
  141. $commentList = $row->comment()->where('status', 'normal')->paginate([
  142. 'path' => 'javascript:load_comment_list([PAGE]);'
  143. ]);
  144. $this->success('', '', $this->fetch('common/comment', ['__goods__' => $row, 'commentList' => $commentList]));
  145. }
  146. }