<?php
|
|
namespace app\branch\controller\auth;
|
|
use app\branch\model\branch\Access as AccessModel;
|
use app\common\model\settings\Setting as SettingModel;
|
use app\branch\controller\Controller;
|
use app\branch\model\auth\User as UserModel;
|
use app\branch\model\auth\Role;
|
use app\branch\model\auth\User as AuthUserModel;
|
|
/**
|
* 管理员
|
*/
|
class User extends Controller
|
{
|
/**
|
* 首页列表
|
* @return \think\response\Json
|
*/
|
public function index()
|
{
|
$model = new UserModel();
|
$list = $model->getList($this->postData(),$this->getBranchId());
|
// 角色列表
|
$roleList = (new Role())->getTreeData($this->getBranchId());
|
return $this->renderSuccess('', compact('list', 'roleList'));
|
}
|
|
|
/**
|
* 新增
|
* @return \think\response\Json
|
*/
|
public function add()
|
{
|
$data = $this->postData();
|
$data['branch_id'] = $this->getBranchId();
|
$model = new UserModel();
|
$num = $model->getUserName(['is_delete' => 0,'user_name' => $data['user_name']]);
|
if ($num > 0) {
|
return $this->renderError('用户名已存在');
|
}
|
if (!isset($data['access_id'])) {
|
return $this->renderError('请选择所属角色');
|
}
|
if ($data['confirm_password'] != $data['password']) {
|
return $this->renderError('确认密码和登录密码不一致');
|
}
|
$model = new UserModel();
|
if ($model->add($data)) {
|
return $this->renderSuccess('添加成功');
|
}
|
return $this->renderError($model->getError()?:'添加失败');
|
}
|
|
/**
|
* 修改信息
|
* @param $branch_user_id
|
* @return \think\response\Json
|
*/
|
public function editInfo($branch_user_id)
|
{
|
$info = UserModel::detail(['branch_user_id' => $branch_user_id], ['userRole', 'user']);
|
|
$role_arr = array_column($info->toArray()['userRole'], 'role_id');
|
|
$model = new Role();
|
// 角色列表
|
$roleList = $model->getTreeData($this->getBranchId());
|
// 绑定用户
|
$user = $info['user'];
|
return $this->renderSuccess('', compact('info', 'roleList', 'role_arr', 'user'));
|
}
|
|
/**
|
* 编辑
|
* @param $branch_user_id
|
* @return \think\response\Json
|
*/
|
public function edit($branch_user_id)
|
{
|
$data = $this->postData();
|
if($this->request->isGet()){
|
return $this->editInfo($branch_user_id);
|
}
|
|
$model = UserModel::detail($branch_user_id, ['user']);
|
if($data['user_name'] != $model['user_name']){
|
$num = $model->getUserName(['is_delete'=> 0,'user_name' => $data['user_name']]);
|
if ($num > 0) {
|
return $this->renderError('用户名已存在');
|
}
|
}
|
if (!isset($data['access_id'])) {
|
return $this->renderError('请选择所属角色');
|
}
|
if (isset($data['password']) && !empty($data['password'])) {
|
if (!isset($data['confirm_password'])) {
|
return $this->renderError('请输入确认密码');
|
} else {
|
if ($data['confirm_password'] != $data['password']) {
|
return $this->renderError('确认密码和登录密码不一致');
|
}
|
}
|
}
|
if (empty($data['password'])) {
|
if (isset($data['confirm_password']) && !empty($data['confirm_password'])) {
|
return $this->renderError('请输入登录密码');
|
}
|
}
|
|
// 更新记录
|
if ($model->edit($data)) {
|
return $this->renderSuccess('更新成功');
|
}
|
return $this->renderError($model->getError()?:'更新失败');
|
|
|
}
|
|
/**
|
* 删除
|
*/
|
public function delete($branch_user_id)
|
{
|
$model = new UserModel();
|
if ($model->del([
|
'branch_user_id' => $branch_user_id,
|
'branch_id' => $this->getBranchId()
|
])) {
|
return $this->renderSuccess('删除成功');
|
}
|
return $this->renderError('删除失败');
|
}
|
|
/**
|
* 获取角色菜单信息
|
*/
|
public function getRoleList()
|
{
|
$user = $this->branch['user'];
|
|
$user_info = (new AuthUserModel())->find($user['branch_user_id']);
|
|
if ($user_info['is_super'] == 1) {
|
$model = new AccessModel();
|
$menus = $model->getList($user['branch_type']);
|
} else {
|
$model = new AccessModel();
|
$menus = $model->getListByUser($user['branch_user_id']);
|
|
foreach ($menus as $key => $val) {
|
if ($val['redirect_name'] != $val['children'][0]['path']) {
|
$menus[$key]['redirect_name'] = $menus[$key]['children'][0]['path'];
|
}
|
}
|
}
|
return $this->renderSuccess('', compact('menus'));
|
}
|
|
/**
|
* 获取用户信息
|
*/
|
public function getUserInfo()
|
{
|
$branch = session('jjjshop_branch');
|
$user = [];
|
if (!empty($branch)) {
|
$user = $branch['user'];
|
}
|
// 商城名称
|
$shop_name = SettingModel::getItem('store')['name'];
|
//当前系统版本
|
$version = get_version();
|
return $this->renderSuccess('', compact('user', 'shop_name', 'version'));
|
}
|
}
|