企微短剧业务系统

CapacityService.php 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2023/3/28
  6. * Time: 17:34
  7. */
  8. namespace App\Service;
  9. use App\Log;
  10. use App\Models\CapacityUser;
  11. use App\Support\EmailQueue;
  12. class CapacityService
  13. {
  14. /**
  15. * 构建签名*
  16. * @param $params array
  17. * @param $secret string 密钥
  18. * @param $timestamp int 时间戳
  19. * @return string
  20. */
  21. public static function createSign($params, $secret, $timestamp)
  22. {
  23. $string = '';
  24. if (!empty($params)) {
  25. ksort($params);
  26. foreach ($params as $name => $value) {
  27. $string .= $name.$value;
  28. }
  29. }
  30. $string = $secret . $string . $timestamp;
  31. return md5($string);
  32. }
  33. public static function orderList($params, $retry = 0)
  34. {
  35. try {
  36. $url = config('capacity.request_url');
  37. $response = HttpService::httpPost($url, json_encode($params), true);
  38. if($response === false && $retry <5) { // 发起重试
  39. $retry++;
  40. return CapacityService::orderList($params, $retry);
  41. }
  42. $responseData = json_decode($response, 1);
  43. Log::logInfo('容量订单', [
  44. 'params' => $params,
  45. 'response' => $responseData
  46. ], 'CapacityOrderList');
  47. if((isset($responseData['code']) && $responseData['code'] != 0) || !isset($responseData['data']['data'])) {
  48. $errMsg = $responseData['msg'] ?? '';
  49. EmailQueue::rPush('获取容量平台下的订单信息返回错误', $errMsg, ['song.shen@kuxuan-inc.com'],
  50. '猎羽');
  51. Log::logError('获取容量平台下的订单信息返回错误', ['response' => $responseData], 'CapacityApi');
  52. return [[], 0];
  53. }
  54. $data = $responseData['data']['data'] ?? [];
  55. $count= $responseData['data']['dataCount'] ?? 0;
  56. return [$data, $count];
  57. } catch (\Exception $e) {
  58. EmailQueue::rPush('获取容量平台下的订单信息返回错误', '', ['song.shen@kuxuan-inc.com'], '猎羽');
  59. Log::logError('获取容量平台下的订单信息返回错误', [
  60. 'line' => $e->getLine(),
  61. 'msg' => $e->getMessage()
  62. ], 'CapacityApi-Exception');
  63. return [[], 0];
  64. }
  65. }
  66. public static function appList($params, $retry=0)
  67. {
  68. try {
  69. $url = config('capacity.request_url');
  70. $response = HttpService::httpPost($url, json_encode($params), true);
  71. if($response === false && $retry <5) { // 发起重试
  72. $retry++;
  73. return CapacityService::appList($params, $retry);
  74. }
  75. $responseData = json_decode($response, 1);
  76. Log::logInfo('应用列表', [$responseData], 'CapacityAppList');
  77. if(isset($responseData['code']) && $responseData['code'] != 0) {
  78. EmailQueue::rPush('获取容量平台下的渠道信息返回错误', $responseData['msg'], ['song.shen@kuxuan-inc.com'],
  79. '猎羽');
  80. Log::logError('获取容量平台下的订单信息返回错误', ['response' => $responseData], 'CapacityApi');
  81. return [];
  82. }
  83. return $responseData['data'] ?? [];
  84. } catch (\Exception $e) {
  85. EmailQueue::rPush('获取容量平台下的渠道信息返回错误', '', ['song.shen@kuxuan-inc.com'], '猎羽');
  86. Log::logError('获取容量平台下的渠道信息返回错误', [
  87. 'line' => $e->getLine(),
  88. 'msg' => $e->getMessage()
  89. ], 'CapacityApi-Exception');
  90. }
  91. return [];
  92. }
  93. public static function saveUserList($data)
  94. {
  95. $type = $data['type'] ?? null;
  96. if(empty($type)) {
  97. EmailQueue::rPush('容量平台推送数据异常', $data, ['song.shen@kuxuan-inc.com'], '猎羽');
  98. return [500, '数据类型异常'];
  99. }
  100. if('订单' == $type) {
  101. $res = DjOrderService::saveCapacityOrder($data);
  102. } else {
  103. $res = CapacityUser::saveCapacityUser($data);
  104. }
  105. if(!$res) {
  106. return [500, '保存失败'];
  107. }
  108. return [0, '保存成功'];
  109. }
  110. public static function dealPayInfo($order) {
  111. // 支付状态
  112. $order['pay_status'] = 0;
  113. switch($order['payStatus']) {
  114. case '已支付':
  115. $order['pay_status'] = 1;
  116. break;
  117. case '预支付':
  118. $order['pay_status'] = 0;
  119. break;
  120. case '已退款':
  121. $order['pay_status'] = -1;
  122. break;
  123. }
  124. // 支付渠道以及订单金额分成比例
  125. $order['order_pay_type'] = 0;
  126. $payType = 1;
  127. switch($order['payType']) {
  128. case '微信支付-jsapi':
  129. $order['order_pay_type'] = 2;
  130. $payType = 2;
  131. break;
  132. case '头条支付':
  133. $order['order_pay_type'] = 3;
  134. $payType = 2;
  135. break;
  136. case '快手支付':
  137. $order['order_pay_type'] = 4;
  138. $payType = 2;
  139. break;
  140. case '支付宝app支付':
  141. $order['order_pay_type'] = 5;
  142. $payType = 2;
  143. break;
  144. case '小程序虚拟支付':
  145. $order['order_pay_type'] = 1;
  146. break;
  147. default:
  148. EmailQueue::rPush('容量平台支付类型异常', json_encode($order, 256), 'song.shen@kuxuan-inc.com', '猎羽');
  149. break;
  150. }
  151. # 获取订单金额分成比例(不用区分是否为小程序虚拟支付)
  152. // $shareInProportionArr = OrderService::getOrderShareInProportion(6);
  153. $playletName = $order['compilationsTitle'] ?? null;
  154. $shareInProportionArr = OrderService::getOrderShareInProportionNew($order['order_source'], $playletName, $order['sys_group_id']);
  155. $shareInProportion = $shareInProportionArr[$payType] ?? 1;
  156. # 计算分成后的金额
  157. $order['origin_pay_money'] = $order['moneyFen'];
  158. if(3 == $order['sys_group_id']) {
  159. $order['moneyFen'] = $order['origin_pay_money'] * $shareInProportion;
  160. }
  161. return $order;
  162. }
  163. public static function getDeviceType($userAgent) {
  164. if(strpos($userAgent, 'Android') !== false) {
  165. return 1;
  166. } else if (strpos($userAgent, 'iPhone') !== false || strpos($userAgent, 'iPad')) {
  167. return 2;
  168. } else if (strpos($userAgent, 'Windows') !== false){
  169. return 0;
  170. } else {
  171. EmailQueue::rPush('第三方平台设备类型异常', json_encode([
  172. 'platform' => '容量', 'msg' => '设备类型字段值不符合预期', 'ua' => $userAgent
  173. ], 256), ['song.shen@kuxuan-inc.com'], '猎羽');
  174. return 0;
  175. }
  176. }
  177. }