Clear.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\shop\controller\setting;
  3. use app\shop\controller\Controller;
  4. use think\facade\Cache;
  5. /**
  6. * 清理缓存控制器
  7. */
  8. class Clear extends Controller
  9. {
  10. /**
  11. * 清理缓存
  12. */
  13. public function index()
  14. {
  15. if($this->request->isGet()){
  16. return $this->fetchData();
  17. }
  18. $this->rmCache( $this->postData()['keys']);
  19. return $this->renderSuccess('操作成功');
  20. }
  21. /**
  22. * 获取数据
  23. */
  24. public function fetchData()
  25. {
  26. $cacheList = $this->getItems();
  27. return $this->renderSuccess('', compact('cacheList'));
  28. }
  29. /**
  30. * 数据缓存项目
  31. */
  32. private function getItems()
  33. {
  34. $app_id = $this->store['app']['app_id'];
  35. return [
  36. 'category' => [
  37. 'type' => 'cache',
  38. 'key' => 'category_' . $app_id,
  39. 'name' => '商品分类'
  40. ],
  41. 'setting' => [
  42. 'type' => 'cache',
  43. 'key' => 'setting_' . $app_id. '_0',
  44. 'name' => '商城设置'
  45. ],
  46. 'app' => [
  47. 'type' => 'cache',
  48. 'key' => 'app_' . $app_id,
  49. 'name' => '应用设置'
  50. ],
  51. 'agent' => [
  52. 'type' => 'cache',
  53. 'key' => 'agent_setting_' . $app_id,
  54. 'name' => '分销设置'
  55. ],
  56. 'temp' => [
  57. 'type' => 'file',
  58. 'name' => '临时图片',
  59. 'dirPath' => [
  60. 'temp' => root_path('public') . '/temp/' . $app_id . '/',
  61. 'runtime' => root_path('runtime') . '/image/' . $app_id . '/'
  62. ]
  63. ],
  64. ];
  65. }
  66. /**
  67. * 删除缓存
  68. */
  69. private function rmCache($keys)
  70. {
  71. $app_id = $this->store['app']['app_id'];
  72. $cacheList = $this->getItems();
  73. $keys = array_intersect(array_keys($cacheList), $keys);
  74. foreach ($keys as $key) {
  75. $item = $cacheList[$key];
  76. if ($item['type'] === 'cache') {
  77. Cache::has($item['key']) && Cache::delete($item['key']);
  78. //如果是app,则多删除
  79. if($item['key'] == 'app'){
  80. Cache::has('app_mp_' . $app_id) && Cache::delete('app_mp_' . $app_id);
  81. Cache::has('app_wx_' . $app_id) && Cache::delete('app_wx_' . $app_id);
  82. }
  83. } elseif ($item['type'] === 'file') {
  84. $this->deltree($item['dirPath']);
  85. }
  86. }
  87. }
  88. /**
  89. * 删除目录下所有文件
  90. */
  91. private function deltree($dirPath)
  92. {
  93. if (is_array($dirPath)) {
  94. foreach ($dirPath as $path)
  95. $this->deleteFolder($path);
  96. } else {
  97. return $this->deleteFolder($dirPath);
  98. }
  99. return true;
  100. }
  101. /**
  102. * 递归删除指定目录下所有文件
  103. */
  104. private function deleteFolder($path)
  105. {
  106. if (!is_dir($path))
  107. return false;
  108. // 扫描一个文件夹内的所有文件夹和文件
  109. foreach (scandir($path) as $val) {
  110. // 排除目录中的.和..
  111. if (!in_array($val, ['.', '..'])) {
  112. // 如果是目录则递归子目录,继续操作
  113. if (is_dir($path . $val)) {
  114. // 子目录中操作删除文件夹和文件
  115. $this->deleteFolder($path . $val . '/');
  116. // 目录清空后删除空文件夹
  117. rmdir($path . $val . '/');
  118. } else {
  119. // 如果是文件直接删除
  120. unlink($path . $val);
  121. }
  122. }
  123. }
  124. return true;
  125. }
  126. }