Files
kiri-wchat/common/Miniprogarampage.php
T
as2252258@163.com 775cdeab7c add clear
2019-12-12 12:17:44 +08:00

206 lines
3.7 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/26 0026
* Time: 10:23
*/
namespace common;
use common\Config;
abstract class Miniprogarampage implements Progaram
{
/** @var Config */
protected $config;
/** @var Miniprogarampage $instance */
protected static $instance = null;
/** @var HttpClient */
protected $request = null;
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;
}
/**
* Miniprogarampage constructor.
*/
public function __construct()
{
$this->request = HttpClient::NewRequest();
$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;
static::$instance->request->setAgent($config->getAgent());
$request = static::$instance->request;
if ($request->getIsSSL()) {
$request->addHeader('ssl_cert_file', $config->getSslCert());
$request->addHeader('ssl_key_file', $config->getSslKey());
}
return static::$instance;
}
/**
* @param Config $config
* @return $this
*/
public function initConfig($config)
{
$this->config = $config;
return $this;
}
/**
* @return \common\Config
*/
public function getConfig()
{
return $this->config;
}
/**
* @return bool|mixed|string
* @throws \Exception
*/
protected function getAccessToken()
{
$access = $this->config->getAccessToken();
if (!empty($access)) {
return $access;
}
$this->request->setMethod(HttpClient::GET);
$data = $this->request->get('/cgi-bin/token', [
'grant_type' => 'client_credential',
'appid' => $this->config->getAppid(),
'secret' => $this->config->getAppsecret(),
]);
if (!$data->isResultsOK()) {
throw new \Exception($data->getMessage());
}
$access = $data->getData('access_token');
$this->config->setAccessToken($access);
return $access;
}
/**
* @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;
}
}