123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2022/10/25
- * Time: 15:41
- */
- namespace App\Service;
- use App\Log;
- use App\RedisModel;
- class DongFangService
- {
- const BASE_URI = 'https://video-wechat-open.eastdrama.com/';
- const SESSION_RDS_KEY = 'Playlet:DongFangSession';
- /*
- * 调用接口取得凭证
- * */
- public static function getSession($accountInfo)
- {
- $session = RedisModel::get(self::SESSION_RDS_KEY.'-'.$accountInfo['username']);
- if(empty($session)) {
- $session = self::setSession($accountInfo);
- }
- return $session;
- }
- public static function setSession($accountInfo, $retry=0)
- {
- $url = self::BASE_URI . 'User/login?time=' . time() .'257';
- $headers = [
- 'Accept: application/json, text/plain, */*',
- 'Accept-Encoding: gzip, deflate, br',
- 'Accept-Language: zh-CN,zh;q=0.9',
- 'Connection: keep-alive',
- 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
- 'Host: video-wechat-open.eastdrama.com',
- 'Origin: https://m.eastdrama.com',
- 'sec-ch-ua: "Chromium";v="106", "Google Chrome";v="106", "Not;A=Brand";v="99"',
- 'sec-ch-ua-mobile: ?0',
- 'sec-ch-ua-platform: "Windows"',
- 'Sec-Fetch-Dest: empty',
- 'Sec-Fetch-Mode: cors',
- 'Sec-Fetch-Site: same-site',
- 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
- ];
- $res = HttpService::httpPost($url, json_encode($accountInfo), false, $headers);
- $res = json_decode($res, true);
- $session = null;
- if( (isset($res['code']) && $res['code']=='0000' && isset($res['data']['session']) )){
- $session = $res['data']['session'];
- RedisModel::set(self::SESSION_RDS_KEY.'-'.$accountInfo['username'], $session);
- } elseif( $retry < 5) {
- $retry++;
- $session = self::setSession($accountInfo, $retry);
- }
- if( !$session ){
- Log::logError('东方接口调用凭证获取失败', [
- 'account_info' => $accountInfo,
- 'res' => $res,
- 'retry' => $retry
- ], 'DongFangSetSession');
- }
- return $session;
- }
- public static function crawlDongFangApi($url, $accountInfo, $body=[], $retry=0)
- {
- # 获取session
- $session = DongFangService::getSession($accountInfo);
- if( !$session ){
- return false;
- }
- $headers = [
- // 'Accept: application/json, text/plain, */*',
- // 'Accept-Encoding: gzip, deflate, br',
- // 'Accept-Language: zh-CN,zh;q=0.9',
- // 'Connection: keep-alive',
- // 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
- // 'Host: video-wechat-open.eastdrama.com',
- // 'Origin: https://m.eastdrama.com',
- // 'sec-ch-ua: "Chromium";v="106", "Google Chrome";v="106", "Not;A=Brand";v="99"',
- // 'sec-ch-ua-mobile: ?0',
- // 'sec-ch-ua-platform: "Windows"',
- // 'Sec-Fetch-Dest: empty',
- // 'Sec-Fetch-Mode: cors',
- // 'Sec-Fetch-Site: same-site',
- 'session: '. $session,
- // 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
- ];
- $res = HttpService::httpPost($url, json_encode($body), TRUE, $headers);
- Log::logInfo('test', [$res], 'DongFangApi');
- $res = json_decode($res, true);
- if(isset($res['code']) && (in_array($res['code'], ['10003', '10004'])) && $retry<5 ){
- $retry++;
- //session过期,重置
- RedisModel::del(DongFangService::SESSION_RDS_KEY.'-'.$accountInfo['username']);
- $res = self::crawlDongFangApi($url, $accountInfo, $body, $retry);
- }
- return $res;
- }
- }
|