273 lines
11 KiB
PHP
273 lines
11 KiB
PHP
<?php
|
||
|
||
namespace app\admin\controller;
|
||
|
||
use think\Controller;
|
||
use think\Db;
|
||
use app\bj\controller\Common;
|
||
use think\Log;
|
||
use \think\Validate;
|
||
|
||
class Device extends Base{
|
||
protected $page_num = 10;
|
||
protected $file_max = 1024*1024*5;//xxxMB
|
||
public function index($page = 1){
|
||
$data = input();
|
||
$pd = true;
|
||
$parameter = [];
|
||
// $parameter['is_del'] = 0;
|
||
if(array_key_exists('tt', $data)){
|
||
$page = $data['page_num'];
|
||
unset($data['page_num']);
|
||
unset($data['tt']);
|
||
$pd = false;
|
||
// if($data['status_num'] === "0" || $data['status_num'] === "1"){
|
||
// $parameter['is_del'] = $data['status_num'];
|
||
// }
|
||
|
||
// if($data['tel']){
|
||
// $parameter['tel'] = $data['tel'];
|
||
// }
|
||
// if($data['email']){
|
||
// $parameter['email'] = $data['email'];
|
||
// }
|
||
// if($data['s_time']){
|
||
// $parameter['create_time'] = ['>=',$data['s_time']];
|
||
// }
|
||
// if($data['e_time']){
|
||
// $parameter['create_time'] = ['<=',$data['e_time']];
|
||
// }
|
||
}
|
||
$num = Db::table('app_device_data')->where($parameter)->count();
|
||
$result = Db::table('app_device_data')->where($parameter)->order('is_del,id desc')->page($page,$this->page_num)->select();
|
||
if(!$pd){
|
||
$result['num'] = $num;
|
||
$result['data'] = $result;
|
||
return $this->msg(0,'success',$result);
|
||
}
|
||
$this->assign([
|
||
'result' => $result,
|
||
'num' => $num,
|
||
]);
|
||
return $this->fetch();
|
||
}
|
||
public function device_add(){
|
||
return $this->fetch();
|
||
}
|
||
public function device_add_action(){
|
||
$file = request()->file('upload_file_app');
|
||
$data = request()->param();
|
||
$num = Db::table('app_device_data')->where(['name'=>$data['device_name'],'bluetooth_type'=>$data['bluetooth_type'],'device_model'=>$data['device_model']])->count();
|
||
if($num > 0){
|
||
return $this->msg(10001,'设备已存在');
|
||
}
|
||
if($file){
|
||
// 移动到框架应用根目录/public/uploads/ 目录下
|
||
$file_name_new = $data['device_model'].$data['bluetooth_type'].'_'.time().'.'.$data['file_extension'];
|
||
$info = $file->validate(['size'=>$this->file_max,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'device',$file_name_new);
|
||
if($info){
|
||
$result = Db::table('app_device_data')->insert([
|
||
'name'=>$data['device_name'],
|
||
'content'=>$data['content'],
|
||
'create_time'=>date('Y-m-d H:i:s'),
|
||
'pic'=>'http://tc.pcxbc.com/device/'.$file_name_new,
|
||
'bluetooth_type'=>$data['bluetooth_type'],
|
||
'device_model'=>$data['device_model'],
|
||
'page_measure'=>$data['page_measure'],
|
||
|
||
]);
|
||
if($result){
|
||
return $this->msg([]);
|
||
}else{
|
||
return $this->msg(10002);
|
||
}
|
||
// 成功上传后 获取上传信息
|
||
// 输出 jpg
|
||
// echo $info->getExtension();
|
||
// // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
|
||
// echo $info->getSaveName();
|
||
// // 输出 42a79759f284b767dfcb2a0197904287.jpg
|
||
// echo $info->getFilename();
|
||
}else{
|
||
// 上传失败获取错误信息
|
||
return $this->msg(10001, $file->getError());
|
||
// echo $file->getError();
|
||
}
|
||
}else{
|
||
return $this->msg(10001, '文件缺失');
|
||
}
|
||
|
||
}
|
||
public function device_edit(){
|
||
$data = input();
|
||
|
||
$result = Db::table('app_device_data')->where(['id'=>$data['id']])->find();
|
||
// dump($result);
|
||
// die;
|
||
$this->assign([
|
||
'result' => $result
|
||
]);
|
||
return $this->fetch();
|
||
}
|
||
public function device_edit_action(){
|
||
$file = request()->file('upload_file_app');
|
||
$data = request()->param();
|
||
$check_data = Db::table('app_device_data')->where(['id'=>$data['id']])->find();
|
||
// dump($check_data);
|
||
// dump($data);
|
||
// die;
|
||
if(!$check_data){
|
||
return $this->msg(10001,'修改数据不存在');
|
||
}
|
||
if($file){
|
||
// 移动到框架应用根目录/public/uploads/ 目录下
|
||
$file_name_new = $data['device_model'].$data['bluetooth_type'].'_'.time().'.'.$data['file_extension'];
|
||
$info = $file->validate(['size'=>$this->file_max,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'device',$file_name_new);
|
||
if($info){
|
||
$result = Db::table('app_device_data')->where(['id'=>$data['id']])->update([
|
||
'name'=>$data['device_name'],
|
||
'content'=>$data['content'],
|
||
'pic'=>'http://tc.pcxbc.com/device/'.$file_name_new,
|
||
'bluetooth_type'=>$data['bluetooth_type'],
|
||
'device_model'=>$data['device_model'],
|
||
'page_measure'=>$data['page_measure'],
|
||
]);
|
||
|
||
if($result){
|
||
$file_name = basename($check_data['pic']);
|
||
$filePath = ROOT_PATH . 'public' . DS . 'device' . DS . $file_name; // ROOT_PATH 是框架定义的根目录路径常量
|
||
if (file_exists($filePath)) {
|
||
if (unlink($filePath)) {
|
||
// 删除成功
|
||
// echo '文件删除成功!';
|
||
} else {
|
||
// 删除失败
|
||
// echo '文件删除失败!';
|
||
}
|
||
} else {
|
||
// 文件不存在
|
||
// echo '文件不存在!';
|
||
}
|
||
return $this->msg([]);
|
||
}else{
|
||
return $this->msg(10002);
|
||
}
|
||
// 成功上传后 获取上传信息
|
||
// 输出 jpg
|
||
// echo $info->getExtension();
|
||
// // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
|
||
// echo $info->getSaveName();
|
||
// // 输出 42a79759f284b767dfcb2a0197904287.jpg
|
||
// echo $info->getFilename();
|
||
}else{
|
||
// 上传失败获取错误信息
|
||
return $this->msg(10001, $file->getError());
|
||
// echo $file->getError();
|
||
}
|
||
}else{
|
||
$result = Db::table('app_device_data')->where(['id'=>$data['id']])->update([
|
||
'name'=>$data['device_name'],
|
||
'content'=>$data['content'],
|
||
'bluetooth_type'=>$data['bluetooth_type'],
|
||
'device_model'=>$data['device_model'],
|
||
'page_measure'=>$data['page_measure'],
|
||
]);
|
||
|
||
if($result){
|
||
return $this->msg([]);
|
||
}else{
|
||
return $this->msg(10002);
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
public function device_del(){
|
||
$data = input();
|
||
if(is_array($data['id'])){
|
||
$data['id'] = implode(',',$data['id']);
|
||
$result = Db::table('app_device_data')->where("id in (".$data['id'].")")->update(['is_del'=>$data['is_del']]);
|
||
}else{
|
||
$result = Db::table('app_device_data')->where(['id'=>$data['id']])->update(['is_del'=>$data['is_del']]);
|
||
}
|
||
|
||
if($result){
|
||
return $this->msg(0,'success');
|
||
}else{
|
||
return $this->msg(10001,'success');
|
||
}
|
||
|
||
// $num = Db::table('app_version_log')->where(['id'=>$data['id']])->update(['is_del'=>1]);
|
||
// if($num){
|
||
// return $this->msg([]);
|
||
// }else{
|
||
// return $this->msg(10002);
|
||
// }
|
||
}
|
||
|
||
|
||
################################################################以下为设备提供接口################################################################
|
||
################################################################以下为设备提供接口################################################################
|
||
################################################################以下为设备提供接口################################################################
|
||
|
||
public function device_request_api(){
|
||
|
||
// $data = input();
|
||
$rawData = file_get_contents('php://input'); // 获取原始请求体
|
||
|
||
// $params = explode(',', trim($rawData, '{}')); // 假设参数是用逗号分隔的,并且被大括号包裹
|
||
|
||
// // 现在$params是一个数组,包含了['03', '180.0', '65.1']
|
||
// $param1 = $params[0]; // 获取第一个参数
|
||
// $param2 = $params[1]; // 获取第二个参数,注意转换为浮点数或保持字符串形式
|
||
// $param3 = $params[2]; // 获取第三个参数
|
||
// dump($data);
|
||
// dump(json_encode($data));
|
||
// dump($rawData);
|
||
$content = json_encode($rawData);
|
||
// dump($content);
|
||
// die;
|
||
$result = Db::table('app_device_log_test')->insert(['content'=>$content,'create_time'=>date('Y-m-d H:i:s')]);
|
||
|
||
if($result){
|
||
return $this->msg(0,'success');
|
||
}else{
|
||
return $this->msg(10002,'存储失败');
|
||
}
|
||
|
||
}
|
||
|
||
|
||
public function see_device_msg(){
|
||
|
||
$result = Db::table('app_device_log_test')->select();
|
||
|
||
if(count($result) <= 0){
|
||
echo "没有找到请求记录";
|
||
}
|
||
|
||
$neirong = '';
|
||
foreach ($result as $key => $value) {
|
||
$neirong = $neirong.'第'.($key+1).'次请求</br>本次请求参数为:'.$value['content'].'</br>'.'请求时间为:'.$value['create_time']."</br></br></br>";
|
||
// $temporary_arr1 = json_decode($value['content'],true);
|
||
// if(count($temporary_arr1) == 0){
|
||
// $temporary_arr2 = '请求参数为:</br>'."本次未发送请求参数</br>";
|
||
// }else{
|
||
// $temporary_arr2 = '请求参数为:</br>';
|
||
// }
|
||
// foreach ($temporary_arr1 as $k => $v) {
|
||
// $temporary_arr2 = $temporary_arr2.$k."(参数名):".$v."(参数值)</br>";
|
||
// }
|
||
// $neirong = $neirong.$temporary_arr2.'请求时间为:'.$value['create_time']."</br></br></br>";
|
||
|
||
}
|
||
|
||
echo $neirong;
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
} |