323 lines
12 KiB
PHP
323 lines
12 KiB
PHP
<?php
|
|
/**
|
|
* Author: cfn <cfn@leapy.cn>
|
|
*/
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Kernel\Annotation\PreAuthorization;
|
|
use App\Kernel\Param;
|
|
use App\Kernel\Str;
|
|
use App\Model\App;
|
|
use App\Model\Device;
|
|
use App\Model\Group;
|
|
use App\Model\Org;
|
|
use App\Model\Topic;
|
|
use Hyperf\HttpServer\Annotation\Controller;
|
|
use Hyperf\HttpServer\Annotation\DeleteMapping;
|
|
use Hyperf\HttpServer\Annotation\GetMapping;
|
|
use App\Request\Org as oRequest;
|
|
use App\Request\App as aRequest;
|
|
use Hyperf\HttpServer\Annotation\PostMapping;
|
|
use Hyperf\HttpServer\Annotation\PutMapping;
|
|
|
|
#[Controller("v1")]
|
|
class TenantController extends AbstractController
|
|
{
|
|
// 租户列表
|
|
#[GetMapping(path: "org/list")]
|
|
#[PreAuthorization(auth: "org:list")]
|
|
public function orgList()
|
|
{
|
|
$param = Param::only(['org_name' => '', 'limit' => 1, 'page' => 10], $this->request->all());
|
|
$paginate = Org::list($param);
|
|
if ($paginate->isEmpty()) {
|
|
return $this->success("机构为空");
|
|
}
|
|
$paginate = $paginate->toArray();
|
|
return $this->success("机构列表", $paginate['data'], $paginate['total']);
|
|
}
|
|
|
|
// 服务商列表
|
|
#[GetMapping(path: "org/option")]
|
|
#[PreAuthorization(needAuth: false)]
|
|
public function orgOption()
|
|
{
|
|
return $this->success("机构列表", Org::options());
|
|
}
|
|
|
|
// 添加账号
|
|
#[PostMapping(path: "org/add")]
|
|
#[PreAuthorization(auth: "org:add")]
|
|
public function orgAdd()
|
|
{
|
|
$request = $this->container->get(oRequest::class);
|
|
$request->scene('add')->validateResolved();
|
|
$param = Param::only(['org_name', 'contact_name','contact_mobile', 'status', 'password' => '123456'], $this->request->post());
|
|
if (!$param['password']) return $this->error("创建机构时,密码不能为空");
|
|
$res = Org::add($param);
|
|
return $res ? $this->success("添加成功") : $this->error("添加失败");
|
|
}
|
|
|
|
// 修改账号
|
|
#[PutMapping(path: "org/edit")]
|
|
#[PreAuthorization(auth: "org:edit")]
|
|
public function orgEdit()
|
|
{
|
|
$request = $this->container->get(oRequest::class);
|
|
$request->scene('edit')->validateResolved();
|
|
$param = Param::only(['org_id', 'org_name', 'contact_name','contact_mobile', 'status', 'password' => '123456'], $this->request->post());
|
|
$res = Org::edit($param);
|
|
return $res ? $this->success("修改成功") : $this->error("修改失败");
|
|
}
|
|
|
|
// 删除账号
|
|
#[DeleteMapping(path: "org/del")]
|
|
#[PreAuthorization(auth: "org:del")]
|
|
public function orgDel()
|
|
{
|
|
$param = Param::only(['ids' => ''], $this->request->all());
|
|
if (!$param['ids']) return $this->error("请选择要删除的机构");
|
|
$res = Org::del($param['ids']);
|
|
return $res ? $this->success("删除成功") : $this->error("删除失败");
|
|
}
|
|
|
|
// 应用列表
|
|
#[GetMapping(path: "app/list")]
|
|
#[PreAuthorization(auth: "app:list", role: "ORG")]
|
|
public function appList()
|
|
{
|
|
$param = Param::only(['app_name' => '', 'page' => 1, 'limit' => 10], $this->request->all());
|
|
$paginate = App::list($this->account(), $param);
|
|
if ($paginate->isEmpty()) {
|
|
return $this->success("应用为空");
|
|
}
|
|
$paginate = $paginate->toArray();
|
|
return $this->success("应用列表", $paginate['data'], $paginate['total']);
|
|
}
|
|
|
|
// 应用列表
|
|
#[GetMapping(path: "app/option")]
|
|
#[PreAuthorization(needAuth: false)]
|
|
public function appOption()
|
|
{
|
|
return $this->success("应用列表", App::options($this->account()));
|
|
}
|
|
|
|
// 添加应用
|
|
#[PostMapping(path: "app/add")]
|
|
#[PreAuthorization(auth: "app:add", role: "ORG")]
|
|
public function appAdd()
|
|
{
|
|
$request = $this->container->get(aRequest::class);
|
|
$request->scene('add')->validateResolved();
|
|
$param = Param::only(['app_name' => '', 'status' => 1], $this->request->post());
|
|
$param['org_id'] = $this->account()['belong_id'];
|
|
$param['create_time'] = date("Y-m-d H:i:s");
|
|
// 创建APPCODE和APPKEY
|
|
$param['app_code'] = md5(Str::randStr(8));
|
|
$param['app_key'] = md5(Str::randStr(8));
|
|
$res = App::insert($param);
|
|
return $res ? $this->success("添加成功") : $this->error("添加失败");
|
|
}
|
|
|
|
// 修改应用
|
|
#[PutMapping(path: "app/edit")]
|
|
#[PreAuthorization(auth: "app:edit", role: "ORG")]
|
|
public function appEdit()
|
|
{
|
|
$request = $this->container->get(aRequest::class);
|
|
$request->scene('edit')->validateResolved();
|
|
$param = Param::only(['app_id' => '', 'app_name' => '', 'status' => 1], $this->request->post());
|
|
$where = ['del_flag' => 0, 'org_id' => $this->account()['belong_id'], 'app_id' => $param['app_id']];
|
|
$param['update_time'] = date("Y-m-d H:i:s");
|
|
$res = App::where($where)->update($param);
|
|
return $res ? $this->success("修改成功") : $this->error("修改失败");
|
|
}
|
|
|
|
// 删除应用
|
|
#[DeleteMapping(path: "app/del")]
|
|
#[PreAuthorization(auth: "app:del", role: "ORG")]
|
|
public function appDel()
|
|
{
|
|
$param = Param::only(['ids' => ''], $this->request->all());
|
|
if (!$param['ids']) return $this->error("请选择要删除的应用");
|
|
$res = App::del($this->account(), $param['ids']);
|
|
return $res ? $this->success("删除成功") : $this->error("删除失败");
|
|
}
|
|
|
|
// 应用列表
|
|
#[GetMapping(path: "group/list")]
|
|
#[PreAuthorization(auth: "group:list", role: "ORG")]
|
|
public function groupList()
|
|
{
|
|
$param = Param::only(['group_name' => '', 'page' => 1, 'limit' => 10], $this->request->all());
|
|
$paginate = Group::list($this->account(), $param);
|
|
if ($paginate->isEmpty()) {
|
|
return $this->success("分组为空");
|
|
}
|
|
$paginate = $paginate->toArray();
|
|
return $this->success("分组列表", $paginate['data'], $paginate['total']);
|
|
}
|
|
|
|
// 应用列表
|
|
#[GetMapping(path: "group/option")]
|
|
#[PreAuthorization(needAuth: false)]
|
|
public function groupOption()
|
|
{
|
|
return $this->success("分组列表", Group::options($this->account()));
|
|
}
|
|
|
|
// 添加应用
|
|
#[PostMapping(path: "group/add")]
|
|
#[PreAuthorization(auth: "group:add", role: "ORG")]
|
|
public function groupAdd()
|
|
{
|
|
$param = Param::only(['group_name' => '', 'rank' => 1], $this->request->post());
|
|
if (!$param['group_name']) return $this->error("分组名称不能为空");
|
|
|
|
$param['org_id'] = $this->account()['belong_id'];
|
|
$param['create_time'] = date("Y-m-d H:i:s");
|
|
$res = Group::insert($param);
|
|
return $res ? $this->success("添加成功") : $this->error("添加失败");
|
|
}
|
|
|
|
// 修改应用
|
|
#[PutMapping(path: "group/edit")]
|
|
#[PreAuthorization(auth: "group:edit", role: "ORG")]
|
|
public function groupEdit()
|
|
{
|
|
$param = Param::only(['group_id' => '', 'group_name' => '', 'rank' => 1], $this->request->post());
|
|
if (!$param['group_name']) return $this->error("分组名称不能为空");
|
|
|
|
$where = ['del_flag' => 0, 'org_id' => $this->account()['belong_id'], 'group_id' => $param['group_id']];
|
|
$param['update_time'] = date("Y-m-d H:i:s");
|
|
$res = Group::where($where)->update($param);
|
|
return $res ? $this->success("修改成功") : $this->error("修改失败");
|
|
}
|
|
|
|
// 删除应用
|
|
#[DeleteMapping(path: "group/del")]
|
|
#[PreAuthorization(auth: "group:del", role: "ORG")]
|
|
public function groupDel()
|
|
{
|
|
$param = Param::only(['ids' => ''], $this->request->all());
|
|
if (!$param['ids']) return $this->error("请选择要删除的分组");
|
|
$res = Group::del($this->account(), $param['ids']);
|
|
return $res ? $this->success("删除成功") : $this->error("删除失败");
|
|
}
|
|
|
|
// 主题列表
|
|
#[GetMapping(path: "topic/list")]
|
|
#[PreAuthorization(auth: "topic:list", role: "ORG")]
|
|
public function topicList()
|
|
{
|
|
$param = Param::only(['group_name' => '', 'page' => 1, 'limit' => 10], $this->request->all());
|
|
$paginate = Topic::list($this->account(), $param);
|
|
if ($paginate->isEmpty()) {
|
|
return $this->success("主题为空");
|
|
}
|
|
$paginate = $paginate->toArray();
|
|
return $this->success("主题列表", $paginate['data'], $paginate['total']);
|
|
}
|
|
|
|
// 应用列表
|
|
#[GetMapping(path: "topic/option")]
|
|
#[PreAuthorization(needAuth: false)]
|
|
public function topicOption()
|
|
{
|
|
return $this->success("主题列表", Topic::options($this->account()));
|
|
}
|
|
|
|
// 添加应用
|
|
#[PostMapping(path: "topic/add")]
|
|
#[PreAuthorization(auth: "topic:add", role: "ORG")]
|
|
public function topicAdd()
|
|
{
|
|
$param = Param::only(['topic_name' => '', 'status' => 1, 'topic_code' => ''], $this->request->post());
|
|
if (!$param['topic_name']) return $this->error("主题名称不能为空");
|
|
if (!$param['topic_code']) return $this->error("主题编号不能为空");
|
|
|
|
$param['org_id'] = $this->account()['belong_id'];
|
|
$param['create_time'] = date("Y-m-d H:i:s");
|
|
$res = Topic::insert($param);
|
|
return $res ? $this->success("添加成功") : $this->error("添加失败");
|
|
}
|
|
|
|
// 修改应用
|
|
#[PutMapping(path: "topic/edit")]
|
|
#[PreAuthorization(auth: "topic:edit", role: "ORG")]
|
|
public function topicEdit()
|
|
{
|
|
$param = Param::only(['topic_id' => '', 'topic_name' => '', 'status' => 1, 'topic_code' => ''], $this->request->post());
|
|
if (!$param['topic_name']) return $this->error("主题名称不能为空");
|
|
if (!$param['topic_code']) return $this->error("主题编号不能为空");
|
|
|
|
$where = ['del_flag' => 0, 'org_id' => $this->account()['belong_id'], 'topic_id' => $param['topic_id']];
|
|
$param['update_time'] = date("Y-m-d H:i:s");
|
|
$res = Topic::where($where)->update($param);
|
|
return $res ? $this->success("修改成功") : $this->error("修改失败");
|
|
}
|
|
|
|
// 删除应用
|
|
#[DeleteMapping(path: "topic/del")]
|
|
#[PreAuthorization(auth: "topic:del", role: "ORG")]
|
|
public function topicDel()
|
|
{
|
|
$param = Param::only(['ids' => ''], $this->request->all());
|
|
if (!$param['ids']) return $this->error("请选择要删除的主题");
|
|
$res = Topic::del($this->account(), $param['ids']);
|
|
return $res ? $this->success("删除成功") : $this->error("删除失败");
|
|
}
|
|
|
|
// 列表
|
|
#[GetMapping(path: "device/list")]
|
|
#[PreAuthorization(auth: "device:list")]
|
|
public function deviceList()
|
|
{
|
|
$param = Param::only(['page' => 1, 'limit' => 10, 'device_sn', 'group_id', 'app_id'], $this->request->all());
|
|
$paginate = Device::pages($param);
|
|
if ($paginate->isEmpty()) {
|
|
return $this->success("暂无数据");
|
|
}
|
|
$paginate = $paginate->toArray();
|
|
return $this->success("设备列表", $paginate['data'], $paginate['total']);
|
|
}
|
|
|
|
// 添加
|
|
#[PostMapping(path: "device/add")]
|
|
#[PreAuthorization(auth: "device:add", role: "ORG")]
|
|
public function deviceAdd()
|
|
{
|
|
$data = Param::only(['device_sn', 'group_id', 'status' => 1, 'app_id'], $this->request->all());
|
|
if (!$data['device_sn']) return $this->error("设备SN存在");
|
|
$data['org_id'] = $this->account()['belong_id'];
|
|
$res = Device::add($data);
|
|
return $res ? $this->success("添加成功") : $this->error("添加失败");
|
|
}
|
|
|
|
// 修改
|
|
#[PutMapping(path: "device/edit")]
|
|
#[PreAuthorization(auth: "device:edit", role: "ORG")]
|
|
public function deviceEdit()
|
|
{
|
|
$data = Param::only(['device_sn', 'group_id', 'status' => 1, 'device_id', 'app_id'], $this->request->all());
|
|
if (!$data['device_sn']) return $this->error("设备SN存在");
|
|
$data['org_id'] = $this->account()['belong_id'];
|
|
$res = Device::edit($data);
|
|
return $res ? $this->success("修改成功") : $this->error("修改失败");
|
|
}
|
|
|
|
// 删除
|
|
#[DeleteMapping(path: "device/del")]
|
|
#[PreAuthorization(auth: "device:del", role: "ORG")]
|
|
public function deviceDel()
|
|
{
|
|
$param = Param::only(['ids' => ''], $this->request->all());
|
|
if (!$param['ids']) {
|
|
return $this->error("请选择要删除的设备");
|
|
}
|
|
$res = Device::del($this->account(), $param['ids']);
|
|
return $res ? $this->success("删除成功") : $this->error("删除失败");
|
|
}
|
|
} |