企微短剧业务系统

DongFangService.php 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2022/10/25
  6. * Time: 15:41
  7. */
  8. namespace App\Service;
  9. use App\Log;
  10. use App\RedisModel;
  11. class DongFangService
  12. {
  13. const BASE_URI = 'https://video-wechat-open.eastdrama.com/';
  14. const SESSION_RDS_KEY = 'Playlet:DongFangSession';
  15. /*
  16. * 调用接口取得凭证
  17. * */
  18. public static function getSession($accountInfo)
  19. {
  20. $session = RedisModel::get(self::SESSION_RDS_KEY.'-'.$accountInfo['username']);
  21. if(empty($session)) {
  22. $session = self::setSession($accountInfo);
  23. }
  24. return $session;
  25. }
  26. public static function setSession($accountInfo, $retry=0)
  27. {
  28. $url = self::BASE_URI . 'User/login?time=' . time() .'257';
  29. $headers = [
  30. 'Accept: application/json, text/plain, */*',
  31. 'Accept-Encoding: gzip, deflate, br',
  32. 'Accept-Language: zh-CN,zh;q=0.9',
  33. 'Connection: keep-alive',
  34. 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
  35. 'Host: video-wechat-open.eastdrama.com',
  36. 'Origin: https://m.eastdrama.com',
  37. 'sec-ch-ua: "Chromium";v="106", "Google Chrome";v="106", "Not;A=Brand";v="99"',
  38. 'sec-ch-ua-mobile: ?0',
  39. 'sec-ch-ua-platform: "Windows"',
  40. 'Sec-Fetch-Dest: empty',
  41. 'Sec-Fetch-Mode: cors',
  42. 'Sec-Fetch-Site: same-site',
  43. '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',
  44. ];
  45. $res = HttpService::httpPost($url, json_encode($accountInfo), false, $headers);
  46. $res = json_decode($res, true);
  47. $session = null;
  48. if( (isset($res['code']) && $res['code']=='0000' && isset($res['data']['session']) )){
  49. $session = $res['data']['session'];
  50. RedisModel::set(self::SESSION_RDS_KEY.'-'.$accountInfo['username'], $session);
  51. } elseif( $retry < 5) {
  52. $retry++;
  53. $session = self::setSession($accountInfo, $retry);
  54. }
  55. if( !$session ){
  56. Log::logError('东方接口调用凭证获取失败', [
  57. 'account_info' => $accountInfo,
  58. 'res' => $res,
  59. 'retry' => $retry
  60. ], 'DongFangSetSession');
  61. }
  62. return $session;
  63. }
  64. public static function crawlDongFangApi($url, $accountInfo, $body=[], $retry=0)
  65. {
  66. # 获取session
  67. $session = DongFangService::getSession($accountInfo);
  68. if( !$session ){
  69. return false;
  70. }
  71. $headers = [
  72. // 'Accept: application/json, text/plain, */*',
  73. // 'Accept-Encoding: gzip, deflate, br',
  74. // 'Accept-Language: zh-CN,zh;q=0.9',
  75. // 'Connection: keep-alive',
  76. // 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
  77. // 'Host: video-wechat-open.eastdrama.com',
  78. // 'Origin: https://m.eastdrama.com',
  79. // 'sec-ch-ua: "Chromium";v="106", "Google Chrome";v="106", "Not;A=Brand";v="99"',
  80. // 'sec-ch-ua-mobile: ?0',
  81. // 'sec-ch-ua-platform: "Windows"',
  82. // 'Sec-Fetch-Dest: empty',
  83. // 'Sec-Fetch-Mode: cors',
  84. // 'Sec-Fetch-Site: same-site',
  85. 'session: '. $session,
  86. // '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'
  87. ];
  88. $res = HttpService::httpPost($url, json_encode($body), TRUE, $headers);
  89. Log::logInfo('test', [$res], 'DongFangApi');
  90. $res = json_decode($res, true);
  91. if(isset($res['code']) && (in_array($res['code'], ['10003', '10004'])) && $retry<5 ){
  92. $retry++;
  93. //session过期,重置
  94. RedisModel::del(DongFangService::SESSION_RDS_KEY.'-'.$accountInfo['username']);
  95. $res = self::crawlDongFangApi($url, $accountInfo, $body, $retry);
  96. }
  97. return $res;
  98. }
  99. }