SystemClearServices.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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 qiniu\basic\BaseServices;
  13. use qiniu\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 bool $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 string $dirName
  47. * @param bool $subdir
  48. * @return bool
  49. */
  50. public function delDirAndFile(string $dirName, $subdir = true)
  51. {
  52. if (strstr($dirName, 'public/uploads') === false) {
  53. return true;
  54. }
  55. if ($handle = @opendir("$dirName")) {
  56. while (false !== ($item = readdir($handle))) {
  57. if ($item != "." && $item != "..") {
  58. if (is_dir("$dirName/$item"))
  59. $this->delDirAndFile("$dirName/$item", false);
  60. else
  61. @unlink("$dirName/$item");
  62. }
  63. }
  64. closedir($handle);
  65. if (!$subdir) @rmdir($dirName);
  66. }
  67. return true;
  68. }
  69. /**
  70. * 替换域名
  71. * @param string $url
  72. * @return mixed
  73. */
  74. public function replaceSiteUrl(string $url)
  75. {
  76. $siteUrl = sys_config('site_url');
  77. $siteUrlJosn = str_replace('://', ':\\\/\\\/', $siteUrl);
  78. $valueJosn = str_replace('://', ':\\\/\\\/', $url);
  79. $prefix = Config::get('database.connections.' . Config::get('database.default') . '.prefix');
  80. $sql = [
  81. "UPDATE `{$prefix}system_attachment` SET `att_dir` = replace(att_dir ,'{$siteUrl}','{$url}'),`satt_dir` = replace(satt_dir ,'{$siteUrl}','{$url}')",
  82. "UPDATE `{$prefix}store_product` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`slider_image` = replace(slider_image ,'{$siteUrlJosn}','{$valueJosn}')",
  83. "UPDATE `{$prefix}store_product_attr_value` SET `image` = replace(image ,'{$siteUrl}','{$url}')",
  84. "UPDATE `{$prefix}store_seckill` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJosn}','{$valueJosn}')",
  85. "UPDATE `{$prefix}store_combination` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJosn}','{$valueJosn}')",
  86. "UPDATE `{$prefix}store_bargain` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJosn}','{$valueJosn}')",
  87. "UPDATE `{$prefix}system_config` SET `value` = replace(value ,'{$siteUrlJosn}','{$valueJosn}')",
  88. "UPDATE `{$prefix}article_category` SET `image` = replace(`image` ,'{$siteUrl}','{$url}')",
  89. "UPDATE `{$prefix}article` SET `image_input` = replace(`image_input` ,'{$siteUrl}','{$url}')",
  90. "UPDATE `{$prefix}article_content` SET `content` = replace(`content` ,'{$siteUrl}','{$url}')",
  91. "UPDATE `{$prefix}store_product_category` SET `pic` = replace(`pic` ,'{$siteUrl}','{$url}')",
  92. "UPDATE `{$prefix}system_group_data` SET `value` = replace(value ,'{$siteUrlJosn}','{$valueJosn}')",
  93. "UPDATE `{$prefix}store_product_description` SET `description`= replace(description,'{$siteUrl}','{$url}')"
  94. ];
  95. return $this->transaction(function () use ($sql) {
  96. try {
  97. foreach ($sql as $item) {
  98. Db::execute($item);
  99. }
  100. } catch (\Throwable $e) {
  101. throw new AdminException('替换失败,失败原因:' . $e->getMessage());
  102. }
  103. });
  104. }
  105. }