ProductController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 void
  134. * 添加产品
  135. */
  136. public function addProduct(Request $request){
  137. $params = $request->all();
  138. if(empty($params)){
  139. return $this->error("数据不能为空!");
  140. }
  141. if(empty($params['type'])){
  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. $type = array_map('intval', explode(',',$type));
  196. $query->whereJsonContains('type',$type);
  197. }
  198. if(!empty($keyword)){
  199. $query->where('name','like','%'.$keyword.'%');
  200. }
  201. $list = $query->where('status',1)
  202. ->paginate(intval($limit));
  203. foreach ($list as $v){
  204. $v['is_collect'] = 0;
  205. if(!empty($this->userId)){
  206. $v['is_collect'] = UserCollect::query()->where('product_id',$v['id'])->where('user_id',$this->userId)->count();
  207. }
  208. }
  209. return $this->success(pages($list,$go));
  210. }
  211. /**
  212. * @return void
  213. * 产品详情
  214. */
  215. public function productDetail(Request $request){
  216. $data = Product::query()
  217. ->with('user:id,name,nickname,avatar,company_name,company_url,production_project')
  218. ->where('status',1)
  219. ->where('id',$request->get('id'))
  220. ->select("id","name","user_id","content","image","type","url")->first();
  221. if(!$data){
  222. return $this->error("数据不存在!");
  223. }
  224. $data['is_collect'] = 0; // 收藏
  225. $data['is_follow'] = 0; // 关注
  226. $data['is_like'] = 0; // 喜欢
  227. if(!empty($this->userId)){
  228. $data['is_collect'] = UserCollect::query()->where('product_id',$data['id'])->where('user_id',$this->userId)->count();
  229. $data['is_follow'] = UserFollow::query()->where('to_user_id',$data['user_id'])->where('user_id',$this->userId)->count();
  230. $data['is_like'] = UserLike::query()->where('product_id',$data['id'])->where('user_id',$this->userId)->count();
  231. }
  232. if(!empty($data['user'])){
  233. $data['user']['follow_count'] = UserFollow::query()->where('user_id',$data['user_id'])->count();
  234. }
  235. return $this->success($data);
  236. }
  237. /**
  238. * @param Request $request
  239. * @return void
  240. * 删除产品
  241. */
  242. public function delProduct(Request $request){
  243. $id = $request->get('id');
  244. if(empty($id)){
  245. return $this->error("缺少参数ID!");
  246. }
  247. $product = Product::query()->where('id',$id)->first();
  248. if(!$product){
  249. return $this->error("产品不存在!");
  250. }
  251. $product->delete();
  252. return $this->success();
  253. }
  254. /**
  255. * @return void
  256. * 添加收藏
  257. */
  258. public function addCollect(Request $request){
  259. $product = Product::query()->where('id',$request->get('product_id'))->first();
  260. if(!$product){
  261. return $this->error("商品不存在!");
  262. }
  263. $collect = UserCollect::query()->where('product_id',$product->id)->where('user_id',$this->userId)->first();
  264. if($collect){
  265. return $this->error("您已收藏过了!");
  266. }
  267. $data =[
  268. 'product_id' => $product->id,
  269. 'user_id' => $this->userId,
  270. ];
  271. $res = UserCollect::query()->create($data);
  272. if(!$res){
  273. return $this->error("收藏失败!");
  274. }
  275. $user = User::query()->where('id',$this->userId)->first();
  276. $product_type = ProductType::query()->whereIn('id',$product['type'])->select('id','zh_name','ko_name')->first();
  277. $type_name = '';
  278. if(!empty($product_type)){
  279. $type_name = $product_type['zh_name'];
  280. }
  281. $msg = [
  282. 'type' => 2,//下载通知
  283. 'title' => "下载通知",
  284. 'content' => "您收藏了".$type_name."中的1张图片",
  285. 'user_id' => $this->userId,
  286. 'to_user_id' => $this->userId,
  287. ];
  288. Msg::query()->create($msg); // 添加通知消息
  289. $msg1 = [
  290. 'type' => 3,//保存通知
  291. 'title' => "保存通知",
  292. 'content' => $user->name."收藏了您".$type_name."中的1张图片",
  293. 'user_id' => $this->userId,
  294. 'to_user_id' => $product['user_id'],
  295. ];
  296. Msg::query()->create($msg1); // 添加通知消息
  297. return $this->success();
  298. }
  299. /**
  300. * @return void
  301. * 取消收藏
  302. */
  303. public function cancelCollect(Request $request){
  304. $product = Product::query()->where('id',$request->get('product_id'))->first();
  305. if(!$product){
  306. return $this->error("产品不存在!");
  307. }
  308. $collect = UserCollect::query()->where('product_id',$product->id)->where('user_id',$this->userId)->first();
  309. if($collect){
  310. $collect->delete();
  311. }
  312. return $this->success();
  313. }
  314. /**
  315. * @return void
  316. * 添加喜欢
  317. */
  318. public function addLike(Request $request){
  319. $product = Product::query()->where('id',$request->get('product_id'))->first();
  320. if(!$product){
  321. return $this->error("数据不存在!");
  322. }
  323. $collect = UserLike::query()->where('product_id',$product->id)->where('user_id',$this->userId)->first();
  324. if($collect){
  325. return $this->error("您已点赞过了!");
  326. }
  327. $data =[
  328. 'product_id' => $product->id,
  329. 'user_id' => $this->userId,
  330. ];
  331. $res = UserLike::query()->create($data);
  332. if(!$res){
  333. return $this->error("操作失败!");
  334. }
  335. $user = User::query()->where('id',$this->userId)->first();
  336. $product_type = ProductType::query()->whereIn('id',$product['type'])->select('id','zh_name','ko_name')->first();
  337. $type_name = '';
  338. if(!empty($product_type)){
  339. $type_name = $product_type['zh_name'];
  340. }
  341. $msg = [
  342. 'type' => 1,//喜欢通知
  343. 'title' => "喜欢通知",
  344. 'content' => $user->name."点赞了您".$type_name."中的1张图片",
  345. 'user_id' => $this->userId,
  346. 'to_user_id' => $product['user_id']
  347. ];
  348. Msg::query()->create($msg); // 添加通知消息
  349. return $this->success();
  350. }
  351. /**
  352. * @return void
  353. * 收藏列表
  354. */
  355. public function collectList(Request $request){
  356. $limit = $request->get('limit',10);
  357. $go = $request->get('go',6);
  358. $user_id = $request->get('user_id');
  359. if(empty($user_id)){
  360. $user_id = $this->userId;
  361. }
  362. $list = UserCollect::query()
  363. ->with('product:id,name,image,url')
  364. ->whereHas('product',function ($query){
  365. $query->where('id','>',0);
  366. })
  367. ->where('user_id',$user_id)
  368. ->where('is_arrange','=',0)
  369. ->select("id","product_id")
  370. ->paginate($limit);
  371. return $this->success(pages($list,$go));
  372. }
  373. /**
  374. * @return void
  375. * 举报列表
  376. */
  377. public function reportList(){
  378. $list = Report::query()->where('status',1)->orderByDesc("sort")->get();
  379. return $this->success($list);
  380. }
  381. /**
  382. * @return void
  383. * 举报图片
  384. */
  385. public function report(Request $request){
  386. $params = $request->all();
  387. if(empty($params['product_id'])){
  388. return $this->error("举报项不能为空!");
  389. }
  390. if(empty($params['report_id'])){
  391. return $this->error("举报问题不能为空!");
  392. }
  393. $params['user_id'] = $this->userId;
  394. $res = ReportLog::query()->create($params);
  395. if(!$res){
  396. return $this->error("举报失败!");
  397. }
  398. return $this->success();
  399. }
  400. }