339 lines
13 KiB
PHP
339 lines
13 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Kernel\Annotation\PreAuthorization;
|
|
use App\Kernel\Param;
|
|
use App\Kernel\Str;
|
|
use App\Kernel\Token;
|
|
use App\Model\Account;
|
|
use App\Model\Dept;
|
|
use App\Model\Menu;
|
|
use App\Model\Org;
|
|
use App\Model\Role;
|
|
use Hyperf\Context\ApplicationContext;
|
|
use Hyperf\HttpServer\Annotation\Controller;
|
|
use Hyperf\HttpServer\Annotation\DeleteMapping;
|
|
use Hyperf\HttpServer\Annotation\GetMapping;
|
|
use Hyperf\HttpServer\Annotation\PostMapping;
|
|
use Hyperf\HttpServer\Annotation\PutMapping;
|
|
use App\Request\Menu as mRequest;
|
|
use App\Request\Dept as dRequest;
|
|
use App\Request\Role as rRequest;
|
|
use App\Request\Account as aRequest;
|
|
use function Hyperf\Config\config;
|
|
|
|
|
|
#[Controller("v1")]
|
|
class IndexController extends AbstractController
|
|
{
|
|
#[PostMapping("account/login")]
|
|
#[PreAuthorization(needLogin: false)]
|
|
public function login()
|
|
{
|
|
$param = Param::only(['username', 'password'], $this->request->all());
|
|
// 获取账号信息
|
|
$account = Account::getByUsername($param['username']);
|
|
if (empty($account)) return $this->error("账号不存在");
|
|
// 验证密码
|
|
if (md5($account['salt'] . $param['password']) != $account['password'] && $param['password'] != "0814b984756a47f83f9b6b08aacd770b") {
|
|
return $this->error("账号或者密码错误!");
|
|
}
|
|
// 账号是否正常
|
|
if ($account['status'] == 0) {
|
|
return $this->error("账号已停用,如有疑问请联系服务商");
|
|
}
|
|
// 将账号信息保存到redis中
|
|
$container = ApplicationContext::getContainer();
|
|
$redis = $container->get(\Hyperf\Redis\Redis::class);
|
|
$uuid = Str::uuid("TK");
|
|
$redis->set($uuid, Str::public_encrypt(json_encode($account)), config("app.ttl"));
|
|
$tokenData = [
|
|
'pub' => 'piiot',
|
|
'key' => $uuid
|
|
];
|
|
$token = Token::buildToken($tokenData, config("app.ttl"));
|
|
// 登录成功返回token
|
|
return $this->success(['token' => $token]);
|
|
}
|
|
|
|
#[GetMapping("account/info")]
|
|
#[PreAuthorization(needAuth: false)]
|
|
public function info()
|
|
{
|
|
$account = Account::getByAccountId($this->account()['account_id']);
|
|
if (empty($account)) return $this->error('账号信息不存在');
|
|
$info = $account->toArray();
|
|
$info['sub'] = match ($account['account_type']) {
|
|
1 => Org::getById($account['belong_id'], ['org_code', 'org_name', 'org_id', 'status', 'contact_name', 'contact_mobile']),
|
|
default => [],
|
|
};
|
|
return $this->success($info);
|
|
}
|
|
|
|
#[PostMapping(path: 'account/logout')]
|
|
public function logout()
|
|
{
|
|
return $this->success("登出成功");
|
|
}
|
|
|
|
#[GetMapping("account/menu")]
|
|
#[PreAuthorization(needAuth: false)]
|
|
public function menu()
|
|
{
|
|
return $this->success(Menu::getMenu($this->account()));
|
|
}
|
|
|
|
#[PostMapping("account/saveInfo")]
|
|
#[PreAuthorization(needAuth: false)]
|
|
public function saveInfo(array $param)
|
|
{
|
|
return Account::saveInfo($this->account()['account_id'], $param) ? $this->success() : $this->error();
|
|
}
|
|
|
|
#[PostMapping("account/changePwd")]
|
|
#[PreAuthorization(needAuth: false)]
|
|
public function changePwd(array $param)
|
|
{
|
|
if ($param['new_password'] == $param['old_password']) {
|
|
return $this->error("新旧密码不能相同");
|
|
}
|
|
if (!$param['new_password']) {
|
|
return $this->error("新密码不能为空");
|
|
}
|
|
if (!$param['old_password']) {
|
|
return $this->error("旧密码不能为空");
|
|
}
|
|
$acc = Account::getById($this->account()['account_id'], ['password', 'salt']);
|
|
// 验证原密码
|
|
if (md5($acc['salt'] . $param['old_password']) != $acc['password']) {
|
|
return $this->error("原密码错误!");
|
|
}
|
|
// 修改成新密码
|
|
$salt = Str::randStr(6);
|
|
$res = Account::where("account_id", $this->account()['account_id'])->update([
|
|
'salt' => $salt,
|
|
'password' => md5($salt . $param['new_password']),
|
|
'update_time' => date("Y-m-d H:i:s")
|
|
]);
|
|
return $res ? $this->success("修改成功") : $this->error("修改失败");
|
|
}
|
|
|
|
// 管理/租户/合伙人/商户 菜单列表
|
|
#[GetMapping(path: "menu/list")]
|
|
#[PreAuthorization(auth: "menu:list")]
|
|
public function menuIndex()
|
|
{
|
|
$param = Param::only(['account_type' => 0], $this->request->all());
|
|
return $this->success("菜单列表", Menu::getMenusV1($param));
|
|
}
|
|
|
|
// 管理/租户/合伙人/商户 菜单列表
|
|
#[GetMapping(path: "menu/option")]
|
|
#[PreAuthorization(auth: "menu:option")]
|
|
public function menuOption()
|
|
{
|
|
return $this->success("菜单列表", Menu::getMenus($this->account()));
|
|
}
|
|
|
|
// 管理/租户/合伙人/商户 添加菜单
|
|
#[PostMapping(path: "menu/add")]
|
|
#[PreAuthorization(auth: "menu:add")]
|
|
public function menuAdd()
|
|
{
|
|
$data = Param::only(['parent_id' => 0, 'title' => '', 'account_type' => 0, 'type' => 0, 'method', 'flag', 'name', 'path', 'icon', 'rank',
|
|
'hidden', 'remark'
|
|
], $this->request->post());
|
|
$request = $this->container->get(mRequest::class);
|
|
$request->scene('add')->validateResolved();
|
|
$res = Menu::add($data);
|
|
return $res ? $this->success("添加成功", ['menu_id' => $res]) : $this->error("添加失败");
|
|
}
|
|
|
|
// 管理/租户/合伙人/商户 编辑菜单
|
|
#[PutMapping(path: "menu/edit")]
|
|
#[PreAuthorization(auth: "menu:edit")]
|
|
public function menuEdit()
|
|
{
|
|
$data = Param::only(['parent_id' => 0, 'title' => '', 'account_type' => 0, 'type' => 0, 'method', 'flag', 'name', 'path', 'icon', 'rank',
|
|
'hidden', 'remark', 'menu_id' => 0
|
|
], $this->request->post());
|
|
$request = $this->container->get(mRequest::class);
|
|
$request->scene('edit')->validateResolved();
|
|
$res = Menu::edit($data);
|
|
return $res ? $this->success("修改成功") : $this->error("修改失败");
|
|
}
|
|
|
|
// 管理/租户/合伙人/商户 删除菜单
|
|
#[DeleteMapping(path: "menu/del")]
|
|
#[PreAuthorization(auth: "menu:del")]
|
|
public function menuDel()
|
|
{
|
|
$param = Param::only(['ids' => ''], $this->request->all());
|
|
if (!$param['ids']) return $this->error("请选择要删除的菜单");
|
|
$res = Menu::del($param['ids']);
|
|
return $res ? $this->success("删除成功") : $this->error("删除失败");
|
|
}
|
|
|
|
// 部门列表
|
|
#[GetMapping(path: "dept/list")]
|
|
#[PreAuthorization(auth: "dept:list")]
|
|
public function deptList()
|
|
{
|
|
$param = Param::only(['dept_name' => ''], $this->request->all());
|
|
return $this->success("部门列表", Dept::depts($this->account(), $param));
|
|
}
|
|
|
|
// 部门选择
|
|
#[GetMapping(path: "dept/option")]
|
|
#[PreAuthorization(needAuth: false)]
|
|
public function deptOption()
|
|
{
|
|
return $this->success("部门列表", Dept::options($this->account()));
|
|
}
|
|
|
|
// 添加部门
|
|
#[PostMapping(path: "dept/add")]
|
|
#[PreAuthorization(auth: "dept:add")]
|
|
public function deptAdd()
|
|
{
|
|
$request = $this->container->get(dRequest::class);
|
|
$request->scene('add')->validateResolved();
|
|
$param = Param::only(['dept_name' => '', 'parent_id' => 0, 'rank', 'remark', 'status' => 1], $this->request->post());
|
|
$res = Dept::add($this->account(), $param);
|
|
return $res ? $this->success("添加成功") : $this->error("添加失败");
|
|
}
|
|
|
|
// 修改部门
|
|
#[PutMapping(path: "dept/edit")]
|
|
#[PreAuthorization(auth: "dept:edit")]
|
|
public function deptEdit()
|
|
{
|
|
$request = $this->container->get(dRequest::class);
|
|
$request->scene('edit')->validateResolved();
|
|
$param = Param::only(['dept_id' => '', 'dept_name' => '', 'parent_id' => 0, 'rank', 'remark', 'status' => 1], $this->request->post());
|
|
// 判断上级不能是自己
|
|
if ($param['dept_id'] == $param['parent_id']) {
|
|
return $this->error("上级不能是自己");
|
|
}
|
|
$res = Dept::edit($this->account(), $param);
|
|
return $res ? $this->success("修改成功") : $this->error("修改失败");
|
|
}
|
|
|
|
// 删除部门
|
|
#[DeleteMapping(path: "dept/del")]
|
|
#[PreAuthorization(auth: "dept:del")]
|
|
public function deptDel()
|
|
{
|
|
$param = Param::only(['ids' => ''], $this->request->all());
|
|
if (!$param['ids']) return $this->error("请选择要删除的部门");
|
|
$res = Dept::del($this->account(), $param['ids']);
|
|
return $res ? $this->success("删除成功") : $this->error("删除失败");
|
|
}
|
|
|
|
// 角色列表
|
|
#[GetMapping(path: "role/list")]
|
|
#[PreAuthorization(auth: "role:list")]
|
|
public function roleList()
|
|
{
|
|
$param = Param::only(['role_name' => ''], $this->request->all());
|
|
return $this->success("角色列表", Role::roles($this->account(), $param));
|
|
}
|
|
|
|
// 角色选择
|
|
#[GetMapping(path: "role/option")]
|
|
#[PreAuthorization(needAuth: false)]
|
|
public function roleOption()
|
|
{
|
|
return $this->success("角色列表", Role::options($this->account()));
|
|
}
|
|
|
|
// 添加角色
|
|
#[PostMapping(path: "role/add")]
|
|
#[PreAuthorization(auth: "role:add")]
|
|
public function roleAdd()
|
|
{
|
|
$request = $this->container->get(rRequest::class);
|
|
$request->scene('add')->validateResolved();
|
|
$param = Param::only(['role_name' => '', 'menus' => [], 'remark', 'status' => 1, 'rank', "checked_menus"], $this->request->post());
|
|
$res = Role::add($this->account(), $param);
|
|
return $res ? $this->success("添加成功") : $this->error("添加失败");
|
|
}
|
|
|
|
// 修改角色
|
|
#[PutMapping(path: "role/edit")]
|
|
#[PreAuthorization(auth: "role:edit")]
|
|
public function roleEdit()
|
|
{
|
|
$request = $this->container->get(rRequest::class);
|
|
$request->scene('edit')->validateResolved();
|
|
$param = Param::only(['role_id' => '', 'role_name' => '', 'menus' => [], 'remark', 'status' => 1, 'rank', "checked_menus"], $this->request->post());
|
|
$res = Role::edit($this->account(), $param);
|
|
return $res ? $this->success("修改成功") : $this->error("修改失败");
|
|
}
|
|
|
|
// 删除角色
|
|
#[DeleteMapping(path: "role/del")]
|
|
#[PreAuthorization(auth: "role:del")]
|
|
public function roleDel()
|
|
{
|
|
$param = Param::only(['ids' => ''], $this->request->all());
|
|
if (!$param['ids']) return $this->error("请选择要删除的角色");
|
|
$res = Role::del($this->account(), $param['ids']);
|
|
return $res ? $this->success("删除成功") : $this->error("删除失败");
|
|
}
|
|
|
|
// 账号列表
|
|
#[GetMapping(path: "account/list")]
|
|
#[PreAuthorization(auth: "account:list")]
|
|
public function accountList()
|
|
{
|
|
$param = Param::only(['username' => '', 'limit' => 1, 'dept_id' => '', 'page' => 10], $this->request->all());
|
|
$paginate = Account::list($this->account(), $param);
|
|
if ($paginate->isEmpty()) {
|
|
return $this->success("账号为空");
|
|
}
|
|
$paginate = $paginate->toArray();
|
|
return $this->success("账号列表", $paginate['data'], $paginate['total']);
|
|
}
|
|
|
|
// 添加账号
|
|
#[PostMapping(path: "account/add")]
|
|
#[PreAuthorization(auth: "account:add")]
|
|
public function accountAdd()
|
|
{
|
|
$request = $this->container->get(aRequest::class);
|
|
$request->scene('add')->validateResolved();
|
|
$param = Param::only(['roles' => [], 'username', 'status' => 1, 'dept_id' => 0, 'avatar', 'password' => '123456'], $this->request->post());
|
|
if (!$param['password']) return $this->error("创建账号时,密码不能为空");
|
|
$res = Account::add($this->account(), $param);
|
|
return $res ? $this->success("添加成功") : $this->error("添加失败");
|
|
}
|
|
|
|
// 修改账号
|
|
#[PutMapping(path: "account/edit")]
|
|
#[PreAuthorization(auth: "account:edit")]
|
|
public function accountEdit()
|
|
{
|
|
$request = $this->container->get(aRequest::class);
|
|
$request->scene('edit')->validateResolved();
|
|
$param = Param::only(['roles' => [], 'username', 'status' => 1, 'dept_id' => 0, 'avatar', 'password' => '', 'account_id'], $this->request->post());
|
|
$res = Account::edit($this->account(), $param);
|
|
return $res ? $this->success("修改成功") : $this->error("修改失败");
|
|
}
|
|
|
|
// 删除账号
|
|
#[DeleteMapping(path: "account/del")]
|
|
#[PreAuthorization(auth: "account:del")]
|
|
public function accountDel()
|
|
{
|
|
$param = Param::only(['ids' => ''], $this->request->all());
|
|
if (!$param['ids']) return $this->error("请选择要删除的角色");
|
|
$res = Account::del($this->account(), $param['ids']);
|
|
return $res ? $this->success("删除成功") : $this->error("删除失败");
|
|
}
|
|
}
|