ProductController.php 1.2 KB
<?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);
    }
}