Shop.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\common\model\ShopGoodsModel;
  4. use app\common\service\ShopGoodsMenuService;
  5. use app\common\service\ShopGoodsService;
  6. use app\Request;
  7. use think\Exception;
  8. use think\facade\Db;
  9. /**
  10. * 商城 by wes
  11. * Class Shop
  12. * @package app\api\controller\v1
  13. */
  14. class Shop
  15. {
  16. /**
  17. * 商品列表
  18. */
  19. public function goodsList(Request $request)
  20. {
  21. try {
  22. $pageSize = $request->post('limit', 10);
  23. $list = ShopGoodsService::make()->getList($request->all(), $pageSize);
  24. return api_succ_return(['msg'=>'成功', 'data'=> isset($list['data'])? $list['data'] : []]);
  25. }catch(Exception $e){
  26. return api_error_return('获取失败:'.$e->getMessage());
  27. }
  28. }
  29. /**
  30. * 专区商品菜单
  31. * @param Request $request
  32. * @return \think\Response
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function goodsMenu(Request $request)
  38. {
  39. try {
  40. $list = ShopGoodsMenuService::make()->getList('',999);
  41. return api_succ_return(['msg'=>'成功', 'data'=> isset($list['data'])? $list['data']:[]]);
  42. } catch (\Exception $e){
  43. return api_error_return('获取失败:'.$e->getMessage());
  44. }
  45. }
  46. /**
  47. * 商品详情
  48. * @param Request $request
  49. * @return \think\Response
  50. */
  51. public function goodsDetail (Request $request)
  52. {
  53. try {
  54. $goodsSn = $request->only(['goods_sn']);
  55. if(empty($goodsSn)){
  56. return api_error_return('参数错误');
  57. }
  58. $data = ShopGoodsService::make()->getDetail($request->all());
  59. if (empty($data)){
  60. return api_error_return('请求失败');
  61. }
  62. // 更新浏览量
  63. ShopGoodsService::make()->updateScanCount($data['goods_id']);
  64. return api_succ_return(['msg'=>'获取成功', 'data'=> $data]);
  65. } catch (\Exception $e) {
  66. return api_error_return($e->getMessage());
  67. }
  68. }
  69. /**
  70. * 收藏商品
  71. * @param Request $request
  72. * @return \think\Response
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. public function goodsAttension (Request $request)
  78. {
  79. $post = $request->post();
  80. $goodsId = isset($post['goods_id'])? intval($post['goods_id']) : 0;
  81. try {
  82. if (ShopGoodsService::make()->goodsAttension($request->uid, $goodsId)){
  83. return api_succ_return('收藏成功');
  84. }
  85. } catch (\Exception $e){
  86. return api_succ_return('失败:'.$e->getMessage());
  87. }
  88. }
  89. /**
  90. * 是否收藏过商品
  91. * @param Request $request
  92. * @return \think\Response
  93. * @throws \think\db\exception\DataNotFoundException
  94. * @throws \think\db\exception\DbException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. */
  97. public function isAttensionGoods(Request $request){
  98. $post = $request->post();
  99. $goodsId = isset($post['goods_id'])? intval($post['goods_id']) : 0;
  100. $isAttension = ShopGoodsService::make()->checkAttension($request->uid, $goodsId);
  101. return api_succ_return(['msg'=>'成功', 'data'=>['attension'=> $isAttension?1:2]]);
  102. }
  103. /**
  104. * 购物车推荐商品
  105. * @param Request $request
  106. * @return \think\Response
  107. * @throws \think\db\exception\DataNotFoundException
  108. * @throws \think\db\exception\DbException
  109. * @throws \think\db\exception\ModelNotFoundException
  110. */
  111. public function storeRecommonGoods(Request $request){
  112. $field = 'sort,category,goods_sn,goods_name,goods_img,min_original_price as original_price,min_price as price,rebate_score,sales_volume,inventory,attension_count,scan_count';
  113. $list = ShopGoodsService::make()->getList(['sort'=> 'scan_count desc'], 99, $field);
  114. return api_succ_return(['msg'=>'成功', 'data'=>$list]);
  115. }
  116. }