<?php
namespace app\store\controller\setting;
use app\store\controller\Controller;
use think\Cache as Driver;
class Cache extends Controller
{
public function clear()
{
if ($this->request->isAjax()) {
$data = $this->postData('cache');
$this->rmCache($data['keys']);
return $this->renderSuccess('操作成功');
}
return $this->fetch('clear', [
'cacheList' => $this->getItems()
]);
}
private function getItems()
{
$wxapp_id = $this->store['wxapp']['wxapp_id'];
return [
'category' => [
'type' => 'cache',
'key' => 'category_' . $wxapp_id,
'name' => '分类'
],
'setting' => [
'type' => 'cache',
'key' => 'setting_' . $wxapp_id,
'name' => '程序设置'
],
'wxapp' => [
'type' => 'cache',
'key' => 'wxapp_' . $wxapp_id,
'name' => '小程序设置'
],
'temp' => [
'type' => 'file',
'name' => '临时图片',
'dirPath' => [
'web' => WEB_PATH . 'temp/' . $wxapp_id . '/',
'runtime' => RUNTIME_PATH . '/' . 'image/' . $wxapp_id . '/'
]
],
];
}
private function rmCache($keys)
{
$cacheList = $this->getItems();
$keys = array_intersect(array_keys($cacheList), $keys);
foreach ($keys as $key) {
$item = $cacheList[$key];
if ($item['type'] === 'cache') {
Driver::has($item['key']) && Driver::rm($item['key']);
} elseif ($item['type'] === 'file') {
$this->deltree($item['dirPath']);
}
}
}
private function deltree($dirPath)
{
if (is_array($dirPath)) {
foreach ($dirPath as $path)
$this->deleteFolder($path);
} else {
return $this->deleteFolder($dirPath);
}
return true;
}
private function deleteFolder($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 . DS);
rmdir($path . $val . DS);
} else {
unlink($path . $val);
}
}
}
return true;
}
}