ProductController.php 13 KB

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