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){ $file = $request->file('file'); if ($file) { $time=time(); $file->move(public_path() . DIRECTORY_SEPARATOR . 'uploads/file/'.date('Y-m-d').'/'.$time.'.'.$file->getUploadExtension()); return json(['code' => 0, 'msg' => 'upload success','data'=>Env('URL').'/uploads/file/'.date('Y-m-d').'/'.$time.'.'. $file->getUploadExtension()]); } return json(['code' => 1, 'msg' => 'file not found']); // 验证数据 $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' =>$mergeInfo]); return json(['code' => 1, 'msg' => 'ok', 'data' =>Env('URL').'/uploads/'.date('Y-m-d').'/'. $mergeInfo['fileName']]); } catch (UploadException $e) { return json(['code' => 0, 'msg' => $e->getMessage()]); } return json($sdk->getConfig()); } public function file_list(Request $request){ $post=$request->post('data'); if($post){ return json(['msg'=>'ok','data'=>$post]); } return json(['msg'=>'失败']); } }