| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- <?php
- namespace app\modules\admin\controllers;
- use app\models\MdFoodsQrcode;
- use app\modules\admin\models\MdFoodsQrcodeForm;
- use app\utils\CurlHelper;
- use app\utils\QrCode;
- use ZipArchive;
- class MdFoodsQrcodeController extends BaseController
- {
- /**
- * 列表
- */
- public function actionList() {
- $form = new MdFoodsQrcodeForm();
- $form->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;
- }
- }
|