<?php
namespace backend\models;

use Yii;
use yii\httpclient\Client;

abstract class BaseApi
{
    /**
     * @var string
     */
    public $baseUrl;
    /**
     * @var string
     */
    public $apiUrl;
    /**
     * @var string
     */
    public $apiSecret;

    /**
     * BaseApi constructor.
     */
    public function __construct()
    {
        $this->baseUrl = Yii::$app->params['xcrmServiceBaseUrl'];
        $this->apiSecret = Yii::$app->params['xcrmServiceApiSecret'];
    }

    /**
     * 发送GET请求
     * @param string $apiUrl
     * @param array $data 
     * @return array|mixed
     */
    public function get($apiUrl, $data = [])
    {
        $client = new Client(['baseUrl' => $this->baseUrl]);
        $response = $client->get($apiUrl, $this->buildToken($data))->send();
        if ($response->getIsOk()) {
            return $response->getData();
        } else {
            return $this->returnArray(0, null, 'Api error: ' . $response->getStatusCode());
        }
    }

    /**
     * 发送POST请求
     * @param string $apiUrl
     * @param array $data
     * @return array|mixed
     */
    public function post($apiUrl, $data = [])
    {
        $client = new Client(['baseUrl' => $this->baseUrl]);
        $response = $client->post($apiUrl, $this->buildToken($data))->send();
        if ($response->getIsOk()) {
            return $response->getData();
        } else {
            return $this->returnArray(0, null, 'Api error: ' . $response->getStatusCode());
        }
    }

    /**
     * 发送PUT请求
     * @param string $apiUrl
     * @param array $data
     * @return array|mixed
     */
    public function put($apiUrl, $data = [])
    {
        $client = new Client(['baseUrl' => $this->baseUrl]);
        $response = $client->put($apiUrl, $this->buildToken($data))->send();
        if ($response->getIsOk()) {
            return $response->getData();
        } else {
            return $this->returnArray(0, null, 'Api error: ' . $response->getStatusCode());
        }
    }

    /**
     * 发送DELETE请求
     * @param string $apiUrl
     * @param array $data
     * @return array|mixed
     */
    public function delete($apiUrl, $data = [])
    {
        $client = new Client(['baseUrl' => $this->baseUrl]);
        $response = $client->delete($apiUrl, $this->buildToken($data))->send();
        if ($response->getIsOk()) {
            return $response->getData();
        } else {
            return $this->returnArray(0, null, 'Api error: ' . $response->getStatusCode());
        }
    }

    /**
     * 生成token
     * @param array $param
     * @return mixed
     */
    public function buildToken($param)
    {
        $secret = $this->apiSecret;
        ksort($param);//升序排序
        $strOauth = "";
        foreach ($param as $key => $val) {
            if ($key == 'token' || is_array($val) || $val === null) {
                continue;
            }
            $val = strval($val);
            $strOauth .= $key . '=' . $val . '&';
        }
        $strOauth = rtrim($strOauth, '&') . $secret;//加上密钥
        $param['token'] = md5($strOauth);
        return $param;
    }

    /**
     * 统一输出结果
     * @param int $code
     * @param array $data
     * @param string $msg
     * @return array
     */
    public function returnArray($code = 0, $data = null, $msg = '')
    {
        return ['code' => $code, 'data' => $data, 'message' => $msg];
    }
}