ProductController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. ->get();
  48. $data = [];
  49. foreach ($list as $v){
  50. if($v['filter_display_pid'] === 0){
  51. $v['pid'] = 0;
  52. }elseif ($v['filter_display_pid'] > 0){
  53. $v['pid'] = $v['filter_display_pid'];
  54. }else{
  55. $v['pid'] = $v['pid'];
  56. }
  57. $data[] = [
  58. 'id' => $v['id'],
  59. 'icon' => $v['icon'],
  60. 'zh_name' => $v['zh_alias'] != null?$v['zh_alias']:$v['zh_name'],
  61. 'ko_name' => $v['ko_alias'] != null?$v['ko_alias']:$v['ko_name'],
  62. 'pid' => $v['pid'],
  63. 'is_display' => $v['is_filter_display']
  64. ];
  65. }
  66. return $this->success($this->recursion($data,$pid?$pid:0));
  67. }
  68. /**
  69. * @param Request $request
  70. * @return \Illuminate\Http\JsonResponse
  71. * 分类数据上传
  72. */
  73. public function uploadTypeList(Request $request){
  74. $pid = $request->input('pid');
  75. $map = [];
  76. $maps = [];
  77. if($pid){
  78. $map[] = ['pid','=',$pid];
  79. $maps[] = ['upload_display_pid','=',$pid];
  80. }
  81. $list = ProductType::query()
  82. ->where('status',1)
  83. ->where($map)
  84. ->orWhere($maps)
  85. ->get();
  86. $data = [];
  87. foreach ($list as $v){
  88. if($v['upload_display_pid'] === 0){
  89. $v['pid'] = 0;
  90. }elseif ($v['upload_display_pid'] > 0){
  91. $v['pid'] = $v['upload_display_pid'];
  92. }else{
  93. $v['pid'] = $v['pid'];
  94. }
  95. $data[] =[
  96. 'id' => $v['id'],
  97. 'icon' => $v['icon'],
  98. 'zh_name' => $v['zh_name'],
  99. 'ko_name' => $v['ko_name'],
  100. 'describe' => $v['describe'],
  101. 'pid' => $v['pid'],
  102. 'is_display' => $v['is_upload_display']
  103. ];
  104. }
  105. return $this->success($this->recursion($data,$pid?$pid:0));
  106. }
  107. /**
  108. * @param $list
  109. * @param $pid
  110. * @return array
  111. * 递归处理
  112. */
  113. public function recursion($list = [],$pid = 0){
  114. $child = [];
  115. foreach ($list as $value) {
  116. if($value['is_display'] != 0){
  117. if ($value['pid'] == $pid ) {
  118. if($this->recursion($list, $value['id'])){
  119. // 递归调用,查找当前数据的子级
  120. $value['child'] = $this->recursion($list, $value['id']);
  121. }
  122. // 把子级数据添加进数组
  123. $child[] = $value;
  124. }
  125. }
  126. }
  127. return $child;
  128. }
  129. /**
  130. * @return void
  131. * 添加产品
  132. */
  133. public function addProduct(Request $request){
  134. $params = $request->all();
  135. if(empty($params)){
  136. return $this->error("数据不能为空!");
  137. }
  138. if(empty($params['type'])){
  139. return $this->error("分类不能能为空!");
  140. }
  141. $params['user_id'] = $this->userId; // 用户ID
  142. $res = Product::query()->create($params);
  143. if(!$res){
  144. return $this->error("上传失败!");
  145. }
  146. $follow = UserFollow::query()->where('to_user_id',$this->userId)->pluck('user_id')->toArray();
  147. if(!empty($follow)){
  148. $user = User::query()->where('id',$this->userId)->first();
  149. foreach ($follow as $v){
  150. $msg = [
  151. 'type' => 5,//关注的人发了新作品
  152. 'title' => "关注的人发了新作品",
  153. 'content' => " 您关注的".$user->name."上传了1张图片",
  154. 'user_id' => $this->userId,
  155. 'to_user_id' => $v,
  156. ];
  157. Msg::query()->create($msg); // 添加通知消息
  158. }
  159. }
  160. return $this->success($res);
  161. }
  162. /**
  163. * @return void
  164. * 我的上传列表
  165. */
  166. public function userProductList(Request $request){
  167. $limit = $request->get('limit',10);
  168. $go = $request->get('go',6);
  169. $list = Product::query()->where('status','=',1)
  170. ->where('user_id','=',$this->userId)
  171. ->orderByDesc('id')
  172. ->paginate($limit);
  173. return $this->success(pages($list,$go));
  174. }
  175. /**
  176. * @param Request $request
  177. * @return \Illuminate\Http\JsonResponse
  178. * 产品列表
  179. */
  180. public function productList(Request $request){
  181. $limit = $request->get('limit',10);
  182. $type = $request->get('type');
  183. $keyword = $request->get('keyword');
  184. $go = $request->get('go',6);
  185. $query = Product::query()
  186. ->with('user:id,name,nickname,avatar,company_name,company_card_color,production_project');
  187. if(!empty($type)){
  188. $type = array_map('intval', explode(',',$type));
  189. $query->whereJsonContains('type',$type);
  190. }
  191. if(!empty($keyword)){
  192. // dd($keyword);
  193. $query->where('name','like','%'.$keyword.'%');
  194. }
  195. $list = $query->where('status',1)
  196. ->paginate(intval($limit));
  197. foreach ($list as $v){
  198. $v['is_collect'] = 0;
  199. if(!empty($this->userId)){
  200. $v['is_collect'] = UserCollect::query()->where('product_id',$v['id'])->where('user_id',$this->userId)->count();
  201. }
  202. }
  203. return $this->success(pages($list,$go));
  204. }
  205. /**
  206. * @return void
  207. * 产品详情
  208. */
  209. public function productDetail(Request $request){
  210. $data = Product::query()
  211. ->with('user:id,name,nickname,avatar,company_name,company_url,production_project')
  212. ->where('status',1)
  213. ->where('id',$request->get('id'))
  214. ->select("id","name","user_id","content","image","type","url")->first();
  215. if(!$data){
  216. return $this->error("数据不存在!");
  217. }
  218. $data['is_collect'] = 0; // 收藏
  219. $data['is_follow'] = 0; // 关注
  220. $data['is_like'] = 0; // 喜欢
  221. if(!empty($this->userId)){
  222. $data['is_collect'] = UserCollect::query()->where('product_id',$data['id'])->where('user_id',$this->userId)->count();
  223. $data['is_follow'] = UserFollow::query()->where('to_user_id',$data['user_id'])->where('user_id',$this->userId)->count();
  224. $data['is_like'] = UserLike::query()->where('product_id',$data['id'])->where('user_id',$this->userId)->count();
  225. }
  226. $data['user']['follow_count'] = UserFollow::query()->where('user_id',$data['user_id'])->count();
  227. return $this->success($data);
  228. }
  229. /**
  230. * @param Request $request
  231. * @return void
  232. * 删除产品
  233. */
  234. public function delProduct(Request $request){
  235. $id = $request->get('id');
  236. if(empty($id)){
  237. return $this->error("缺少参数ID!");
  238. }
  239. $product = Product::query()->where('id',$id)->first();
  240. if(!$product){
  241. return $this->error("产品不存在!");
  242. }
  243. $product->delete();
  244. return $this->success();
  245. }
  246. /**
  247. * @return void
  248. * 添加收藏
  249. */
  250. public function addCollect(Request $request){
  251. $product = Product::query()->where('id',$request->get('product_id'))->first();
  252. if(!$product){
  253. return $this->error("商品不存在!");
  254. }
  255. $collect = UserCollect::query()->where('product_id',$product->id)->where('user_id',$this->userId)->first();
  256. if($collect){
  257. return $this->error("您已收藏过了!");
  258. }
  259. $data =[
  260. 'product_id' => $product->id,
  261. 'user_id' => $this->userId,
  262. ];
  263. $res = UserCollect::query()->create($data);
  264. if(!$res){
  265. return $this->error("收藏失败!");
  266. }
  267. $user = User::query()->where('id',$this->userId)->first();
  268. $product_type = ProductType::query()->whereIn('id',$product['type'])->select('id','zh_name','ko_name')->first();
  269. $type_name = '';
  270. if(!empty($product_type)){
  271. $type_name = $product_type['zh_name'];
  272. }
  273. $msg = [
  274. 'type' => 2,//喜欢通知
  275. 'title' => "下载通知",
  276. 'content' => "您收藏了".$type_name."中的1张图片",
  277. 'user_id' => $this->userId,
  278. 'to_user_id' => $this->userId,
  279. ];
  280. Msg::query()->create($msg); // 添加通知消息
  281. $msg1 = [
  282. 'type' => 3,//喜欢通知
  283. 'title' => "保存通知",
  284. 'content' => $user->name."收藏了您".$type_name."中的1张图片",
  285. 'user_id' => $this->userId,
  286. 'to_user_id' => $product['user_id'],
  287. ];
  288. Msg::query()->create($msg1); // 添加通知消息
  289. return $this->success();
  290. }
  291. /**
  292. * @return void
  293. * 取消收藏
  294. */
  295. public function cancelCollect(Request $request){
  296. $product = Product::query()->where('id',$request->get('product_id'))->first();
  297. if(!$product){
  298. return $this->error("产品不存在!");
  299. }
  300. $collect = UserCollect::query()->where('product_id',$product->id)->where('user_id',$this->userId)->first();
  301. if($collect){
  302. $collect->delete();
  303. }
  304. return $this->success();
  305. }
  306. /**
  307. * @return void
  308. * 添加喜欢
  309. */
  310. public function addLike(Request $request){
  311. $product = Product::query()->where('id',$request->get('product_id'))->first();
  312. if(!$product){
  313. return $this->error("数据不存在!");
  314. }
  315. $collect = UserLike::query()->where('product_id',$product->id)->where('user_id',$this->userId)->first();
  316. if($collect){
  317. return $this->error("您已点赞过了!");
  318. }
  319. $data =[
  320. 'product_id' => $product->id,
  321. 'user_id' => $this->userId,
  322. ];
  323. $res = UserLike::query()->create($data);
  324. if(!$res){
  325. return $this->error("操作失败!");
  326. }
  327. $user = User::query()->where('id',$this->userId)->first();
  328. $product_type = ProductType::query()->whereIn('id',$product['type'])->select('id','zh_name','ko_name')->first();
  329. $type_name = '';
  330. if(!empty($product_type)){
  331. $type_name = $product_type['zh_name'];
  332. }
  333. $msg = [
  334. 'type' => 1,//喜欢通知
  335. 'title' => "喜欢通知",
  336. 'content' => $user->name."点赞了您".$type_name."中的1张图片",
  337. 'user_id' => $this->userId,
  338. 'to_user_id' => $product['user_id']
  339. ];
  340. Msg::query()->create($msg); // 添加通知消息
  341. return $this->success();
  342. }
  343. /**
  344. * @return void
  345. * 收藏列表
  346. */
  347. public function collectList(Request $request){
  348. $limit = $request->get('limit',10);
  349. $list = UserCollect::query()
  350. ->with('product:id,name,image')
  351. ->where('user_id',$this->userId)
  352. ->where('is_arrange','=',0)
  353. ->select("id","product_id")
  354. ->paginate($limit);
  355. return $this->success($this->page($list));
  356. }
  357. /**
  358. * @return void
  359. * 举报列表
  360. */
  361. public function reportList(){
  362. $list = Report::query()->where('status',1)->orderByDesc("sort")->get();
  363. return $this->success($list);
  364. }
  365. /**
  366. * @return void
  367. * 举报图片
  368. */
  369. public function report(Request $request){
  370. $params = $request->all();
  371. if(empty($params['product_id'])){
  372. return $this->error("举报项不能为空!");
  373. }
  374. if(empty($params['report_id'])){
  375. return $this->error("举报问题不能为空!");
  376. }
  377. $params['user_id'] = $this->userId;
  378. $res = ReportLog::query()->create($params);
  379. if(!$res){
  380. return $this->error("举报失败!");
  381. }
  382. return $this->success();
  383. }
  384. }