Files
YwxAppThink/addon/iot/controller/backend/Devices.php
T
yangwx 18101cf11a feat: 上线前安全加固与发布工程化;appmall 由插件迁移为独立应用
安全(P0)
- 发布包签名:新增 scripts/package_keygen.php、scripts/package_verify.php、
  ywxapp\service\PackageVerifier;FrameworkService::apply() 解压前强制验签
  (ywxapp.framework_package_verify,默认开启)
- JWT 收紧:config/jwt.php 移除公开默认密钥,JwtService::assertStrongSecret()
  校验非空 / 长度≥32 / 已知弱值;.env.production.example 键名由 JWT_SECRET_KEY 修正为 JWT_SECRET
- /api/v1/Article:免登录白名单收窄为只读接口,写操作字段白名单 + assertOwner() 越权校验
- 开发者令牌:查找接口不再回显明文、统一失败文案 + 限流
- TemplateInstaller:文件名白名单 + realpath 归一化 + 目录内断言,修复 Zip Slip;补 use ZipArchive
- 后台登录:接入验证码、失败锁定与登录限流(config/ratelimit.php)
- 缓存 / Session 驱动可由 .env 覆盖(CACHE_DRIVER / SESSION_TYPE),集群部署改 redis
- paycenter 支付日志开关改为 PAY_LOG,不再跟随 APP_DEBUG

工程化 / 交付
- 新增 CHANGELOG.md 与 .env.production.example;composer.lock 入库锁定依赖版本
- 插件离线安装 CLI 增加 --uid,后台上传安装透传 uid,付费插件授权校验可通过
- 授权拒绝文案区分「未携带 uid」与「未找到有效授权」,便于排障
- .gitignore 放行 .env.production.example / .env.addon.example / .gitattributes / public/.htaccess

结构
- appmall 由 addon/appmall 迁移为独立应用 app/appmall(市场 / 开发者中心 / 审核 / 分润)
2026-10-22 00:53:39 +08:00

191 lines
6.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
// +----------------------------------------------------------------------
// | YwxApp [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: ywxapp<admin@ywxapp.cn>
// +----------------------------------------------------------------------
declare(strict_types=1);
namespace addon\iot\controller\backend;
use think\facade\Db;
use think\facade\View;
use ywxapp\controller\BackendBase;
use addon\iot\service\DeviceService;
use addon\iot\service\DeviceLogService;
use addon\iot\service\SensorConfigService;
/**
* 智能设备控制器(绑定表,预登记 SN → App 扫码绑定激活)
*/
class Devices extends BackendBase
{
/**
* 设备管理页(layui.table 前端拉取)
*/
public function device()
{
return View::fetch('admin/device');
}
/**
* 设备列表(layui.table 分页 JSON,支持关键词过滤)
*/
public function deviceTable()
{
$kw = trim((string) input('get.kw', ''));
$page = max(1, (int) input('get.page', 1));
$limit = (int) input('get.limit', 20);
if ($limit < 1 || $limit > 100) {
$limit = 20;
}
$q = Db::name('iot_device');
if ($kw !== '') {
$q->where(function ($query) use ($kw) {
$query->whereLike('sn', "%{$kw}%")
->whereOr('title', 'like', "%{$kw}%")
->whereOr('uuid', 'like', "%{$kw}%");
});
}
$count = (int) (clone $q)->count();
$rows = $q->order('id', 'desc')->page($page, $limit)->select()->toArray();
foreach ($rows as &$d) {
$d['online'] = DeviceService::isOnline((string) $d['uuid']);
$d['member_account'] = (int) $d['member_id'] > 0
? DeviceService::memberAccount((int) $d['member_id'])
: '';
unset($d['secret']);
}
unset($d);
$this->result->success(['list' => $rows, 'count' => $count]);
}
/**
* 登记/编辑设备(华为式:新增=预登记SN,自动生成配对码+激活令牌,绑定激活由设备/App 完成)
*/
public function saveDevice()
{
$id = (int) input('post.id', 0);
$sn = trim((string) input('post.sn', ''));
$title = trim((string) input('post.title', ''));
$model = trim((string) input('post.model', ''));
$room = trim((string) input('post.room', ''));
$status = (int) input('post.status', 1);
$pairCode = trim((string) input('post.pair_code', ''));
if ($title === '') {
$this->result->error('设备名称不能为空');
}
try {
if ($id > 0) {
$update = [
'title' => $title, 'model' => $model, 'room' => $room,
'status' => $status ? 1 : 0, 'update_at' => time(),
];
if ($pairCode !== '') {
$update['pair_code'] = $pairCode;
}
Db::name('iot_device')->where('id', $id)->update($update);
} else {
$res = DeviceService::register($sn, $title, $model, $room, $pairCode);
$this->result->success($res, '登记成功');
}
} catch (\Throwable $e) {
$this->result->error('保存失败:' . $e->getMessage());
}
$this->result->success('保存成功(预登记设备由设备激活/App 扫码绑定)');
}
/**
* 重置设备密钥(返回新明文,前端展示一次)
*/
public function resetDeviceSecret()
{
$uuid = trim((string) input('post.uuid', ''));
try {
$res = DeviceService::resetSecret($uuid);
} catch (\Throwable $e) {
$this->result->error($e->getMessage());
}
$this->result->success($res);
}
/**
* 重新生成激活令牌(仅未激活设备,按 SN 定位;已激活设备如需换凭证走 resetDeviceSecret)
*/
public function resetActivateToken()
{
$sn = trim((string) input('post.sn', ''));
try {
$res = DeviceService::resetActivateToken($sn);
} catch (\Throwable $e) {
$this->result->error($e->getMessage());
}
$this->result->success($res);
}
/**
* 删除设备(含 MQTT账号/ACL/影子/订阅/离线队列)
*/
public function deleteDevice()
{
$uuid = trim((string) input('post.uuid', ''));
DeviceService::remove($uuid);
$this->result->success('已删除(含MQTT账号/ACL/影子/订阅)');
}
/**
* 读取设备传感器配置(配置真源在服务端:后台编辑 → retained 下发设备 + App 共用同一份)
*/
public function deviceConfig()
{
$uuid = trim((string) input('get.uuid', ''));
if ($uuid === '' || !DeviceService::findByUuid($uuid)) {
$this->result->error('设备不存在或未激活(未分配 uuid)');
}
$cfg = SensorConfigService::get($uuid);
$cfg['uuid'] = $uuid;
$this->result->success($cfg);
}
/**
* 保存设备传感器配置(保存即生效:立即入 retained 下发队列,设备在线秒级生效、离线上线即生效)
* param: uuid(必填), sensors(JSON 数组:{index,alias,probes,upper,lower,gap,enabled})
* 位置序号键统一 `index`(旧键 `i` 仍兼容)
*/
public function saveDeviceConfig()
{
$uuid = trim((string) input('post.uuid', ''));
if ($uuid === '' || !DeviceService::findByUuid($uuid)) {
$this->result->error('设备不存在或未激活(未分配 uuid)');
}
$sensors = input('post.sensors', '');
if (is_string($sensors)) {
$sensors = json_decode($sensors, true);
}
if (!is_array($sensors)) {
$this->result->error('sensors 参数须为 JSON 数组');
}
try {
$cfg = SensorConfigService::save($uuid, $sensors);
} catch (\Throwable $e) {
$this->result->error($e->getMessage());
}
$cfg['uuid'] = $uuid;
$this->result->success($cfg, '配置已保存并下发(设备离线则上线后自动生效)');
}
/**
* 设备日志(JSON,按设备 uuid 分页)
*/
public function deviceLogs()
{
$uuid = trim((string) input('get.uuid', ''));
$page = max(1, (int) input('get.page', 1));
$size = min(100, max(1, (int) input('get.size', 20)));
$data = $uuid !== '' ? DeviceLogService::page($uuid, $page, $size) : ['list' => [], 'total' => 0];
$this->result->success($data);
}
}