企微短剧业务系统

JiaShuService.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2022/10/21
  6. * Time: 14:29
  7. */
  8. namespace App\Service;
  9. use App\Log;
  10. class JiaShuService
  11. {
  12. /*
  13. * 创建推广链接
  14. * */
  15. public static function savePromotion(
  16. $apiKey, $customerId, $appId, $videoId, $dramaSort, $remark
  17. )
  18. {
  19. $requestUri = config('jiashu.save_promotion');
  20. $paramsArr['app_id'] = $appId;
  21. $paramsArr['customer_id'] = $customerId;
  22. $paramsArr['drama_sort'] = $dramaSort;
  23. $paramsArr['only_url'] = 1;
  24. $paramsArr['remark'] = $remark;
  25. $paramsArr['timestamp'] = time();
  26. $paramsArr['video_id'] = $videoId;
  27. # 生成签名
  28. $params = self::createSign($apiKey, $paramsArr);
  29. $response = HttpService::httpPost($requestUri, $params);
  30. $responseData = json_decode($response, 1);
  31. Log::logInfo('创建推广链接', [
  32. 'params' => [
  33. 'api_key' => $apiKey,
  34. 'customer_id' => $customerId,
  35. 'app_id' => $appId,
  36. 'video_id' => $videoId,
  37. 'drama_sort' => $dramaSort,
  38. 'remark' => $remark
  39. ],
  40. 'response' => $responseData
  41. ], 'jiashuApi');
  42. sleep(1);
  43. return $responseData;
  44. }
  45. /*
  46. * 获取剧列表
  47. * */
  48. public static function getContentList(
  49. $apiKey, $customerId, $page = null, $appId
  50. )
  51. {
  52. $requestUri = config('jiashu.get_content_list');
  53. $paramsArr['app_id'] = $appId;
  54. $paramsArr['customer_id'] = $customerId;
  55. if(!is_null($page)) $paramsArr['page'] = $page;
  56. $paramsArr['timestamp'] = time();
  57. $params = self::createSign($apiKey, $paramsArr);
  58. $requestUri .= http_build_query($params);
  59. # 获取响应数据
  60. $response = HttpService::httpGet($requestUri);
  61. $responseData = json_decode($response, true);
  62. Log::logInfo('获取短剧列表', [
  63. 'params' => [
  64. 'api_key' => $apiKey,
  65. 'customer_id' => $customerId,
  66. 'app_id' => $appId,
  67. 'page' => $page,
  68. ],
  69. 'response' => $responseData
  70. ], 'jiashuApi');
  71. return $responseData;
  72. }
  73. /*
  74. * 获取剧集列表
  75. * */
  76. public static function getSectionList($apiKey, $customerId, $appId, $videoId)
  77. {
  78. $requestUri = config('jiashu.get_section_list');
  79. $paramsArr['app_id'] = $appId;
  80. $paramsArr['customer_id'] = $customerId;
  81. $paramsArr['timestamp'] = time();
  82. $paramsArr['video_id'] = $videoId;
  83. $params = self::createSign($apiKey, $paramsArr);
  84. $requestUri .= http_build_query($params);
  85. # 获取响应数据
  86. $response = HttpService::httpGet($requestUri);
  87. $responseData = json_decode($response, true);
  88. Log::logInfo('获取剧集列表', [
  89. 'params' => [
  90. 'api_key' => $apiKey,
  91. 'customer_id' => $customerId,
  92. 'app_id' => $appId,
  93. 'video_id' => $videoId,
  94. ],
  95. 'response' => $responseData
  96. ], 'jiashuApi');
  97. return $responseData;
  98. }
  99. /*
  100. * 获取应用列表
  101. * */
  102. public static function getAppList($apiKey, $customerId)
  103. {
  104. $requestUri = config('jiashu.get_app_list');
  105. $paramsArr['customer_id'] = $customerId;
  106. $paramsArr['timestamp'] = time();
  107. $params = self::createSign($apiKey, $paramsArr);
  108. # 获取响应数据
  109. $response = HttpService::httpPost($requestUri, $params);
  110. $responseData = json_decode($response, true);
  111. Log::logInfo('获取产品列表', [
  112. 'params' => [
  113. 'api_key' => $apiKey,
  114. 'customer_id' => $customerId,
  115. ],
  116. 'response' => $responseData
  117. ], 'jiashuApi');
  118. return $responseData;
  119. }
  120. public static function createSign($apiKey, $paramsArr)
  121. {
  122. $params = [];
  123. foreach($paramsArr as $k=> $v) {
  124. $params[] = $k.'='.$v;
  125. }
  126. sort($params);
  127. $string = implode('&', $params);
  128. $sign = md5($apiKey.$string);
  129. $paramsArr['sign'] = $sign;
  130. return $paramsArr;
  131. }
  132. # 获取推广链接
  133. public static function getArticle($apiKey, $customerId, $appId, $article, $retry = 0)
  134. {
  135. $requestUri = config('jiashu.get_article');
  136. $paramsArr['app_id'] = $appId;
  137. $paramsArr['article'] = $article;
  138. $paramsArr['customer_id'] = $customerId;
  139. $paramsArr['timestamp'] = time();
  140. $params = self::createSign($apiKey, $paramsArr);
  141. $requestUri .= http_build_query($params);
  142. # 获取响应数据
  143. $response = HttpService::httpGet($requestUri);
  144. $responseData = json_decode($response, true);
  145. Log::logInfo('获取推广链接', [
  146. 'params' => [
  147. 'api_key' => $apiKey,
  148. 'customer_id' => $customerId,
  149. 'app_id' => $appId,
  150. 'article' => $article,
  151. 'retry' => $retry
  152. ],
  153. 'response' => $responseData
  154. ], 'jiashuApi');
  155. $code = $responseData['code'] ?? 1;
  156. if($code) {
  157. $retry++;
  158. if($retry < 2) {
  159. sleep(1);
  160. return self::getArticle($apiKey, $customerId, $appId, $article, $retry);
  161. }
  162. }
  163. return $responseData;
  164. }
  165. public static function getArticleList($apiKey, $customerId, $appId, $statTime, $endTime, $retry = 0)
  166. {
  167. $requestUri = config('jiashu.get_article_data');
  168. $paramsArr['app_id'] = $appId;
  169. $paramsArr['customer_id'] = $customerId;
  170. $paramsArr['end_time'] = $endTime;
  171. $paramsArr['start_time'] = $statTime;
  172. $paramsArr['timestamp'] = time();
  173. $params = self::createSign($apiKey, $paramsArr);
  174. $requestUri .= http_build_query($params);
  175. # 获取响应数据
  176. $response = HttpService::httpGet($requestUri);
  177. $responseData = json_decode($response, true);
  178. Log::logInfo('获取公众号推文数据', [
  179. 'params' => [
  180. 'api_key' => $apiKey,
  181. 'customer_id' => $customerId,
  182. 'app_id' => $appId,
  183. 'start_time' => $statTime,
  184. 'end_time' => $endTime,
  185. 'retry' => $retry
  186. ],
  187. 'response' => $responseData
  188. ], 'jiashuApi');
  189. $code = $responseData['code'] ?? 1;
  190. if($code) {
  191. $retry++;
  192. if($retry < 2) {
  193. sleep(1);
  194. return self::getArticleList($apiKey, $customerId, $appId, $statTime, $endTime, $retry);
  195. }
  196. }
  197. return $responseData;
  198. }
  199. }