[更新]增加测试文件,修正基类

This commit is contained in:
Anyon 2018-01-31 15:46:04 +08:00
parent 0f4ec8bdd9
commit e9f175143f
17 changed files with 284 additions and 29 deletions

186
WeChat/Contracts/WeChat.php Normal file
View File

@ -0,0 +1,186 @@
<?php
// +----------------------------------------------------------------------
// | WeChatDeveloper
// +----------------------------------------------------------------------
// | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目https://github.com/zoujingli/WeChatDeveloper
// +----------------------------------------------------------------------
namespace WeChat\Contracts;
use WeChat\Exceptions\InvalidArgumentException;
use WeChat\Exceptions\InvalidResponseException;
/**
* Class Wechat
* @package Wechat\Contracts
*/
class WeChat
{
/**
* 当前微信配置
* @var Config
*/
public $config;
/**
* 访问AccessToken
* @var string
*/
public $access_token = '';
/**
* 当前请求方法参数
* @var array
*/
private $currentMethod = [];
/**
* 当前模式
* @var bool
*/
private $isTry = false;
/**
* Wechat constructor.
* @param array $options
*/
public function __construct(array $options)
{
if (empty($options['appid'])) {
throw new InvalidArgumentException("Missing Config -- [appid]");
}
if (empty($options['appsecret'])) {
throw new InvalidArgumentException("Missing Config -- [appsecret]");
}
$this->config = new Config($options);
}
/**
* 获取访问accessToken
* @return string
* @throws \Wechat\Exceptions\InvalidResponseException
* @throws \Wechat\Exceptions\LocalCacheException
*/
public function getAccesstoken()
{
if (!empty($this->access_token)) {
return $this->access_token;
}
$cacheKey = $this->config->get('appid') . '_accesstoken';
$this->access_token = Tools::getCache($cacheKey);
if (!empty($this->access_token)) {
return $this->access_token;
}
list($appid, $secret) = [$this->config->get('appid'), $this->config->get('appsecret')];
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
$result = Tools::json2arr(Tools::get($url));
if (!empty($result['access_token'])) {
Tools::setCache($cacheKey, $result['access_token'], 6000);
}
return $result['access_token'];
}
/**
* 清理删除accessToken
* @return bool
*/
public function delAccessToken()
{
$this->access_token = '';
return Tools::delCache($this->config->get('appid') . '_accesstoken');
}
/**
* 以GET获取接口数据并转为数组
* @param string $url 接口地址
* @return array
*/
protected function httpGetForJson($url)
{
try {
return Tools::json2arr(Tools::get($url));
} catch (InvalidResponseException $e) {
if (!$this->isTry && in_array($e->getCode(), ['40014', '40001', '41001', '42001'])) {
$this->delAccessToken();
$this->isTry = true;
return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']);
}
}
}
/**
* 以POST获取接口数据并转为数组
* @param string $url 接口地址
* @param array $data 请求数据
* @param bool $buildToJson
* @return array
*/
protected function httpPostForJson($url, array $data, $buildToJson = true)
{
try {
return Tools::json2arr(Tools::post($url, $buildToJson ? Tools::arr2json($data) : $data));
} catch (InvalidResponseException $e) {
if (!$this->isTry && in_array($e->getCode(), ['40014', '40001', '41001', '42001'])) {
$this->delAccessToken();
$this->isTry = true;
return call_user_func_array([$this, $this->currentMethod['method']], $this->currentMethod['arguments']);
}
}
}
/**
* 注册当前请求接口
* @param string $url 接口地址
* @param string $method 当前接口方法
* @param array $arguments 请求参数
* @return mixed
* @throws \Wechat\Exceptions\InvalidResponseException
* @throws \Wechat\Exceptions\LocalCacheException
*/
protected function registerApi(&$url, $method, $arguments = [])
{
$this->currentMethod = ['method' => $method, 'arguments' => $arguments];
if (empty($this->access_token)) {
$this->access_token = $this->getAccesstoken();
}
return $url = str_replace('ACCESS_TOKEN', $this->access_token, $url);
}
/**
* 接口通用POST请求方法
* @param string $url 接口URL
* @param array $data POST提交接口参数
* @param bool $isBuildJson
* @return array
* @throws InvalidResponseException
* @throws \Wechat\Exceptions\LocalCacheException
*/
public function callPostApi($url, array $data, $isBuildJson = true)
{
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpPostForJson($url, $data, $isBuildJson);
}
/**
* 接口通用GET请求方法
* @param string $url 接口URL
* @return array
* @throws InvalidResponseException
* @throws \Wechat\Exceptions\LocalCacheException
*/
public function callGetApi($url)
{
$this->registerApi($url, __FUNCTION__, func_get_args());
return $this->httpGetForJson($url);
}
}

View File

@ -14,14 +14,14 @@
namespace WeChat;
use WeChat\Contracts\WePay;
use WeChat\Contracts\WeChat;
/**
* 客服消息处理
* Class Custom
* @package WeChat
*/
class Custom extends WePay
class Custom extends WeChat
{
/**
* 添加客服帐号

View File

@ -15,7 +15,7 @@
namespace WeChat;
use WeChat\Contracts\Tools;
use WeChat\Contracts\WePay;
use WeChat\Contracts\WeChat;
use WeChat\Exceptions\InvalidResponseException;
/**
@ -23,7 +23,7 @@ use WeChat\Exceptions\InvalidResponseException;
* Class Media
* @package WeChat
*/
class Media extends WePay
class Media extends WeChat
{
/**
* 新增临时素材

View File

@ -14,14 +14,14 @@
namespace WeChat;
use WeChat\Contracts\WePay;
use WeChat\Contracts\WeChat;
/**
* 微信菜单管理
* Class Menu
* @package WeChat
*/
class Menu extends WePay
class Menu extends WeChat
{
/**

View File

@ -9,7 +9,7 @@
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目https://github.com/zoujingli/WePayDeveloper
// | github开源项目https://github.com/zoujingli/WeChatDeveloper
// +----------------------------------------------------------------------
namespace WeChat;
@ -40,7 +40,7 @@ class Pay
protected $config;
/**
* WePay constructor.
* WeChat constructor.
* @param array $options
*/
public function __construct(array $options)

View File

@ -15,14 +15,14 @@
namespace WeChat;
use WeChat\Contracts\WePay;
use WeChat\Contracts\WeChat;
/**
* 商店管理
* Class Product
* @package WeChat
*/
class Product extends WePay
class Product extends WeChat
{
/**
* 提交审核/取消发布商品

View File

@ -14,14 +14,14 @@
namespace WeChat;
use WeChat\Contracts\WePay;
use WeChat\Contracts\WeChat;
/**
* 二维码管理
* Class Qrcode
* @package WeChat
*/
class Qrcode extends WePay
class Qrcode extends WeChat
{
/**

View File

@ -14,14 +14,14 @@
namespace WeChat;
use WeChat\Contracts\WePay;
use WeChat\Contracts\WeChat;
/**
* 扫一扫接入管理
* Class Scan
* @package WeChat
*/
class Scan extends WePay
class Scan extends WeChat
{
/**
* 获取商户信息

View File

@ -15,7 +15,7 @@
namespace WeChat;
use WeChat\Contracts\Tools;
use WeChat\Contracts\WePay;
use WeChat\Contracts\WeChat;
use WeChat\Exceptions\InvalidResponseException;
/**
@ -23,7 +23,7 @@ use WeChat\Exceptions\InvalidResponseException;
* Class Script
* @package WeChat
*/
class Script extends WePay
class Script extends WeChat
{
/**

View File

@ -16,14 +16,14 @@ namespace WeChat;
use WeChat\Contracts\Tools;
use WeChat\Contracts\WePay;
use WeChat\Contracts\WeChat;
/**
* 揺一揺周边
* Class Shake
* @package WeChat
*/
class Shake extends WePay
class Shake extends WeChat
{
/**
* 申请开通功能

View File

@ -14,14 +14,14 @@
namespace WeChat;
use WeChat\Contracts\WePay;
use WeChat\Contracts\WeChat;
/**
* 用户标签管理
* Class Tags
* @package WeChat
*/
class Tags extends WePay
class Tags extends WeChat
{
/**
* 获取粉丝标签列表

View File

@ -14,14 +14,14 @@
namespace WeChat;
use WeChat\Contracts\WePay;
use WeChat\Contracts\WeChat;
/**
* 模板消息
* Class Template
* @package WeChat
*/
class Template extends WePay
class Template extends WeChat
{
/**
* 设置所属行业

View File

@ -14,14 +14,14 @@
namespace WeChat;
use WeChat\Contracts\WePay;
use WeChat\Contracts\WeChat;
/**
* 微信粉丝管理
* Class User
* @package WeChat
*/
class User extends WePay
class User extends WeChat
{
/**
@ -30,7 +30,6 @@ class User extends WePay
* @param string $remark
* @return array
* @throws Exceptions\InvalidResponseException
* @throws Exceptions\LocalCacheException
*/
public function updateMark($openid, $remark)
{

View File

@ -15,14 +15,14 @@
namespace WeChat;
use WeChat\Contracts\WePay;
use WeChat\Contracts\WeChat;
/**
* 门店 WIFI 管理
* Class Wifi
* @package WeChat
*/
class Wifi extends WePay
class Wifi extends WeChat
{
/**

View File

@ -5,7 +5,7 @@
"description": "WeChat development of SDK",
"license": "MIT",
"keywords": [
"WeChatDeveloper",
"WeChatDeveloper",
"wechat",
"wxsdk"
],
@ -16,7 +16,7 @@
},
"autoload": {
"psr-4": {
"Wechat\\": "./Wechat"
"WeChat\\": "./WeChat"
}
}
}

21
include.php Normal file
View File

@ -0,0 +1,21 @@
<?php
// +----------------------------------------------------------------------
// | WeChatDeveloper
// +----------------------------------------------------------------------
// | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目https://github.com/zoujingli/WeChatDeveloper
// +----------------------------------------------------------------------
// 动态注册SDK自动加载
spl_autoload_register(function ($classname) {
$filename = __DIR__ . DIRECTORY_SEPARATOR . str_replace('\\/', DIRECTORY_SEPARATOR, $classname) . '.php';
if (stripos($classname, 'WeChat') === 0 && file_exists($filename)) {
include $filename;
}
});

49
test.php Normal file
View File

@ -0,0 +1,49 @@
<?php
// +----------------------------------------------------------------------
// | WeChatDeveloper
// +----------------------------------------------------------------------
// | 版权所有 2014~2018 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目https://github.com/zoujingli/WeChatDeveloper
// +----------------------------------------------------------------------
// 1. 手动加载入口文件
include 'include.php';
// 2. 准备公众号配置参数
$config = [
'token' => 'test',
'appid' => 'wx60a43dd8161666d4',
'appsecret' => '71308e96a204296c57d7cd4b21b883e8',
'encodingaeskey' => 'BJIUzE0gqlWy0GxfPp4J1oPTBmOrNDIGPNav1YFH5Z5',
// 配置商户支付参数
'mch_id' => "1235704602",
'mch_key' => 'IKI4kpHjU94ji3oqre5zYaQMwLHuZPmj',
// 配置商户支付双向证书目录
'ssl_key' => '',
'ssl_cer' => '',
];
try {
// 3. 实例对应的接口对象
$user = new \WeChat\User($config);
// 4. 调用接口对象方法
$list = $user->getUserList();
echo '<pre>';
var_export($list);
} catch (Exception $e) {
// 出错啦,处理下吧
echo $e->getMessage() . PHP_EOL;
}