菜单设置
- 1.放在自定义用户管理下面, 然后添加给各级管理员

代码
<template>
<el-row :gutter="0">
<el-col class="user-filter" :span="3"><div class="grid-content ep-bg-purple" />
<el-input v-model="filterText" placeholder="Filter keyword" />
<el-tree
ref="treeRef"
class="filter-tree"
:data="list"
:props="{children: 'children', label: 'label'}"
default-expand-all
:filter-node-method="filterNode"
:expand-on-click-node="false"
@node-click="NodeClick"
/>
</el-col>
<el-col :span="21">
<div class="default-main ba-table-box">
<el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />
<TableHeader
:buttons="['refresh', 'add', 'edit', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
:quick-search-placeholder="t('Quick search placeholder', { fields: t('goods.quick Search Fields') })"
></TableHeader>
<Table ref="tableRef"></Table>
<PopupForm />
</div>
</el-col>
</el-row>
</template>
<script setup lang="ts">
import { ref, provide, onMounted } from 'vue'
import baTableClass from '/@/utils/baTable'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import { useI18n } from 'vue-i18n'
import PopupForm from './popupForm.vue'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import goodsStatus from '/@/config/status/goodsStatus'
import { goodsCategory, userUser } from '/@/api/controllerUrls'
defineOptions({
name: 'goods',
})
const { t } = useI18n()
const tableRef = ref()
const optButtons: OptButton[] = defaultOptButtons(['edit', 'delete'])
const baTable = new baTableClass(
new baTableApi('/admin/Goods/'),
{
pk: 'id',
column: [
{ type: 'selection', operator: false },
{ label: t('goods.id'), prop: 'id', width: 70, sortable: 'custom', operator: false},
{ label: 'category', prop: 'category_id', comSearchRender: 'remoteSelect', show:false, remote: {
pk: 'id',
field: 'title',
remoteUrl: goodsCategory +'index'
}},
{ label: '用户', prop: 'seller_id', show:false},
],
dblClickNotEditColumn: ['all'],
},
{
defaultItems: { created_at: null },
}
)
provide('baTable', baTable)
onMounted(() => {
baTable.table.ref = tableRef.value
baTable.mount()
baTable.getIndex()?.then(() => {
baTable.initSort()
baTable.dragSort()
})
})
import { watch, reactive } from 'vue'
import { ElTree } from 'element-plus'
import { getArrayKey } from '/@/utils/common'
import { getUserTree } from '/@/api/public/member/member'
interface Tree {
[key: string]: any
}
const filterText = ref('')
const treeRef = ref<InstanceType<typeof ElTree>>()
watch(filterText, (val) => {
treeRef.value!.filter(val)
})
const filterNode = (value: string, data: Tree) => {
if (!value) return true
return data.label.includes(value)
}
const list = ref<Tree[]>([])
getUserTree().then((res) => {
console.log("@@获取树形用户", res);
list.value = res.data.list
})
const NodeClick = (data: { id: any }) => {
baTable.table.showComSearch = true
baTable.comSearch.form = Object.assign(baTable.comSearch.form, {
'seller_id': data.id
})
const fieldDataTemp = baTable.comSearch.fieldData.get('seller_id')
if (fieldDataTemp) {
let comSearchData: comSearchData = {
field: 'seller_id',
val: data.id,
operator: fieldDataTemp.operator,
render: fieldDataTemp.render,
}
let index = getArrayKey(baTable.table.filter!.search!, 'field', 'seller_id')
if (!index) {
baTable.table.filter!.search!.push(comSearchData)
} else {
baTable.table.filter!.search![index] = comSearchData
}
baTable.onTableHeaderAction('refresh', {})
} else {
console.log('没有找到 id 字段的公共搜索数据,因为该字段禁止了公共搜索')
}
}
</script>
<style scoped lang="scss">
.user-filter {
margin-top: 20px;
padding: 0 5px;
}
</style>
- 2.封装http请求
web\src\api\public\member\member.ts,可以看到不用写batoken
import createAxios from '/@/utils/axios'
export function getUserTree() {
return createAxios({
url: '/admin/member/getUserTree',
method: 'get',
})
}
- 3.Member控制器(没有放在公共接口控制器之类的)
app\admin\controller\Member.php
<?php
namespace app\admin\controller;
use app\common\controller\Backend;
class Member extends Backend
{
protected object $model;
protected $uid = null;
protected $child = [];
public function initialize(): void
{
parent::initialize();
$this->model = new \app\admin\model\Member;
$this->uid = $this->auth->getAdmin()->id;
$this->child = $this->model->getChildList($this->uid, $this->auth->isSuperAdmin());
}
public function getUserTree()
{
$list = $this->model->getUserTree($this->uid, $this->auth->isSuperAdmin());
return $this->success('', [
'list' => $list, // 注意层级
'total' => count($list),
]);
}
}
<?php
namespace app\admin\model;
use think\Model;
class Member extends Model
{
protected $name = 'admin';
protected $autoWriteTimestamp = true;
public function pidTable(): \think\model\relation\BelongsTo
{
return $this->belongsTo(\app\admin\model\Admin::class, 'pid', 'id');
}
public function getUserTree($uid = 0, $isSuper = false)
{
if ($isSuper) {
$res = $this->field(['id','username label','pid'])->where("pid", 0)->where('id', '<>', 1)->select()->toArray();
} else {
$res = $this->field(['id','username label','pid'])->where("id", $uid)->select()->toArray();
}
$list = [];
foreach ($res as $item) {
$list[] = [
'id' => $item['id'],
'pid' => $item['pid'],
'label' => $item['label'],
];
}
foreach ($list as &$user) {
$child = $this->getChildren($user['id']);
$user['children'] = $child;
}
return $list;
}
public function getChildren($id)
{
$children = $this->hidden(['group_arr'])->field(['id','username label','pid'])->where('pid', $id)->select()->toArray();
$list = [];
foreach ($children as $item) {
$list[] = [
'id' => $item['id'],
'pid' => $item['pid'],
'label' => $item['label'],
];
}
foreach ($list as &$child) {
$child['children'] = $this->getChildren($child['id']);
}
return $list;
}
public function getChildList($uid, $isSuperAdmin = false)
{
if ($isSuperAdmin && $uid == 1) {
$result = $this->getAllUids();
} else {
$result = $this->getChildIds($uid);
$result[] = $uid;
}
return $result;
}
private function getChildIds($uid)
{
$subordinateIds = [];
$subordinates = $this->where('pid', $uid)->select();
foreach ($subordinates as $subordinate) {
$subordinateIds[] = $subordinate->id;
$subordinateIds = array_merge($subordinateIds, $this->getChildIds($subordinate->id));
}
return $subordinateIds;
}
private function getAllUids()
{
$result = $this->where('id', '<>', 1)->column('id');
return $result;
}
}