|
|
|
@ -16,7 +16,10 @@
|
|
|
|
|
|
|
|
|
|
namespace DtApp\ThinkLibrary\service\WeChat;
|
|
|
|
|
|
|
|
|
|
use DtApp\ThinkLibrary\cache\Mysql;
|
|
|
|
|
use DtApp\ThinkLibrary\exception\CacheException;
|
|
|
|
|
use DtApp\ThinkLibrary\exception\CurlException;
|
|
|
|
|
use DtApp\ThinkLibrary\exception\WeChatException;
|
|
|
|
|
use DtApp\ThinkLibrary\Service;
|
|
|
|
|
use DtApp\ThinkLibrary\service\Curl\HttpService;
|
|
|
|
|
|
|
|
|
@ -33,6 +36,12 @@ class MiniService extends Service
|
|
|
|
|
private $app_secret;
|
|
|
|
|
private $grant_type = "client_credential";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 驱动方式
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $cache = "file";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $appId
|
|
|
|
|
* @return $this
|
|
|
|
@ -53,12 +62,25 @@ class MiniService extends Service
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 驱动方式
|
|
|
|
|
* @param string $cache
|
|
|
|
|
* @return $this
|
|
|
|
|
*/
|
|
|
|
|
public function cache(string $cache)
|
|
|
|
|
{
|
|
|
|
|
$this->cache = $cache;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用户支付完成后,获取该用户的 UnionId,无需用户授权
|
|
|
|
|
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/user-info/auth.getPaidUnionId.html
|
|
|
|
|
* @param string $openid
|
|
|
|
|
* @return bool|mixed|string
|
|
|
|
|
* @throws CacheException
|
|
|
|
|
* @throws CurlException
|
|
|
|
|
* @throws WeChatException
|
|
|
|
|
*/
|
|
|
|
|
public function getPaidUnionId(string $openid)
|
|
|
|
|
{
|
|
|
|
@ -75,7 +97,9 @@ class MiniService extends Service
|
|
|
|
|
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.createQRCode.html
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return array|bool|mixed|string
|
|
|
|
|
* @throws CacheException
|
|
|
|
|
* @throws CurlException
|
|
|
|
|
* @throws WeChatException
|
|
|
|
|
*/
|
|
|
|
|
public function createWxaQrCode(array $data = [])
|
|
|
|
|
{
|
|
|
|
@ -94,7 +118,9 @@ class MiniService extends Service
|
|
|
|
|
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.get.html
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return array|bool|mixed|string
|
|
|
|
|
* @throws CacheException
|
|
|
|
|
* @throws CurlException
|
|
|
|
|
* @throws WeChatException
|
|
|
|
|
*/
|
|
|
|
|
public function getWxaCode(array $data = [])
|
|
|
|
|
{
|
|
|
|
@ -113,7 +139,9 @@ class MiniService extends Service
|
|
|
|
|
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.getUnlimited.html
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return array|bool|mixed|string
|
|
|
|
|
* @throws CacheException
|
|
|
|
|
* @throws CurlException
|
|
|
|
|
* @throws WeChatException
|
|
|
|
|
*/
|
|
|
|
|
public function getWxaCodeUnLimit(array $data = [])
|
|
|
|
|
{
|
|
|
|
@ -132,7 +160,9 @@ class MiniService extends Service
|
|
|
|
|
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.addTemplate.html
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return bool|mixed|string
|
|
|
|
|
* @throws CacheException
|
|
|
|
|
* @throws CurlException
|
|
|
|
|
* @throws WeChatException
|
|
|
|
|
*/
|
|
|
|
|
public function addTemplate(array $data = [])
|
|
|
|
|
{
|
|
|
|
@ -150,7 +180,9 @@ class MiniService extends Service
|
|
|
|
|
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.deleteTemplate.html
|
|
|
|
|
* @param string $priTmplId 要删除的模板id
|
|
|
|
|
* @return bool|mixed|string
|
|
|
|
|
* @throws CacheException
|
|
|
|
|
* @throws CurlException
|
|
|
|
|
* @throws WeChatException
|
|
|
|
|
*/
|
|
|
|
|
public function deleteTemplate(string $priTmplId)
|
|
|
|
|
{
|
|
|
|
@ -170,7 +202,9 @@ class MiniService extends Service
|
|
|
|
|
* 获取小程序账号的类目
|
|
|
|
|
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getCategory.html
|
|
|
|
|
* @return bool|mixed|string
|
|
|
|
|
* @throws CacheException
|
|
|
|
|
* @throws CurlException
|
|
|
|
|
* @throws WeChatException
|
|
|
|
|
*/
|
|
|
|
|
public function getCategory()
|
|
|
|
|
{
|
|
|
|
@ -187,7 +221,9 @@ class MiniService extends Service
|
|
|
|
|
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getPubTemplateKeyWordsById.html
|
|
|
|
|
* @param string $tid 模板标题 id
|
|
|
|
|
* @return bool|mixed|string
|
|
|
|
|
* @throws CacheException
|
|
|
|
|
* @throws CurlException
|
|
|
|
|
* @throws WeChatException
|
|
|
|
|
*/
|
|
|
|
|
public function getPubTemplateKeyWordsById(string $tid)
|
|
|
|
|
{
|
|
|
|
@ -208,7 +244,9 @@ class MiniService extends Service
|
|
|
|
|
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getPubTemplateTitleList.html
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return bool|mixed|string
|
|
|
|
|
* @throws CacheException
|
|
|
|
|
* @throws CurlException
|
|
|
|
|
* @throws WeChatException
|
|
|
|
|
*/
|
|
|
|
|
public function getPubTemplateTitleList(array $data = [])
|
|
|
|
|
{
|
|
|
|
@ -225,7 +263,9 @@ class MiniService extends Service
|
|
|
|
|
* 获取当前帐号下的个人模板列表
|
|
|
|
|
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
|
|
|
|
|
* @return bool|mixed|string
|
|
|
|
|
* @throws CacheException
|
|
|
|
|
* @throws CurlException
|
|
|
|
|
* @throws WeChatException
|
|
|
|
|
*/
|
|
|
|
|
public function getTemplateList()
|
|
|
|
|
{
|
|
|
|
@ -242,7 +282,9 @@ class MiniService extends Service
|
|
|
|
|
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return bool|mixed|string
|
|
|
|
|
* @throws CacheException
|
|
|
|
|
* @throws CurlException
|
|
|
|
|
* @throws WeChatException
|
|
|
|
|
*/
|
|
|
|
|
public function send(array $data = [])
|
|
|
|
|
{
|
|
|
|
@ -292,7 +334,9 @@ class MiniService extends Service
|
|
|
|
|
* 获取小程序全局唯一后台接口调用凭据(access_token)
|
|
|
|
|
* https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
|
|
|
|
|
* @return bool|mixed|string
|
|
|
|
|
* @throws CacheException
|
|
|
|
|
* @throws CurlException
|
|
|
|
|
* @throws WeChatException
|
|
|
|
|
*/
|
|
|
|
|
public function accessToken()
|
|
|
|
|
{
|
|
|
|
@ -304,10 +348,14 @@ class MiniService extends Service
|
|
|
|
|
* 获取access_token信息
|
|
|
|
|
* @return array|bool|mixed|string|string[]
|
|
|
|
|
* @throws CurlException
|
|
|
|
|
* @throws WeChatException
|
|
|
|
|
* @throws CacheException
|
|
|
|
|
*/
|
|
|
|
|
private function getAccessToken()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
$this->grant_type = "client_credential";
|
|
|
|
|
if ($this->cache == "file") {
|
|
|
|
|
// 文件名
|
|
|
|
|
$file = "{$this->app->getRootPath()}runtime/{$this->app_id}_access_token.json";
|
|
|
|
|
// 获取数据
|
|
|
|
@ -340,5 +388,28 @@ class MiniService extends Service
|
|
|
|
|
$accessToken = $accessToken_res;
|
|
|
|
|
}
|
|
|
|
|
return $accessToken;
|
|
|
|
|
} else if ($this->cache == "mysql") {
|
|
|
|
|
$access_token = [];
|
|
|
|
|
// 文件名
|
|
|
|
|
$file = "{$this->app_id}_access_token";
|
|
|
|
|
// 获取数据
|
|
|
|
|
$cache_mysql = new Mysql();
|
|
|
|
|
$cache_mysql_value = $cache_mysql
|
|
|
|
|
->name($file)
|
|
|
|
|
->get();
|
|
|
|
|
if (!empty($cache_mysql_value)) {
|
|
|
|
|
$access_token['access_token'] = $cache_mysql_value;
|
|
|
|
|
} else {
|
|
|
|
|
$accessToken_res = HttpService::instance()
|
|
|
|
|
->url("{$this->api_url}cgi-bin/token?grant_type={$this->grant_type}&appid={$this->app_id}&secret={$this->app_secret}")
|
|
|
|
|
->toArray();
|
|
|
|
|
$cache_mysql
|
|
|
|
|
->name($file)
|
|
|
|
|
->expire(time() + 6000)
|
|
|
|
|
->set($accessToken_res['access_token']);
|
|
|
|
|
$access_token['access_token'] = $accessToken_res['access_token'];
|
|
|
|
|
}
|
|
|
|
|
return $access_token;
|
|
|
|
|
} else throw new WeChatException("驱动方式错误");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|