// +---------------------------------------------------------------------- namespace app\controller\admin\system; use app\common\AdminBaseController; use app\Request; use app\services\system\SystemClearServices; use qiniu\services\CacheService; use think\db\exception\DataNotFoundException; use think\db\exception\DbException; use think\db\exception\ModelNotFoundException; use think\exception\ValidateException; use think\facade\App; use app\services\system\attachment\SystemAttachmentServices; /** * 清除默认数据理控制器 * Class SystemClearData * @package app\controller\admin\v1\system */ class SystemClearData extends AdminBaseController { /** * 构造方法 * SystemClearData constructor. * @param App $app * @param SystemClearServices $services */ public function __construct(Request $request, SystemClearServices $services) { parent::__construct($request); $this->service = $services; //生产模式下不允许清除数据 if (!env('APP_DEBUG', false)) { throw new ValidateException('生产模式下,禁止操作'); } } /** * 清除方法入口 * @param $type * @return mixed * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function clear($type) { switch ($type) { case 'temp': return $this->userTemp(); case 'attachment': return $this->attachmentData(); case 'article': return $this->articleData(); case 'system': return $this->systemData(); case 'user': return $this->userRelevantData(); default: return $this->error('参数有误'); } } /** * 清除用户生成的临时文件 * @return mixed * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function userTemp() { /** @var SystemAttachmentServices $services */ $services = app()->make(SystemAttachmentServices::class); $ids = implode(',', $services->getColumn(['module_type' => 2], 'att_id')); $services->del($ids); $services->delete(2, 'module_type'); return $this->success('清除数据成功!'); } /** * 清除用户数据 * @return mixed */ public function userRelevantData() { $this->service->clearData([ 'user_money', 'user_brokerage', 'user_bill', 'user', 'user_spread', 'user_group', 'user_address' ], true); $this->service->delDirAndFile('./public/uploads/store/comment'); return $this->success('清除数据成功!'); } /** * 清除所有附件 * @return mixed */ public function attachmentData() { $this->service->clearData([ 'system_attachment', 'system_attachment_category' ], true); $this->service->delDirAndFile('./public/uploads/'); return $this->success('清除上传文件成功!'); } /** * 清楚内容数据 * @return mixed */ public function articleData() { $this->service->clearData([ 'article_category', 'article', 'article_content' ], true); return $this->success('清除数据成功!'); } /** * 清楚系统记录 * @return mixed */ public function systemData() { $this->service->clearData([ 'system_log' ], true); return $this->success('清除数据成功!'); } /** * 替换域名方法 * @return mixed */ public function replaceSiteUrl() { [$url] = $this->request->postMore([ ['url', ''] ], true); if (!$url) return $this->error('请输入需要更换的域名'); $url = verify_domain($url); if (!$url) return $this->error('域名不合法'); $this->service->replaceSiteUrl($url); return $this->success('替换成功!'); } }