123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2023/3/28
- * Time: 17:34
- */
- namespace App\Service;
- use App\Log;
- use App\Models\CapacityUser;
- use App\Support\EmailQueue;
- class CapacityService
- {
- /**
- * 构建签名*
- * @param $params array
- * @param $secret string 密钥
- * @param $timestamp int 时间戳
- * @return string
- */
- public static function createSign($params, $secret, $timestamp)
- {
- $string = '';
- if (!empty($params)) {
- ksort($params);
- foreach ($params as $name => $value) {
- $string .= $name.$value;
- }
- }
- $string = $secret . $string . $timestamp;
- return md5($string);
- }
- public static function orderList($params, $retry = 0)
- {
- try {
- $url = config('capacity.request_url');
- $response = HttpService::httpPost($url, json_encode($params), true);
- if($response === false && $retry <5) { // 发起重试
- $retry++;
- return CapacityService::orderList($params, $retry);
- }
- $responseData = json_decode($response, 1);
- Log::logInfo('容量订单', [
- 'params' => $params,
- 'response' => $responseData
- ], 'CapacityOrderList');
- if((isset($responseData['code']) && $responseData['code'] != 0) || !isset($responseData['data']['data'])) {
- $errMsg = $responseData['msg'] ?? '';
- EmailQueue::rPush('获取容量平台下的订单信息返回错误', $errMsg, ['song.shen@kuxuan-inc.com'],
- '猎羽');
- Log::logError('获取容量平台下的订单信息返回错误', ['response' => $responseData], 'CapacityApi');
- return [[], 0];
- }
- $data = $responseData['data']['data'] ?? [];
- $count= $responseData['data']['dataCount'] ?? 0;
- return [$data, $count];
- } catch (\Exception $e) {
- EmailQueue::rPush('获取容量平台下的订单信息返回错误', '', ['song.shen@kuxuan-inc.com'], '猎羽');
- Log::logError('获取容量平台下的订单信息返回错误', [
- 'line' => $e->getLine(),
- 'msg' => $e->getMessage()
- ], 'CapacityApi-Exception');
- return [[], 0];
- }
- }
- public static function appList($params, $retry=0)
- {
- try {
- $url = config('capacity.request_url');
- $response = HttpService::httpPost($url, json_encode($params), true);
- if($response === false && $retry <5) { // 发起重试
- $retry++;
- return CapacityService::appList($params, $retry);
- }
- $responseData = json_decode($response, 1);
- Log::logInfo('应用列表', [$responseData], 'CapacityAppList');
- if(isset($responseData['code']) && $responseData['code'] != 0) {
- EmailQueue::rPush('获取容量平台下的渠道信息返回错误', $responseData['msg'], ['song.shen@kuxuan-inc.com'],
- '猎羽');
- Log::logError('获取容量平台下的订单信息返回错误', ['response' => $responseData], 'CapacityApi');
- return [];
- }
- return $responseData['data'] ?? [];
- } catch (\Exception $e) {
- EmailQueue::rPush('获取容量平台下的渠道信息返回错误', '', ['song.shen@kuxuan-inc.com'], '猎羽');
- Log::logError('获取容量平台下的渠道信息返回错误', [
- 'line' => $e->getLine(),
- 'msg' => $e->getMessage()
- ], 'CapacityApi-Exception');
- }
- return [];
- }
- public static function saveUserList($data)
- {
- $type = $data['type'] ?? null;
- if(empty($type)) {
- EmailQueue::rPush('容量平台推送数据异常', $data, ['song.shen@kuxuan-inc.com'], '猎羽');
- return [500, '数据类型异常'];
- }
- if('订单' == $type) {
- $res = DjOrderService::saveCapacityOrder($data);
- } else {
- $res = CapacityUser::saveCapacityUser($data);
- }
- if(!$res) {
- return [500, '保存失败'];
- }
- return [0, '保存成功'];
- }
- public static function dealPayInfo($order) {
- // 支付状态
- $order['pay_status'] = 0;
- switch($order['payStatus']) {
- case '已支付':
- $order['pay_status'] = 1;
- break;
- case '预支付':
- $order['pay_status'] = 0;
- break;
- case '已退款':
- $order['pay_status'] = -1;
- break;
- }
- // 支付渠道以及订单金额分成比例
- $order['order_pay_type'] = 0;
- $payType = 1;
- switch($order['payType']) {
- case '微信支付-jsapi':
- $order['order_pay_type'] = 2;
- $payType = 2;
- break;
- case '头条支付':
- $order['order_pay_type'] = 3;
- $payType = 2;
- break;
- case '快手支付':
- $order['order_pay_type'] = 4;
- $payType = 2;
- break;
- case '支付宝app支付':
- $order['order_pay_type'] = 5;
- $payType = 2;
- break;
- case '小程序虚拟支付':
- $order['order_pay_type'] = 1;
- break;
- default:
- EmailQueue::rPush('容量平台支付类型异常', json_encode($order, 256), 'song.shen@kuxuan-inc.com', '猎羽');
- break;
- }
- # 获取订单金额分成比例(不用区分是否为小程序虚拟支付)
- // $shareInProportionArr = OrderService::getOrderShareInProportion(6);
- $playletName = $order['compilationsTitle'] ?? null;
- $shareInProportionArr = OrderService::getOrderShareInProportionNew($order['order_source'], $playletName, $order['sys_group_id']);
- $shareInProportion = $shareInProportionArr[$payType] ?? 1;
- # 计算分成后的金额
- $order['origin_pay_money'] = $order['moneyFen'];
- if(3 == $order['sys_group_id']) {
- $order['moneyFen'] = $order['origin_pay_money'] * $shareInProportion;
- }
- return $order;
- }
- public static function getDeviceType($userAgent) {
- if(strpos($userAgent, 'Android') !== false) {
- return 1;
- } else if (strpos($userAgent, 'iPhone') !== false || strpos($userAgent, 'iPad')) {
- return 2;
- } else if (strpos($userAgent, 'Windows') !== false){
- return 0;
- } else {
- EmailQueue::rPush('第三方平台设备类型异常', json_encode([
- 'platform' => '容量', 'msg' => '设备类型字段值不符合预期', 'ua' => $userAgent
- ], 256), ['song.shen@kuxuan-inc.com'], '猎羽');
- return 0;
- }
- }
- }
|