| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568 |
- <?php
- namespace App\Http\Controllers\V1;
- use App\Models\Msg;
- use App\Models\Product;
- use App\Models\ProductType;
- use App\Models\Report;
- use App\Models\ReportLog;
- use App\Models\User;
- use App\Models\UserCollect;
- use App\Models\UserFolder;
- use App\Models\UserFollow;
- use App\Models\UserLike;
- use Illuminate\Http\Request;
- /**
- *
- * 产品
- */
- class ProductController extends Controller
- {
- public function __construct()
- {
- $this->user = auth('api')->user();
- $this->userId = $this->user ? $this->user->id : 0;
- //如果用户被删除,会自动退出登录
- if (!empty($this->user->deleted_at)) {
- $this->user->online = 0;
- $this->user->save();
- auth('api')->logout();
- }
- }
- /**
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- *分类数据筛选
- */
- public function filterTypeList(Request $request)
- {
- $pid = $request->input('pid');
- $map = [];
- $maps = [];
- if ($pid) {
- $map[] = ['pid', '=', $pid];
- $maps[] = ['filter_display_pid', '=', $pid];
- }
- $list = ProductType::query()
- ->where('status', 1)
- ->where($map)
- ->orWhere($maps)
- ->orderByDesc("sort")
- ->get();
- $data = [];
- foreach ($list as $v) {
- if ($v['filter_display_pid'] === 0) {
- $v['pid'] = 0;
- } elseif ($v['filter_display_pid'] > 0) {
- $v['pid'] = $v['filter_display_pid'];
- } else {
- $v['pid'] = $v['pid'];
- }
- $data[] = [
- 'id' => $v['id'],
- 'icon' => $v['icon'],
- 'zh_name' => $v['zh_alias'] != null ? $v['zh_alias'] : $v['zh_name'],
- 'ko_name' => $v['ko_alias'] != null ? $v['ko_alias'] : $v['ko_name'],
- 'pid' => $v['pid'],
- 'is_display' => $v['is_filter_display'],
- 'product_count' => Product::query()->whereJsonContains('type', [$v['id']])->where('status', 1)->count()
- ];
- }
- return $this->success($this->recursion($data, $pid ? $pid : 0));
- }
- /**
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * 分类数据上传
- */
- public function uploadTypeList(Request $request)
- {
- $pid = $request->input('pid');
- $map = [];
- $maps = [];
- if ($pid) {
- $map[] = ['pid', '=', $pid];
- $maps[] = ['upload_display_pid', '=', $pid];
- }
- $list = ProductType::query()
- ->where('status', 1)
- ->where($map)
- ->orWhere($maps)
- ->orderByDesc("sort")
- ->get();
- $data = [];
- foreach ($list as $v) {
- if ($v['upload_display_pid'] === 0) {
- $v['pid'] = 0;
- } elseif ($v['upload_display_pid'] > 0) {
- $v['pid'] = $v['upload_display_pid'];
- } else {
- $v['pid'] = $v['pid'];
- }
- $data[] = [
- 'id' => $v['id'],
- 'icon' => $v['icon'],
- 'zh_name' => $v['zh_name'],
- 'ko_name' => $v['ko_name'],
- 'describe' => $v['describe'],
- 'pid' => $v['pid'],
- 'is_display' => $v['is_upload_display']
- ];
- }
- return $this->success($this->recursion($data, $pid ? $pid : 0));
- }
- /**
- * @param $list
- * @param $pid
- * @return array
- * 递归处理
- */
- public function recursion($list = [], $pid = 0)
- {
- $child = [];
- foreach ($list as $value) {
- if ($value['is_display'] != 0) {
- if ($value['pid'] == $pid) {
- if ($this->recursion($list, $value['id'])) {
- // 递归调用,查找当前数据的子级
- $value['child'] = $this->recursion($list, $value['id']);
- }
- // 把子级数据添加进数组
- $child[] = $value;
- }
- }
- }
- return $child;
- }
- /**
- * @return mixed
- * 添加产品
- */
- public function addProduct(Request $request)
- {
- $params = $request->all();
- if (empty($params['type'])) {
- return $this->error("分类不能能为空!");
- }
- if (empty($params['url']) && empty($params['name']) && empty($params['content'])) {
- return $this->error("数据不能为空!");
- }
- $params['user_id'] = $this->userId; // 用户ID
- $res = Product::query()->create($params);
- if (!$res) {
- return $this->error("上传失败!");
- }
- $follow = UserFollow::query()->where('to_user_id', $this->userId)->pluck('user_id')->toArray();
- if (!empty($follow)) {
- $user = User::query()->where('id', $this->userId)->first();
- foreach ($follow as $v) {
- $msg = [
- 'type' => 5,//关注的人发了新作品
- 'title' => "关注的人发了新作品",
- 'content' => " 您关注的" . $user->name . "上传了1张图片",
- 'user_id' => $this->userId,
- 'to_user_id' => $v,
- 'product_id'=> $res->id,
- ];
- Msg::query()->create($msg); // 添加通知消息
- }
- }
- return $this->success($res);
- }
- /**
- * @return void
- * 我的上传列表
- */
- public function userProductList(Request $request)
- {
- $limit = $request->get('limit', 10);
- $go = $request->get('go', 6);
- $user_id = $request->get('user_id');
- if (empty($user_id)) {
- $user_id = $this->userId;
- }
- $list = Product::query()->where('status', '=', 1)
- ->where('user_id', '=', $user_id)
- ->orderByDesc('id')
- ->paginate($limit);
- return $this->success(pages($list, $go));
- }
- /**
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- * 产品列表
- */
- public function productList(Request $request)
- {
- $limit = $request->get('limit', 10);
- $type = $request->get('type');
- $keyword = $request->get('keyword');
- $go = $request->get('go', 6);
- $query = Product::with('user:id,name,nickname,avatar,company_name,company_card_color,production_project,member_type,follow_count')
- ->join('users', 'users.id', '=', 'product.user_id')
- ->select('product.id','product.type','user_id','product.name','content','image','url','product.created_at','product.updated_at');
- if (!empty($type)) {
- $arr = explode(',', $type);
- foreach ($arr as $v) {
- $query->orWhereJsonContains('type', [intval($v)]);
- }
- }
- $query->whereHas('user', function ($q) {
- $q->where('is_stop', 0);
- });
- if (!empty($keyword)) {
- $query->where('name', 'like', '%' . $keyword . '%');
- }
- $list = $query->where('product.status', 1)
- ->orderByDesc('users.member_type')
- ->orderByDesc('users.follow_count')
- ->orderByDesc("product.like_count")
- ->orderBy("product.id")
- ->paginate(intval($limit));
- foreach ($list as $v) {
- $v['is_collect'] = 0;
- if (!empty($this->userId)) {
- $v['is_collect'] = UserCollect::query()->where('product_id', $v['id'])->where('user_id', $this->userId)->count();
- }
- }
- return $this->success(pages($list, $go));
- }
- /**
- * @return void
- * 产品详情
- */
- public function productDetail(Request $request)
- {
- $data = Product::query()
- ->with('user:id,name,nickname,avatar,company_name,company_url,production_project')
- ->where('status', 1)
- ->where('id', $request->get('id'))
- ->whereHas('user', function ($q) {
- $q->where('is_stop', 0);
- })
- ->select("id", "name", "user_id", "content", "image", "type", "url")->first();
- if (!$data) {
- return $this->error("数据不存在!");
- }
- $data['is_collect'] = 0; // 收藏
- $data['is_follow'] = 0; // 关注
- $data['is_like'] = 0; // 喜欢
- if (!empty($this->userId)) {
- $data['is_collect'] = UserCollect::query()->where('product_id', $data['id'])->where('user_id', $this->userId)->count();
- $data['is_follow'] = UserFollow::query()->where('to_user_id', $data['user_id'])->where('user_id', $this->userId)->count();
- $data['is_like'] = UserLike::query()->where('product_id', $data['id'])->where('user_id', $this->userId)->count();
- }
- if (!empty($data['user'])) {
- $data['user']['follow_count'] = UserFollow::query()->where('user_id', $data['user_id'])->count();
- }
- return $this->success($data);
- }
- /**
- * 获取推荐信息
- *
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function getProductRecommend(Request $request)
- {
- $limit = $request->get('limit', 10);
- $type = $request->get('type');
- $keyword = $request->get('keyword');
- $go = $request->get('go', 6);
- $type = Product::where('id', $request->get('id'))->where('status', 1)->value('type');
- $recommend = Product::with('user:id,name,nickname,avatar,company_name,company_url,production_project');
- foreach ($type ?? [] as $v) {
- $recommend->orWhereJsonContains('type', [intval($v)]);
- }
- $recommend->where('status', 1)
- ->select("id", "name", "user_id", "content", "image", "type", "url")
- ->where('id', '<>', $request->get('id'))
- ->whereHas('user', function ($q) {
- $q->where('is_stop', 0);
- });
- $data = $recommend->orderBy('id', 'desc')->paginate(intval($limit))->toArray();
- foreach ($data['data'] ?? [] as $key => $value) {
- if ($value['id'] == $request->get('id')) {
- unset($data['data'][$key]);
- }
- }
- $data['data'] = array_values($data['data']);
- if (empty($data['data'])) {
- $data = Product::with('user:id,name,nickname,avatar,company_name,company_url,production_project')
- ->where('status', 1)
- ->where('id', '<>', $request->get('id'))
- ->select("id", "name", "user_id", "content", "image", "type", "url")
- ->whereHas('user', function ($q) {
- $q->where('is_stop', 0);
- })
- ->orderByRaw('rand()')->paginate();
- }
- foreach ($data['data'] as $v) {
- $v['is_collect'] = 0;
- if (!empty($this->userId)) {
- $v['is_collect'] = UserCollect::query()->where('product_id', $v['id'])->where('user_id', $this->userId)->count();
- }
- }
- return $this->success(pagesArr($data, $go));
- // return $this->success($this->pageByArr($data));
- }
- /**
- * @param Request $request
- * @return void
- * 删除产品
- */
- public function delProduct(Request $request)
- {
- $id = $request->get('id');
- if (empty($id)) {
- return $this->error("缺少参数ID!");
- }
- $product = Product::query()->where('id', $id)->first();
- if (!$product) {
- return $this->error("产品不存在!");
- }
- $product->delete();
- return $this->success();
- }
- /**
- * @return void
- * 添加收藏
- */
- public function addCollect(Request $request)
- {
- $product = Product::query()->where('id', $request->get('product_id'))->first();
- if (!$product) {
- return $this->error("商品不存在!");
- }
- $collect = UserCollect::query()->where('product_id', $product->id)->where('user_id', $this->userId)->first();
- if ($collect) {
- return $this->error("您已收藏过了!");
- }
- $data = [
- 'product_id' => $product->id,
- 'user_id' => $this->userId,
- ];
- $res = UserCollect::query()->create($data);
- if (!$res) {
- return $this->error("收藏失败!");
- }
- $user = User::query()->where('id', $this->userId)->first();
- $product_type = ProductType::query()->whereIn('id', $product['type'])->select('id', 'zh_name', 'ko_name')->first();
- $type_name = '';
- if (!empty($product_type)) {
- $type_name = $product_type['zh_name'];
- }
- $msg = [
- 'type' => 1,//下载通知
- 'title' => "收藏通知",
- 'content' => "您收藏了" . $type_name . "中的1张图片",
- 'user_id' => $this->userId,
- 'to_user_id' => $this->userId,
- 'product_id' => $product['id'],
- ];
- Msg::query()->create($msg); // 添加通知消息
- $msg1 = [
- 'type' => 1,//保存通知
- 'title' => "收藏通知",
- 'content' => $user->name . "收藏了您" . $type_name . "中的1张图片",
- 'user_id' => $this->userId,
- 'to_user_id' => $product['user_id'],
- 'product_id' => $product['id'],
- ];
- Msg::query()->create($msg1); // 添加通知消息
- return $this->success();
- }
- public function folderAddCollect(Request $request)
- {
- $product = Product::query()->where('id', $request->get('product_id'))->first();
- if (!$product) {
- return $this->error("商品不存在!");
- }
- $collect = UserCollect::query()->where('product_id', $product->id)->where('user_id', $this->userId)->first();
- if ($collect) {
- return $this->error("您已收藏过了!");
- }
- $data = [
- 'product_id' => $product->id,
- 'user_id' => $this->userId,
- ];
- $res = UserCollect::query()->create($data);
- if (!$res) {
- return $this->error("收藏失败!");
- }
- $user = User::query()->where('id', $this->userId)->first();
- $folderName = UserFolder::where('id', $request->get('folderId'))->first();
- $product_type = ProductType::query()->whereIn('id', $product['type'])->select('id', 'zh_name', 'ko_name')->first();
- $type_name = '';
- if (!empty($product_type)) {
- $type_name = $product_type['zh_name'];
- }
- $msg = [
- 'type' => 1,//下载通知
- 'title' => "收藏通知",
- 'content' => "您收藏了" . $type_name . "中的1张图片",
- 'user_id' => $this->userId,
- 'to_user_id' => $this->userId,
- 'product_id' => $product['id'],
- ];
- Msg::query()->create($msg); // 添加通知消息
- $msg1 = [
- 'type' => 1,//保存通知
- 'title' => "收藏通知",
- 'content' => $user->name . "收藏了您" . $folderName->name . "中的1张图片",
- 'user_id' => $this->userId,
- 'to_user_id' => $product['user_id'],
- 'product_id' => $product['id'],
- ];
- Msg::query()->create($msg1); // 添加通知消息
- return $this->success();
- }
- /**
- * @return void
- * 取消收藏
- */
- public function cancelCollect(Request $request)
- {
- $product = Product::query()->where('id', $request->get('product_id'))->first();
- if (!$product) {
- return $this->error("产品不存在!");
- }
- $collect = UserCollect::query()->where('product_id', $product->id)->where('user_id', $this->userId)->first();
- if ($collect) {
- $collect->delete();
- }
- return $this->success();
- }
- /**
- * @return void
- * 添加喜欢
- */
- public function addLike(Request $request)
- {
- $product = Product::query()->where('id', $request->get('product_id'))->first();
- if (!$product) {
- return $this->error("数据不存在!");
- }
- $collect = UserLike::query()->where('product_id', $product->id)->where('user_id', $this->userId)->first();
- if ($collect) {
- return $this->error("您已点赞过了!");
- }
- $data = [
- 'product_id' => $product->id,
- 'user_id' => $this->userId,
- ];
- $res = UserLike::query()->create($data);
- if (!$res) {
- return $this->error("操作失败!");
- }
- Product::where('id', $request->get('product_id'))->increment('like_count');
- $user = User::query()->where('id', $this->userId)->first();
- $product_type = ProductType::query()->whereIn('id', $product['type'])->select('id', 'zh_name', 'ko_name')->first();
- $type_name = '';
- if (!empty($product_type)) {
- $type_name = $product_type['zh_name'];
- }
- $msg = [
- 'type' => 1,//喜欢通知
- 'title' => "喜欢通知",
- 'content' => $user->name . "点赞了您" . $type_name . "中的1张图片",
- 'user_id' => $this->userId,
- 'to_user_id' => $product['user_id']
- ];
- Msg::query()->create($msg); // 添加通知消息
- return $this->success();
- }
- /**
- * @return void
- * 收藏列表
- */
- public function collectList(Request $request)
- {
- $limit = $request->get('limit', 10);
- $go = $request->get('go', 6);
- $user_id = $request->get('user_id');
- if (empty($user_id)) {
- $user_id = $this->userId;
- }
- $list = UserCollect::query()
- ->with('product:id,name,image,url')
- ->whereHas('product', function ($query) {
- $query->where('id', '>', 0);
- $query->whereHas('user', function ($q) {
- $q->where('is_stop', 0);
- });
- })
- ->where('user_id', $user_id)
- ->where('is_arrange', '=', 0)
- ->select("id", "product_id")
- ->orderByDesc("id")
- ->paginate($limit);
- return $this->success(pages($list, $go));
- }
- /**
- * @return void
- * 举报列表
- */
- public function reportList()
- {
- $list = Report::query()->where('status', 1)->orderByDesc("sort")->get();
- return $this->success($list);
- }
- /**
- * @return void
- * 举报图片
- */
- public function report(Request $request)
- {
- $params = $request->all();
- if (empty($params['product_id'])) {
- return $this->error("举报文件不能为空!");
- }
- if (empty($params['report_id'])) {
- return $this->error("举报问题不能为空!");
- }
- $params['user_id'] = $this->userId;
- $res = ReportLog::query()->create($params);
- if (!$res) {
- return $this->error("举报失败!");
- }
- return $this->success();
- }
- }
|