diff --git a/src/cache/Mysql.php b/src/cache/Mysql.php index 3e567d8..a33fcb1 100644 --- a/src/cache/Mysql.php +++ b/src/cache/Mysql.php @@ -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 + ]); + } } diff --git a/src/exception/CacheException.php b/src/exception/CacheException.php new file mode 100644 index 0000000..6aca653 --- /dev/null +++ b/src/exception/CacheException.php @@ -0,0 +1,32 @@ +getMessage(); + } +} diff --git a/src/service/WeChat/MiniService.php b/src/service/WeChat/MiniService.php index 45e5c60..7e36dbc 100644 --- a/src/service/WeChat/MiniService.php +++ b/src/service/WeChat/MiniService.php @@ -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("驱动方式错误"); } } diff --git a/src/service/WeChat/WebApps.php b/src/service/WeChat/WebApps.php index ab88bb8..f255c2f 100644 --- a/src/service/WeChat/WebApps.php +++ b/src/service/WeChat/WebApps.php @@ -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("驱动方式错误"); } }