fix: 部门列表空/职务无法启用 — fetchTree适配树形API响应, positions表加status/code/description字段
This commit is contained in:
@@ -21,7 +21,10 @@ class PositionController extends Controller
|
||||
{
|
||||
$data = $request->validate([
|
||||
'name' => 'required|string|max:50',
|
||||
'code' => 'nullable|string|max:50',
|
||||
'sort' => 'nullable|integer',
|
||||
'status' => 'in:0,1',
|
||||
'description' => 'nullable|string|max:255',
|
||||
]);
|
||||
|
||||
$data['store_id'] = auth()->user()->store_id;
|
||||
@@ -34,7 +37,10 @@ class PositionController extends Controller
|
||||
{
|
||||
$data = $request->validate([
|
||||
'name' => 'string|max:50',
|
||||
'code' => 'nullable|string|max:50',
|
||||
'sort' => 'nullable|integer',
|
||||
'status' => 'in:0,1',
|
||||
'description' => 'nullable|string|max:255',
|
||||
]);
|
||||
|
||||
$position->update($data);
|
||||
|
||||
@@ -9,5 +9,5 @@ class Position extends Model
|
||||
{
|
||||
use BelongsToStore;
|
||||
|
||||
protected $fillable = ['store_id', 'name', 'sort'];
|
||||
protected $fillable = ['store_id', 'name', 'code', 'sort', 'status', 'description'];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('positions', function (Blueprint $table) {
|
||||
$table->string('code', 50)->nullable()->after('name')->comment('职务编码');
|
||||
$table->tinyInteger('status')->default(1)->after('sort')->comment('1启用 0停用');
|
||||
$table->string('description', 255)->nullable()->after('status')->comment('备注');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('positions', function (Blueprint $table) {
|
||||
$table->dropColumn(['code', 'status', 'description']);
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -74,9 +74,15 @@ async function fetchTree() {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await departmentApi.getList({ per_page: 500 })
|
||||
const flat = res.data?.list || res.data?.data || []
|
||||
flatDepts.value = flat.map(d => ({ id: d.id, name: d.name }))
|
||||
treeData.value = buildTree(flat)
|
||||
// Backend already returns a tree array directly in res.data
|
||||
if (Array.isArray(res.data)) {
|
||||
treeData.value = res.data
|
||||
flatDepts.value = flattenTree(res.data)
|
||||
} else {
|
||||
const flat = res.data?.list || res.data?.data || []
|
||||
flatDepts.value = flat.map(d => ({ id: d.id, name: d.name }))
|
||||
treeData.value = buildTree(flat)
|
||||
}
|
||||
} catch {
|
||||
//
|
||||
} finally {
|
||||
@@ -84,9 +90,9 @@ async function fetchTree() {
|
||||
}
|
||||
}
|
||||
|
||||
function buildTree(nodes, parentId = null) {
|
||||
function buildTree(nodes, parentId = 0) {
|
||||
return nodes
|
||||
.filter(n => (n.parent_id ?? null) === parentId)
|
||||
.filter(n => (n.parent_id ?? 0) === parentId)
|
||||
.map(n => ({ ...n, children: buildTree(nodes, n.id) }))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user