<?php namespace Connect\Lib\Func;
use Dever;
class Api extends Base
{
    protected $type = 'api';

    # 执行某个业务接口
    public function run($type, $cate_id, $channel, $param = array())
    {
        $state = $this->setting($type, $cate_id, $channel, $param);
        if (!$state) {
            return $state;
        }
        return $this->curl();
    }

    # 构建请求头
    protected function requestHeader(&$header)
    {
        $this->request($header, 'api_request_header', array('api_id' => $this->info['id']));
    }

    # 构建请求体
    protected function requestBody(&$body)
    {
        $this->request($body, 'api_request_body', array('api_id' => $this->info['id']));
    }

    # 数据响应格式处理
    protected function responseHandle(&$data)
    {
        $api_response = Dever::db('api_response', 'connect')->select(array('api_id' => $this->info['id']));
        if ($api_response) {
            foreach ($api_response as $k => $v) {
                if (isset($data[$v['key']])) {
                    $data[$v['key']] = $this->value($data, $v['key'], $v['key'], $v['type'] * 100);
                }
            }
        }
        $data = $this->service($data);
    }

    # 生成回调
    protected function createNotify($order_id)
    {
        $encode = \Dever\Helper\Str::encode($this->connect['id'] . '|' . $this->info['id'] . '|' . $order_id);
        return Dever::url('connect/notify.common', array('s' => $encode));
    }
}