ProductController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace App\Http\Controllers\V1;
  3. use App\Models\Product;
  4. use App\Models\ProductType;
  5. use App\Models\ReportLog;
  6. use App\Models\UserCollect;
  7. use App\Models\UserLike;
  8. use Illuminate\Http\Request;
  9. /**
  10. *
  11. * 产品
  12. */
  13. class ProductController extends Controller
  14. {
  15. public function __construct()
  16. {
  17. $this->user = auth('api')->user();
  18. $this->userId = $this->user ? $this->user->id : 0;
  19. //如果用户被删除,会自动退出登录
  20. if (!empty($this->user->deleted_at)) {
  21. $this->user->online = 0;
  22. $this->user->save();
  23. auth('api')->logout();
  24. }
  25. }
  26. /**
  27. * @param Request $request
  28. * @return \Illuminate\Http\JsonResponse
  29. *分类数据筛选
  30. */
  31. public function filterTypeList(Request $request){
  32. $pid = $request->input('pid');
  33. $map = [];
  34. $maps = [];
  35. if($pid){
  36. $map[] = ['pid','=',$pid];
  37. $maps[] = ['filter_display_pid','=',$pid];
  38. }
  39. $list = ProductType::query()
  40. ->where('status',1)
  41. ->where($map)
  42. ->orWhere($maps)
  43. ->get();
  44. $data = [];
  45. foreach ($list as $v){
  46. if($v['filter_display_pid'] === 0){
  47. $v['pid'] = 0;
  48. }elseif ($v['filter_display_pid'] > 0){
  49. $v['pid'] = $v['filter_display_pid'];
  50. }else{
  51. $v['pid'] = $v['pid'];
  52. }
  53. $data[] = [
  54. 'id' => $v['id'],
  55. 'icon' => $v['icon'],
  56. 'zh_name' => $v['zh_alias'] != null?$v['zh_alias']:$v['zh_name'],
  57. 'ko_name' => $v['ko_alias'] != null?$v['ko_alias']:$v['ko_name'],
  58. 'pid' => $v['pid'],
  59. 'is_display' => $v['is_filter_display']
  60. ];
  61. }
  62. return $this->success($this->recursion($data,$pid?$pid:0));
  63. }
  64. /**
  65. * @param Request $request
  66. * @return \Illuminate\Http\JsonResponse
  67. * 分类数据上传
  68. */
  69. public function uploadTypeList(Request $request){
  70. $pid = $request->input('pid');
  71. $map = [];
  72. $maps = [];
  73. if($pid){
  74. $map[] = ['pid','=',$pid];
  75. $maps[] = ['upload_display_pid','=',$pid];
  76. }
  77. $list = ProductType::query()
  78. ->where('status',1)
  79. ->where($map)
  80. ->orWhere($maps)
  81. ->get();
  82. $data = [];
  83. foreach ($list as $v){
  84. if($v['upload_display_pid'] === 0){
  85. $v['pid'] = 0;
  86. }elseif ($v['upload_display_pid'] > 0){
  87. $v['pid'] = $v['upload_display_pid'];
  88. }else{
  89. $v['pid'] = $v['pid'];
  90. }
  91. $data[] =[
  92. 'id' => $v['id'],
  93. 'icon' => $v['icon'],
  94. 'zh_name' => $v['zh_name'],
  95. 'ko_name' => $v['ko_name'],
  96. 'describe' => $v['describe'],
  97. 'pid' => $v['pid'],
  98. 'is_display' => $v['is_upload_display']
  99. ];
  100. }
  101. return $this->success($this->recursion($data,$pid?$pid:0));
  102. }
  103. /**
  104. * @param $list
  105. * @param $pid
  106. * @return array
  107. * 递归处理
  108. */
  109. public function recursion($list = [],$pid = 0){
  110. $child = [];
  111. foreach ($list as $value) {
  112. if($value['is_display'] != 0){
  113. if ($value['pid'] == $pid ) {
  114. if($this->recursion($list, $value['id'])){
  115. // 递归调用,查找当前数据的子级
  116. $value['child'] = $this->recursion($list, $value['id']);
  117. }
  118. // 把子级数据添加进数组
  119. $child[] = $value;
  120. }
  121. }
  122. }
  123. return $child;
  124. }
  125. /**
  126. * @return void
  127. * 添加产品
  128. */
  129. public function addProduct(Request $request){
  130. $params = $request->all();
  131. if(empty($params)){
  132. return $this->error("数据不能为空!");
  133. }
  134. if(empty($params['type'])){
  135. return $this->error("分类不能能为空!");
  136. }
  137. $params['user_id'] = $this->userId; // 用户ID
  138. $res = Product::query()->create($params);
  139. if(!$res){
  140. return $this->error("上传失败!");
  141. }
  142. return $this->success($res);
  143. }
  144. /**
  145. * @return void
  146. * 我的上传列表
  147. */
  148. public function userProductList(Request $request){
  149. $limit = $request->get('limit',10);
  150. $list = Product::query()->where('status','=',1)
  151. ->where('user_id','=',$this->userId)
  152. ->orderByDesc('id')
  153. ->paginate($limit);
  154. return $this->success($this->page($list));
  155. }
  156. /**
  157. * @param Request $request
  158. * @return \Illuminate\Http\JsonResponse
  159. * 产品列
  160. */
  161. public function productList(Request $request){
  162. $limit = $request->get('limit',10);
  163. $type = $request->get('type');
  164. $go = $request->get('go',6);
  165. $query = Product::query()
  166. ->with('user:id,name,nickname,avatar,company_name,company_card_color,production_project');
  167. if(!empty($type)){
  168. $type = array_map('intval', explode(',',$type));
  169. $query->whereJsonContains('type',$type);
  170. }
  171. $list = $query->where('status',1)
  172. ->paginate($limit);
  173. return $this->success(pages($list,$go));
  174. }
  175. /**
  176. * @return void
  177. * 产品详情
  178. */
  179. public function productDetail(Request $request){
  180. $data = Product::query()
  181. ->with('user:id,name,nickname,avatar,company_name,company_url,production_project')
  182. ->select("id","name","user_id","content","image","type")->first();
  183. return $this->success($data);
  184. }
  185. /**
  186. * @return void
  187. * 添加收藏
  188. */
  189. public function addCollect(Request $request){
  190. $product = Product::query()->where('id',$request->get('product_id'))->first();
  191. if(!$product){
  192. return $this->error("商品不存在!");
  193. }
  194. $collect = UserCollect::query()->where('product_id',$product->id)->where('user_id',$this->userId)->first();
  195. if($collect){
  196. return $this->error("您已收藏过了!");
  197. }
  198. $data =[
  199. 'product_id' => $product->id,
  200. 'user_id' => $this->userId,
  201. ];
  202. $res = UserCollect::query()->create($data);
  203. if(!$res){
  204. return $this->error("收藏失败!");
  205. }
  206. return $this->success();
  207. }
  208. /**
  209. * @return void
  210. * 添加喜欢
  211. */
  212. public function addLike(Request $request){
  213. $product = Product::query()->where('id',$request->get('product_id'))->first();
  214. if(!$product){
  215. return $this->error("商品不存在!");
  216. }
  217. $collect = UserLike::query()->where('product_id',$product->id)->where('user_id',$this->userId)->first();
  218. if($collect){
  219. return $this->error("您已点赞过了!");
  220. }
  221. $data =[
  222. 'product_id' => $product->id,
  223. 'user_id' => $this->userId,
  224. ];
  225. $res = UserLike::query()->create($data);
  226. if(!$res){
  227. return $this->error("操作失败!");
  228. }
  229. return $this->success();
  230. }
  231. /**
  232. * @return void
  233. * 收藏列表
  234. */
  235. public function collectList(Request $request){
  236. $limit = $request->get('limit',10);
  237. $list = UserCollect::query()
  238. ->with('product:id,name,image')
  239. ->where('user_id',$this->userId)
  240. ->where('is_arrange','=',0)
  241. ->select("id","product_id")
  242. ->paginate($limit);
  243. return $this->success($this->page($list));
  244. }
  245. /**
  246. * @return void
  247. * 举报图片
  248. */
  249. public function report(Request $request){
  250. $params = $request->all();
  251. if(empty($params['product_id'])){
  252. return $this->error("举报项不能为空!");
  253. }
  254. if(empty($params['report_id'])){
  255. return $this->error("举报问题不能为空!");
  256. }
  257. $params['user_id'] = $this->userId;
  258. $res = ReportLog::query()->create($params);
  259. if(!$res){
  260. return $this->error("举报失败!");
  261. }
  262. return $this->success();
  263. }
  264. }