2019-07-17 17:17:37 +08:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* Created by PhpStorm.
|
|
|
|
|
* User: whwyy
|
|
|
|
|
* Date: 2018/3/26 0026
|
|
|
|
|
* Time: 10:23
|
|
|
|
|
*/
|
|
|
|
|
|
2019-11-11 18:14:47 +08:00
|
|
|
namespace common;
|
2019-07-17 17:17:37 +08:00
|
|
|
|
2019-11-11 18:14:47 +08:00
|
|
|
use common\Config;
|
|
|
|
|
|
|
|
|
|
abstract class Miniprogarampage implements Progaram
|
2019-07-17 17:17:37 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/** @var Config */
|
|
|
|
|
protected $config;
|
|
|
|
|
|
2019-11-04 14:39:59 +08:00
|
|
|
/** @var Miniprogarampage $instance */
|
2019-08-21 12:11:36 +08:00
|
|
|
protected static $instance = null;
|
2019-07-17 17:17:37 +08:00
|
|
|
|
2019-11-11 18:14:47 +08:00
|
|
|
/** @var HttpClient */
|
2019-07-17 17:17:37 +08:00
|
|
|
protected $request = null;
|
|
|
|
|
|
2019-11-11 18:14:47 +08:00
|
|
|
protected $errorCode = 0;
|
|
|
|
|
protected $errorMsg = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $message
|
|
|
|
|
* @param int $code
|
|
|
|
|
* @return Result
|
|
|
|
|
*/
|
|
|
|
|
protected function sendError($message, $code = 500)
|
|
|
|
|
{
|
|
|
|
|
return new Result(['code' => $code, 'message' => $message]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $code
|
|
|
|
|
*/
|
|
|
|
|
public function setErrorCode($code)
|
|
|
|
|
{
|
|
|
|
|
$this->errorCode = $code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $message
|
|
|
|
|
*/
|
|
|
|
|
public function setErrorMessage($message)
|
|
|
|
|
{
|
|
|
|
|
$this->errorMsg = $message;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function getErrorCode()
|
|
|
|
|
{
|
|
|
|
|
return $this->errorCode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getErrorMessage()
|
|
|
|
|
{
|
|
|
|
|
return $this->errorMsg;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-17 17:17:37 +08:00
|
|
|
/**
|
|
|
|
|
* Miniprogarampage constructor.
|
|
|
|
|
*/
|
2019-12-12 12:17:44 +08:00
|
|
|
public function __construct()
|
2019-07-17 17:17:37 +08:00
|
|
|
{
|
2019-11-11 18:14:47 +08:00
|
|
|
$this->request = HttpClient::NewRequest();
|
2019-07-17 17:17:37 +08:00
|
|
|
$this->request->setIsSSL(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Config $config
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public static function getInstance(Config $config)
|
|
|
|
|
{
|
|
|
|
|
if (static::$instance === null) {
|
|
|
|
|
static::$instance = new static();
|
|
|
|
|
}
|
|
|
|
|
static::$instance->config = $config;
|
2019-09-19 19:04:43 +08:00
|
|
|
static::$instance->request->setAgent($config->getAgent());
|
2020-01-07 12:05:27 +08:00
|
|
|
static::$instance->request->setUseSwoole($config->isUsrSwoole());
|
2019-11-04 14:39:59 +08:00
|
|
|
|
|
|
|
|
$request = static::$instance->request;
|
|
|
|
|
if ($request->getIsSSL()) {
|
|
|
|
|
$request->addHeader('ssl_cert_file', $config->getSslCert());
|
|
|
|
|
$request->addHeader('ssl_key_file', $config->getSslKey());
|
|
|
|
|
}
|
2019-07-17 17:17:37 +08:00
|
|
|
return static::$instance;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-11 18:14:47 +08:00
|
|
|
/**
|
|
|
|
|
* @param Config $config
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function initConfig($config)
|
|
|
|
|
{
|
|
|
|
|
$this->config = $config;
|
2020-01-07 12:11:00 +08:00
|
|
|
$this->request->setUseSwoole($config->isUsrSwoole());
|
|
|
|
|
$this->request->setAgent($config->getAgent());
|
2020-01-07 12:21:01 +08:00
|
|
|
if (!empty($config->getSslCert())) {
|
|
|
|
|
$this->request->addHeader('ssl_cert_file', $config->getSslCert());
|
|
|
|
|
}
|
|
|
|
|
if (!empty($config->getSslKey())) {
|
|
|
|
|
$this->request->addHeader('ssl_key_file', $config->getSslKey());
|
|
|
|
|
}
|
2019-11-11 18:14:47 +08:00
|
|
|
return $this;
|
|
|
|
|
}
|
2019-07-17 17:17:37 +08:00
|
|
|
|
2019-11-11 18:29:09 +08:00
|
|
|
/**
|
|
|
|
|
* @return \common\Config
|
|
|
|
|
*/
|
|
|
|
|
public function getConfig()
|
|
|
|
|
{
|
|
|
|
|
return $this->config;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-17 17:17:37 +08:00
|
|
|
/**
|
|
|
|
|
* @return bool|mixed|string
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
|
|
|
|
protected function getAccessToken()
|
|
|
|
|
{
|
2019-07-17 17:27:31 +08:00
|
|
|
$access = $this->config->getAccessToken();
|
|
|
|
|
if (!empty($access)) {
|
|
|
|
|
return $access;
|
|
|
|
|
}
|
2019-11-11 18:14:47 +08:00
|
|
|
$this->request->setMethod(HttpClient::GET);
|
2019-07-17 17:17:37 +08:00
|
|
|
$data = $this->request->get('/cgi-bin/token', [
|
|
|
|
|
'grant_type' => 'client_credential',
|
2019-11-04 14:39:59 +08:00
|
|
|
'appid' => $this->config->getAppid(),
|
|
|
|
|
'secret' => $this->config->getAppsecret(),
|
2019-07-17 17:17:37 +08:00
|
|
|
]);
|
|
|
|
|
if (!$data->isResultsOK()) {
|
|
|
|
|
throw new \Exception($data->getMessage());
|
|
|
|
|
}
|
2019-07-17 17:27:31 +08:00
|
|
|
$access = $data->getData('access_token');
|
|
|
|
|
$this->config->setAccessToken($access);
|
|
|
|
|
return $access;
|
2019-07-17 17:17:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $data
|
|
|
|
|
* @param $body
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
|
|
|
|
protected function buildResult($data, $body = NULL)
|
|
|
|
|
{
|
|
|
|
|
$data = $this->checkSign($data);
|
|
|
|
|
if (!$data) {
|
|
|
|
|
$return['code'] = -1;
|
|
|
|
|
$return['message'] = '签名错误.';
|
|
|
|
|
} else {
|
|
|
|
|
if (isset($data['return_code'])) {
|
|
|
|
|
if ($data['return_code'] == 'FAIL') {
|
|
|
|
|
$return['code'] = -1;
|
|
|
|
|
$return['message'] = $data['return_msg'];
|
|
|
|
|
} else {
|
|
|
|
|
$return['code'] = 0;
|
|
|
|
|
$return['data'] = $data;
|
|
|
|
|
$return['data']['postBody'] = $body;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if ($data['errcode'] == 'FAIL') {
|
|
|
|
|
$return['code'] = -1;
|
|
|
|
|
$return['message'] = $data['errmsg'];
|
|
|
|
|
} else {
|
|
|
|
|
$return['code'] = 0;
|
|
|
|
|
$return['data'] = $data;
|
|
|
|
|
$return['data']['postBody'] = $body;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $result
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
|
|
|
|
protected function checkSign($result)
|
|
|
|
|
{
|
|
|
|
|
$data = Help::toArray($result);
|
|
|
|
|
|
|
|
|
|
if (!isset($data['sign'])) {
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sign = $data['sign'];
|
|
|
|
|
|
|
|
|
|
unset($data['sign']);
|
|
|
|
|
|
|
|
|
|
$key = $this->config->getKey();
|
|
|
|
|
$sign_type = $this->config->getSignType();
|
|
|
|
|
|
|
|
|
|
$_sign = Help::sign($data, $key, $sign_type);
|
|
|
|
|
if ($sign != $_sign) {
|
|
|
|
|
return FALSE;
|
|
|
|
|
}
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|