diff --git a/Application/Base/Service/GameRebateService.class.php b/Application/Base/Service/GameRebateService.class.php index 49f3fa541..fad7d843f 100644 --- a/Application/Base/Service/GameRebateService.class.php +++ b/Application/Base/Service/GameRebateService.class.php @@ -1,6 +1,7 @@ ['url' => '/acf/send_gift.php', 'method' => 'post'], + ]; + + public function __construct() + { + $this->client = new Client([ + 'base_uri' => $this->baseUri, + 'timeout' => 10.0, + ]); + } + + public function api($apiName, array $params = []) + { + $api = $this->apis[$apiName] ?? null; + if (is_null($api)) { + throw new \Exception('接口不存在'); + } + $params[self::SIGN_NAME] = $this->sign($params); + try { + $response = $this->request($api, $params); + $result = json_decode($response, true); + if (empty($result)) { + return [ + 'code' => -98, + 'msg' => '返回数据异常', + ]; + } + return $result; + } catch (\Exception $e) { + $env = C('APP_ENV', null, 'prod'); + $this->log('RequestError ' . $e->getMessage(), Log::ERR); + return [ + 'code' => -99, + 'msg' => '网络异常', + ]; + } + } + + protected function buildUrl($url) { + return $url; + } + + public function request($api, $params) + { + if ($api['method'] == 'get') { + return $this->get($this->buildUrl($api['url']), $params); + } else { + return $this->post($this->buildUrl($api['url']), $params); + } + } + + protected function post($url, array $params = []) + { + $response = $this->client->post($url, [ + 'verify' => false, + 'form_params' => $params, + ]); + $result = (string)$response->getBody(); + $result = json_encode([ + 'code' => -97, + 'msg' => '模拟发放,自定失败', + ]); + $this->log('RequestData ' . json_encode($params)); + $this->log('ResponseData ' . $result); + return $result; + } + + protected function get($url, array $params = []) + { + $response = $this->client->get($url, [ + 'verify' => false, + 'query' => $params, + ]); + $result = (string)$response->getBody(); + $result = json_encode([ + 'code' => -97, + 'msg' => '模拟发放,自定失败', + ]); + $this->log('RequestData ' . json_encode($params)); + $this->log('ResponseData ' . $result); + return $result; + } + + protected function sign($params) + { + unset($params[self::SIGN_NAME]); + ksort($params); + $signString = ''; + foreach ($params as $key => $value) { + $signString .= $value; + } + $signString .= $this->key; + $this->log('SignString ' . $signString); + return md5($signString); + } + + public function apply($order, $role) + { + throw new Exception('接口未实现'); + } + + public function sendProps($order) + { + } + + public function sendGold($gold, $order) + { + } + + public function sendEmail($giftItem, $order) + { + $data = [ + 'appId' => 'chengfeng', + 'userId' => $order['user_id'], + 'orderNum' => $this->generateOrderNumber($order, $giftItem['index']), + 'title' => '测试邮件', + 'content' => '测试礼包发送', + 'packages' => json_encode([ + [ + 'packageId' => $giftItem['id'], + 'count' => 1, + ], + ]), + 'serverId' => $order['server_id'], + 'roleId' => $order['role_id'], + 'time' => time(), + ]; + + $result = $this->api('mail', $data); + return $this->getCommonResult($result); + } + + protected function getCommonResult($result) { + if (intval($result['code']) === self::SUCCESS) { + return [ + 'status' => true, + 'message' => $result['msg'], + 'result' => ['result' => $result] + ]; + } else { + return [ + 'status' => false, + 'message' => $result['msg'], + 'result' => ['result' => $result] + ]; + } + } + + protected function generateOrderNumber($order, $index = 0) + { + $length = 8 - strlen(strval($order['id'])); + $indexLength = 3 - strlen(strval($index)); + return date('Ymd') . str_repeat('0', $length) . $order['id'] . str_repeat('0', $indexLength) . $index; + } + + public function getResourceTypes($deviceType) + { + return []; + } + + public function getResources($typeId, $deviceType) + { + return []; + } + + protected function log($message, $level = Log::INFO) { + PlatformLog::write($message, 'game_api/jmxy', $level); + } +} \ No newline at end of file