|
@@ -6,6 +6,7 @@ use App\Model\WxArticle;
|
|
use App\Model\Settings;
|
|
use App\Model\Settings;
|
|
use App\Model\WxArticleDetail;
|
|
use App\Model\WxArticleDetail;
|
|
use App\Model\WxArticleDetailContent;
|
|
use App\Model\WxArticleDetailContent;
|
|
|
|
+use App\Service\FileUploadService;
|
|
use App\Service\Service;
|
|
use App\Service\Service;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Log;
|
|
@@ -14,6 +15,7 @@ use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
class WeixinService extends Service
|
|
class WeixinService extends Service
|
|
{
|
|
{
|
|
|
|
+ const dir = 'wx_img/';
|
|
const wx_img = "app/public/wx_img/";
|
|
const wx_img = "app/public/wx_img/";
|
|
|
|
|
|
public function getToken(){
|
|
public function getToken(){
|
|
@@ -202,6 +204,8 @@ class WeixinService extends Service
|
|
}
|
|
}
|
|
|
|
|
|
public function get_helper_for_img($url,$header=[],$timeout = 20){
|
|
public function get_helper_for_img($url,$header=[],$timeout = 20){
|
|
|
|
+ if(empty($url)) return [true, ""];
|
|
|
|
+
|
|
$ch = curl_init();
|
|
$ch = curl_init();
|
|
curl_setopt_array($ch, array(
|
|
curl_setopt_array($ch, array(
|
|
CURLOPT_URL => $url,
|
|
CURLOPT_URL => $url,
|
|
@@ -255,7 +259,10 @@ class WeixinService extends Service
|
|
public function getTemporaryUrl($body,$url)
|
|
public function getTemporaryUrl($body,$url)
|
|
{
|
|
{
|
|
// 定义本地文件路径
|
|
// 定义本地文件路径
|
|
- $name = md5($url) . '.jpg';
|
|
|
|
|
|
+ list($micro, $seconds) = explode(' ', microtime());
|
|
|
|
+ // 从微秒部分截取全部6位作为唯一标识符
|
|
|
|
+ $microSuffix = substr($micro, 2, 6);
|
|
|
|
+ $name = date('YmdHis', $seconds) . $microSuffix . '.jpg';
|
|
$localFilePath = storage_path(self::wx_img . $name);
|
|
$localFilePath = storage_path(self::wx_img . $name);
|
|
|
|
|
|
// 写入文件前先检查文件是否存在
|
|
// 写入文件前先检查文件是否存在
|
|
@@ -329,6 +336,8 @@ class WeixinService extends Service
|
|
try {
|
|
try {
|
|
DB::beginTransaction();
|
|
DB::beginTransaction();
|
|
|
|
|
|
|
|
+ //保存数据
|
|
|
|
+ $upload = $delete = [];
|
|
foreach ($result['item'] as $value){
|
|
foreach ($result['item'] as $value){
|
|
$model = WxArticle::where('del_time',0)
|
|
$model = WxArticle::where('del_time',0)
|
|
->where('article_id', $value['article_id'])
|
|
->where('article_id', $value['article_id'])
|
|
@@ -346,8 +355,8 @@ class WeixinService extends Service
|
|
// 从微秒部分截取全部6位作为唯一标识符
|
|
// 从微秒部分截取全部6位作为唯一标识符
|
|
$microSuffix = substr($micro, 2, 6);
|
|
$microSuffix = substr($micro, 2, 6);
|
|
$content_name = date('YmdHis', $seconds) . $microSuffix;
|
|
$content_name = date('YmdHis', $seconds) . $microSuffix;
|
|
- $content_img = $this->saveContent($val['content'], $content_name);
|
|
|
|
- $this->get_helper_for_img($val['thumb_url']);
|
|
|
|
|
|
+ list($content_img,$name) = $this->saveContent($val['content'], $content_name);
|
|
|
|
+ list($status, $my_thumb_url) = $this->get_helper_for_img($val['thumb_url']);
|
|
|
|
|
|
$modelDetail = new WxArticleDetail();
|
|
$modelDetail = new WxArticleDetail();
|
|
$modelDetail->wx_article_id = $id;
|
|
$modelDetail->wx_article_id = $id;
|
|
@@ -360,6 +369,7 @@ class WeixinService extends Service
|
|
$modelDetail->show_cover_pic = $val['show_cover_pic'];
|
|
$modelDetail->show_cover_pic = $val['show_cover_pic'];
|
|
$modelDetail->url = $val['url'];
|
|
$modelDetail->url = $val['url'];
|
|
$modelDetail->thumb_url = $val['thumb_url'];
|
|
$modelDetail->thumb_url = $val['thumb_url'];
|
|
|
|
+ $modelDetail->my_thumb_url = $my_thumb_url;
|
|
$modelDetail->need_open_comment = $val['need_open_comment'];
|
|
$modelDetail->need_open_comment = $val['need_open_comment'];
|
|
$modelDetail->only_fans_can_comment = $val['only_fans_can_comment'];
|
|
$modelDetail->only_fans_can_comment = $val['only_fans_can_comment'];
|
|
$modelDetail->is_deleted = $val['is_deleted'];
|
|
$modelDetail->is_deleted = $val['is_deleted'];
|
|
@@ -370,6 +380,7 @@ class WeixinService extends Service
|
|
if(! empty($content_img)){
|
|
if(! empty($content_img)){
|
|
$insert = [];
|
|
$insert = [];
|
|
foreach ($content_img as $value){
|
|
foreach ($content_img as $value){
|
|
|
|
+ $upload[] = $value;
|
|
$insert[] = [
|
|
$insert[] = [
|
|
'wx_article_detail_id' => $modelDetail->id,
|
|
'wx_article_detail_id' => $modelDetail->id,
|
|
'url' => $value
|
|
'url' => $value
|
|
@@ -377,6 +388,8 @@ class WeixinService extends Service
|
|
}
|
|
}
|
|
WxArticleDetailContent::insert($insert);
|
|
WxArticleDetailContent::insert($insert);
|
|
}
|
|
}
|
|
|
|
+ if(! empty($name)) $upload[] = $name;
|
|
|
|
+ if(! empty($my_thumb_url)) $upload[] = $my_thumb_url;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}else{
|
|
}else{
|
|
@@ -408,8 +421,8 @@ class WeixinService extends Service
|
|
// 从微秒部分截取全部6位作为唯一标识符
|
|
// 从微秒部分截取全部6位作为唯一标识符
|
|
$microSuffix = substr($micro, 2, 6);
|
|
$microSuffix = substr($micro, 2, 6);
|
|
$content_name = date('YmdHis', $seconds) . $microSuffix;
|
|
$content_name = date('YmdHis', $seconds) . $microSuffix;
|
|
- $content_img = $this->saveContent($val['content'], $content_name);
|
|
|
|
- $this->get_helper_for_img($val['thumb_url']);
|
|
|
|
|
|
+ list($content_img,$name) = $this->saveContent($val['content'], $content_name);
|
|
|
|
+ list($status, $my_thumb_url) = $this->get_helper_for_img($val['thumb_url']);
|
|
|
|
|
|
$modelDetail = new WxArticleDetail();
|
|
$modelDetail = new WxArticleDetail();
|
|
$modelDetail->wx_article_id = $id;
|
|
$modelDetail->wx_article_id = $id;
|
|
@@ -422,6 +435,7 @@ class WeixinService extends Service
|
|
$modelDetail->show_cover_pic = $val['show_cover_pic'];
|
|
$modelDetail->show_cover_pic = $val['show_cover_pic'];
|
|
$modelDetail->url = $val['url'];
|
|
$modelDetail->url = $val['url'];
|
|
$modelDetail->thumb_url = $val['thumb_url'];
|
|
$modelDetail->thumb_url = $val['thumb_url'];
|
|
|
|
+ $modelDetail->my_thumb_url = $my_thumb_url;
|
|
$modelDetail->need_open_comment = $val['need_open_comment'];
|
|
$modelDetail->need_open_comment = $val['need_open_comment'];
|
|
$modelDetail->only_fans_can_comment = $val['only_fans_can_comment'];
|
|
$modelDetail->only_fans_can_comment = $val['only_fans_can_comment'];
|
|
$modelDetail->is_deleted = $val['is_deleted'];
|
|
$modelDetail->is_deleted = $val['is_deleted'];
|
|
@@ -432,6 +446,7 @@ class WeixinService extends Service
|
|
if(! empty($content_img)){
|
|
if(! empty($content_img)){
|
|
$insert = [];
|
|
$insert = [];
|
|
foreach ($content_img as $value){
|
|
foreach ($content_img as $value){
|
|
|
|
+ $upload[] = $value;
|
|
$insert[] = [
|
|
$insert[] = [
|
|
'wx_article_detail_id' => $modelDetail->id,
|
|
'wx_article_detail_id' => $modelDetail->id,
|
|
'url' => $value
|
|
'url' => $value
|
|
@@ -439,15 +454,27 @@ class WeixinService extends Service
|
|
}
|
|
}
|
|
WxArticleDetailContent::insert($insert);
|
|
WxArticleDetailContent::insert($insert);
|
|
}
|
|
}
|
|
|
|
+ if(! empty($name)) $upload[] = $name;
|
|
|
|
+ if(! empty($my_thumb_url)) $upload[] = $my_thumb_url;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- $this->delFile($detail);
|
|
|
|
- $this->delFile($detail2);
|
|
|
|
|
|
+ foreach ($detail as $value){
|
|
|
|
+ if(! empty($value['txt'])) $delete[] = $value['txt'];
|
|
|
|
+ if(! empty($value['img'])) $delete[] = $value['img'];
|
|
|
|
+ }
|
|
|
|
+ foreach ($detail2 as $value){
|
|
|
|
+ if(! empty($value['img'])) $delete[] = $value['img'];
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ //上传阿里云
|
|
|
|
+ $service = new FileUploadService();
|
|
|
|
+ $service->createOssUploadWx($upload);
|
|
|
|
+ $service->createOssDeleteWx($delete);
|
|
|
|
+
|
|
DB::commit();
|
|
DB::commit();
|
|
return [true, ''];
|
|
return [true, ''];
|
|
}catch (\Throwable $exception){
|
|
}catch (\Throwable $exception){
|
|
@@ -484,7 +511,7 @@ class WeixinService extends Service
|
|
file_put_contents($localFilePath, $content);
|
|
file_put_contents($localFilePath, $content);
|
|
}
|
|
}
|
|
|
|
|
|
- return $img ?? [];
|
|
|
|
|
|
+ return [$img ?? [], $name];
|
|
}
|
|
}
|
|
|
|
|
|
public function dealContent1($content){
|
|
public function dealContent1($content){
|
|
@@ -513,7 +540,7 @@ class WeixinService extends Service
|
|
foreach ($urls as $url) {
|
|
foreach ($urls as $url) {
|
|
list($status, $msg) = $this->get_helper_for_img($url,$header);
|
|
list($status, $msg) = $this->get_helper_for_img($url,$header);
|
|
if($status){
|
|
if($status){
|
|
- $newImageUrl = "https://adminapi.qingyaokeji.com/weixin/getWxFileLocal/" . $msg;
|
|
|
|
|
|
+ $newImageUrl = $msg;
|
|
|
|
|
|
// 构建完整的 data-src 属性
|
|
// 构建完整的 data-src 属性
|
|
$oldDataSrc = 'data-src="' . $url . '"';
|
|
$oldDataSrc = 'data-src="' . $url . '"';
|
|
@@ -531,7 +558,7 @@ class WeixinService extends Service
|
|
public function delFile($detail){
|
|
public function delFile($detail){
|
|
if(empty($detail)) return;
|
|
if(empty($detail)) return;
|
|
|
|
|
|
- $dir = 'wx_img/';
|
|
|
|
|
|
+ $dir = Self::dir;
|
|
foreach ($detail as $value){
|
|
foreach ($detail as $value){
|
|
if(isset($value['txt'])){
|
|
if(isset($value['txt'])){
|
|
$dir_content = $dir . $value['txt'] . '.txt';
|
|
$dir_content = $dir . $value['txt'] . '.txt';
|
|
@@ -554,7 +581,7 @@ class WeixinService extends Service
|
|
|
|
|
|
$model = WxArticleDetail::where('del_time',0)
|
|
$model = WxArticleDetail::where('del_time',0)
|
|
->where('is_deleted', 0)
|
|
->where('is_deleted', 0)
|
|
- ->select('title','author','digest','thumb_url','id','crt_time')
|
|
|
|
|
|
+ ->select('title','author','digest','thumb_url','id','crt_time','my_thumb_url')
|
|
->orderBy('crt_time','desc');
|
|
->orderBy('crt_time','desc');
|
|
|
|
|
|
$list = $this->limit($model,'',$data);
|
|
$list = $this->limit($model,'',$data);
|
|
@@ -568,11 +595,10 @@ class WeixinService extends Service
|
|
public function organizationData($data){
|
|
public function organizationData($data){
|
|
if(empty($data['data'])) return $data;
|
|
if(empty($data['data'])) return $data;
|
|
|
|
|
|
|
|
+ $service = new FileUploadService();
|
|
foreach ($data['data'] as $key => $value){
|
|
foreach ($data['data'] as $key => $value){
|
|
- $my_thumb_url = "";
|
|
|
|
- if(! empty($value['thumb_url'])) $my_thumb_url = "getWxFileLocal/" . md5($value['thumb_url']) . '.jpg';
|
|
|
|
- $data['data'][$key]['my_thumb_url'] = $my_thumb_url;
|
|
|
|
$data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i", $value['crt_time']) : "";
|
|
$data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i", $value['crt_time']) : "";
|
|
|
|
+ $data['data'][$key]['my_thumb_url'] = $service->getFileShowWx($value['my_thumb_url']);
|
|
}
|
|
}
|
|
|
|
|
|
return $data;
|
|
return $data;
|
|
@@ -591,17 +617,41 @@ class WeixinService extends Service
|
|
if(empty($detail)) return [false, '文章不存在或已被删除'];
|
|
if(empty($detail)) return [false, '文章不存在或已被删除'];
|
|
$detail = $detail->toArray();
|
|
$detail = $detail->toArray();
|
|
if($detail['is_deleted']) return [false, '文章不存在或被删除'];
|
|
if($detail['is_deleted']) return [false, '文章不存在或被删除'];
|
|
-
|
|
|
|
if(empty($detail['content_name'])) return [true, ['content' => '']];
|
|
if(empty($detail['content_name'])) return [true, ['content' => '']];
|
|
|
|
|
|
- // 定义本地文件路径
|
|
|
|
- $name = $detail['content_name'] . '.txt';
|
|
|
|
- $localFilePath = storage_path(self::wx_img . $name);
|
|
|
|
|
|
+ //获取文件
|
|
|
|
+ $service = new FileUploadService();
|
|
|
|
+ $content = $service->getFileShowWx($detail['content_name']);
|
|
|
|
|
|
// 读取文件内容
|
|
// 读取文件内容
|
|
- $fileContent = file_get_contents($localFilePath);
|
|
|
|
|
|
+ $fileContent = file_get_contents($content);
|
|
|
|
+ $fileContent = $this->dealContent2($fileContent);
|
|
|
|
|
|
$detail['content'] = $fileContent;
|
|
$detail['content'] = $fileContent;
|
|
return [true, ['data' => $detail]];
|
|
return [true, ['data' => $detail]];
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public function dealContent2($content){
|
|
|
|
+ // 正则表达式匹配 data-src 属性
|
|
|
|
+ $pattern = '/src="([^"]+)"/';
|
|
|
|
+
|
|
|
|
+ // 匹配所有 data-src 属性
|
|
|
|
+ preg_match_all($pattern, $content, $matches);
|
|
|
|
+
|
|
|
|
+ // 提取所有匹配到的 URL
|
|
|
|
+ $urls = $matches[1];
|
|
|
|
+
|
|
|
|
+ // 遍历每个匹配项并替换
|
|
|
|
+ $service = new FileUploadService();
|
|
|
|
+ foreach ($urls as $url) {
|
|
|
|
+ $newImageUrl = $service->getFileShowWx($url);
|
|
|
|
+ $oldDataSrc = 'src="' . $url . '"';
|
|
|
|
+ $newDataSrc = 'src="' . $newImageUrl . '"';
|
|
|
|
+
|
|
|
|
+ // 替换
|
|
|
|
+ $content = str_replace($oldDataSrc, $newDataSrc, $content);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return $content;
|
|
|
|
+ }
|
|
}
|
|
}
|