ProductController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. <?php
  2. namespace App\Http\Controllers\V1;
  3. use App\Models\Msg;
  4. use App\Models\Product;
  5. use App\Models\ProductType;
  6. use App\Models\Report;
  7. use App\Models\ReportLog;
  8. use App\Models\User;
  9. use App\Models\UserCollect;
  10. use App\Models\UserFollow;
  11. use App\Models\UserLike;
  12. use Illuminate\Http\Request;
  13. /**
  14. *
  15. * 产品
  16. */
  17. class ProductController extends Controller
  18. {
  19. public function __construct()
  20. {
  21. $this->user = auth('api')->user();
  22. $this->userId = $this->user ? $this->user->id : 0;
  23. //如果用户被删除,会自动退出登录
  24. if (!empty($this->user->deleted_at)) {
  25. $this->user->online = 0;
  26. $this->user->save();
  27. auth('api')->logout();
  28. }
  29. }
  30. /**
  31. * @param Request $request
  32. * @return \Illuminate\Http\JsonResponse
  33. *分类数据筛选
  34. */
  35. public function filterTypeList(Request $request){
  36. $pid = $request->input('pid');
  37. $map = [];
  38. $maps = [];
  39. if($pid){
  40. $map[] = ['pid','=',$pid];
  41. $maps[] = ['filter_display_pid','=',$pid];
  42. }
  43. $list = ProductType::query()
  44. ->where('status',1)
  45. ->where($map)
  46. ->orWhere($maps)
  47. ->orderByDesc("sort")
  48. ->get();
  49. $data = [];
  50. foreach ($list as $v){
  51. if($v['filter_display_pid'] === 0){
  52. $v['pid'] = 0;
  53. }elseif ($v['filter_display_pid'] > 0){
  54. $v['pid'] = $v['filter_display_pid'];
  55. }else{
  56. $v['pid'] = $v['pid'];
  57. }
  58. $data[] = [
  59. 'id' => $v['id'],
  60. 'icon' => $v['icon'],
  61. 'zh_name' => $v['zh_alias'] != null?$v['zh_alias']:$v['zh_name'],
  62. 'ko_name' => $v['ko_alias'] != null?$v['ko_alias']:$v['ko_name'],
  63. 'pid' => $v['pid'],
  64. 'is_display' => $v['is_filter_display'],
  65. 'product_count' => Product::query()->whereJsonContains('type',[$v['id']])->where('status',1)->count()
  66. ];
  67. }
  68. return $this->success($this->recursion($data,$pid?$pid:0));
  69. }
  70. /**
  71. * @param Request $request
  72. * @return \Illuminate\Http\JsonResponse
  73. * 分类数据上传
  74. */
  75. public function uploadTypeList(Request $request){
  76. $pid = $request->input('pid');
  77. $map = [];
  78. $maps = [];
  79. if($pid){
  80. $map[] = ['pid','=',$pid];
  81. $maps[] = ['upload_display_pid','=',$pid];
  82. }
  83. $list = ProductType::query()
  84. ->where('status',1)
  85. ->where($map)
  86. ->orWhere($maps)
  87. ->orderByDesc("sort")
  88. ->get();
  89. $data = [];
  90. foreach ($list as $v){
  91. if($v['upload_display_pid'] === 0){
  92. $v['pid'] = 0;
  93. }elseif ($v['upload_display_pid'] > 0){
  94. $v['pid'] = $v['upload_display_pid'];
  95. }else{
  96. $v['pid'] = $v['pid'];
  97. }
  98. $data[] = [
  99. 'id' => $v['id'],
  100. 'icon' => $v['icon'],
  101. 'zh_name' => $v['zh_name'],
  102. 'ko_name' => $v['ko_name'],
  103. 'describe' => $v['describe'],
  104. 'pid' => $v['pid'],
  105. 'is_display' => $v['is_upload_display']
  106. ];
  107. }
  108. return $this->success($this->recursion($data,$pid?$pid:0));
  109. }
  110. /**
  111. * @param $list
  112. * @param $pid
  113. * @return array
  114. * 递归处理
  115. */
  116. public function recursion($list = [],$pid = 0){
  117. $child = [];
  118. foreach ($list as $value) {
  119. if($value['is_display'] != 0){
  120. if ($value['pid'] == $pid ) {
  121. if($this->recursion($list, $value['id'])){
  122. // 递归调用,查找当前数据的子级
  123. $value['child'] = $this->recursion($list, $value['id']);
  124. }
  125. // 把子级数据添加进数组
  126. $child[] = $value;
  127. }
  128. }
  129. }
  130. return $child;
  131. }
  132. /**
  133. * @return mixed
  134. * 添加产品
  135. */
  136. public function addProduct(Request $request){
  137. $params = $request->all();
  138. if(empty($params['type'])){
  139. return $this->error("分类不能能为空!");
  140. }
  141. if(empty($params['url']) && empty($params['name']) && empty($params['content'])){
  142. return $this->error("数据不能为空!");
  143. }
  144. $params['user_id'] = $this->userId; // 用户ID
  145. $res = Product::query()->create($params);
  146. if(!$res){
  147. return $this->error("上传失败!");
  148. }
  149. $follow = UserFollow::query()->where('to_user_id',$this->userId)->pluck('user_id')->toArray();
  150. if(!empty($follow)){
  151. $user = User::query()->where('id',$this->userId)->first();
  152. foreach ($follow as $v){
  153. $msg = [
  154. 'type' => 5,//关注的人发了新作品
  155. 'title' => "关注的人发了新作品",
  156. 'content' => " 您关注的".$user->name."上传了1张图片",
  157. 'user_id' => $this->userId,
  158. 'to_user_id' => $v,
  159. ];
  160. Msg::query()->create($msg); // 添加通知消息
  161. }
  162. }
  163. return $this->success($res);
  164. }
  165. /**
  166. * @return void
  167. * 我的上传列表
  168. */
  169. public function userProductList(Request $request){
  170. $limit = $request->get('limit',10);
  171. $go = $request->get('go',6);
  172. $user_id = $request->get('user_id');
  173. if(empty($user_id)){
  174. $user_id = $this->userId;
  175. }
  176. $list = Product::query()->where('status','=',1)
  177. ->where('user_id','=',$user_id)
  178. ->orderByDesc('id')
  179. ->paginate($limit);
  180. return $this->success(pages($list,$go));
  181. }
  182. /**
  183. * @param Request $request
  184. * @return \Illuminate\Http\JsonResponse
  185. * 产品列表
  186. */
  187. public function productList(Request $request){
  188. $limit = $request->get('limit',10);
  189. $type = $request->get('type');
  190. $keyword = $request->get('keyword');
  191. $go = $request->get('go',6);
  192. $query = Product::query()
  193. ->with('user:id,name,nickname,avatar,company_name,company_card_color,production_project');
  194. if(!empty($type)){
  195. $arr = explode(',',$type);
  196. foreach ($arr as $v){
  197. $query->orWhereJsonContains('type',[intval($v)]);
  198. }
  199. }
  200. $query->whereHas('user',function ($q){
  201. $q->where('is_stop',0);
  202. });
  203. if(!empty($keyword)){
  204. $query->where('name','like','%'.$keyword.'%');
  205. }
  206. $list = $query->where('status',1)
  207. ->orderByDesc("id")
  208. // ->toSql();
  209. // echo $list;
  210. ->paginate(intval($limit));
  211. foreach ($list as $v){
  212. $v['is_collect'] = 0;
  213. if(!empty($this->userId)){
  214. $v['is_collect'] = UserCollect::query()->where('product_id',$v['id'])->where('user_id',$this->userId)->count();
  215. }
  216. }
  217. return $this->success(pages($list,$go));
  218. }
  219. /**
  220. * @return void
  221. * 产品详情
  222. */
  223. public function productDetail(Request $request){
  224. $data = Product::query()
  225. ->with('user:id,name,nickname,avatar,company_name,company_url,production_project')
  226. ->where('status',1)
  227. ->where('id',$request->get('id'))
  228. ->whereHas('user',function ($q){
  229. $q->where('is_stop',0);
  230. })
  231. ->select("id","name","user_id","content","image","type","url")->first();
  232. if(!$data){
  233. return $this->error("数据不存在!");
  234. }
  235. $data['is_collect'] = 0; // 收藏
  236. $data['is_follow'] = 0; // 关注
  237. $data['is_like'] = 0; // 喜欢
  238. if(!empty($this->userId)){
  239. $data['is_collect'] = UserCollect::query()->where('product_id',$data['id'])->where('user_id',$this->userId)->count();
  240. $data['is_follow'] = UserFollow::query()->where('to_user_id',$data['user_id'])->where('user_id',$this->userId)->count();
  241. $data['is_like'] = UserLike::query()->where('product_id',$data['id'])->where('user_id',$this->userId)->count();
  242. }
  243. if(!empty($data['user'])){
  244. $data['user']['follow_count'] = UserFollow::query()->where('user_id',$data['user_id'])->count();
  245. }
  246. return $this->success($data);
  247. }
  248. /**
  249. * 获取推荐信息
  250. *
  251. * @param Request $request
  252. * @return \Illuminate\Http\JsonResponse
  253. */
  254. public function getProductRecommend(Request $request){
  255. $type = Product::where('id',$request->get('id'))->where('status',1)->value('type');
  256. $recommend = Product::with('user:id,name,nickname,avatar,company_name,company_url,production_project');
  257. foreach ($type ?? [] as $v){
  258. $recommend->orWhereJsonContains('type',[intval($v)]);
  259. }
  260. $recommend->where('status',1)
  261. ->select("id","name","user_id","content","image","type","url")
  262. ->where('id','<>',$request->get('id'))
  263. ->whereHas('user',function ($q){
  264. $q->where('is_stop',0);
  265. });
  266. $data = $recommend->orderBy('id','desc')->paginate()->toArray();
  267. foreach ($data['data'] ?? [] as $key => $value){
  268. if ($value['id'] == $request->get('id')){
  269. unset($data['data'][$key]);
  270. }
  271. }
  272. $data['data'] = array_values($data['data']);
  273. if (empty($data['data'])){
  274. $data = Product::with('user:id,name,nickname,avatar,company_name,company_url,production_project')
  275. ->where('status',1)
  276. ->where('id','<>',$request->get('id'))
  277. ->select("id","name","user_id","content","image","type","url")
  278. ->whereHas('user',function ($q){
  279. $q->where('is_stop',0);
  280. })
  281. ->orderByRaw('rand()')->paginate();
  282. }
  283. return $this->success($data);
  284. }
  285. /**
  286. * @param Request $request
  287. * @return void
  288. * 删除产品
  289. */
  290. public function delProduct(Request $request){
  291. $id = $request->get('id');
  292. if(empty($id)){
  293. return $this->error("缺少参数ID!");
  294. }
  295. $product = Product::query()->where('id',$id)->first();
  296. if(!$product){
  297. return $this->error("产品不存在!");
  298. }
  299. $product->delete();
  300. return $this->success();
  301. }
  302. /**
  303. * @return void
  304. * 添加收藏
  305. */
  306. public function addCollect(Request $request){
  307. $product = Product::query()->where('id',$request->get('product_id'))->first();
  308. if(!$product){
  309. return $this->error("商品不存在!");
  310. }
  311. $collect = UserCollect::query()->where('product_id',$product->id)->where('user_id',$this->userId)->first();
  312. if($collect){
  313. return $this->error("您已收藏过了!");
  314. }
  315. $data =[
  316. 'product_id' => $product->id,
  317. 'user_id' => $this->userId,
  318. ];
  319. $res = UserCollect::query()->create($data);
  320. if(!$res){
  321. return $this->error("收藏失败!");
  322. }
  323. $user = User::query()->where('id',$this->userId)->first();
  324. $product_type = ProductType::query()->whereIn('id',$product['type'])->select('id','zh_name','ko_name')->first();
  325. $type_name = '';
  326. if(!empty($product_type)){
  327. $type_name = $product_type['zh_name'];
  328. }
  329. $msg = [
  330. 'type' => 2,//下载通知
  331. 'title' => "下载通知",
  332. 'content' => "您收藏了".$type_name."中的1张图片",
  333. 'user_id' => $this->userId,
  334. 'to_user_id' => $this->userId,
  335. ];
  336. Msg::query()->create($msg); // 添加通知消息
  337. $msg1 = [
  338. 'type' => 3,//保存通知
  339. 'title' => "保存通知",
  340. 'content' => $user->name."收藏了您".$type_name."中的1张图片",
  341. 'user_id' => $this->userId,
  342. 'to_user_id' => $product['user_id'],
  343. ];
  344. Msg::query()->create($msg1); // 添加通知消息
  345. return $this->success();
  346. }
  347. /**
  348. * @return void
  349. * 取消收藏
  350. */
  351. public function cancelCollect(Request $request){
  352. $product = Product::query()->where('id',$request->get('product_id'))->first();
  353. if(!$product){
  354. return $this->error("产品不存在!");
  355. }
  356. $collect = UserCollect::query()->where('product_id',$product->id)->where('user_id',$this->userId)->first();
  357. if($collect){
  358. $collect->delete();
  359. }
  360. return $this->success();
  361. }
  362. /**
  363. * @return void
  364. * 添加喜欢
  365. */
  366. public function addLike(Request $request){
  367. $product = Product::query()->where('id',$request->get('product_id'))->first();
  368. if(!$product){
  369. return $this->error("数据不存在!");
  370. }
  371. $collect = UserLike::query()->where('product_id',$product->id)->where('user_id',$this->userId)->first();
  372. if($collect){
  373. return $this->error("您已点赞过了!");
  374. }
  375. $data = [
  376. 'product_id' => $product->id,
  377. 'user_id' => $this->userId,
  378. ];
  379. $res = UserLike::query()->create($data);
  380. if(!$res){
  381. return $this->error("操作失败!");
  382. }
  383. $user = User::query()->where('id',$this->userId)->first();
  384. $product_type = ProductType::query()->whereIn('id',$product['type'])->select('id','zh_name','ko_name')->first();
  385. $type_name = '';
  386. if(!empty($product_type)){
  387. $type_name = $product_type['zh_name'];
  388. }
  389. $msg = [
  390. 'type' => 1,//喜欢通知
  391. 'title' => "喜欢通知",
  392. 'content' => $user->name."点赞了您".$type_name."中的1张图片",
  393. 'user_id' => $this->userId,
  394. 'to_user_id' => $product['user_id']
  395. ];
  396. Msg::query()->create($msg); // 添加通知消息
  397. return $this->success();
  398. }
  399. /**
  400. * @return void
  401. * 收藏列表
  402. */
  403. public function collectList(Request $request){
  404. $limit = $request->get('limit',10);
  405. $go = $request->get('go',6);
  406. $user_id = $request->get('user_id');
  407. if(empty($user_id)){
  408. $user_id = $this->userId;
  409. }
  410. $list = UserCollect::query()
  411. ->with('product:id,name,image,url')
  412. ->whereHas('product',function ($query){
  413. $query->where('id','>',0);
  414. $query->whereHas('user',function ($q){
  415. $q->where('is_stop',0);
  416. });
  417. })
  418. ->where('user_id',$user_id)
  419. ->where('is_arrange','=',0)
  420. ->select("id","product_id")
  421. ->orderByDesc("id")
  422. ->paginate($limit);
  423. return $this->success(pages($list,$go));
  424. }
  425. /**
  426. * @return void
  427. * 举报列表
  428. */
  429. public function reportList(){
  430. $list = Report::query()->where('status',1)->orderByDesc("sort")->get();
  431. return $this->success($list);
  432. }
  433. /**
  434. * @return void
  435. * 举报图片
  436. */
  437. public function report(Request $request){
  438. $params = $request->all();
  439. if(empty($params['product_id'])){
  440. return $this->error("举报文件不能为空!");
  441. }
  442. if(empty($params['report_id'])){
  443. return $this->error("举报问题不能为空!");
  444. }
  445. $params['user_id'] = $this->userId;
  446. $res = ReportLog::query()->create($params);
  447. if(!$res){
  448. return $this->error("举报失败!");
  449. }
  450. return $this->success();
  451. }
  452. }