SchoolPhysicalExamination/application/admin/controller/Device.php

207 lines
8.0 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);
// }
}
}