- 增加缓存驱动

v6 v6.0.12
Chaim 4 years ago
parent 66497c9893
commit ce4ad01a87

140
src/cache/Mysql.php vendored

@ -16,7 +16,147 @@
namespace DtApp\ThinkLibrary\cache;
use DtApp\ThinkLibrary\exception\CacheException;
use think\db\exception\DbException;
use think\facade\Db;
/**
* 缓存数据库驱动
* Class Mysql
* @package DtApp\ThinkLibrary\cache
*/
class Mysql
{
private $table = "think_cache";
private $cache_name;
private $cache_expire = 0;
/**
* 名称
* @param string $cache_name
* @return $this
*/
public function name(string $cache_name)
{
$this->cache_name = $cache_name;
return $this;
}
/**
* 过期时间
* @param string $cache_expire
* @return $this
*/
public function expire(string $cache_expire)
{
$this->cache_expire = $cache_expire;
return $this;
}
/**
* 设置
* @param $cache_value
* @return int|string
* @throws CacheException
*/
public function set($cache_value)
{
if (empty($this->cache_name)) throw new CacheException("名称未配置");
return Db::table($this->table)
->insert([
'cache_name' => $this->cache_name,
'cache_value' => $this->cache_expire,
'cache_expire' => $this->cache_expire
]);
}
/**
* 获取
* @return string
* @throws CacheException
*/
public function get()
{
if (empty($this->cache_name)) throw new CacheException("名称未配置");
$cache = Db::table($this->table)
->where('cache_name', $this->cache_name)
->field('cache_expire,cache_value');
if (empty($cache['cache_expire'])) return $cache['cache_value'];
if ($cache['cache_expire'] < time()) return "";
return $cache['cache_value'];
}
/**
* 删除
* @return int
* @throws CacheException
* @throws DbException
*/
public function delete()
{
if (empty($this->cache_name)) throw new CacheException("名称未配置");
return Db::table($this->table)
->where('cache_name', $this->cache_name)
->delete();
}
/**
* 更新
* @param $cache_value
* @return int
* @throws CacheException
* @throws DbException
*/
public function update($cache_value)
{
if (empty($this->cache_name)) throw new CacheException("名称未配置");
if (empty($this->cache_expire)) {
return Db::table($this->table)
->where('cache_name', $this->cache_name)
->update([
'cache_value' => $this->cache_expire,
]);
} else {
return Db::table($this->table)
->where('cache_name', $this->cache_name)
->update([
'cache_value' => $cache_value,
'cache_expire' => $this->cache_expire
]);
}
}
/**
* 自增
* @param int $int
* @return int
* @throws CacheException
* @throws DbException
*/
public function inc(int $int = 1)
{
$cache_value = $this->get();
return Db::table($this->table)
->where('cache_name', $this->cache_name)
->update([
'cache_value' => $cache_value + $int
]);
}
/**
* 自减
* @param int $int
* @return int
* @throws CacheException
* @throws DbException
*/
public function dec(int $int = 1)
{
$cache_value = $this->get();
return Db::table($this->table)
->where('cache_name', $this->cache_name)
->update([
'cache_value' => $cache_value - $int
]);
}
}

@ -0,0 +1,32 @@
<?php
// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 https://github.com/GC0202/ThinkLibrary
// | Packagist 地址 https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
namespace DtApp\ThinkLibrary\exception;
use Exception;
/**
* 缓存错误处理
* Class CacheException
* @package DtApp\ThinkLibrary\exception
*/
class CacheException extends Exception
{
public function errorMessage()
{
return $this->getMessage();
}
}

@ -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,41 +348,68 @@ 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";
// 文件名
$file = "{$this->app->getRootPath()}runtime/{$this->app_id}_access_token.json";
// 获取数据
$accessToken = file_exists($file) ? json_decode(file_get_contents($file), true) : [];
if (empty($accessToken) || !is_array($accessToken)) $accessToken = [
'access_token' => '',
'expires_in' => '',
'expires_time' => '',
];
if (empty($accessToken['expires_time'])) {
$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();
$accessToken_res['expires_time'] = time() + 6000;
file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
$accessToken = $accessToken_res;
} else if (!isset($accessToken['access_token'])) {
$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();
$accessToken_res['expires_time'] = time() + 6000;
file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
$accessToken = $accessToken_res;
} else if ($accessToken['expires_time'] <= time()) {
$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();
$accessToken_res['expires_time'] = time() + 6000;
file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
$accessToken = $accessToken_res;
}
return $accessToken;
if ($this->cache == "file") {
// 文件名
$file = "{$this->app->getRootPath()}runtime/{$this->app_id}_access_token.json";
// 获取数据
$accessToken = file_exists($file) ? json_decode(file_get_contents($file), true) : [];
if (empty($accessToken) || !is_array($accessToken)) $accessToken = [
'access_token' => '',
'expires_in' => '',
'expires_time' => '',
];
if (empty($accessToken['expires_time'])) {
$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();
$accessToken_res['expires_time'] = time() + 6000;
file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
$accessToken = $accessToken_res;
} else if (!isset($accessToken['access_token'])) {
$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();
$accessToken_res['expires_time'] = time() + 6000;
file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
$accessToken = $accessToken_res;
} else if ($accessToken['expires_time'] <= time()) {
$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();
$accessToken_res['expires_time'] = time() + 6000;
file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
$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("驱动方式错误");
}
}

@ -16,6 +16,8 @@
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\facade\Pregs;
@ -59,6 +61,12 @@ class WebApps extends Service
private $state = "";
private $grant_type = "authorization_code";
/**
* 驱动方式
* @var string
*/
private $cache = "file";
/**
* 公众号的唯一标识
* @param string $appId
@ -119,6 +127,17 @@ class WebApps extends Service
return $this;
}
/**
* 驱动方式
* @param string $cache
* @return $this
*/
public function cache(string $cache)
{
$this->cache = $cache;
return $this;
}
/**
* 网页授权
* https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#0
@ -201,7 +220,7 @@ class WebApps extends Service
* 分享
* @return array
* @throws CurlException
* @throws WeChatException
* @throws WeChatException|CacheException
*/
public function share()
{
@ -243,7 +262,9 @@ class WebApps extends Service
* 生成二维码
* @param array $data
* @return array|bool|mixed|string
* @throws CacheException
* @throws CurlException
* @throws WeChatException
*/
public function qrCode(array $data)
{
@ -260,7 +281,9 @@ class WebApps extends Service
* 发送模板消息
* @param array $data
* @return array|bool|mixed|string
* @throws CacheException
* @throws CurlException
* @throws WeChatException
*/
public function messageTemplateSend(array $data = [])
{
@ -278,7 +301,9 @@ class WebApps extends Service
* 将一条长链接转成短链接
* @param string $long_url
* @return bool
* @throws CacheException
* @throws CurlException
* @throws WeChatException
*/
public function shortUrl(string $long_url)
{
@ -298,41 +323,66 @@ class WebApps extends Service
* 获取access_token信息
* @return array|bool|mixed|string|string[]
* @throws CurlException
* @throws CacheException|WeChatException
*/
private function getAccessToken()
{
$this->grant_type = "client_credential";
// 文件名
$file = "{$this->app->getRootPath()}runtime/{$this->app_id}_access_token.json";
// 获取数据
$accessToken = file_exists($file) ? json_decode(file_get_contents($file), true) : [];
if (empty($accessToken) || !is_array($accessToken)) $accessToken = [
'access_token' => '',
'expires_in' => '',
'expires_time' => '',
];
if (empty($accessToken['expires_time'])) {
$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();
$accessToken_res['expires_time'] = time() + 6000;
file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
$accessToken = $accessToken_res;
} else if (!isset($accessToken['access_token'])) {
$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();
$accessToken_res['expires_time'] = time() + 6000;
file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
$accessToken = $accessToken_res;
} else if ($accessToken['expires_time'] <= time()) {
$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();
$accessToken_res['expires_time'] = time() + 6000;
file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
$accessToken = $accessToken_res;
}
return $accessToken;
if ($this->cache == "file") {
// 文件名
$file = "{$this->app->getRootPath()}runtime/{$this->app_id}_access_token.json";
// 获取数据
$accessToken = file_exists($file) ? json_decode(file_get_contents($file), true) : [];
if (empty($accessToken) || !is_array($accessToken)) $accessToken = [
'access_token' => '',
'expires_in' => '',
'expires_time' => '',
];
if (empty($accessToken['expires_time'])) {
$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();
$accessToken_res['expires_time'] = time() + 6000;
file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
$accessToken = $accessToken_res;
} else if (!isset($accessToken['access_token'])) {
$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();
$accessToken_res['expires_time'] = time() + 6000;
file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
$accessToken = $accessToken_res;
} else if ($accessToken['expires_time'] <= time()) {
$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();
$accessToken_res['expires_time'] = time() + 6000;
file_put_contents($file, json_encode($accessToken_res, JSON_UNESCAPED_UNICODE));
$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("驱动方式错误");
}
}

Loading…
Cancel
Save