'选择项', ]; } public function getBallQrcode() { $ball = Dev::findOne($this->ids); if ($ball == null) { return ['code' => 1, 'msg' => '该设备不存在']; } $store = Store::findOne($ball->store_id); if ($store == null) { return ['code' => 1, 'msg' => '该设备所在门店不存在']; } return $this->getQrcode($store, $ball); } /** * @param $store * @param $ball * @return array */ public function getQrcode($store, $ball) { $path = 'qrcode/'; if (!is_dir($path)) { mkdir($path, 0777, true); } $hostname = $this->url; $filename = 'qrcode/' . $ball->dev_number . '.png'; if (!file_exists($filename)) { $qr_data = $hostname . "?dev_id={$ball->id}&store_id={$ball->store_id}&time={$ball->created_at}&path=basicsInfo"; $errorCorrectionLevel = 'L'; //容错级别 $matrixPointSize = 5; //生成图片大小 QRcode::png($qr_data, $filename, $errorCorrectionLevel, $matrixPointSize, 2, true); $url = \Yii::$app->request->getHostInfo() . '/' . $filename; if (!file_exists($filename)) { return ['code' => 1, 'msg' => '生成二维码失败']; } else { return ['code' => 0, 'msg' => 'ok', 'data' => $url]; } // if (!file_exists($filename)) { // return [ // 'code' => 1, // 'msg' => 'Failed to generate QR code.' // ]; // } else { // $url = $hostname . '/' . $filename; // $file = fopen($filename, "w");//打开文件准备写入 // fwrite($file, $url);//写入,$res为图片二进制内容 // fclose($file);//关闭 // //图片是否存在 // if (!file_exists($filename)) { // return ['code' => 1, 'msg' => '生成二维码失败']; // } else { // return ['code' => 0, 'msg' => 'ok', 'data' => $url]; // } // } } else { return ['code' => 0, 'msg' => 'ok', 'data' => \Yii::$app->request->getHostInfo() . '/' . $filename]; } } //获取小程序二维码 public function wxmpQrcode($body) { $plugin = new \app\models\common\PluginService(); $wxmpService = $plugin->getWxmpService(0); $accessToken = $wxmpService->getAccessToken(); if (empty($accessToken)) { $accessToken = $wxmpService->getAccessToken(true); } $body = json_encode($body); return $wxmpService->Qrcode->getWxappQrcode($accessToken, $body); } public function BatchQrcode() { $ball = Dev::find()->select('id,store_id,name,dev_number,created_at')->where(['is_delete' => 0])->all(); if ($ball == null) { return ['code' => 1, 'msg' => '该设备不存在']; } $path = 'qrcode/dev_qrcode_zip'; if (!is_dir($path)) { mkdir($path, 0777, true); } $paths = []; foreach ($ball as $value) { $store = Store::findOne($value->store_id); if ($store == null) { return $value->dev_number . '编号设备所在门店不存在'; } $r = $this->getQrcode($store, $value); if ($r['code'] != 0) { return $r['msg']; } $name = $store->name . '_' . $value->dev_number . '.png'; array_push($paths, ['name' => $name, 'path' => $r['data']]); } if (empty($paths)) { return '无下载数据'; } $zipName = '设备二维码_' . date('Y年m月d日') . '下载' . '.zip'; // 压缩包文件名 return (new ZipFile())->downloadZipImg($zipName, $path, $paths); } }