ClearMerchantData.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\command;
  12. use think\console\Command;
  13. use think\console\Input;
  14. use think\console\Output;
  15. use think\facade\Db;
  16. class ClearMerchantData extends Command
  17. {
  18. protected function configure()
  19. {
  20. // 指令配置
  21. $this->setName('clear:merchant')
  22. ->setDescription('清空数据(除系统配置以外的所有数据)');
  23. }
  24. /**
  25. * 清除商城数据
  26. * @param Input $input
  27. * @param Output $output
  28. * @return int|void|null
  29. * @throws \think\db\exception\DbException
  30. * @author wuhaotian
  31. * @email 442384644@qq.com
  32. * @date 2024/7/12
  33. */
  34. protected function execute(Input $input, Output $output)
  35. {
  36. $flag = $output->confirm($input, '清空数据前务必做好数据库的备份,防止数据被误删 !!!', false);
  37. if (!$flag) return;
  38. $tables = Db::query('SHOW TABLES FROM ' . env('database.database', ''));
  39. $pre = env('database.prefix', '');
  40. $bakTables = [
  41. $pre . 'page_link',
  42. $pre . 'page_category',
  43. $pre . 'diy',
  44. $pre . 'city_area',
  45. $pre . 'express',
  46. $pre . 'system_admin',
  47. $pre . 'system_city',
  48. $pre . 'system_config',
  49. $pre . 'system_config_classify',
  50. $pre . 'system_config_value',
  51. $pre . 'system_group',
  52. $pre . 'system_group_data',
  53. $pre . 'system_menu',
  54. $pre . 'system_role',
  55. $pre . 'template_message',
  56. $pre . 'system_notice_config',
  57. $pre . 'cache',
  58. ];
  59. foreach ($tables as $table) {
  60. $name = array_values($table)[0];
  61. if (!in_array($name, $bakTables)) {
  62. Db::table($name)->delete(true);
  63. }
  64. }
  65. Db::table( $pre . 'cache')->whereNotIn('key','copyright_context,copyright_image,copyright_status')->delete(true);
  66. $output->info('删除成功');
  67. }
  68. }