getMedia($media_id, 1); if (empty($qrcodeArr['body'])) { //WxLog::init(); AppLog::DEBUG("=========== saveWxImgToRemoveServer wx error ==============="); AppLog::DEBUG("=========== errorCode =============== mediaId: {$media_id} ". $wechat->errCode.' errMsg:'. $wechat->errMsg); AppErrorLog::error("== saveWxImgToRemoveServer mediaId: {$media_id} WxErrorCode:==".$wechat->errCode. ' errMsg:'. $wechat->errMsg); $imgModel = BindDeviceApplyImg::findOne(['wx_server_id'=>$media_id]); if ($imgModel) { $imgModel->error_msg = $wechat->errCode .":".$wechat->errMsg; $imgModel->save(); } //WxLog::ERROR("=========== errorMsg ===============". $wechat->errMsg."\r\n"); return false; } $filename_root = Yii::getAlias('@app/wx') . "/web/"; $path = $filename_root.'/tmp'; if(!is_dir($path)) @mkdir($path, 0777); $fileStr = $media_id; $filename = $path.'/'.time().'_'.$fileStr.'.jpg'; $local_file = fopen($filename, 'w'); if (false !== $local_file) { if (false !== fwrite($local_file, $qrcodeArr['body'])) { fclose($local_file); } } // 若未保存记录, imgPathId 应该是二维码 $stat_path = ImageManager::getBindApplyImgPath($imgPathId, $fileStr); if (!file_exists($filename)) { return null; } $fileSize = filesize($filename); if ($fileSize < 2) { return null; } $res = ImageManager::add($filename, $stat_path); if (empty($res)) { return false; } else { // 删除缓存文件,这里会出现同时操作删除,结果一个报错,在前面加@ if (file_exists($filename)) { //@unlink($filename); } } return $stat_path; } /** * */ public static function saveBase64ImgFileToLocal($imageStr, $tmpPath = 'tmp/') { if ($tmpPath) { $tmpPastePath = $tmpPath; } else { $tmpPastePath = 'tmp/'; } $somePath = $dir = Yii::getAlias('@site') . "/".$tmpPastePath; if (!is_dir($somePath)) { mkdir($somePath, 0777, true); } if (!is_writable($somePath)) { chmod($somePath, 0777); } $image = imagecreatefromstring($imageStr); $fileNameId = self::TMP_PASTE_PRE. date('YmdHis').'_'.mt_rand(100000, 999999); $filename = $fileNameId.'.jpg'; $pathFile = $somePath .$filename; imagejpeg($image, $pathFile); $tmpPathName = $tmpPastePath.$filename; return ['fileNameId' => $fileNameId, 'tmpPathName' => $tmpPathName]; } /** * 直接本地传都OSS */ public static function saveLocalImgToRemoveServer($fileNameId = '', $applyId) { $tmpPastePath = self::TMP_PASTE_PATH; $somePath = Yii::getAlias('@app/wx') . "/web/".$tmpPastePath; $filename = $somePath.$fileNameId.'.jpg'; if (!file_exists($filename)) { return null; } $stat_path = ImageManager::getBindApplyImgPath($applyId, $fileNameId); $res = ImageManager::add($filename, $stat_path); if (empty($res)) { return false; } return $stat_path; } /** * 筛选出wx 图片和本地图片的id */ public static function filterImage($applyImgIds) { $resultArr = []; foreach ($applyImgIds as $k => $v) { if ( FileHelper::TMP_PASTE_PRE == substr($v, 0, strlen(FileHelper::TMP_PASTE_PRE))) { $resultArr['local'][] = $v; } else { $resultArr['wx'][] = $v; } } return $resultArr; } }