iot-admin/app/api/controller/IndexController.php
2023-12-03 23:09:30 +08:00

93 lines
3.2 KiB
PHP

<?php
namespace app\api\controller;
use app\BaseController;
use Illuminate\Support\Env;
use mon\util\exception\UploadException;
use Mongdch\WebmanUploadslice\UploadSlice;
use support\Log;
use support\Request;
use mon\util\Validate;
use think\facade\Db;
class IndexController extends BaseController{
public function index(){
}
public function demo1(){
$parmas=$this->request->post();
Log::error('测试',$parmas);
return json(['msg'=>'ok']);
}
public function mqtt(){
$parmas=$this->request->post();
if(!$parmas|| !isset($parmas['username']) || $parmas['username']==''){
return json(['msg'=>'参数错误']);
}
$data=[
'username'=>$parmas['username'],
'topic'=>$parmas['topic'],
'qos'=>$parmas['qos'],
'data'=>$parmas['payload'],
'clientid'=>$parmas['clientid'],
'create_time'=>date('Y-m-d H:i:s'),
];
$res=Db::name('msg')->insert($data);
if($res){
return json(['msg'=>'ok']);
}else{
return json(['msg'=>'添加失败']);
}
}
public function upload(Request $request){
return json(['code' => 0, 'msg' => 'upload faild']);
$data = $request->post();
// 验证数据
$validate = new Validate();
$check = $validate->data($data)->rule([
'action' => ['in:slice,merge'],
'filename' => ['required', 'str'],
'chunk' => ['int', 'min:0'],
'chunkLength' => ['required', 'int', 'min:0'],
'uuid' => ['required', 'str']
])->message([
'action' => 'action faild',
'filename' => 'filename faild',
'chunk' => 'chunk faild',
'chunkLength' => 'chunkLength faild',
'uuid' => 'uuid faild'
])->check();
if (!$check) {
return json(['code' => 0, 'msg' => $validate->getError()]);
}
// 验证上传分片必须的参数
if ($request->post('action') == 'slice' && is_null($request->post('chunk'))) {
return json(['code' => 0, 'msg' => 'chunk required']);
}
if ($request->post('action') == 'slice' && empty($request->file())) {
return json(['code' => 0, 'msg' => 'upload faild']);
}
// 上传
$sdk = new UploadSlice();
$file = $request->file('file');
try {
if ($data['action'] == 'slice') {
// 保存分片
$saveInfo = $sdk->upload($data['uuid'], $file, $data['chunk']);
return json(['code' => 1, 'msg' => 'ok', 'data' => $saveInfo]);
}
// 合并
$mergeInfo = $sdk->merge($data['uuid'], $data['chunkLength'], $data['filename']);
// $mergeInfo = $sdk->merge($data['uuid'], $data['chunkLength'], $data['filename'], 'dirname');
// return json(['code' => 1, 'msg' => 'ok', 'data' =>Env('URL'). $mergeInfo]);
} catch (UploadException $e) {
return json(['code' => 0, 'msg' => $e->getMessage()]);
}
return json($sdk->getConfig());
}
}