WeixinService.php 18 KB

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