130 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
		
		
			
		
	
	
			130 lines
		
	
	
		
			3.0 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| 
								 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace app\api\controller\backend;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use app\common\controller\Api;
							 | 
						||
| 
								 | 
							
								use app\api\model\Admin as AdminModel;
							 | 
						||
| 
								 | 
							
								use think\Db;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/**
							 | 
						||
| 
								 | 
							
								 * 菜单控制器
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								class AuthRule extends Api
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    protected $noNeedLogin = ['*'];
							 | 
						||
| 
								 | 
							
								    protected $noNeedRight = ['*'];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 首页
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function getAuthRuleData()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        // 从数据库中获取所有数据
							 | 
						||
| 
								 | 
							
								        $data = Db::name('auth_rule')->order('weigh asc')->select();
							 | 
						||
| 
								 | 
							
								 
							 | 
						||
| 
								 | 
							
								        // 构建层级结构
							 | 
						||
| 
								 | 
							
								        $tree = $this->buildTree($data);
							 | 
						||
| 
								 | 
							
								 
							 | 
						||
| 
								 | 
							
								        return $this->success('请求成功',$tree);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								 
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 构建树结构
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param array $data 数据库查询结果数组
							 | 
						||
| 
								 | 
							
								     * @param int $parentId 父ID
							 | 
						||
| 
								 | 
							
								     * @return array
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    private function buildTree(array $data, $parentId = 0)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $tree = [];
							 | 
						||
| 
								 | 
							
								        foreach ($data as $item) {
							 | 
						||
| 
								 | 
							
								            if ($item['pid'] == $parentId) {
							 | 
						||
| 
								 | 
							
								                $children = $this->buildTree($data, $item['id']);
							 | 
						||
| 
								 | 
							
								                if ($children) {
							 | 
						||
| 
								 | 
							
								                    $item['children'] = $children;
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								                $tree[] = $item;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        return $tree;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     *单个科室查询
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function getAuthRuleFind()
							 | 
						||
| 
								 | 
							
								    {   
							 | 
						||
| 
								 | 
							
								        $id = $this->request->post('id');
							 | 
						||
| 
								 | 
							
								        if(!$id){
							 | 
						||
| 
								 | 
							
								            return $this->error('缺少参数');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        // 从数据库中获取所有数据
							 | 
						||
| 
								 | 
							
								        $data = Db::name('auth_rule')->where('id', $id)->find();
							 | 
						||
| 
								 | 
							
								 
							 | 
						||
| 
								 | 
							
								        return $this->success('请求成功',$data);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     *添加数据
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function create()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $data = $this->request->post();
							 | 
						||
| 
								 | 
							
								        $data['createtime'] = date('Y-m-d H:i:s');
							 | 
						||
| 
								 | 
							
								        $data['updatetime'] = date('Y-m-d H:i:s');
							 | 
						||
| 
								 | 
							
								        $result = Db::name('auth_rule')->strict(false)->insert($data);
							 | 
						||
| 
								 | 
							
								        if ($result) {
							 | 
						||
| 
								 | 
							
								            return $this->success('添加成功',$result);
							 | 
						||
| 
								 | 
							
								        } else {
							 | 
						||
| 
								 | 
							
								            return $this->error('添加失败',$result);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 更新记录
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param Request $request
							 | 
						||
| 
								 | 
							
								     * @param int $id
							 | 
						||
| 
								 | 
							
								     * @return \think\Response
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function update()
							 | 
						||
| 
								 | 
							
								    {   
							 | 
						||
| 
								 | 
							
								        $id = $this->request->post('id');
							 | 
						||
| 
								 | 
							
								        $data = $this->request->post();
							 | 
						||
| 
								 | 
							
								        $data['updatetime'] = date('Y-m-d H:i:s');
							 | 
						||
| 
								 | 
							
								        $result = Db::name('auth_rule')->where('id', $id)->strict(false)->update($data);
							 | 
						||
| 
								 | 
							
								        if ($result) {
							 | 
						||
| 
								 | 
							
								            return $this->success('更新成功',$result);
							 | 
						||
| 
								 | 
							
								        } else {
							 | 
						||
| 
								 | 
							
								            return $this->error('更新失败',$result);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 删除记录
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param int $id
							 | 
						||
| 
								 | 
							
								     * @return \think\Response
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function delete()
							 | 
						||
| 
								 | 
							
								    {   
							 | 
						||
| 
								 | 
							
								        $id = $this->request->post('id');
							 | 
						||
| 
								 | 
							
								        if(!$id){
							 | 
						||
| 
								 | 
							
								            return $this->error('缺少参数');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        $result = Db::name('auth_rule')->delete($id);
							 | 
						||
| 
								 | 
							
								        if ($result) {
							 | 
						||
| 
								 | 
							
								            return $this->success('删除成功',$result);
							 | 
						||
| 
								 | 
							
								        } else {
							 | 
						||
| 
								 | 
							
								            return $this->error('删除失败',$result);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								}
							 |