123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- <?php
- namespace app\admin\controller;
- use app\common\util\QrcodeUtil;
- use think\Db;
- class DinnerAddress extends Auth
- {
- public function index(){
- if(request()->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];
- }
- $map[] = ['del','=',0];
- $map[] = ['org_id','=',$this->orgId];
- $map= empty($map) ? true: $map;
- //数据查询
- $lists = db('dinner_address')->where($map)->limit($start,$length)->order($order)->select();
- foreach ($lists as $k=>$v){
- $lists[$k]['types_text'] = $v['type']==0?'预定餐':'当日餐';
- }
- //数据返回
- $totalCount = db('dinner_address')->where($map)->count();
- $totalPage = ceil($totalCount/$length);
- $result['page'] = $page;
- $result['total'] = $totalPage;
- $result['records'] = $totalCount;
- $result['rows'] = $lists;
- return json($result);
- }else{
- return $this->fetch();
- }
- }
- /**
- * 新增/编辑
- */
- public function add($id=0){
- if(request()->isPost()){
- $res = model('DinnerAddress')->updates();
- if($res){
- $this->success('操作成功',url('index'));
- }else{
- $this->error(model('Address')->getError());
- }
- }else{
- if($id){
- $info = db('dinner_address')->where('id',$id)->find();
- $this->assign('info',$info);
- }
- return $this->fetch();
- }
- }
- /**
- * 删除记录
- * @param int $id
- */
- public function del($id=0){
- if(!$id){
- $this->error('参数错误');
- }
- $res = db('dinner_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('dinner_address')->where('id',$id)->setField($fn,$fv);
- if($res){
- $name = '科室地址:';
- $content = '';
- if($fn == 'enable'){
- $content = $name.'禁用';
- if($fv == 1){
- $content = $name.'启用';
- }
- }
- $this->success('操作成功');
- }else{
- $this->error('操作失败');
- }
- }
- public function qrcode($id=0){
- $info = Db::name('dinner_address')->where('id',$id)->find();
- $str = get_qrcode_str('dinner_address',$id);
- $code = url('h5/WxHome/index',[],false,true).'?code='.$str;
- $this->assign('url',$code);
- $this->assign('code',think_encrypt($code));
- $this->assign('info',$info);
- return $this->fetch();
- }
- // 地址下载
- public function addrdown(){
- $orgid = $this->orgId;
- $map[] = ['del','=',0];
- $map[] = ['org_id','=',$this->orgId];
- $map= empty($map) ? true: $map;
- //数据查询
- $lists = db('dinner_address')->where($map)->select();
- foreach ($lists as $k=>$v){
- $str = get_qrcode_str('dinner_address',$v['id']);
- $code = url('h5/WxHome/index',['code'=>$str],false,true);
- $lists[$k]['code'] =$code;
- }
- $path = date('Ymd').'_'.$orgid.'_'.time().mt_rand(1000,9999);
- $dir = './uploads/temp/'.$path.'/';
- foreach ($lists as $v1){
- $name = $v1['title'].'('.$v1['id'].')'.'.jpg';
- $filepath = $dir.'/'.$name;
- QrcodeUtil::create($v1['code'],1,$filepath);
- }
- $zippath = './uploads/temp/'.$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');
- 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(1,1,'备注');
- $excel->getActiveSheet()->setCellValue('A2', '示例地址');
- $excel->getActiveSheet()->setCellValue('B2', 'XXX');
- // $excel->getActiveSheet()->setCellValue('C2', 'XXX');
- 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('文件格式不正确');
- }
- $uploaddir = config('app.upload_dir');
- // 移动到框架应用根目录/uploads/ 目录下
- $info = $file->validate(config('app.import_upload_file'))
- ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . $uploaddir . DIRECTORY_SEPARATOR . 'files');
- if(!$info){
- exit('文件上传失败');
- }
- $img = './'.$uploaddir.'/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 "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,地址为空,未导入</font><br />";
- continue;
- }
- // if(!$v[1]){
- // echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型为空,未导入</font><br />";
- // continue;
- // }
- // $a =['预定餐','当日餐'];
- // if(!in_array($v[1],$a)){
- // echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型错误,未导入</font><br />";
- // continue;
- // }
- $check = Db::name('dinner_address')
- ->where('org_id',$orgId)
- ->where('del',0)
- ->where('title',$v[0])
- ->find();
- if($check){
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,地址已存在,未导入</font><br />";
- continue;
- }
- $rData = [
- 'org_id'=>$orgId,
- 'title'=>$v[0],
- 'remark'=>$v[1],
- // 'type'=>$v[1],
- 'create_time'=>getTime()
- ];
- $ret=Db::name('dinner_address')->insertGetId($rData);
- if(!$ret){
- echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
- }else{
- echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
- }
- }
- }else{
- exit('请上传文件');
- }
- }
- }
|