You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.3 KiB
PHP

<?php
namespace Base\Service;
use GuzzleHttp\Client;
class GameRebateService
{
const SIGN_KEY = 'Eza65532qaOIAAWasdq962aqweasd';
private function getClient()
{
return new Client([
'base_uri' => 'http://rebate.99you.cn/xyy_apply.php/23400/',
'timeout' => 10.0,
]);
}
public function sendByOrder($order)
{
$gift = M('rebate_gifts', 'tab_')->where(['type' => $order['type'], 'gift_key' => $order['gift_key']])->find();
$hasError = false;
$sendResult = '';
if ($gift['game_currency'] > 0) {
$result = $this->sendCurrency($order['server_id'], $order['role_id'], $gift['game_currency']);
if ($result['code'] != 1) {
$hasError = true;
}
$sendResult .= ($result['msg'] ?? '');
$sendResult .= ';';
}
if ($gift['gift_id'] > 0) {
$result = $this->sendGift($order['server_id'], $order['role_id'], $gift['gift_id']);
if ($result['code'] != 1) {
$hasError = true;
}
$sendResult .= ($result['msg'] ?? '');
}
M('rebate_orders', 'tab_')->where(['id' => $order['id']])->save([
'send_status' => $hasError ? 2 : 1,
'send_time' => time(),
'send_result' => $sendResult,
]);
}
public function sendCurrency($serverId, $roleId, $currency)
{
$sign = md5($currency.$serverId.$roleId.self::SIGN_KEY);
$params = [
'act' => 'sendgold',
'serverid' => $serverId,
'role_id' => $roleId,
'money' => $currency,
'sign' => $sign
];
return $this->get('', $params);
}
public function sendGift($serverId, $roleId, $giftId)
{
$sign = md5($giftId.$serverId.$roleId.self::SIGN_KEY);
$params = [
'act' => 'send_email',
'serverid' => $serverId,
'role_id' => $roleId,
'prop_id' => $giftId,
'sign' => $sign
];
return $this->get('', $params);
}
protected function get($uri, array $params = [])
{
try {
$response = $this->getClient()->get($uri, [
'verify' => false,
'query' => $params,
]);
$result = (string)$response->getBody();
return json_decode($result, true);
} catch (\Exception $e) {
return [
'code' => 3,
'msg' => '网络异常:' . $e->getMessage(),
];
}
}
public function review(array $ids, $status)
{
if (!in_array($status, [1, 2])) {
throw new \Exception('状态异常');
}
if (count($ids) == 0) {
throw new \Exception('请选择要操作的记录');
}
$orders = M('rebate_orders', 'tab_')->field(['id'])->where(['review_status' => 0, 'id' => ['in', $ids]])->select();
if (count($orders) != count($ids)) {
throw new \Exception('含有不存在的记录或者已审核的记录');
}
$adminInfo = $_SESSION['onethink_admin']['user_auth'];
M('rebate_orders', 'tab_')->where(['review_status' => 0, 'id' => ['in', $ids]])->save([
'review_status' => $status,
'review_time' => time(),
'reviewer_id' => $adminInfo['uid'],
'reviewer_username' => $adminInfo['username'],
]);
}
public function sendAll($type)
{
$orders = M('rebate_orders', 'tab_')->where(['type' => $type, 'review_status' => 1, 'send_status' => 0])->select();
foreach ($orders as $order) {
if ($type == 'B') {
if (time() < strtotime($order['award_started_at'] . ' 00:00:00') || time() > strtotime($order['award_ended_at'] . ' 23:59:59')) {
continue;
}
}
$this->sendByOrder($order);
}
}
public function sendDaily($date)
{
$map = [
'type' => 'B',
'review_status' => 1,
'send_status' => 1,
'award_started_at' => ['elt', $date],
'award_ended_at' => ['egt', $date]
];
$orders = M('rebate_orders', 'tab_')->where($map)->select();
foreach ($orders as $order) {
$this->sendByOrder($order);
}
}
}