MdFoodsQrcodeController.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace app\modules\admin\controllers;
  3. use app\models\MdFoodsQrcode;
  4. use app\modules\admin\models\MdFoodsQrcodeForm;
  5. use app\utils\CurlHelper;
  6. use app\utils\QrCode;
  7. use ZipArchive;
  8. class MdFoodsQrcodeController extends BaseController
  9. {
  10. /**
  11. * 列表
  12. */
  13. public function actionList() {
  14. $form = new MdFoodsQrcodeForm();
  15. $form->attributes = get_params();
  16. $this->asJson($form->list());
  17. }
  18. /**
  19. * 添加
  20. */
  21. public function actionCreateQrcode() {
  22. $form = new MdFoodsQrcodeForm();
  23. $form->attributes = post_params();
  24. $this->asJson($form->createQrcode());
  25. }
  26. /**
  27. * 修改桌号 / 门店
  28. */
  29. public function actionEditQrcode() {
  30. $form = new MdFoodsQrcodeForm();
  31. $form->attributes = post_params();
  32. $this->asJson($form->editQrcode());
  33. }
  34. /**
  35. * 删除
  36. */
  37. public function actionDelQrcode() {
  38. $form = new MdFoodsQrcodeForm();
  39. $form->attributes = post_params();
  40. $this->asJson($form->delQrcode());
  41. }
  42. //生成二维码
  43. public function actionQrcodeCreate(){
  44. if(empty(post_params('ids'))){
  45. return $this->asJson([
  46. 'code'=>1,
  47. 'msg'=>'没有选择数据'
  48. ]);
  49. }
  50. $ids = post_params('ids');
  51. $img = [];
  52. foreach ($ids as $index => $item){
  53. $filename = md5(date('Ym') . $item . 'md_foods_qrcode');
  54. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  55. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  56. if (file_exists($path)) {
  57. $img[]= $pic_url;
  58. continue;
  59. }
  60. $md_foods_qrcode = MdFoodsQrcode::findOne($item);
  61. // if(empty($md_foods_qrcode->qrcode_url) || !file_exists(str_replace(\Yii::$app->request->hostInfo,\Yii::$app->basePath, $md_foods_qrcode->qrcode_url))){
  62. //
  63. // }else{
  64. // $img[]= $md_foods_qrcode->qrcode_url;
  65. // }
  66. if(!empty($md_foods_qrcode->url_path)){
  67. $text = $md_foods_qrcode->url_path;
  68. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  69. $img[]= $pic_url;
  70. $md_foods_qrcode->qrcode_url = $pic_url;
  71. $md_foods_qrcode->save();
  72. }
  73. }
  74. return $this->asJson([
  75. 'code'=>0,
  76. 'msg'=>'',
  77. 'data'=>$img
  78. ]);
  79. }
  80. public function actionDownQr() {
  81. if(empty(post_params('ids'))){
  82. return $this->asJson([
  83. 'code'=>1,
  84. 'msg'=>'没有选择数据'
  85. ]);
  86. }
  87. $ids = post_params('ids');
  88. $ids = explode(',', $ids);
  89. $mdFoods = [];
  90. foreach ($ids as $index => $item){
  91. $mdFoods[$index]['id'] = $item;
  92. $filename = md5(date('Ym') . $item . 'md_foods_qrcode');
  93. $path = \Yii::$app->runtimePath . '/image/' . $filename . '.jpg';
  94. $pic_url = str_replace('http://', 'https://', \Yii::$app->request->hostInfo . '/runtime/image/' . $filename . '.jpg');
  95. if (file_exists($path)) {
  96. $mdFoods[$index]['qrcode_url'] = $pic_url;
  97. continue;
  98. }
  99. $md_foods_qrcode = MdFoodsQrcode::findOne($item);
  100. if(!empty($md_foods_qrcode->url_path)){
  101. $text = $md_foods_qrcode->url_path;
  102. QrCode::image($text, 500, false, 'L', 'JPEG', 0, ['255,255,255', '0,0,0'], 1, false, $path);
  103. $mdFoods[$index]['qrcode_url'] = $pic_url;
  104. $md_foods_qrcode->qrcode_url = $pic_url;
  105. $md_foods_qrcode->save();
  106. }
  107. }
  108. return $this->asJson([
  109. 'code' => 0,
  110. 'msg' => '获取成功',
  111. 'data' => $this->createZip($mdFoods)
  112. ]);
  113. }
  114. public function createZip($files = []) {
  115. // 创建压缩包对象
  116. $zip = new ZipArchive();
  117. // 创建临时压缩包文件
  118. $zipName = 'example.zip';
  119. $zip->open($zipName, ZipArchive::CREATE | ZipArchive::OVERWRITE);
  120. foreach ($files as $file) {
  121. // 添加要压缩的文件
  122. $filePath = $this->saveTempImage($file['qrcode_url']);
  123. $zip->addFile($filePath, $file['id'] . '.jpg');
  124. }
  125. // 关闭压缩包
  126. $zip->close();
  127. // 将压缩包移动到指定目录
  128. // $destination = \Yii::$app->runtimePath . '/zip/' . $zipName;
  129. // rename($zipName, $destination);
  130. // 返回压缩包的下载链接
  131. return \Yii::$app->request->hostInfo . '/' . $zipName;
  132. }
  133. //获取网络图片到临时目录
  134. private function saveTempImage($url)
  135. {
  136. if (strpos($url,'http') === false) {
  137. $url = 'http:'. trim($url);
  138. }
  139. if (!is_dir(\Yii::$app->runtimePath . '/image')) {
  140. mkdir(\Yii::$app->runtimePath . '/image');
  141. }
  142. $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
  143. CurlHelper::download($url, $save_path);
  144. return $save_path;
  145. }
  146. }