|
|
@@ -8,13 +8,13 @@
|
|
|
// +----------------------------------------------------------------------
|
|
|
// | Author: 萤火科技 <admin@yiovo.com>
|
|
|
// +----------------------------------------------------------------------
|
|
|
-declare (strict_types = 1);
|
|
|
+declare (strict_types=1);
|
|
|
|
|
|
namespace app\store\controller\setting;
|
|
|
|
|
|
-use app\common\library\helper;
|
|
|
+use think\response\Json;
|
|
|
+use app\store\service\setting\Clear as ClearService;
|
|
|
use app\store\controller\Controller;
|
|
|
-use think\facade\Cache as CacheDrive;
|
|
|
|
|
|
/**
|
|
|
* 清理缓存
|
|
|
@@ -25,125 +25,23 @@ class Cache extends Controller
|
|
|
{
|
|
|
/**
|
|
|
* 数据缓存项目(只显示key和name)
|
|
|
- * @return array
|
|
|
+ * @return Json
|
|
|
*/
|
|
|
- public function items()
|
|
|
+ public function items(): Json
|
|
|
{
|
|
|
- $items = [];
|
|
|
- foreach ($this->getItems() as $key => $item) {
|
|
|
- $items[] = [
|
|
|
- 'key' => $key,
|
|
|
- 'name' => $item['name']
|
|
|
- ];
|
|
|
- }
|
|
|
+ $ClearService = new ClearService;
|
|
|
+ $items = $ClearService->items();
|
|
|
return $this->renderSuccess(compact('items'));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 清理缓存
|
|
|
- * @return mixed
|
|
|
+ * @return Json
|
|
|
*/
|
|
|
- public function clear()
|
|
|
+ public function clear(): Json
|
|
|
{
|
|
|
- // 删除缓存
|
|
|
- $this->rmCache($this->postForm()['keys']);
|
|
|
+ $ClearService = new ClearService;
|
|
|
+ $ClearService->rmCache($this->postForm()['keys']);
|
|
|
return $this->renderSuccess('操作成功');
|
|
|
}
|
|
|
-
|
|
|
- /**
|
|
|
- * 数据缓存项目
|
|
|
- * @return array
|
|
|
- */
|
|
|
- private function getItems()
|
|
|
- {
|
|
|
- $storeId = $this->store['store_id'];
|
|
|
- return [
|
|
|
- 'category' => [
|
|
|
- 'type' => 'cache',
|
|
|
- 'key' => "category_{$storeId}",
|
|
|
- 'name' => '商品分类'
|
|
|
- ],
|
|
|
- 'setting' => [
|
|
|
- 'type' => 'cache',
|
|
|
- 'key' => "setting_{$storeId}",
|
|
|
- 'name' => '商城设置'
|
|
|
- ],
|
|
|
- 'wxapp' => [
|
|
|
- 'type' => 'cache',
|
|
|
- 'key' => "wxapp_{$storeId}",
|
|
|
- 'name' => '小程序设置'
|
|
|
- ],
|
|
|
- 'temp' => [
|
|
|
- 'type' => 'file',
|
|
|
- 'name' => '临时图片',
|
|
|
- 'dirPath' => [
|
|
|
- 'web' => web_path() . "temp/{$storeId}/",
|
|
|
- 'runtime' => runtime_root_path() . "/image/{$storeId}/",
|
|
|
- ]
|
|
|
- ],
|
|
|
- ];
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除缓存
|
|
|
- * @param $keys
|
|
|
- */
|
|
|
- private function rmCache(array $keys)
|
|
|
- {
|
|
|
- $cacheList = $this->getItems();
|
|
|
- $keys = array_intersect(array_keys($cacheList), $keys);
|
|
|
- foreach ($keys as $key) {
|
|
|
- $item = $cacheList[$key];
|
|
|
- if ($item['type'] === 'cache') {
|
|
|
- $cache = CacheDrive::instance();
|
|
|
- $cache->has($item['key']) && $cache->delete($item['key']);
|
|
|
- } elseif ($item['type'] === 'file') {
|
|
|
- $this->deltree($item['dirPath']);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 删除目录下所有文件
|
|
|
- * @param $dirPath
|
|
|
- * @return bool
|
|
|
- */
|
|
|
- private function deltree($dirPath)
|
|
|
- {
|
|
|
- if (is_array($dirPath)) {
|
|
|
- foreach ($dirPath as $path)
|
|
|
- $this->deleteFolder($path);
|
|
|
- } else {
|
|
|
- return $this->deleteFolder($dirPath);
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 递归删除指定目录下所有文件
|
|
|
- * @param $path
|
|
|
- * @return bool
|
|
|
- */
|
|
|
- private function deleteFolder(string $path)
|
|
|
- {
|
|
|
- if (!is_dir($path)) return false;
|
|
|
- // 扫描一个文件夹内的所有文件夹和文件
|
|
|
- foreach (scandir($path) as $val) {
|
|
|
- // 排除目录中的.和..
|
|
|
- if (!in_array($val, ['.', '..'])) {
|
|
|
- // 如果是目录则递归子目录,继续操作
|
|
|
- if (is_dir("{$path}{$val}")) {
|
|
|
- // 子目录中操作删除文件夹和文件
|
|
|
- $this->deleteFolder("{$path}{$val}/");
|
|
|
- // 目录清空后删除空文件夹
|
|
|
- rmdir("{$path}{$val}/");
|
|
|
- } else {
|
|
|
- // 如果是文件直接删除
|
|
|
- unlink("{$path}{$val}");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
}
|