From 9a1c040c00397d83927a89c924887bc04ffacfdd Mon Sep 17 00:00:00 2001 From: Anyon Date: Mon, 19 Mar 2018 13:48:36 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=9B=B4=E6=96=B0]=E4=BF=AE=E6=94=B9=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E6=A8=A1=E5=9D=97=E5=8F=8C=E6=A8=A1=E5=9D=97=E5=85=BC?= =?UTF-8?q?=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/wechat/service/MediaService.php | 45 ++++++++++++++++----- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/application/wechat/service/MediaService.php b/application/wechat/service/MediaService.php index 8dd14f7a1..bdf8b3734 100644 --- a/application/wechat/service/MediaService.php +++ b/application/wechat/service/MediaService.php @@ -14,6 +14,7 @@ namespace app\wechat\service; +use service\FileService; use service\WechatService; use think\Db; @@ -56,17 +57,20 @@ class MediaService * @return string * @throws \WeChat\Exceptions\InvalidResponseException * @throws \WeChat\Exceptions\LocalCacheException + * @throws \think\Exception + * @throws \think\exception\PDOException */ public static function uploadImage($local_url) { - $media_url = Db::name('WechatNewsImage')->where(['md5' => md5($local_url)])->value('media_url'); - if (!empty($media_url)) { + $map = ['md5' => md5($local_url)]; + if (!($media_url = Db::name('WechatNewsImage')->where($map)->value('media_url'))) { return $media_url; } - $result = WechatService::wechat()->upFile(base64_encode(file_get_contents($local_url)), $local_url); - $info = WechatService::media()->uploadImg($result['file']); - WechatService::wechat()->rmFile($local_url); - $data = ['local_url' => $local_url, 'media_url' => $info['url'], 'md5' => md5($local_url)]; + $info = WechatService::media()->uploadImg(self::getServerPath($local_url)); + if (strtolower(sysconf('wechat_type')) === 'thr') { + WechatService::wechat()->rmFile($local_url); + } + $data = ['local_url' => $local_url, 'media_url' => $info['url'], 'md5' => $map['md5']]; Db::name('WechatNewsImage')->insert($data); return $info['url']; } @@ -88,13 +92,36 @@ class MediaService if (($media_id = Db::name('WechatNewsMedia')->where($map)->value('media_id'))) { return $media_id; } - $result = WechatService::wechat()->upFile(base64_encode(file_get_contents($local_url)), $local_url); - $result = WechatService::media()->addMaterial($result['file'], $type, $video_info); - WechatService::wechat()->rmFile($local_url); + $result = WechatService::media()->addMaterial(self::getServerPath($local_url), $type, $video_info); + if (strtolower(sysconf('wechat_type')) === 'thr') { + WechatService::wechat()->rmFile($local_url); + } $data = ['md5' => $map['md5'], 'type' => $type, 'appid' => $map['appid'], 'media_id' => $result['media_id'], 'local_url' => $local_url]; isset($result['url']) && $data['media_url'] = $result['url']; Db::name('WechatNewsMedia')->insert($data); return $data['media_id']; } + /** + * 文件位置处理 + * @param string $local + * @return string + * @throws \think\Exception + * @throws \think\exception\PDOException + */ + protected static function getServerPath($local) + { + switch (strtolower(sysconf('wechat_type'))) { + case 'api': + if (file_exists($local)) { + return $local; + } + return FileService::download($local)['file']; + case 'thr': + return WechatService::wechat()->upFile(base64_encode(file_get_contents($local)), $local)['file']; + default: + return $local; + } + } + } \ No newline at end of file