DinnerAddress.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\util\QrcodeUtil;
  4. use think\Db;
  5. class DinnerAddress extends Auth
  6. {
  7. public function index(){
  8. if(request()->isAjax()){
  9. //分页参数
  10. $length = input('rows',10,'intval'); //每页条数
  11. $page = input('page',1,'intval'); //第几页
  12. $start = ($page - 1) * $length; //分页开始位置
  13. //排序
  14. $sortRow = input('sidx','id','trim'); //排序列
  15. $sort = input('sord','desc','trim'); //排序方式
  16. $order = $sortRow.' '.$sort;
  17. $title = input('title','','trim');
  18. if($title){
  19. $map[] = ['title','like','%'.$title.'%'];
  20. }
  21. $enable = input('enable','','trim');
  22. if($enable != ''){
  23. $map[] = ['enable','=',$enable];
  24. }
  25. $map[] = ['del','=',0];
  26. $map[] = ['org_id','=',$this->orgId];
  27. $map= empty($map) ? true: $map;
  28. //数据查询
  29. $lists = db('dinner_address')->where($map)->limit($start,$length)->order($order)->select();
  30. foreach ($lists as $k=>$v){
  31. $lists[$k]['types_text'] = $v['type']==0?'预定餐':'当日餐';
  32. }
  33. //数据返回
  34. $totalCount = db('dinner_address')->where($map)->count();
  35. $totalPage = ceil($totalCount/$length);
  36. $result['page'] = $page;
  37. $result['total'] = $totalPage;
  38. $result['records'] = $totalCount;
  39. $result['rows'] = $lists;
  40. return json($result);
  41. }else{
  42. return $this->fetch();
  43. }
  44. }
  45. /**
  46. * 新增/编辑
  47. */
  48. public function add($id=0){
  49. if(request()->isPost()){
  50. $res = model('DinnerAddress')->updates();
  51. if($res){
  52. $this->success('操作成功',url('index'));
  53. }else{
  54. $this->error(model('Address')->getError());
  55. }
  56. }else{
  57. if($id){
  58. $info = db('dinner_address')->where('id',$id)->find();
  59. $this->assign('info',$info);
  60. }
  61. return $this->fetch();
  62. }
  63. }
  64. /**
  65. * 删除记录
  66. * @param int $id
  67. */
  68. public function del($id=0){
  69. if(!$id){
  70. $this->error('参数错误');
  71. }
  72. $res = db('dinner_address')->where('id',$id)->setField('del',1);
  73. if($res){
  74. $this->success('删除成功');
  75. }else{
  76. $this->error('删除失败');
  77. }
  78. }
  79. /**
  80. * 改变字段值
  81. * @param int $fv
  82. * @param string $fn
  83. * @param int $fv
  84. */
  85. public function changeField($id=0,$fn='',$fv=0){
  86. if(!$fn||!$id){
  87. $this->error('参数错误');
  88. }
  89. $res = db('dinner_address')->where('id',$id)->setField($fn,$fv);
  90. if($res){
  91. $name = '科室地址:';
  92. $content = '';
  93. if($fn == 'enable'){
  94. $content = $name.'禁用';
  95. if($fv == 1){
  96. $content = $name.'启用';
  97. }
  98. }
  99. $this->success('操作成功');
  100. }else{
  101. $this->error('操作失败');
  102. }
  103. }
  104. public function qrcode($id=0){
  105. $info = Db::name('dinner_address')->where('id',$id)->find();
  106. $str = get_qrcode_str('dinner_address',$id);
  107. $code = url('h5/WxHome/index',[],false,true).'?code='.$str;
  108. $this->assign('url',$code);
  109. $this->assign('code',think_encrypt($code));
  110. $this->assign('info',$info);
  111. return $this->fetch();
  112. }
  113. // 地址下载
  114. public function addrdown(){
  115. $orgid = $this->orgId;
  116. $map[] = ['del','=',0];
  117. $map[] = ['org_id','=',$this->orgId];
  118. $map= empty($map) ? true: $map;
  119. //数据查询
  120. $lists = db('dinner_address')->where($map)->select();
  121. foreach ($lists as $k=>$v){
  122. $str = get_qrcode_str('dinner_address',$v['id']);
  123. $code = url('h5/WxHome/index',['code'=>$str],false,true);
  124. $lists[$k]['code'] =$code;
  125. }
  126. $path = date('Ymd').'_'.$orgid.'_'.time().mt_rand(1000,9999);
  127. $dir = './uploads/temp/'.$path.'/';
  128. foreach ($lists as $v1){
  129. $name = $v1['title'].'('.$v1['id'].')'.'.jpg';
  130. $filepath = $dir.'/'.$name;
  131. QrcodeUtil::create($v1['code'],1,$filepath);
  132. }
  133. $zippath = './uploads/temp/'.$path.'.zip';
  134. $spath = $dir;
  135. @unlink($zippath);
  136. $zip = new \ZipArchive();
  137. if($zip->open($zippath, \ZipArchive::CREATE)=== TRUE){
  138. add_file_to_zip($spath,$zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
  139. $zip->close(); //关闭处理的zip文件
  140. if(!file_exists($zippath)){
  141. $this->error("打包失败"); //即使创建,仍有可能失败。。。。
  142. }
  143. deldir($dir); // 删除生成的目录
  144. $downname = session('orgName').'订餐地点二维码.zip';
  145. header("Cache-Control: public");
  146. header("Content-Description: File Transfer");
  147. header('Content-disposition: attachment; filename='.$downname); //文件名
  148. header("Content-Type: application/zip"); //zip格式的
  149. header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
  150. header('Content-Length: '. filesize($zippath)); //告诉浏览器,文件大小
  151. @readfile($zippath);
  152. }else{
  153. $this->error("打包失败");
  154. }
  155. }
  156. public function import(){
  157. return $this->fetch();
  158. }
  159. /**
  160. * 下载点模板
  161. */
  162. public function downloadtem(){
  163. set_time_limit(0);
  164. ini_set("memory_limit","512M");
  165. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  166. $fileName = '订餐地点模板.xlsx';
  167. $excel = new \PHPExcel();
  168. $sheet = $excel->setActiveSheetIndex(0);
  169. $arr = array('A','B','C','D');
  170. foreach($arr as $k=>$v){
  171. $excel->getActiveSheet()->getStyle($v)->getAlignment()->setWrapText(true);//换行
  172. $excel->getActiveSheet()->getStyle($v.'1')->getFont()->setBold(true);
  173. if($k==3){
  174. $excel->getActiveSheet()->getColumnDimension($v)->setWidth(100);
  175. }else{
  176. $excel->getActiveSheet()->getColumnDimension($v)->setWidth(18);
  177. }
  178. }
  179. $sheet->setCellValueByColumnAndRow(0,1,'名称');
  180. // $sheet->setCellValueByColumnAndRow(1,1,'类型[预定餐,当日餐]');
  181. $sheet->setCellValueByColumnAndRow(1,1,'备注');
  182. $excel->getActiveSheet()->setCellValue('A2', '示例地址');
  183. $excel->getActiveSheet()->setCellValue('B2', 'XXX');
  184. // $excel->getActiveSheet()->setCellValue('C2', 'XXX');
  185. ob_end_clean();//清除缓冲区,避免乱码
  186. header('Content-Type: application/vnd.ms-excel');
  187. header('Content-Disposition: attachment;filename="'.$fileName.'"');
  188. header('Cache-Control: max-age=0');
  189. $objWriter = \PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
  190. $objWriter->save('php://output'); //文件通过浏览器下载
  191. }
  192. /**
  193. * 导入
  194. */
  195. public function importexcel(){
  196. set_time_limit(0);
  197. ini_set("memory_limit", -1);
  198. ob_flush();//清空缓存
  199. flush();//刷新缓存
  200. include_once env('root_path').'/extend/phpexcel/Classes/PHPExcel.php';
  201. $orgId = $this->orgId;
  202. if(request()->file()) {
  203. $file = request()->file('file');
  204. //获取文件后缀
  205. $e = explode('.',$_FILES['file']['name']);
  206. $ext = $e[count($e)-1];
  207. if($ext == 'xls'){
  208. \PHPExcel_IOFactory::createReader( 'Excel5');
  209. }else{
  210. \PHPExcel_IOFactory::createReader('Excel2007');
  211. }
  212. $newArr=['xls','xlsx'];
  213. if(!in_array($ext,$newArr)){
  214. exit('文件格式不正确');
  215. }
  216. $uploaddir = config('app.upload_dir');
  217. // 移动到框架应用根目录/uploads/ 目录下
  218. $info = $file->validate(config('app.import_upload_file'))
  219. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . $uploaddir . DIRECTORY_SEPARATOR . 'files');
  220. if(!$info){
  221. exit('文件上传失败');
  222. }
  223. $img = './'.$uploaddir.'/files/' . $info->getSaveName();
  224. $filePath = str_replace('\\', '/', $img);
  225. $newPath = \PHPExcel_IOFactory::load($filePath);
  226. $excelArray = $newPath->getsheet(0)->toArray(); //转换为数组格式
  227. array_shift($excelArray); //删除第一个数组(标题);
  228. if(empty($excelArray)){
  229. exit('文件内容为空');
  230. }
  231. foreach ($excelArray as $k => $v) {
  232. if(!$v[0]){
  233. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,地址为空,未导入</font><br />";
  234. continue;
  235. }
  236. // if(!$v[1]){
  237. // echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型为空,未导入</font><br />";
  238. // continue;
  239. // }
  240. // $a =['预定餐','当日餐'];
  241. // if(!in_array($v[1],$a)){
  242. // echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,类型错误,未导入</font><br />";
  243. // continue;
  244. // }
  245. $check = Db::name('dinner_address')
  246. ->where('org_id',$orgId)
  247. ->where('del',0)
  248. ->where('title',$v[0])
  249. ->find();
  250. if($check){
  251. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,地址已存在,未导入</font><br />";
  252. continue;
  253. }
  254. $rData = [
  255. 'org_id'=>$orgId,
  256. 'title'=>$v[0],
  257. 'remark'=>$v[1],
  258. // 'type'=>$v[1],
  259. 'create_time'=>getTime()
  260. ];
  261. $ret=Db::name('dinner_address')->insertGetId($rData);
  262. if(!$ret){
  263. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  264. }else{
  265. echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  266. }
  267. }
  268. }else{
  269. exit('请上传文件');
  270. }
  271. }
  272. }