Commit e7b4abcd by 杨树贤

完成文章富文本图片替换

parent 87c90f89
...@@ -150,3 +150,5 @@ FOOTSTONE_URL=http://footstone.liexindev.net ...@@ -150,3 +150,5 @@ FOOTSTONE_URL=http://footstone.liexindev.net
TAG_URL=http://192.168.1.18:32581 TAG_URL=http://192.168.1.18:32581
SKIP_SEND_EMAIL=true SKIP_SEND_EMAIL=true
FILE_SERVER_URL=http://file.liexindev.net
...@@ -17,31 +17,123 @@ class DealImageService ...@@ -17,31 +17,123 @@ class DealImageService
preg_match_all($pattern, $text, $matches); preg_match_all($pattern, $text, $matches);
$imageUrls = $matches[1]; $imageUrls = $matches[1];
dump($imageUrls); // 输出所有提取出的 'img.ichunt.com' 图片链接 return $imageUrls;
} }
//先去处理芯媒体的图片链接 //先去处理芯媒体的图片链接
public static function dealNewsImage() public static function dealNewsImage()
{ {
$newsList = DB::connection('liexin')->table('article_addon')->limit(10)->get(); $newsList = DB::connection('liexin')->table('article')->where('deal_status', '!=', 1)->orderBy('art_id', 'desc')->limit(100)->get();
foreach ($newsList as $news) { foreach ($newsList as $news) {
self::getAllImage($news['body']); $addon = DB::connection('liexin')->table('article_addon')->where('art_id', $news['art_id'])->first();
if (empty($addon)) {
continue;
}
$news = array_merge($news, $addon);
if (!DB::connection('liexin')->table('article')->where('art_id', $news['art_id'])->exists()) {
dump("不存在的文章");
continue;
}
$imageUrls = self::getAllImage($news['body']);
if (empty($imageUrls)) {
continue;
}
//dump($imageUrls); // 输出所有提取出的 'img.ichunt.com' 图片链接
$imageMap = self::downloadAndUploadToPicServer($imageUrls);
//进行全局替换
foreach ($imageMap as $originImage => $newImage) {
$news['body'] = str_replace($originImage, $newImage, $news['body']);
}
DB::connection('liexin')->table('article_addon')->where('art_id', $news['art_id'])->update([
'body' => $news['body'],
]);
DB::connection('liexin')->table('article')->where('art_id', $news['art_id'])->update([
'deal_status' => 1,
]);
dump($news['art_id']);
} }
} }
//处理cms的图片链接
public function dealCmsImage()
{
}
//处理sku详细描述
public function dealSkuDetail()
{
//先找出所有爱智的sku,按搜索的来
}
//根据url下载图片并且上传到图片服务 //根据url下载图片并且上传到图片服务
public static function downloadAndUploadToPicServer($url) public static function downloadAndUploadToPicServer($urlList)
{ {
$path = storage_path('/dealImage/') . md5($url); $imageMap = [];
$client = new \GuzzleHttp\Client(); $path = '';
$response = $client->get($url); foreach ($urlList as $url) {
$contents = $response->getBody()->getContents(); if (str_contains($url, 'comhttp')) {
file_put_contents($path, $contents); continue;
}
try {
$info = pathinfo($url);
$path = storage_path('logs/') . md5($url) . '.' . $info['extension'];;
$client = new \GuzzleHttp\Client();
$response = $client->get($url);
$contents = $response->getBody()->getContents();
file_put_contents($path, $contents);
//然后上传到图片服务器
$response = $client->request('POST', env('FILE_SERVER_URL') . '/uploadFile?sys_type=5&create_uid=1000', [
'multipart' => [
[
'name' => 'file',
'contents' => fopen($url, 'r')
]
]
]);
if ($response->getStatusCode() != 200) {
dump('上传网络错误');
continue;
}
//然后上传到图片服务器 $res = json_decode($response->getBody()->getContents(), true);
if (isset($res['code'])) {
if ($res['code'] == 0) {
$ossFileUrl = $res['data']['oss_file_url'];
$imageMap[$url] = $ossFileUrl;
} else {
dump('上传服务接口返回错误 : ' . json_encode($res));
continue;
}
} else {
dump('上传网络错误,返回不一致的响应 : ' . json_encode($res));
continue;
}
} catch (\Exception $exception) {
dump("发生错误 : " . $exception->getMessage());
} finally {
if (file_exists($path)) {
//最后删除本地的文件
unlink($path);
}
}
}
return $imageMap;
//最后删除本地的文件
unlink($path);
} }
} }
...@@ -84,7 +84,7 @@ Route::group(['middleware' => ['external'], 'namespace' => 'Sync'], function () ...@@ -84,7 +84,7 @@ Route::group(['middleware' => ['external'], 'namespace' => 'Sync'], function ()
}); });
Route::match(['get', 'post'], '/test', function () { Route::match(['get', 'post'], '/test', function () {
\App\Http\Services\DataService::dealNewsImage(); \App\Http\Services\DealImageService::dealNewsImage();
//\App\Http\Services\SupplierAddressService::initUnitedAddress(); //\App\Http\Services\SupplierAddressService::initUnitedAddress();
//\App\Http\Services\DataService::checkSupplierBandAccount(); //\App\Http\Services\DataService::checkSupplierBandAccount();
//(new CompanyService())->checkSupplierCompanyAndAddress(11042); //(new CompanyService())->checkSupplierCompanyAndAddress(11042);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment