123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
- namespace App\Service;
- use App\Log;
- use App\RedisModel;
- use App\Support\EmailQueue;
- class MaiBuService
- {
- const MAI_BU_ACCESS_TOKEN_RDS = 'Playlet::MaiBuAccessToken_';
- /**
- * 获取token
- * */
- public static function getAccessToken($channelId)
- {
- $accessToken = RedisModel::get(MaiBuService::MAI_BU_ACCESS_TOKEN_RDS . $channelId);
- if(empty($accessToken)) {
- $accessToken = self::login($channelId);
- }
- return $accessToken;
- }
- /**
- * 用户登录-获取Token接口
- * */
- public static function login($channelId, $retry=0)
- {
- $requestUri = config('maiBu.get_access_token');
- $params = ['id' => $channelId];
- $requestUri .= http_build_query($params);
- # 获取响应数据
- $accessToken = '';
- $response = HttpService::httpGet($requestUri);
- $responseData = json_decode($response, true);
- if(isset($responseData['code']) && $responseData['code'] == 0) {
- $accessToken = $responseData['data'] ?? '';
- if(empty($accessToken) && $retry <5) {
- $retry++;
- return self::login($channelId, $retry);
- }
- if(!empty($accessToken))
- RedisModel::set(MaiBuService::MAI_BU_ACCESS_TOKEN_RDS . $channelId, $accessToken);
- RedisModel::expire(MaiBuService::MAI_BU_ACCESS_TOKEN_RDS . $channelId, 86300*2);
- } else { // 获取失败,发起重试
- if($retry < 5) {
- $retry++;
- return self::login($channelId, $retry);
- }
- }
- if(empty($accessToken)) {
- // EmailQueue::rPush('迈步Token获取失败', json_encode([
- // 'channelId' => $channelId,
- // 'response' => $responseData
- // ], 256), ['xiaohua.hou@kuxuan-inc.com'], '迈步Token获取失败');
- Log::logError('迈步Token获取失败', [
- 'label' => $channelId,
- 'retry' => $retry
- ], 'MaiBuAccessToken');
- return false;
- }
- return $accessToken;
- }
- /**
- * 获取剧列表
- * */
- public static function getContentList(
- $partnerId, $pageIndex, $pageSize, $token, $finishState=-1, $state=-1, $appId=null, $contentName=null, $feeType=null
- )
- {
- $requestUri = config('maiBu.get_content_list');
- $params = [
- 'partnerId' => $partnerId,
- 'pageIndex' => $pageIndex,
- 'pageSize' => $pageSize
- ];
- # appid
- if(!is_null($appId))
- $params['appId'] = $appId;
- # 内容名称
- if(!is_null($contentName))
- $params['contentName'] = $contentName;
- # 完结状态
- if(!is_null($finishState))
- $params['finishState'] = $finishState;
- # 收费类型
- if(!is_null($feeType))
- $params['feeType'] = $feeType;
- # 剧上下架状态
- if(!is_null($state))
- $params['state'] = $state;
- $requestUri .= http_build_query($params);
- $header = ['authorization:' . $token];
- # 获取响应数据
- $response = HttpService::httpGet($requestUri, $header);
- $responseData = json_decode($response, true);
- return $responseData;
- }
- /**
- * 获取剧集列表
- * */
- public static function getSectionList($contentId, $pageIndex, $pageSize, $token)
- {
- $requestUri = config('maiBu.get_section_list');
- $params = [
- 'contentId' => $contentId,
- 'pageIndex' => $pageIndex,
- 'pageSize' => $pageSize
- ];
- $requestUri .= http_build_query($params);
- $header = ['authorization:' . $token];
- # 获取响应数据
- $response = HttpService::httpGet($requestUri, $header);
- $responseData = json_decode($response, true);
- return $responseData;
- }
- /**
- * 创建推广链接
- * */
- public static function savePromotion(
- $partnerId, $appId, $name, $contentId, $sectionId, $forceSubscribe=null, $forceSubscribeSectionId=null, $type=null
- )
- {
- $token = self::getAccessToken($partnerId);
- $requestUri = config('maiBu.save_promotion');
- $params = [
- 'partnerId' => $partnerId,
- 'appId' => $appId,
- 'name' => $name,
- 'contentId' =>$contentId,
- 'sectionId' => $sectionId
- ];
- # 是否强关
- if(!is_null($forceSubscribe))
- $params['forceSubscribe'] = $forceSubscribe;
- # 强关章节ID
- if(!is_null($forceSubscribeSectionId))
- $params['forceSubscribeSectionId'] = $forceSubscribeSectionId;
- # 链接类型
- if(!is_null($type))
- $params['type'] = $type;
- $requestUri .= http_build_query($params);
- $header = ['authorization:' . $token];
- # 获取响应数据
- $response = HttpService::httpGet($requestUri, $header);
- $responseData = json_decode($response, true);
- return $responseData;
- }
- /**
- * 获取应用列表
- * */
- public static function getAppList($partnerId, $pageIndex, $pageSize, $token)
- {
- $requestUri = config('maiBu.get_app_list');
- $params = [
- 'partnerId' => $partnerId,
- 'pageIndex' => $pageIndex,
- 'pageSize' => $pageSize
- ];
- $requestUri .= http_build_query($params);
- $header = ['authorization:' . $token];
- # 获取响应数据
- $response = HttpService::httpGet($requestUri, $header);
- $responseData = json_decode($response, true);
- return $responseData;
- }
- public static function getPromotionList($token, $partnerId, $appId, $beginDate, $endDate, $type, $pageIndex, $pageSize, $lastMinId, $retry = 0)
- {
- $requestUri = config('maiBu.get_promotion_list');
- $params = [
- 'partnerId' => $partnerId,
- 'appId' => $appId,
- 'beginDate' => $beginDate,
- 'endDate' => $endDate,
- 'type' => $type,
- 'pageIndex' => $pageIndex,
- 'pageSize' => $pageSize,
- 'lastMinId' => $lastMinId
- ];
- $requestUri .= http_build_query($params);
- $header = ['authorization:' . $token];
- # 获取响应数据
- $response = HttpService::httpGet($requestUri, $header);
- $responseData = json_decode($response, true);
- Log::logInfo('获取公众号推文数据', [
- 'params' => [
- 'partnerId' => $partnerId,
- 'appId' => $appId,
- 'beginDate' => $beginDate,
- 'endDate' => $endDate,
- 'type' => $type,
- 'pageIndex' => $pageIndex,
- 'pageSize' => $pageSize,
- 'lastMinId' => $lastMinId,
- 'retry' => $retry
- ],
- 'response' => $responseData
- ], 'maibuApi');
- $code = $responseData['code'] ?? 1;
- if($code) {
- $retry++;
- if($retry < 2) {
- sleep(1);
- return self::getPromotionList($token, $partnerId, $appId, $beginDate, $endDate, $type, $pageIndex, $pageSize, $lastMinId, $retry);
- }
- }
- return $responseData;
- }
- public static function getOrderPayType($payType) {
- $payTypeArr= [
- 0 => 6, // 微信小程序支付
- 1 => 7, // 微信_APP支付
- 2 => 8, // 微信_H5支付
- 3 => 5, // 支付宝_APP支付
- 4 => 9, // 微信快手支付
- 5 => 10,// 微信抖音支付
- 6 => 11,// 苹果_APP支付
- 7 => 12,// H5微信内支付
- 8 => 13,// 华为APP支付
- 9 => 14,// 百度百度小程序支付
- 10=> 1, // 微信_小程序虚拟支付
- ];
- return $payTypeArr[$payType] ?? 0;
- }
- }
|