open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE); foreach ($devices as $device){ $r = $this->generateImg($device->id,$device->device_city); if ($r) { $str = str_replace(['/','\\',':','*','"','<','>','|','?'],'_',$device->device_name); $zip->addFile($r, $str.".png"); } } $zip->close(); return $this->response()->download('/qrcode.zip'); } public function generateImg($device_id,$device_city) { $wechat_app = SystemConfig::get('wechat_config'); if (!$wechat_app || !isset($wechat_app['appId']) || !isset($wechat_app['appSecret'])) { return false; } $config = [ 'app_id' => $wechat_app['appId'], 'secret' => $wechat_app['appSecret'], 'response_type' => 'array' ]; $app = Factory::miniProgram($config); try{ $response = $app->app_code->getQrCode('pages/qrcode/qrcode?id='.$device_id.'&community='.$device_city,300); }catch (\Exception $exception){ return false; } $filename = "error"; if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) { $filename = $response->saveAs(public_path('upload/DeviceQRCODE'), request('id').'.jpg'); } if ($filename == "error") return false; return public_path("upload/DeviceQRCODE/{$filename}"); } public function html() { return << 下载设备图片 HTML; } }