SystemClearData.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\controller\admin\system;
  12. use app\common\AdminBaseController;
  13. use app\Request;
  14. use app\services\system\SystemClearServices;
  15. use qiniu\services\CacheService;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use think\exception\ValidateException;
  20. use think\facade\App;
  21. use app\services\system\attachment\SystemAttachmentServices;
  22. /**
  23. * 清除默认数据理控制器
  24. * Class SystemClearData
  25. * @package app\controller\admin\v1\system
  26. */
  27. class SystemClearData extends AdminBaseController
  28. {
  29. /**
  30. * 构造方法
  31. * SystemClearData constructor.
  32. * @param App $app
  33. * @param SystemClearServices $services
  34. */
  35. public function __construct(Request $request, SystemClearServices $services)
  36. {
  37. parent::__construct($request);
  38. $this->service = $services;
  39. //生产模式下不允许清除数据
  40. if (!env('APP_DEBUG', false)) {
  41. throw new ValidateException('生产模式下,禁止操作');
  42. }
  43. }
  44. /**
  45. * 清除方法入口
  46. * @param $type
  47. * @return mixed
  48. * @throws DataNotFoundException
  49. * @throws DbException
  50. * @throws ModelNotFoundException
  51. */
  52. public function clear($type)
  53. {
  54. switch ($type) {
  55. case 'temp':
  56. return $this->userTemp();
  57. case 'attachment':
  58. return $this->attachmentData();
  59. case 'article':
  60. return $this->articleData();
  61. case 'system':
  62. return $this->systemData();
  63. case 'user':
  64. return $this->userRelevantData();
  65. default:
  66. return $this->error('参数有误');
  67. }
  68. }
  69. /**
  70. * 清除用户生成的临时文件
  71. * @return mixed
  72. * @throws DataNotFoundException
  73. * @throws DbException
  74. * @throws ModelNotFoundException
  75. */
  76. public function userTemp()
  77. {
  78. /** @var SystemAttachmentServices $services */
  79. $services = app()->make(SystemAttachmentServices::class);
  80. $ids = implode(',', $services->getColumn(['module_type' => 2], 'att_id'));
  81. $services->del($ids);
  82. $services->delete(2, 'module_type');
  83. return $this->success('清除数据成功!');
  84. }
  85. /**
  86. * 清除用户数据
  87. * @return mixed
  88. */
  89. public function userRelevantData()
  90. {
  91. $this->service->clearData([
  92. 'user_money', 'user_brokerage', 'user_bill', 'user', 'user_spread', 'user_group', 'user_address'
  93. ], true);
  94. $this->service->delDirAndFile('./public/uploads/store/comment');
  95. return $this->success('清除数据成功!');
  96. }
  97. /**
  98. * 清除所有附件
  99. * @return mixed
  100. */
  101. public function attachmentData()
  102. {
  103. $this->service->clearData([
  104. 'system_attachment', 'system_attachment_category'
  105. ], true);
  106. $this->service->delDirAndFile('./public/uploads/');
  107. return $this->success('清除上传文件成功!');
  108. }
  109. /**
  110. * 清楚内容数据
  111. * @return mixed
  112. */
  113. public function articleData()
  114. {
  115. $this->service->clearData([
  116. 'article_category', 'article', 'article_content'
  117. ], true);
  118. return $this->success('清除数据成功!');
  119. }
  120. /**
  121. * 清楚系统记录
  122. * @return mixed
  123. */
  124. public function systemData()
  125. {
  126. $this->service->clearData([
  127. 'system_log'
  128. ], true);
  129. return $this->success('清除数据成功!');
  130. }
  131. /**
  132. * 替换域名方法
  133. * @return mixed
  134. */
  135. public function replaceSiteUrl()
  136. {
  137. [$url] = $this->request->postMore([
  138. ['url', '']
  139. ], true);
  140. if (!$url)
  141. return $this->error('请输入需要更换的域名');
  142. $url = verify_domain($url);
  143. if (!$url)
  144. return $this->error('域名不合法');
  145. $this->service->replaceSiteUrl($url);
  146. return $this->success('替换成功!');
  147. }
  148. }