Files
kiri-wchat/wx/Miniprogarampage.php
T

135 lines
2.6 KiB
PHP
Raw Normal View History

2019-07-17 17:17:37 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/3/26 0026
* Time: 10:23
*/
namespace wchat;
abstract class Miniprogarampage
{
/** @var Config */
protected $config;
/** @var mixed $instance */
2019-08-21 12:11:36 +08:00
protected static $instance = null;
2019-07-17 17:17:37 +08:00
/** @var WxClient */
protected $request = null;
/**
* Miniprogarampage constructor.
*/
private function __construct()
{
2019-09-19 19:04:43 +08:00
$this->request = WxClient::getInstance();
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());
2019-07-17 17:17:37 +08:00
return static::$instance;
}
/**
* @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-07-17 17:17:37 +08:00
$this->request->setMethod(WxClient::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());
}
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;
}
}