ProductController.php
1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
namespace App\Http\Controllers\Api;
use App\Enums\Common\Code;
use App\Models\Product\Product;
use App\Services\ProjectServer;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
/**
* Class ProductController
* @package App\Http\Controllers\Api
* @author zbj
* @date 2024/2/1
*/
class ProductController extends BaseController
{
/**
* 获取项目随机产品的图片
* @param Request $request
* @return void
*/
public function getImages(Request $request)
{
$project_id = $request->input('project_id');
$project = ProjectServer::useProject($project_id);
if (!$project) {
$this->response('项目不存在', Code::SYSTEM_ERROR);
}
try {
$gallery = Product::where('status', Product::STATUS_ON)->whereNotNull('gallery')->inRandomOrder()->value('gallery');
$gallery = array_map(function ($item) use ($project) {
return getImageUrl($item, $project['storage_type'], $project['project_location']);
}, Arr::pluck($gallery, 'url'));
} catch (\Exception $e) {
$gallery = [];
}
$this->response('success', Code::SUCCESS, $gallery);
}
}