isAjax()){
//分页参数
$length = input('rows',10,'intval'); //每页条数
$page = input('page',1,'intval'); //第几页
$start = ($page - 1) * $length; //分页开始位置
//排序
$sortRow = input('sidx','id','trim'); //排序列
$sort = input('sord','desc','trim'); //排序方式
$order = $sortRow.' '.$sort;
$title = input('title','','trim');
if($title){
$map[] = ['title','like','%'.$title.'%'];
}
$enable = input('enable','','trim');
if($enable != ''){
$map[] = ['enable','=',$enable];
}
$type = input('type','','trim');
if($type != ''){
$map[]=['','exp',Db::raw("FIND_IN_SET($type,types)")];
}
$map[] = ['del','=',0];
$map[] = ['org_id','=',$this->orgId];
$map= empty($map) ? true: $map;
//数据查询
$lists = db('address')->where($map)->limit($start,$length)->order($order)->select();
foreach ($lists as $k=>$v){
$ts = $v['types']?explode(',',$v['types']):[];
$lists[$k]['types_text'] = model('Address')->getTypeStr($ts);
}
//数据返回
$totalCount = db('address')->where($map)->count();
$totalPage = ceil($totalCount/$length);
$result['page'] = $page;
$result['total'] = $totalPage;
$result['records'] = $totalCount;
$result['rows'] = $lists;
return json($result);
}else{
$types = model('Address')->getTypes();
$this->assign('types',$types);
return $this->fetch();
}
}
/**
* 新增/编辑
*/
public function add($id=0){
if(request()->isPost()){
$res = model('Address')->updates();
if($res){
$this->success('操作成功',url('index'));
}else{
$this->error(model('Address')->getError());
}
}else{
if($id){
$info = db('address')->where('id',$id)->find();
if($info){
$info['types'] = $info['types']?explode(',',$info['types']):[];
}
$this->assign('info',$info);
}
$ts = model('Address')->getTypes();
$types = [];
foreach ($ts as $k=>$v){
$types[] = [
'id' => $k,
'title' => $v
];
}
$dep = model('dep')->getList();
$this->assign('dep',$dep);
$this->assign('types',$types);
return $this->fetch();
}
}
/**
* 删除记录
* @param int $id
*/
public function del($id=0){
if(!$id){
$this->error('参数错误');
}
$res = db('address')->where('id',$id)->setField('del',1);
if($res){
$this->success('删除成功');
}else{
$this->error('删除失败');
}
}
/**
* 改变字段值
* @param int $fv
* @param string $fn
* @param int $fv
*/
public function changeField($id=0,$fn='',$fv=0){
if(!$fn||!$id){
$this->error('参数错误');
}
$res = db('address')->where('id',$id)->setField($fn,$fv);
if($res){
$this->success('操作成功');
}else{
$this->error('操作失败');
}
}
public function qrcode($id=0){
$info = Db::name('address')->where('id',$id)->find();
$code = get_qrcode_str('address',$id);
$config = config('app.addr_url');
$code = $config.$code;
$this->assign('code',$code);
$this->assign('info',$info);
return $this->fetch();
}
// 地址下载
public function addrdown(){
$orgid = $this->orgId;
$map[] = ['del','=',0];
$map[] = ['org_id','=',$this->orgId];
$title = input('title','','trim');
if($title){
$map[] = ['title','like','%'.$title.'%'];
}
$enable = input('enable','','trim');
if($enable != ''){
$map[] = ['enable','=',$enable];
}
$type = input('type','','trim');
if($type != ''){
$map[]=['','exp',Db::raw("FIND_IN_SET($type,types)")];
}
$ids = input('ids','','trim');
if($ids !=''){
$map[] = ['id','in',explode(',',$ids)];
}
$map= empty($map) ? true: $map;
//数据查询
$lists = db('address')->where($map)->select();
$config = config('app.addr_url');
foreach ($lists as $k=>$v){
$lists[$k]['code'] = $config.get_qrcode_str('address',$v['id']);
}
$path = date('Ymd').'_'.$orgid.'_'.time().mt_rand(1000,9999);
$dir = './uploads/qrcodeimg/'.$path.'/';
foreach ($lists as $v1){
$name = str_ireplace('/','_',$v1['title']).'('.$v1['id'].')'.'.jpg';
$filepath = $dir.'/'.$name;
create_qrcode($v1['code'],$filepath);
}
$zippath = './uploads/qrcodeimg/'.$path.'.zip';
$spath = $dir;
@unlink($zippath);
$zip = new \ZipArchive();
if($zip->open($zippath, \ZipArchive::CREATE)=== TRUE){
add_file_to_zip($spath,$zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
$zip->close(); //关闭处理的zip文件
if(!file_exists($zippath)){
$this->error("打包失败"); //即使创建,仍有可能失败。。。。
}
deldir($dir); // 删除生成的目录
$downname = session('orgName').'地点二维码.zip';
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.$downname); //文件名
header("Content-Type: application/zip"); //zip格式的
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
header('Content-Length: '. filesize($zippath)); //告诉浏览器,文件大小
@readfile($zippath);
}else{
$this->error("打包失败");
}
}
public function import(){
return $this->fetch();
}
/**
* 下载点模板
*/
public function downloadtem(){
set_time_limit(0);
ini_set("memory_limit","512M");
include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
$fileName = '地点模板.xlsx';
$excel = new \PHPExcel();
$sheet = $excel->setActiveSheetIndex(0);
$arr = array('A','B','C','D');
$types = model('Address')->getTypes();
ksort($types);
foreach($arr as $k=>$v){
$excel->getActiveSheet()->getStyle($v)->getAlignment()->setWrapText(true);//换行
$excel->getActiveSheet()->getStyle($v.'1')->getFont()->setBold(true);
if($k==3){
$excel->getActiveSheet()->getColumnDimension($v)->setWidth(100);
}else{
$excel->getActiveSheet()->getColumnDimension($v)->setWidth(18);
}
}
$sheet->setCellValueByColumnAndRow(0,1,'名称');
$sheet->setCellValueByColumnAndRow(1,1,'设备编号');
$sheet->setCellValueByColumnAndRow(2,1,'备注');
$sheet->setCellValueByColumnAndRow(3,1,'类型('.implode(',',$types).') 多个用英文逗号隔开');
$excel->getActiveSheet()->setCellValue('A2', '示例地址');
$excel->getActiveSheet()->setCellValue('B2', 'XXX');
$excel->getActiveSheet()->setCellValue('C2', 'XXX');
$excel->getActiveSheet()->setCellValue('D2', '报修,运送');
ob_end_clean();//清除缓冲区,避免乱码
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$fileName.'"');
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
$objWriter->save('php://output'); //文件通过浏览器下载
}
/**
* 导入
*/
public function importexcel(){
set_time_limit(0);
ini_set("memory_limit", -1);
ob_flush();//清空缓存
flush();//刷新缓存
include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
$orgId = $this->orgId;
if(request()->file()) {
$file = request()->file('file');
//获取文件后缀
$e = explode('.',$_FILES['file']['name']);
$ext = $e[count($e)-1];
if($ext == 'xls'){
\PHPExcel_IOFactory::createReader( 'Excel5');
}else{
\PHPExcel_IOFactory::createReader('Excel2007');
}
$newArr=['xls','xlsx'];
if(!in_array($ext,$newArr)){
exit('文件格式不正确');
}
// 移动到框架应用根目录/uploads/ 目录下
$info = $file->validate([ 'size'=>config('app.max_upload_file_size') ])
->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'files');
if(!$info){
exit('文件上传失败');
}
$img = './uploads/files/' . $info->getSaveName();
$filePath = str_replace('\\', '/', $img);
$newPath = \PHPExcel_IOFactory::load($filePath);
$excelArray = $newPath->getsheet(0)->toArray(); //转换为数组格式
array_shift($excelArray); //删除第一个数组(标题);
if(empty($excelArray)){
exit('文件内容为空');
}
foreach ($excelArray as $k => $v) {
if(!$v[0]){
echo "第".($k+1)."行,名称为空,未导入
";
continue;
}
if(!$v[3]){
echo "第".($k+1)."行,类型为空,未导入
";
continue;
}
$check = Db::name('address')
->where('org_id',$orgId)
->where('del',0)
->where('title',$v[0])
->find();
if($check){
echo "第".($k+1)."行,名称已存在,未导入
";
continue;
}
$typeId = [];
$type = explode(',',$v[3]);
$types = model('Address')->getTypes();
$types = array_flip($types);
foreach ($type as $k1=>$v1){
if(isset($types[$v1])){
$typeId[] = $types[$v1];
}
}
if(count($type)!=count($typeId) || empty($typeId)){
echo "第".($k+1)."行,类型字段格式错误,未导入
";
continue;
}
$rData = [
'org_id'=>$orgId,
'title'=>$v[0],
'sn'=>$v[1],
'remark'=>$v[2],
'types'=>implode(',',$typeId),
'create_time'=>getTime()
];
$ret=Db::name('address')->insert($rData);
if(!$ret){
echo "第".($k+1)."行,导入失败
";
}else{
echo "第".($k+1)."行,导入成功
";
}
}
}else{
exit('请上传文件');
}
}
public function three($id=0){
if(request()->isPost()){
$id = input('id/d',0);
$x = input('x','','trim');
$y = input('y','','trim');
$z = input('z','','trim');
$xyz[] = $x;
$xyz[] = $y;
$xyz[] = $z;
$ret = Db::name('address')->where('id',$id)->update(['xyz'=>implode(',',$xyz),'update_time' => date('Y-m-d H:i:s')]);
if($ret){
$this->success('操作成功');
}else{
$this->error('操作失败');
}
}else{
$info = Db::name('address')->where('id',$id)->find();
$arr = $info['xyz']?explode(',',$info['xyz']):[];
$x = '';
$y = '';
$z = '';
if(count($arr) == 3){
$x = $arr[0];
$y = $arr[1];
$z = $arr[2];
}
$this->assign('x',$x);
$this->assign('y',$y);
$this->assign('z',$z);
$this->assign('id',$id);
return $this->fetch();
}
}
}