企微短剧业务系统

MaiBuService.php 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\RedisModel;
  5. use App\Support\EmailQueue;
  6. class MaiBuService
  7. {
  8. const MAI_BU_ACCESS_TOKEN_RDS = 'Playlet::MaiBuAccessToken_';
  9. /**
  10. * 获取token
  11. * */
  12. public static function getAccessToken($channelId)
  13. {
  14. $accessToken = RedisModel::get(MaiBuService::MAI_BU_ACCESS_TOKEN_RDS . $channelId);
  15. if(empty($accessToken)) {
  16. $accessToken = self::login($channelId);
  17. }
  18. return $accessToken;
  19. }
  20. /**
  21. * 用户登录-获取Token接口
  22. * */
  23. public static function login($channelId, $retry=0)
  24. {
  25. $requestUri = config('maiBu.get_access_token');
  26. $params = ['id' => $channelId];
  27. $requestUri .= http_build_query($params);
  28. # 获取响应数据
  29. $accessToken = '';
  30. $response = HttpService::httpGet($requestUri);
  31. $responseData = json_decode($response, true);
  32. if(isset($responseData['code']) && $responseData['code'] == 0) {
  33. $accessToken = $responseData['data'] ?? '';
  34. if(empty($accessToken) && $retry <5) {
  35. $retry++;
  36. return self::login($channelId, $retry);
  37. }
  38. if(!empty($accessToken))
  39. RedisModel::set(MaiBuService::MAI_BU_ACCESS_TOKEN_RDS . $channelId, $accessToken);
  40. RedisModel::expire(MaiBuService::MAI_BU_ACCESS_TOKEN_RDS . $channelId, 86300*2);
  41. } else { // 获取失败,发起重试
  42. if($retry < 5) {
  43. $retry++;
  44. return self::login($channelId, $retry);
  45. }
  46. }
  47. if(empty($accessToken)) {
  48. // EmailQueue::rPush('迈步Token获取失败', json_encode([
  49. // 'channelId' => $channelId,
  50. // 'response' => $responseData
  51. // ], 256), ['xiaohua.hou@kuxuan-inc.com'], '迈步Token获取失败');
  52. Log::logError('迈步Token获取失败', [
  53. 'label' => $channelId,
  54. 'retry' => $retry
  55. ], 'MaiBuAccessToken');
  56. return false;
  57. }
  58. return $accessToken;
  59. }
  60. /**
  61. * 获取剧列表
  62. * */
  63. public static function getContentList(
  64. $partnerId, $pageIndex, $pageSize, $token, $finishState=-1, $state=-1, $appId=null, $contentName=null, $feeType=null
  65. )
  66. {
  67. $requestUri = config('maiBu.get_content_list');
  68. $params = [
  69. 'partnerId' => $partnerId,
  70. 'pageIndex' => $pageIndex,
  71. 'pageSize' => $pageSize
  72. ];
  73. # appid
  74. if(!is_null($appId))
  75. $params['appId'] = $appId;
  76. # 内容名称
  77. if(!is_null($contentName))
  78. $params['contentName'] = $contentName;
  79. # 完结状态
  80. if(!is_null($finishState))
  81. $params['finishState'] = $finishState;
  82. # 收费类型
  83. if(!is_null($feeType))
  84. $params['feeType'] = $feeType;
  85. # 剧上下架状态
  86. if(!is_null($state))
  87. $params['state'] = $state;
  88. $requestUri .= http_build_query($params);
  89. $header = ['authorization:' . $token];
  90. # 获取响应数据
  91. $response = HttpService::httpGet($requestUri, $header);
  92. $responseData = json_decode($response, true);
  93. return $responseData;
  94. }
  95. /**
  96. * 获取剧集列表
  97. * */
  98. public static function getSectionList($contentId, $pageIndex, $pageSize, $token)
  99. {
  100. $requestUri = config('maiBu.get_section_list');
  101. $params = [
  102. 'contentId' => $contentId,
  103. 'pageIndex' => $pageIndex,
  104. 'pageSize' => $pageSize
  105. ];
  106. $requestUri .= http_build_query($params);
  107. $header = ['authorization:' . $token];
  108. # 获取响应数据
  109. $response = HttpService::httpGet($requestUri, $header);
  110. $responseData = json_decode($response, true);
  111. return $responseData;
  112. }
  113. /**
  114. * 创建推广链接
  115. * */
  116. public static function savePromotion(
  117. $partnerId, $appId, $name, $contentId, $sectionId, $forceSubscribe=null, $forceSubscribeSectionId=null, $type=null
  118. )
  119. {
  120. $token = self::getAccessToken($partnerId);
  121. $requestUri = config('maiBu.save_promotion');
  122. $params = [
  123. 'partnerId' => $partnerId,
  124. 'appId' => $appId,
  125. 'name' => $name,
  126. 'contentId' =>$contentId,
  127. 'sectionId' => $sectionId
  128. ];
  129. # 是否强关
  130. if(!is_null($forceSubscribe))
  131. $params['forceSubscribe'] = $forceSubscribe;
  132. # 强关章节ID
  133. if(!is_null($forceSubscribeSectionId))
  134. $params['forceSubscribeSectionId'] = $forceSubscribeSectionId;
  135. # 链接类型
  136. if(!is_null($type))
  137. $params['type'] = $type;
  138. $requestUri .= http_build_query($params);
  139. $header = ['authorization:' . $token];
  140. # 获取响应数据
  141. $response = HttpService::httpGet($requestUri, $header);
  142. $responseData = json_decode($response, true);
  143. return $responseData;
  144. }
  145. /**
  146. * 获取应用列表
  147. * */
  148. public static function getAppList($partnerId, $pageIndex, $pageSize, $token)
  149. {
  150. $requestUri = config('maiBu.get_app_list');
  151. $params = [
  152. 'partnerId' => $partnerId,
  153. 'pageIndex' => $pageIndex,
  154. 'pageSize' => $pageSize
  155. ];
  156. $requestUri .= http_build_query($params);
  157. $header = ['authorization:' . $token];
  158. # 获取响应数据
  159. $response = HttpService::httpGet($requestUri, $header);
  160. $responseData = json_decode($response, true);
  161. return $responseData;
  162. }
  163. public static function getPromotionList($token, $partnerId, $appId, $beginDate, $endDate, $type, $pageIndex, $pageSize, $lastMinId, $retry = 0)
  164. {
  165. $requestUri = config('maiBu.get_promotion_list');
  166. $params = [
  167. 'partnerId' => $partnerId,
  168. 'appId' => $appId,
  169. 'beginDate' => $beginDate,
  170. 'endDate' => $endDate,
  171. 'type' => $type,
  172. 'pageIndex' => $pageIndex,
  173. 'pageSize' => $pageSize,
  174. 'lastMinId' => $lastMinId
  175. ];
  176. $requestUri .= http_build_query($params);
  177. $header = ['authorization:' . $token];
  178. # 获取响应数据
  179. $response = HttpService::httpGet($requestUri, $header);
  180. $responseData = json_decode($response, true);
  181. Log::logInfo('获取公众号推文数据', [
  182. 'params' => [
  183. 'partnerId' => $partnerId,
  184. 'appId' => $appId,
  185. 'beginDate' => $beginDate,
  186. 'endDate' => $endDate,
  187. 'type' => $type,
  188. 'pageIndex' => $pageIndex,
  189. 'pageSize' => $pageSize,
  190. 'lastMinId' => $lastMinId,
  191. 'retry' => $retry
  192. ],
  193. 'response' => $responseData
  194. ], 'maibuApi');
  195. $code = $responseData['code'] ?? 1;
  196. if($code) {
  197. $retry++;
  198. if($retry < 2) {
  199. sleep(1);
  200. return self::getPromotionList($token, $partnerId, $appId, $beginDate, $endDate, $type, $pageIndex, $pageSize, $lastMinId, $retry);
  201. }
  202. }
  203. return $responseData;
  204. }
  205. public static function getOrderPayType($payType) {
  206. $payTypeArr= [
  207. 0 => 6, // 微信小程序支付
  208. 1 => 7, // 微信_APP支付
  209. 2 => 8, // 微信_H5支付
  210. 3 => 5, // 支付宝_APP支付
  211. 4 => 9, // 微信快手支付
  212. 5 => 10,// 微信抖音支付
  213. 6 => 11,// 苹果_APP支付
  214. 7 => 12,// H5微信内支付
  215. 8 => 13,// 华为APP支付
  216. 9 => 14,// 百度百度小程序支付
  217. 10=> 1, // 微信_小程序虚拟支付
  218. ];
  219. return $payTypeArr[$payType] ?? 0;
  220. }
  221. }