WeixinService.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. <?php
  2. namespace App\Service\Weixin;
  3. use App\Model\WxArticle;
  4. use App\Model\Settings;
  5. use App\Model\WxArticleDetail;
  6. use App\Model\WxArticleDetailContent;
  7. use App\Service\Service;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. use Illuminate\Support\Facades\Redis;
  11. use Illuminate\Support\Facades\Storage;
  12. class WeixinService extends Service
  13. {
  14. const wx_img = "app/public/wx_img/";
  15. public function getToken(){
  16. $config = config('qingyaoWx');
  17. $token_key = $config['redis_key'];
  18. $token = Redis::get($token_key);
  19. if(empty($token)){
  20. $url = sprintf($config['get_token'], $config['appid'], $config['appsecret']);
  21. $res = $this->curlOpen($url);
  22. $res = json_decode($res,true);
  23. if(isset($res['errmsg'])) return [false, $res['errmsg']];
  24. if(! isset($res['access_token'])) return [false, 'request error'];
  25. $token = $res['access_token'];
  26. $expire_time = $res['expires_in']-300;
  27. Redis::set($token_key,$token);
  28. Redis::expire($token_key, $expire_time);
  29. return [true,$token];
  30. }
  31. return [true, $token];
  32. }
  33. public function getPublicWxArticle($data){
  34. list($status, $msg) = $this->rule($data);
  35. if(! $status) {
  36. file_put_contents('record_ip.txt',date("Y-m-d H:i:s",time()).json_encode($data) . PHP_EOL."来源IP".$msg.PHP_EOL,8);
  37. return [false, 'IP未入白名单'];
  38. }
  39. list($status, $msg) = $this->getToken();
  40. if(! $status) return [false, $msg];
  41. $config = config('qingyaoWx');
  42. $url = sprintf($config['get_article'], $msg);
  43. $offset = empty($data['page_index']) ? 0 : $data['page_index'] - 1;
  44. $count = empty($data['page_size']) || $data['page_size'] > 10 ? 10 : $data['page_size'];
  45. $post = [
  46. 'offset' => $offset,
  47. 'count' => $count,
  48. 'no_content' => 0,
  49. ];
  50. $result = $this->curlOpen($url, ['post' => json_encode($post)]);
  51. $result = json_decode($result,true);
  52. if(isset($result['errmsg'])) return [false, $result['errmsg']];
  53. Log::channel('apiLog')->info('wxget', ["message" => $result]);
  54. return [true, ['data' => $result['item'] ?? [], 'total' => $result['total_count'], 'data_count' => $result['item_count']]];
  55. }
  56. public function getPublicWxArticleDetail($data){
  57. list($status, $msg) = $this->rule($data);
  58. if(! $status) {
  59. file_put_contents('record_ip.txt',date("Y-m-d H:i:s",time()).json_encode($data) . PHP_EOL."来源IP".$msg.PHP_EOL,8);
  60. return [false, 'IP未入白名单'];
  61. }
  62. if(empty($data['article_id'])) return [false, '文章ID不能为空'];
  63. list($status, $msg) = $this->getToken();
  64. if(! $status) return [false, $msg];
  65. $config = config('qingyaoWx');
  66. $url = sprintf($config['get_article_detail'], $msg);
  67. $post = [
  68. 'article_id' => $data['article_id'],
  69. ];
  70. $result = $this->curlOpen($url, ['post' => json_encode($post)]);
  71. $result = json_decode($result,true);
  72. if(isset($result['errmsg'])) return [false, $result['errmsg']];
  73. return [true, ['data' => $result['news_item'] ?? [] ]];
  74. }
  75. public function getPublicWxMaterial($data){
  76. list($status, $msg) = $this->rule($data);
  77. if(! $status) {
  78. file_put_contents('record_ip.txt',date("Y-m-d H:i:s",time()).json_encode($data) . PHP_EOL."来源IP".$msg.PHP_EOL,8);
  79. return [false, 'IP未入白名单'];
  80. }
  81. list($status, $msg) = $this->getToken();
  82. if(! $status) return [false, $msg];
  83. $config = config('qingyaoWx');
  84. $url = sprintf($config['get_material'], $msg);
  85. $offset = empty($data['page_index']) ? 1 : $data['page_index'] - 1;
  86. $count = empty($data['page_size']) || $data['page_size'] > 10 ? 10 : $data['page_size'];
  87. $post = [
  88. 'offset' => $offset,
  89. 'count' => $count,
  90. 'type' => 'news',
  91. ];
  92. $result = $this->curlOpen($url, ['post' => json_encode($post)]);
  93. $result = json_decode($result,true);
  94. if(isset($result['errmsg'])) return [false, $result['errmsg']];
  95. return [true, ['data' => $result['item'] ?? [], 'total' => $result['total_count'], 'data_count' => $result['item_count']]];
  96. }
  97. public function getPublicWxDraft($data){
  98. list($status, $msg) = $this->rule($data);
  99. if(! $status) {
  100. file_put_contents('record_ip.txt',date("Y-m-d H:i:s",time()).json_encode($data) . PHP_EOL."来源IP".$msg.PHP_EOL,8);
  101. return [false, 'IP未入白名单'];
  102. }
  103. list($status, $msg) = $this->getToken();
  104. if(! $status) return [false, $msg];
  105. $config = config('qingyaoWx');
  106. $url = sprintf($config['get_draft'], $msg);
  107. $offset = empty($data['page_index']) ? 1 : $data['page_index'] - 1;
  108. $count = empty($data['page_size']) || $data['page_size'] > 10 ? 10 : $data['page_size'];
  109. $post = [
  110. 'offset' => $offset,
  111. 'count' => $count,
  112. 'no_content' => 0,
  113. ];
  114. $result = $this->curlOpen($url, ['post' => json_encode($post)]);
  115. $result = json_decode($result,true);
  116. if(isset($result['errmsg'])) return [false, $result['errmsg']];
  117. return [true, ['data' => $result['item'] ?? [], 'total' => $result['total_count'], 'data_count' => $result['item_count']]];
  118. }
  119. public function getWxFile($data){
  120. list($status, $msg) = $this->rule($data);
  121. if(! $status) {
  122. file_put_contents('record_ip.txt',date("Y-m-d H:i:s",time()).json_encode($data) . PHP_EOL."来源IP".$msg.PHP_EOL,8);
  123. return [false, 'IP未入白名单'];
  124. }
  125. if(empty($data['wx_url'])) return [false, "URL不存在"];
  126. $header = ['Content-Type:application/json'];
  127. list($status,$msg) = $this->get_helper_for_img($data['wx_url'],$header);
  128. if(! $status) return [false, $msg];
  129. return [true, $msg];
  130. }
  131. public function rule($data){
  132. // 获取用户的IP地址
  133. $userIP = $_SERVER['REMOTE_ADDR'];
  134. // 获取设置的IP地址
  135. $allowedIPs = $this->allowedIPs();
  136. if(empty($allowedIPs)) return [false, $userIP];
  137. // 校验用户IP是否在允许的范围内
  138. $isValidIP = false;
  139. foreach ($allowedIPs as $allowedIP) {
  140. if (strpos($allowedIP, '/') !== false) {
  141. // IP段表示法校验
  142. list($subnet, $mask) = explode('/', $allowedIP);
  143. if ((ip2long($userIP) & ~((1 << (32 - $mask)) - 1)) == ip2long($subnet)) {
  144. $isValidIP = true;
  145. break;
  146. }
  147. } else {
  148. // 单个IP地址校验
  149. if ($allowedIP === $userIP) {
  150. $isValidIP = true;
  151. break;
  152. }
  153. }
  154. }
  155. return [$isValidIP, $userIP];
  156. }
  157. public function allowedIPs(){
  158. $allowedIPs = Settings::where('setting_name','allowedIPs')->first();
  159. if(empty($allowedIPs) || empty($allowedIPs->setting_value)) return [];
  160. return explode(',',$allowedIPs->setting_value);
  161. }
  162. public function get_helper_for_img($url,$header=[],$timeout = 20){
  163. $ch = curl_init();
  164. curl_setopt_array($ch, array(
  165. CURLOPT_URL => $url,
  166. CURLOPT_RETURNTRANSFER => true,
  167. CURLOPT_ENCODING => '',
  168. CURLOPT_MAXREDIRS => 10,
  169. CURLOPT_TIMEOUT => $timeout,
  170. CURLOPT_FOLLOWLOCATION => true,
  171. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  172. CURLOPT_CUSTOMREQUEST => 'GET',
  173. CURLOPT_SSL_VERIFYPEER => false,
  174. CURLOPT_HTTPHEADER => $header,
  175. ));
  176. $r = curl_exec($ch);
  177. if ($r === false) {
  178. // 获取错误号
  179. $errorNumber = curl_errno($ch);
  180. // 获取错误信息
  181. $errorMessage = curl_error($ch);
  182. $message = "cURL Error #{$errorNumber}: {$errorMessage}";
  183. Log::channel('apiLog')->info('wx', ["message" => $message]);
  184. return [false, $message];
  185. }
  186. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  187. curl_close($ch);
  188. // 检查是否为图片
  189. if (! $this->isImage($r)) return [false, "资源不是图片"];
  190. if ($httpCode == 200) {
  191. // 检查是否为图片
  192. list($status, $msg) = $this->getTemporaryUrl($r, $url);
  193. if(! $status) return [false, $msg];
  194. $img = $msg;
  195. } else {
  196. return [false, ''];
  197. }
  198. return [true, $img];
  199. }
  200. function isImage($data) {
  201. $imageInfo = getimagesizefromstring($data);
  202. return $imageInfo !== false;
  203. }
  204. //生成临时文件
  205. public function getTemporaryUrl($body,$url)
  206. {
  207. // 定义本地文件路径
  208. $name = md5($url) . '.jpg';
  209. $localFilePath = storage_path(self::wx_img . $name);
  210. // 写入文件前先检查文件是否存在
  211. if (! file_exists($localFilePath)) {
  212. // 检查目录是否存在,如果不存在则创建
  213. $directoryPath = dirname($localFilePath);
  214. if (!is_dir($directoryPath)) {
  215. // 设置目录权限,可以根据需要更改
  216. $mode = 0777;
  217. // 使用递归选项创建目录
  218. if (!mkdir($directoryPath, $mode, true)) {
  219. return [false, '目录创建失败'];
  220. }
  221. }
  222. file_put_contents($localFilePath, $body);
  223. }
  224. return [true, $name];
  225. }
  226. public function getArticle(){
  227. list($status, $msg) = $this->getToken();
  228. if(! $status) return [false, $msg];
  229. $config = config('qingyaoWx');
  230. $url = sprintf($config['get_article'], $msg);
  231. $offset = 0;
  232. $count = 20;
  233. $get_total = 0;
  234. do {
  235. $post = [
  236. 'offset' => $offset,
  237. 'count' => $count,
  238. 'no_content' => 0,
  239. ];
  240. $result = $this->curlOpen($url, ['post' => json_encode($post)]);
  241. $result = json_decode($result, true);
  242. Log::channel('apiLog')->info('wxget', ["message" => $result]);
  243. if (isset($result['errmsg'])) return [false, $result['errmsg']];
  244. //保存文章
  245. list($status,$msg) = $this->saveWxArticle($result);
  246. if(! $status) return [false, $msg];
  247. // 更新 offset
  248. $offset += 1;
  249. // 更新 total_count
  250. $get_total += $result['total_count'];
  251. // 检查是否还有更多数据
  252. $hasMore = isset($result['total_count']) && ($result['total_count'] > $get_total);
  253. } while ($hasMore);
  254. return [true, ''];
  255. }
  256. public function saveWxArticle($result){
  257. if(! empty($result['item'])){
  258. try {
  259. DB::beginTransaction();
  260. foreach ($result['item'] as $value){
  261. $model = WxArticle::where('del_time',0)
  262. ->where('article_id', $value['article_id'])
  263. ->first();
  264. if(empty($model)){
  265. $model = new WxArticle();
  266. $model->article_id = $value['article_id'];
  267. $model->upd_time = $value['update_time'];
  268. $model->save();
  269. $id = $model->id;
  270. if(! empty($value['content']['news_item'])){
  271. foreach ($value['content']['news_item'] as $val){
  272. // 获取当前时间的微秒级时间戳
  273. list($micro, $seconds) = explode(' ', microtime());
  274. // 从微秒部分截取全部6位作为唯一标识符
  275. $microSuffix = substr($micro, 2, 6);
  276. $content_name = date('YmdHis', $seconds) . $microSuffix;
  277. $content_img = $this->saveContent($val['content'], $content_name);
  278. $this->get_helper_for_img($val['thumb_url']);
  279. $modelDetail = new WxArticleDetail();
  280. $modelDetail->wx_article_id = $id;
  281. $modelDetail->title = $val['title'];
  282. $modelDetail->author = $val['author'];
  283. $modelDetail->digest = $val['digest'];
  284. $modelDetail->content_name = $content_name;
  285. $modelDetail->content_source_url = $val['content_source_url'];
  286. $modelDetail->thumb_media_id = $val['thumb_media_id'];
  287. $modelDetail->show_cover_pic = $val['show_cover_pic'];
  288. $modelDetail->url = $val['url'];
  289. $modelDetail->thumb_url = $val['thumb_url'];
  290. $modelDetail->need_open_comment = $val['need_open_comment'];
  291. $modelDetail->only_fans_can_comment = $val['only_fans_can_comment'];
  292. $modelDetail->is_deleted = $val['is_deleted'];
  293. $modelDetail->upd_time = $value['content']['update_time'];
  294. $modelDetail->crt_time = $value['content']['create_time'];
  295. $modelDetail->save();
  296. if(! empty($content_img)){
  297. $insert = [];
  298. foreach ($content_img as $value){
  299. $insert[] = [
  300. 'wx_article_detail_id' => $modelDetail->id,
  301. 'url' => $value
  302. ];
  303. }
  304. WxArticleDetailContent::insert($insert);
  305. }
  306. }
  307. }
  308. }else{
  309. if($model->upd_time != $value['update_time']){
  310. $model->upd_time = $value['update_time'];
  311. $model->save();
  312. $id = $model->id;
  313. $detail = WxArticleDetail::where('del_time',0)
  314. ->where('wx_article_id',$id)
  315. ->select('content_name as txt','thumb_url as img','id')
  316. ->get()->toArray();
  317. WxArticleDetail::where('del_time',0)
  318. ->where('wx_article_id',$id)
  319. ->update(['del_time' => time()]);
  320. $detail2 = WxArticleDetailContent::where('del_time',0)
  321. ->whereIn('id', array_column($detail,'id'))
  322. ->select('url as img')
  323. ->get()->toArray();
  324. WxArticleDetailContent::where('del_time',0)
  325. ->whereIn('id', array_column($detail,'id'))
  326. ->update(['del_time' => time()]);
  327. if(! empty($value['content']['news_item'])){
  328. foreach ($value['content']['news_item'] as $val){
  329. // 获取当前时间的微秒级时间戳
  330. list($micro, $seconds) = explode(' ', microtime());
  331. // 从微秒部分截取全部6位作为唯一标识符
  332. $microSuffix = substr($micro, 2, 6);
  333. $content_name = date('YmdHis', $seconds) . $microSuffix;
  334. $content_img = $this->saveContent($val['content'], $content_name);
  335. $this->get_helper_for_img($val['thumb_url']);
  336. $modelDetail = new WxArticleDetail();
  337. $modelDetail->wx_article_id = $id;
  338. $modelDetail->title = $val['title'];
  339. $modelDetail->author = $val['author'];
  340. $modelDetail->digest = $val['digest'];
  341. $modelDetail->content_name = $content_name;
  342. $modelDetail->content_source_url = $val['content_source_url'];
  343. $modelDetail->thumb_media_id = $val['thumb_media_id'];
  344. $modelDetail->show_cover_pic = $val['show_cover_pic'];
  345. $modelDetail->url = $val['url'];
  346. $modelDetail->thumb_url = $val['thumb_url'];
  347. $modelDetail->need_open_comment = $val['need_open_comment'];
  348. $modelDetail->only_fans_can_comment = $val['only_fans_can_comment'];
  349. $modelDetail->is_deleted = $val['is_deleted'];
  350. $modelDetail->upd_time = $value['content']['update_time'];
  351. $modelDetail->crt_time = $value['content']['create_time'];
  352. $modelDetail->save();
  353. if(! empty($content_img)){
  354. $insert = [];
  355. foreach ($content_img as $value){
  356. $insert[] = [
  357. 'wx_article_detail_id' => $modelDetail->id,
  358. 'url' => $value
  359. ];
  360. }
  361. WxArticleDetailContent::insert($insert);
  362. }
  363. }
  364. }
  365. $this->delFile($detail);
  366. $this->delFile($detail2);
  367. }
  368. }
  369. }
  370. DB::commit();
  371. return [true, ''];
  372. }catch (\Throwable $exception){
  373. DB::rollBack();
  374. return [false, $exception->getMessage() . '|' . $exception->getLine() . '|' . $exception->getFile()];
  375. }
  376. }
  377. return [false, '暂无数据'];
  378. }
  379. //生成临时文件
  380. public function saveContent($content, $name)
  381. {
  382. // 定义本地文件路径
  383. $name = $name . '.txt';
  384. $localFilePath = storage_path(self::wx_img . $name);
  385. // 写入文件前先检查文件是否存在
  386. if (! file_exists($localFilePath)) {
  387. // 检查目录是否存在,如果不存在则创建
  388. $directoryPath = dirname($localFilePath);
  389. if (!is_dir($directoryPath)) {
  390. // 设置目录权限,可以根据需要更改
  391. $mode = 0777;
  392. // 使用递归选项创建目录
  393. if (!mkdir($directoryPath, $mode, true)) {
  394. return [false, '目录创建失败'];
  395. }
  396. }
  397. list($content, $img) = $this->dealContent($content);
  398. file_put_contents($localFilePath, $content);
  399. }
  400. return $img ?? [];
  401. }
  402. public function dealContent($content){
  403. // 正则表达式匹配 data-src 属性
  404. $pattern = '/data-src="([^"]+)"/';
  405. // 匹配所有 data-src 属性
  406. preg_match_all($pattern, $content, $matches);
  407. // 提取所有匹配到的 URL
  408. $urls = $matches[1];
  409. $return = [];
  410. $header = ['Content-Type:application/json'];
  411. // 遍历每个匹配项并替换
  412. foreach ($urls as $url) {
  413. list($status, $msg) = $this->get_helper_for_img($url,$header);
  414. if($status){
  415. $newImageUrl = "https://adminapi.qingyaokeji.com/weixin/getWxFileLocal/" . $msg;
  416. // 构建完整的 data-src 属性
  417. $oldDataSrc = 'data-src="' . $url . '"';
  418. $newDataSrc = 'data-src="' . $newImageUrl . '"';
  419. // 替换
  420. $content = str_replace($oldDataSrc, $newDataSrc, $content);
  421. $return[] = $msg;
  422. }
  423. }
  424. return [$content,$return];
  425. }
  426. public function delFile($detail){
  427. if(empty($detail)) return;
  428. $dir = 'wx_img/';
  429. foreach ($detail as $value){
  430. if(isset($value['txt'])){
  431. $dir_content = $dir . $value['txt'] . '.txt';
  432. if(Storage::disk('public')->exists($dir_content)) Storage::disk('public')->delete($dir_content);
  433. }
  434. if(! empty($value['img'])){
  435. $dir_thumb = $dir . md5($value['img']) . '.jpg';
  436. if(Storage::disk('public')->exists($dir_thumb)) Storage::disk('public')->delete($dir_thumb);
  437. }
  438. }
  439. }
  440. public function getArticleList($data){
  441. list($status, $msg) = $this->rule($data);
  442. if(! $status) {
  443. file_put_contents('record_ip.txt',date("Y-m-d H:i:s",time()).json_encode($data) . PHP_EOL."来源IP".$msg.PHP_EOL,8);
  444. return [false, 'IP未入白名单'];
  445. }
  446. $model = WxArticleDetail::where('del_time',0)
  447. ->where('is_deleted', 0)
  448. ->select('title','author','digest','thumb_url','id','crt_time')
  449. ->orderBy('crt_time','desc');
  450. $list = $this->limit($model,'',$data);
  451. //组织数据
  452. $list = $this->organizationData($list);
  453. return [true, $list];
  454. }
  455. public function organizationData($data){
  456. if(empty($data['data'])) return $data;
  457. foreach ($data['data'] as $key => $value){
  458. $my_thumb_url = "";
  459. if(! empty($value['thumb_url'])) $my_thumb_url = "getWxFileLocal/" . md5($value['thumb_url']) . '.jpg';
  460. $data['data'][$key]['my_thumb_url'] = $my_thumb_url;
  461. $data['data'][$key]['crt_time'] = $value['crt_time'] ? date("Y-m-d H:i", $value['crt_time']) : "";
  462. }
  463. return $data;
  464. }
  465. public function getArticleContent($data){
  466. list($status, $msg) = $this->rule($data);
  467. if(! $status) {
  468. file_put_contents('record_ip.txt',date("Y-m-d H:i:s",time()).json_encode($data) . PHP_EOL."来源IP".$msg.PHP_EOL,8);
  469. return [false, 'IP未入白名单'];
  470. }
  471. if(empty($data['id'])) return [false, 'ID不能为空'];
  472. $detail = WxArticleDetail::where('id',$data['id'])->first();
  473. if(empty($detail)) return [false, '文章不存在或已被删除'];
  474. $detail = $detail->toArray();
  475. if($detail['is_deleted']) return [false, '文章不存在或被删除'];
  476. if(empty($detail['content_name'])) return [true, ['content' => '']];
  477. // 定义本地文件路径
  478. $name = $detail['content_name'] . '.txt';
  479. $localFilePath = storage_path(self::wx_img . $name);
  480. // 读取文件内容
  481. $fileContent = file_get_contents($localFilePath);
  482. $detail['content'] = $fileContent;
  483. return [true, ['data' => $detail]];
  484. }
  485. }