['uri' => '/wanmeng/prop.php', 'method' => 'get'], 'provide' => ['uri' => '/wanmeng/prop.php?action=prop', 'method' => 'post'], ]; public function __construct() { $this->client = new Client([ 'base_uri' => 'http://47.114.91.166:1096', 'timeout' => 10.0, ]); } public function api($api, array $params = []) { $api = $this->apis[$api] ?? null; if (is_null($api)) { throw new \Exception('接口不存在'); } $params[self::SIGN_NAME] = $this->sign($params); try { return $this->request($api, $params); } catch (\Exception $e) { $env = C('APP_ENV', null, 'prod'); return ['status' => 0, 'msg' => '接口请求错误。' . ($env == 'prod' ? '' : $e->getMessage()) , 'data' => []]; } } public function request($api, $params) { if ($api['method'] == 'get') { return $this->get($api['uri'], $params); } else { return $this->post($api['uri'], $params); } } protected function post($uri, array $params = []) { $response = $this->client->post($uri, [ 'verify' => false, 'form_params' => $params, ]); $result = (string)$response->getBody(); return json_decode($result, true); } protected function get($uri, array $params = []) { $response = $this->client->get($uri, [ 'verify' => false, 'query' => $params, ]); $result = (string)$response->getBody(); return json_decode($result, true); } protected function sign($params) { unset($params[self::SIGN_NAME]); ksort($params); $signRows = []; foreach ($params as $key => $value) { $signRows[] = $key . '=' . $value; } return md5(implode('&', $signRows) . self::KEY); } public function apply($order, $role) { $result = $this->api('provide', [ 'role_id' => $role['role_id'], 'goods_id' => $order['ref_id'], 'server_no' => $role['server_id'], 'send_time' => time() ]); if ($result['status'] == 1) { return [ 'status' => true, 'message' => $result['msg'], 'result' => $result ]; } else { return [ 'status' => false, 'message' => $result['msg'], 'result' => $result ?? [] ]; } } public function getResourceTypes($deviceType) { if ($deviceType == 'andriod') { return [['id' => 1, 'name' => '通用', 'device_type' => 'andriod']]; } elseif ($deviceType == 'ios') { return [['id' => 2, 'name' => '通用', 'device_type' => 'ios']]; } } public function getResources($typeId, $deviceType) { $resources = []; $result = $this->api('get-pay-type', ['action' => 'getItems']); if ($result['status'] == 1) { $items = $result['data']; foreach ($items as $item) { $resources[$item['id']] = [ 'ref_id' => $item['id'], 'name' => $item['name'], 'amount' => $item['money'], ]; } } return $resources; } }