ProductController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace App\Http\Controllers\V1;
  3. use App\Models\Product;
  4. use App\Models\ProductType;
  5. use App\Models\ReportLog;
  6. use App\Models\UserCollect;
  7. use App\Models\UserLike;
  8. use Illuminate\Http\Request;
  9. /**
  10. *
  11. * 产品
  12. */
  13. class ProductController extends Controller
  14. {
  15. public function __construct()
  16. {
  17. $this->user = auth('api')->user();
  18. $this->userId = $this->user ? $this->user->id : 0;
  19. //如果用户被删除,会自动退出登录
  20. if (!empty($this->user->deleted_at)) {
  21. $this->user->online = 0;
  22. $this->user->save();
  23. auth('api')->logout();
  24. }
  25. }
  26. /**
  27. * @param Request $request
  28. * @return \Illuminate\Http\JsonResponse
  29. *分类数据筛选
  30. */
  31. public function filterTypeList(Request $request){
  32. $pid = $request->input('pid');
  33. $map = [];
  34. $maps = [];
  35. if($pid){
  36. $map[] = ['pid','=',$pid];
  37. $maps[] = ['filter_display_pid','=',$pid];
  38. }
  39. $list = ProductType::query()
  40. ->where('status',1)
  41. ->where($map)
  42. ->orWhere($maps)
  43. ->get();
  44. $data = [];
  45. foreach ($list as $v){
  46. $data[] =[
  47. 'id' => $v['id'],
  48. 'icon' => $v['icon'],
  49. 'zh_name' => $v['zh_alias'] != null?$v['zh_alias']:$v['zh_name'],
  50. 'ko_name' => $v['ko_alias'] != null?$v['ko_alias']:$v['ko_name'],
  51. 'pid' => $v['filter_display_pid'] > 0?$v['filter_display_pid']:$v['pid'],
  52. 'is_display' => $v['is_filter_display']
  53. ];
  54. }
  55. return $this->success($this->recursion($data,$pid?$pid:0));
  56. }
  57. /**
  58. * @param Request $request
  59. * @return \Illuminate\Http\JsonResponse
  60. * 分类数据上传
  61. */
  62. public function uploadTypeList(Request $request){
  63. $pid = $request->input('pid');
  64. $map = [];
  65. $maps = [];
  66. if($pid){
  67. $map[] = ['pid','=',$pid];
  68. $maps[] = ['upload_display_pid','=',$pid];
  69. }
  70. $list = ProductType::query()
  71. ->where('status',1)
  72. ->where($map)
  73. ->orWhere($maps)
  74. ->get();
  75. $data = [];
  76. foreach ($list as $v){
  77. $data[] =[
  78. 'id' => $v['id'],
  79. 'icon' => $v['icon'],
  80. 'zh_name' => $v['zh_alias'] != null?$v['zh_alias']:$v['zh_name'],
  81. 'ko_name' => $v['ko_alias'] != null?$v['ko_alias']:$v['ko_name'],
  82. 'pid' => $v['upload_display_pid'] > 0?$v['upload_display_pid']:$v['pid'],
  83. 'is_display' => $v['is_upload_display']
  84. ];
  85. }
  86. return $this->success($this->recursion($data,$pid?$pid:0));
  87. }
  88. /**
  89. * @param $list
  90. * @param $pid
  91. * @return array
  92. * 递归处理
  93. */
  94. public function recursion($list = [],$pid = 0){
  95. $child = [];
  96. foreach ($list as $value) {
  97. if($value['is_display'] != 0){
  98. if ($value['pid'] == $pid ) {
  99. if($this->recursion($list, $value['id'])){
  100. // 递归调用,查找当前数据的子级
  101. $value['child'] = $this->recursion($list, $value['id']);
  102. }
  103. // 把子级数据添加进数组
  104. $child[] = $value;
  105. }
  106. }
  107. }
  108. return $child;
  109. }
  110. /**
  111. * @return void
  112. * 添加产品
  113. */
  114. public function addProduct(Request $request){
  115. $params = $request->all();
  116. if(empty($params)){
  117. return $this->error("数据不能为空!");
  118. }
  119. $params['user_id'] = $this->userId; // 用户ID
  120. $res = Product::query()->create($params);
  121. if(!$res){
  122. return $this->error("上传失败!");
  123. }
  124. return $this->success();
  125. }
  126. /**
  127. * @return void
  128. * 我的上传列表
  129. */
  130. public function userProductList(Request $request){
  131. $limit = $request->get('limit',10);
  132. $list = Product::query()->where('status','=',1)
  133. ->where('user_id','=',$this->userId)
  134. ->orderByDesc('id')
  135. ->paginate($limit);
  136. return $this->success($this->page($list));
  137. }
  138. /**
  139. * @param Request $request
  140. * @return \Illuminate\Http\JsonResponse
  141. * 产品列
  142. */
  143. public function productList(Request $request){
  144. $limit = $request->get('limit',10);
  145. $type = $request->get('type');
  146. $query = Product::query()->with('user:id,name,nickname,avatar,company_name,company_card_color,production_project');
  147. if(!empty($type)){
  148. $query->whereIn('type',$type);
  149. }
  150. $list = $query->where('status',1)->paginate($limit);
  151. return $this->success($this->page($list));
  152. }
  153. /**
  154. * @return void
  155. * 产品详情
  156. */
  157. public function productDetail(Request $request){
  158. $data = Product::query()
  159. ->with('user:id,name,nickname,avatar,company_name,company_url,production_project')
  160. ->select("id","name","user_id","content","image","type")->first();
  161. return $this->success($data);
  162. }
  163. /**
  164. * @return void
  165. * 添加收藏
  166. */
  167. public function addCollect(Request $request){
  168. $product = Product::query()->where('id',$request->get('product_id'))->first();
  169. if(!$product){
  170. return $this->error("商品不存在!");
  171. }
  172. $collect = UserCollect::query()->where('product_id',$product->id)->where('user_id',$this->userId)->first();
  173. if($collect){
  174. return $this->error("您已收藏过了!");
  175. }
  176. $data =[
  177. 'product_id' => $product->id,
  178. 'user_id' => $this->userId,
  179. ];
  180. $res = UserCollect::query()->create($data);
  181. if(!$res){
  182. return $this->error("收藏失败!");
  183. }
  184. return $this->success();
  185. }
  186. /**
  187. * @return void
  188. * 添加喜欢
  189. */
  190. public function addLike(Request $request){
  191. $product = Product::query()->where('id',$request->get('product_id'))->first();
  192. if(!$product){
  193. return $this->error("商品不存在!");
  194. }
  195. $collect = UserLike::query()->where('product_id',$product->id)->where('user_id',$this->userId)->first();
  196. if($collect){
  197. return $this->error("您已点赞过了!");
  198. }
  199. $data =[
  200. 'product_id' => $product->id,
  201. 'user_id' => $this->userId,
  202. ];
  203. $res = UserLike::query()->create($data);
  204. if(!$res){
  205. return $this->error("操作失败!");
  206. }
  207. return $this->success();
  208. }
  209. /**
  210. * @return void
  211. * 收藏列表
  212. */
  213. public function collectList(Request $request){
  214. $limit = $request->get('limit',10);
  215. $list = UserCollect::query()
  216. ->with('product:id,name,image')
  217. ->where('user_id',$this->userId)
  218. ->where('is_arrange','=',0)
  219. ->select("id","product_id")
  220. ->paginate($limit);
  221. return $this->success($this->page($list));
  222. }
  223. /**
  224. * @return void
  225. * 举报图片
  226. */
  227. public function report(Request $request){
  228. $params = $request->all();
  229. if(empty($params['product_id'])){
  230. return $this->error("举报项不能为空!");
  231. }
  232. if(empty($params['report_id'])){
  233. return $this->error("举报问题不能为空!");
  234. }
  235. $params['user_id'] = $this->userId;
  236. $res = ReportLog::query()->create($params);
  237. if(!$res){
  238. return $this->error("举报失败!");
  239. }
  240. return $this->success();
  241. }
  242. }