ProductController.php 19 KB

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