attributes = get_params(); $this->asJson($form->list()); } /** * 添加 */ public function actionCreateQrcode() { $form = new MdFoodsQrcodeForm(); $form->attributes = post_params(); $this->asJson($form->createQrcode()); } /** * 修改桌号 / 门店 */ public function actionEditQrcode() { $form = new MdFoodsQrcodeForm(); $form->attributes = post_params(); $this->asJson($form->editQrcode()); } /** * 删除 */ public function actionDelQrcode() { $form = new MdFoodsQrcodeForm(); $form->attributes = post_params(); $this->asJson($form->delQrcode()); } //生成二维码 public function actionQrcodeCreate(){ if(empty(post_params('ids'))){ return $this->asJson([ 'code'=>1, 'msg'=>'没有选择数据' ]); } $ids = post_params('ids'); $img = []; foreach ($ids as $index => $item){ $filename = md5(date('Ym') . $item . 'md_foods_qrcode'); $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg'; $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg'); if (file_exists($path)) { $img[]= $pic_url; continue; } $md_foods_qrcode = MdFoodsQrcode::findOne($item); // if(empty($md_foods_qrcode->qrcode_url) || !file_exists(str_replace(\Yii::$app->request->hostInfo,\Yii::$app->basePath, $md_foods_qrcode->qrcode_url))){ // // }else{ // $img[]= $md_foods_qrcode->qrcode_url; // } if(!empty($md_foods_qrcode->url_path)){ $text = $md_foods_qrcode->url_path; QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path); $img[]= $pic_url; $md_foods_qrcode->qrcode_url = $pic_url; $md_foods_qrcode->save(); } } return $this->asJson([ 'code'=>0, 'msg'=>'', 'data'=>$img ]); } public function actionDownQr() { if(empty(post_params('ids'))){ return $this->asJson([ 'code'=>1, 'msg'=>'没有选择数据' ]); } $ids = post_params('ids'); $ids = explode(',', $ids); $mdFoods = []; foreach ($ids as $index => $item){ $mdFoods[$index]['id'] = $item; $filename = md5(date('Ym') . $item . 'md_foods_qrcode'); $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg'; $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg'); if (file_exists($path)) { $mdFoods[$index]['qrcode_url'] = $pic_url; continue; } $md_foods_qrcode = MdFoodsQrcode::findOne($item); if(!empty($md_foods_qrcode->url_path)){ $text = $md_foods_qrcode->url_path; QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path); $mdFoods[$index]['qrcode_url'] = $pic_url; $md_foods_qrcode->qrcode_url = $pic_url; $md_foods_qrcode->save(); } } return $this->asJson([ 'code' => 0, 'msg' => '获取成功', 'data' => $this->createZip($mdFoods) ]); } public function createZip($files = []) { // 创建压缩包对象 $zip = new ZipArchive(); // 创建临时压缩包文件 $zipName = 'example.zip'; $zip->open($zipName, ZipArchive::CREATE | ZipArchive::OVERWRITE); foreach ($files as $file) { // 添加要压缩的文件 $filePath = $this->saveTempImage($file['qrcode_url']); $zip->addFile($filePath, $file['id'] . '.jpg'); } // 关闭压缩包 $zip->close(); // 将压缩包移动到指定目录 // $destination = \Yii::$app->runtimePath . '/zip/' . $zipName; // rename($zipName, $destination); // 返回压缩包的下载链接 return \Yii::$app->request->hostInfo . '/' . $zipName; } //获取网络图片到临时目录 private function saveTempImage($url) { if (strpos($url,'http') === false) { $url = 'http:'. trim($url); } if (!is_dir(\Yii::$app->runtimePath . '/image')) { mkdir(\Yii::$app->runtimePath . '/image'); } $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg'; CurlHelper::download($url, $save_path); return $save_path; } }