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.

48 lines
1.2 KiB

2 weeks ago
<?php
namespace App\Http\Controllers\Miniapp;
use App\Http\Controllers\Controller;
use App\Models\DictItem;
use App\Models\DictType;
use App\Support\ApiResponse;
use Illuminate\Http\JsonResponse;
class DictController extends Controller
{
use ApiResponse;
public function items(string $code): JsonResponse
{
if (! preg_match('/^[a-z0-9_]{1,64}$/', $code)) {
return $this->fail('字典编码不合法', 422);
}
$type = DictType::query()->where('code', $code)->where('status', 1)->first();
if (! $type) {
return $this->fail('字典类型不存在或已停用', 404);
}
$items = DictItem::query()
->where('dict_type_id', $type->id)
->where('status', 1)
->orderBy('sort')
->orderBy('id')
->get()
->map(fn (DictItem $item) => [
'id' => $item->id,
'label' => $item->label,
'value' => $item->value,
'sort' => (int) $item->sort,
])
->values()
->all();
return $this->ok([
'code' => $type->code,
'name' => $type->name,
'items' => $items,
]);
}
}