群控系统服务端开发模式后端基础框架现在就差个人资料、及权限验证。下面开发个人资料。
一、获取个人信息
1、添加路由
在根目录下route文件夹下面app.php文件中添加如下代码:
Route::post('member/personal_data','permission.Member/personalData');// 获取个人信息接口
2、添加控制层管理及对应方法
在根目录下app文件夹下controller文件夹下permission文件夹中创建个人资料控制文件并命名为Member,代码如下:
<?php
/**
* 个人信息控制-也就是登录者信息及角色对外方法
* User: 龙哥·三年风水
* Date: 2024/10/30
* Time: 14:18
*/
namespace app\controller\permission;
use app\controller\Base;
use app\model\param\Department as PDModel;
use app\model\param\Grade as PGModel;
use app\model\permission\Role as PRModel;
use app\model\param\System as PSModel;
use app\model\permission\Admin as PAModel;
use Other\Tree;
use Redis\Redis;
use app\model\permission\Menu as PMModel;
class Member extends Base
{
/**
* 获取个人信息
* User: 龙哥·三年风水
* Date: 2024/10/30
* Time: 14:23
* @ return \think\response\Json
*/
public function personalData(){
$data = [];
$data['username'] = $this->username;
$data['avatar'] = $this->avatar;
$data['email'] = $this->email;
$data['realname'] = $this->realname;
$data['ip'] = empty($this->ip) ? '' : $this->ip;
$departmentTitle = PDModel::dataFind(['id' => $this->departmentId],'title',false);
$data['department_title'] = empty($departmentTitle) ? '' : $departmentTitle;
$gradeTitle = PGModel::dataFind(['id' => $this->gradeId],'title',false);
$data['grade_title'] = empty($gradeTitle) ? '' : $gradeTitle;
$data['rolename'] = PRModel::dataFind(['id' => $this->roleId],'rolename',false);
$res = $this->getRoleMenu();
$data['key'] = $res['key'];
$data['butt'] = $res['butts'];
return succ('操作成功',$data);
}
}
二、获取登录者菜单
1、添加路由
在根目录下route文件夹下面app.php文件中添加如下代码:
Route::post('member/personal_menu','permission.Member/personalMenu');// 获取个人菜单接口
2、添加方法
在根目录下app文件夹下controller文件夹下permission文件夹下Member控制文件中添加获取个人菜单接口的方法,代码如下:
/**
* 登录者菜单权限
* User: 龙哥·三年风水
* Date: 2024/11/5
* Time: 17:32
* @ return \think\response\Json
*/
public function personalMenu(){
$listMenu = unserialize(Redis::select(config('cache.stores.redis.cache_db'))->get('menu_list'.$this->userId));
if($listMenu === false){
if(count($this->rules) == 0)$this->setAuth();
$listMenu = PMModel::getAll([['id', 'in', $this->rules], ['status', '=', 1], ['is_menu', '=', 1]], 'id,menuname,pid,title,path,component,icon,redirect,always_show,is_hidden,is_icon,is_cache','sort DESC,id DESC');
if(empty($listMenu)) return err("该用户没有权限");
$seconds = rand(60,120);
Redis::select(config('cache.stores.redis.cache_db'))->setex('menu_list'.$this->userId,$seconds,serialize($listMenu));
}
$data['menu'] = Tree::list_to_tree($listMenu,0, 'id', 'pid', 'children');
return succ('操作成功',$data);
}
三、修改个人信息
1、添加路由
在根目录下route文件夹下面app.php文件中添加如下代码:
Route::post('member/update_personal','permission.Member/updatePersonal');// 修改个人信息接口
2、添加方法
在根目录下app文件夹下controller文件夹下permission文件夹下Member控制文件中添加编辑个人信息接口的方法,代码如下:
/**
* 修改个人资料
* User: 龙哥·三年风水
* Date: 2024/11/5
* Time: 9:31
*/
public function updatePersonal(){
$param = $this->request->param();
$validate = new \app\validate\permission\UpdateAdmin;
$param['id'] = $this->userId;
if (!$validate->check($param)) return err($validate->getError());
if(!empty($param['password'])){
$securityPassword = PSModel::dataFind(['id' => 1],'security_password')['security_password'];
$data['password'] = sha1($param['password'] . $securityPassword);
}
$data['username'] = $param['username'];
$data['avatar'] = empty($param['avatar']) ? $this->avatar : $param['avatar'];
$data['realname'] = $param['realname'];
$data['email'] = $param['email'];
PAModel::redisSave($data, $this->userId);
return succ('操作成功',[]);
}