SystemClearServices.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\system;
  12. use app\services\BaseServices;
  13. use crmeb\exceptions\AdminException;
  14. use think\facade\Config;
  15. use think\facade\Db;
  16. /**
  17. * 清除数据
  18. * Class SystemClearServices
  19. * @package app\services\system
  20. */
  21. class SystemClearServices extends BaseServices
  22. {
  23. /**
  24. * 清除表数据
  25. * @param string|array $table_name
  26. * @param $status
  27. */
  28. public function clearData($table_name, bool $status)
  29. {
  30. $prefix = config('database.connections.' . config('database.default'))['prefix'];
  31. if (is_string($table_name)) {
  32. $clearData = [$table_name];
  33. } else {
  34. $clearData = $table_name;
  35. }
  36. foreach ($clearData as $name) {
  37. if ($status) {
  38. Db::execute('TRUNCATE TABLE ' . $prefix . $name);
  39. } else {
  40. Db::execute('DELETE FROM' . $prefix . $name);
  41. }
  42. }
  43. }
  44. /**
  45. * 递归删除文件,只能删除 public/uploads下的文件
  46. * @param $dirName
  47. * @param bool $subdir
  48. */
  49. public function delDirAndFile(string $dirName, $subdir = true)
  50. {
  51. if (strstr($dirName, 'public/uploads') === false) {
  52. return true;
  53. }
  54. if ($handle = @opendir("$dirName")) {
  55. while (false !== ($item = readdir($handle))) {
  56. if ($item != "." && $item != "..") {
  57. if (is_dir("$dirName/$item"))
  58. $this->delDirAndFile("$dirName/$item", false);
  59. else
  60. @unlink("$dirName/$item");
  61. }
  62. }
  63. closedir($handle);
  64. if (!$subdir) @rmdir($dirName);
  65. }
  66. }
  67. /**
  68. * 替换域名
  69. * @param string $url
  70. * @return mixed
  71. */
  72. public function replaceSiteUrl(string $url)
  73. {
  74. $siteUrl = sys_config('site_url');
  75. $siteUrlJosn = str_replace('http://', 'http:\\\/\\\/', $siteUrl);
  76. $valueJosn = str_replace('http://', 'http:\\\/\\\/', $url);
  77. $prefix = Config::get('database.connections.' . Config::get('database.default') . '.prefix');
  78. $sql = [
  79. "UPDATE `{$prefix}article` SET `image_input` = replace(`image_input` ,'{$siteUrl}','{$url}')",
  80. "UPDATE `{$prefix}article_category` SET `image` = replace(`image` ,'{$siteUrl}','{$url}')",
  81. "UPDATE `{$prefix}article_content` SET `content` = replace(`content` ,'{$siteUrl}','{$url}')",
  82. "UPDATE `{$prefix}diy` SET `value` = replace(value ,'{$siteUrlJosn}','{$valueJosn}'),`default_value` = replace(default_value ,'{$siteUrlJosn}','{$valueJosn}')",
  83. "UPDATE `{$prefix}luck_lottery` SET `image` = replace(image ,'{$siteUrlJosn}','{$valueJosn}')",
  84. "UPDATE `{$prefix}luck_prize` SET `image` = replace(image ,'{$siteUrlJosn}','{$valueJosn}')",
  85. "UPDATE `{$prefix}store_bargain` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJosn}','{$valueJosn}')",
  86. "UPDATE `{$prefix}store_category` SET `pic` = replace(`pic` ,'{$siteUrl}','{$url}'),`big_pic` = replace(`big_pic` ,'{$siteUrl}','{$url}')",
  87. "UPDATE `{$prefix}store_combination` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJosn}','{$valueJosn}')",
  88. "UPDATE `{$prefix}store_integral` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJosn}','{$valueJosn}')",
  89. "UPDATE `{$prefix}store_product` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`slider_image` = replace(slider_image ,'{$siteUrlJosn}','{$valueJosn}'),`recommend_image` = replace(recommend_image ,'{$siteUrlJosn}','{$valueJosn}')",
  90. "UPDATE `{$prefix}store_product_attr_result` SET `result` = replace(result ,'{$siteUrl}','{$url}')",
  91. "UPDATE `{$prefix}store_product_attr_value` SET `image` = replace(image ,'{$siteUrl}','{$url}')",
  92. "UPDATE `{$prefix}store_product_description` SET `description`= replace(description,'{$siteUrl}','{$url}')",
  93. "UPDATE `{$prefix}store_seckill` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJosn}','{$valueJosn}')",
  94. "UPDATE `{$prefix}system_attachment` SET `att_dir` = replace(att_dir ,'{$siteUrl}','{$url}'),`satt_dir` = replace(satt_dir ,'{$siteUrl}','{$url}')",
  95. "UPDATE `{$prefix}system_config` SET `value` = replace(value ,'{$siteUrlJosn}','{$valueJosn}')",
  96. "UPDATE `{$prefix}system_group_data` SET `value` = replace(value ,'{$siteUrlJosn}','{$valueJosn}')",
  97. "UPDATE `{$prefix}user` SET `avatar` = replace(avatar ,'{$siteUrlJosn}','{$valueJosn}')",
  98. ];
  99. return $this->transaction(function () use ($sql) {
  100. try {
  101. foreach ($sql as $item) {
  102. Db::execute($item);
  103. }
  104. } catch (\Throwable $e) {
  105. throw new AdminException(400612, ['msg' => $e->getMessage()]);
  106. }
  107. });
  108. }
  109. }