'选择项', ]; } 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); } //设备二维码 public function actionQrWechatcode($id = 0) { $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' => '该设备所在门店不存在']; } $path = 'qrcode/'; if (!is_dir($path)) { mkdir($path, 0777, true); } $hostname = $this->url; $filename = 'qrcode/' . md5($ball->dev_number."wechat_01") . '.png'; if(is_dir($filename)){ return ['code' => 0, 'msg' => 'ok', 'data' => \Yii::$app->request->getHostInfo() . '/' . $filename]; } //微信小程序 $wechat_app = WechatApp::findOne([ 'cx_mch_id' => 0, 'is_delete' => 0 ]); if($wechat_app){ if (!is_dir(\Yii::$app->runtimePath . '/pem')) { mkdir(\Yii::$app->runtimePath . '/pem'); file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', ''); } $cert_pem_file = null; if ($wechat_app->cert_pem) { $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_app->cert_pem); if (!file_exists($cert_pem_file)) file_put_contents($cert_pem_file, $wechat_app->cert_pem); } $key_pem_file = null; if ($wechat_app->key_pem) { $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_app->key_pem); if (!file_exists($key_pem_file)) file_put_contents($key_pem_file, $wechat_app->key_pem); } $this->wechat_mp = new Wechat([ 'appId' => $wechat_app->app_id, 'appSecret' => $wechat_app->app_secret, 'mchId' => $wechat_app->mch_id, 'apiKey' => $wechat_app->key, 'certPem' => $cert_pem_file, 'keyPem' => $key_pem_file, ]); } $url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$this->wechat_mp->getAccessToken(); $arr = [ 'action_name' => 'QR_LIMIT_SCENE', 'action_info' => [ 'scene' => [ 'scene_id' => $ball->id, ], ], ]; $res = $this->wechat_mp->curl->post($url,json_encode($arr)); $res = json_decode($res->response,true); $url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($res['ticket']); $res = $this->wechat_mp->curl->get($url); file_put_contents($filename,$res->response); return ['code' => 0, 'msg' => 'ok', 'data' => \Yii::$app->request->getHostInfo() . '/' . $filename]; } /** * @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); } }