info($id); $this->platform(); $this->field($field); return $this; } protected function info($id) { $this->info = is_array($id) ? $id : Dever::db($this->type, 'api')->find($id); if (!$this->info) { Dever::error('info error'); } } protected function platform() { $this->platform = Dever::db('platform', 'api')->find($this->info['platform_id']); if (!$this->platform) { Dever::error('platform error'); } $this->sign = $this->response = array('id' => $this->platform['id']); foreach ($this->platform as $k => $v) { if (strstr($k, 'sign_')) { $k = str_replace('sign_', '', $k); $this->sign[$k] = $v; } if (strstr($k, 'response_')) { $k = str_replace('response_', '', $k); $this->response[$k] = $v; } } if ($this->info['method'] == -1) { $this->info['method'] = $this->platform['method']; $this->info['post_method'] = $this->platform['post_method']; } if ($this->info['method'] == 1) { $this->info['post_method'] = 1; } } protected function field($field) { $this->field = new Field($field); $this->field->setHost($this->platform['host']); $this->field->setUri($this->info['uri']); $this->field->setNotify($this->createNotify($field['order_num'] ?? '')); $setting = Dever::db('platform_setting', 'api')->select(array('platform_id' => $this->platform['id'])); if ($setting) { foreach ($setting as $k => $v) { if (isset($field[$v['key']])) { $v['value'] = $field[$v['key']]; } $key = $v['key']; # 如果有自定义的设置 if (!$this->field->$key) { $this->field->set($key, $v['value']); } } } } # 发起请求 protected function curl($url = false) { if (!$url) { $url = $this->url(); } $method = $this->method(); $request = new Request($this->field, $this->platform['id'], $this->type, $this->info['id']); $request_body = $request->body(); if ($this->info['sign_col']) { $this->platform['sign_col'] = $this->info['sign_col']; } $sign = new Sign($this->field, $this->sign); $sign = $sign->get(); if ($this->platform['sign_name']) { $request_body[$this->platform['sign_name']] = $sign; } $request_header = $request->header(); $json = $this->info['post_method'] == 3 ? true : false; $curl = Dever::curl($url, $request_body, $method, $json, $request_header); $curl->setResultHeader(true); $response_body = $curl->result(); $response_header = $curl->header(); $response = new Response($response_body, $response_header, $this->response, $this->field, $this->type, $this->info['id'], $this->sign); $result = $response->out(); $log['platform_id'] = $this->platform['id']; $log['type'] = $this->type; $log['type_id'] = $this->info['id']; $log['url'] = $url; $log['method'] = $method; $log['json'] = $json; $log['request_body'] = Dever::json_encode($request_body); $log['request_header'] = Dever::json_encode($request_header); $log['response_body'] = $response_body; $log['response_header'] = Dever::json_encode($response_header); Dever::debug($log); return $result; } # 直接跳转 protected function location($url = false) { if (!$url) { $url = $this->url(); } $method = $this->method(); $request = new Request($this->field, $this->platform['id'], $this->type, $this->info['id']); $request_body = $request->body(); if ($this->info['sign_col']) { $this->platform['sign_col'] = $this->info['sign_col']; } $sign = new Sign($this->field, $this->sign); $sign = $sign->get(); if ($this->platform['sign_name']) { $request_body[$this->platform['sign_name']] = $sign; } $url .= '?'; foreach ($request_body as $k => $v) { if ($k == '#') { $url .= $k . $v; } else { $url .= $k . '=' . $v . '&'; } } header('Location: '. $url); } protected function method() { $method = 'get'; if ($this->info['post_method'] == 2) { $method = 'file'; } elseif ($this->info['method'] == 2) { $method = 'post'; } $this->field->setMethod($method); return $method; } protected function url() { if (strstr($this->info['uri'], 'http')) { $this->platform['host'] = ''; } $uri = Dever::db('api_uri', 'api')->select(array('api_id' => $this->info['id'])); if ($uri) { $path = array(); $param = array(); foreach ($uri as $k => $v) { $v['value'] = $this->field->value($v['value']); if ($v['type'] == 1) { $path[] = $v['value']; } elseif ($v['type'] == 2) { $path[] = $v['key'] . '/' . $v['value']; } elseif ($v['type'] == 3) { $path[] = $v['key'] . '=' . $v['value']; } elseif ($v['type'] == 4) { $param[] = $v['value']; } elseif ($v['type'] == 5) { $param[] = $v['key'] . '=' . $v['value']; } } if ($path) { $this->info['uri'] .= implode('/', $path); } if ($param) { if (!strstr($this->info['uri'], '?')) { $this->info['uri'] .= '?'; } $this->info['uri'] .= implode('&', $param); } } $this->field->setUri($this->info['uri']); return $this->platform['host'] . $this->info['uri']; } }