You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
3.2 KiB
93 lines
3.2 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Miniapp;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Demand;
|
|
use App\Models\DictItem;
|
|
use App\Models\DictType;
|
|
use App\Models\MiniappUser;
|
|
use App\Support\ApiResponse;
|
|
use App\Support\Miniapp\MiniappPresenter;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class DemandController extends Controller
|
|
{
|
|
use ApiResponse;
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
/** @var MiniappUser $user */
|
|
$user = $request->user();
|
|
if (! MiniappPresenter::userPayload($user)['is_partner']) {
|
|
return $this->fail('仅正式伙伴可发布需求', 403);
|
|
}
|
|
|
|
$items = Demand::query()
|
|
->with(['typeItem', 'statusItem'])
|
|
->where('miniapp_user_id', $user->id)
|
|
->orderByDesc('submitted_at')
|
|
->orderByDesc('id')
|
|
->get()
|
|
->map(fn (Demand $demand) => $this->serialize($demand))
|
|
->values()
|
|
->all();
|
|
|
|
return $this->ok(['items' => $items]);
|
|
}
|
|
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
/** @var MiniappUser $user */
|
|
$user = $request->user();
|
|
if (! MiniappPresenter::userPayload($user)['is_partner']) {
|
|
return $this->fail('仅正式伙伴可发布需求', 403);
|
|
}
|
|
|
|
$typeDictId = DictType::query()->where('code', 'demand_type')->where('status', 1)->value('id');
|
|
$statusDictId = DictType::query()->where('code', 'demand_status')->where('status', 1)->value('id');
|
|
$defaultStatusId = $statusDictId
|
|
? DictItem::query()->where('dict_type_id', $statusDictId)->where('status', 1)->orderBy('sort')->value('id')
|
|
: null;
|
|
|
|
$data = $request->validate([
|
|
'type_dict_item_id' => $typeDictId
|
|
? ['required', 'integer', Rule::exists('dict_items', 'id')->where('dict_type_id', $typeDictId)]
|
|
: ['required', 'integer'],
|
|
'title' => ['required', 'string', 'max:255'],
|
|
'content' => ['required', 'string'],
|
|
'company' => ['nullable', 'string', 'max:255'],
|
|
]);
|
|
|
|
$demand = Demand::query()->create([
|
|
'miniapp_user_id' => $user->id,
|
|
'teacher_id' => $user->teacher_id,
|
|
'type_dict_item_id' => $data['type_dict_item_id'],
|
|
'status_dict_item_id' => $defaultStatusId,
|
|
'title' => $data['title'],
|
|
'content' => $data['content'],
|
|
'contact_name' => $user->name,
|
|
'company' => $data['company'] ?? $user->company,
|
|
'submitted_at' => now(),
|
|
]);
|
|
|
|
return $this->ok($this->serialize($demand->load(['typeItem', 'statusItem'])), '需求已提交');
|
|
}
|
|
|
|
/** @return array<string, mixed> */
|
|
protected function serialize(Demand $demand): array
|
|
{
|
|
return [
|
|
'id' => $demand->id,
|
|
'type' => $demand->typeItem?->label,
|
|
'type_dict_item_id' => $demand->type_dict_item_id,
|
|
'status' => $demand->statusItem?->label ?? '已提交',
|
|
'title' => $demand->title,
|
|
'description' => $demand->content,
|
|
'date' => $demand->submitted_at?->toDateString(),
|
|
];
|
|
}
|
|
}
|