SystemClearData.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace app\adminapi\controller\v1\system;
  3. use app\adminapi\controller\AuthController;
  4. use app\models\store\StoreProduct;
  5. use app\models\system\SystemAttachment;
  6. use crmeb\services\UtilService;
  7. use think\facade\Db;
  8. use think\facade\Config;
  9. /**
  10. * 清除默认数据理控制器
  11. * Class SystemClearData
  12. * @package app\admin\controller\system
  13. */
  14. class SystemClearData extends AuthController
  15. {
  16. /**
  17. * 统一方法
  18. * @param $type
  19. */
  20. public function index($type)
  21. {
  22. switch ($type) {
  23. case 'temp':
  24. return $this->userTemp();
  25. break;
  26. case 'recycle':
  27. return $this->recycleProduct();
  28. break;
  29. case 'user':
  30. return $this->userRelevantData();
  31. break;
  32. case 'store':
  33. return $this->storeData();
  34. break;
  35. case 'category':
  36. return $this->categoryData();
  37. break;
  38. case 'order':
  39. return $this->orderData();
  40. break;
  41. case 'kefu':
  42. return $this->kefuData();
  43. break;
  44. case 'wechat':
  45. return $this->wechatData();
  46. break;
  47. case 'attachment':
  48. return $this->attachmentData();
  49. break;
  50. case 'wechatuser':
  51. return $this->wechatuserData();
  52. break;
  53. case 'article':
  54. return $this->articledata();
  55. break;
  56. case 'system':
  57. return $this->systemdata();
  58. break;
  59. default:
  60. return $this->fail('参数有误');
  61. }
  62. }
  63. /**
  64. * 清除用户生成的临时附件
  65. * @param int $type
  66. * @throws \Exception
  67. */
  68. public function userTemp()
  69. {
  70. SystemAttachment::where('module_type', 2)->delete();
  71. return $this->success('清除数据成功!');
  72. }
  73. //清除回收站商品
  74. public function recycleProduct()
  75. {
  76. StoreProduct::where('is_del', 1)->delete();
  77. return $this->success('清除数据成功!');
  78. }
  79. //清除用户数据
  80. public function userRelevantData()
  81. {
  82. self::clearData('user_recharge', 1);
  83. self::clearData('user_address', 1);
  84. self::clearData('user_bill', 1);
  85. self::clearData('user_enter', 1);
  86. self::clearData('user_extract', 1);
  87. self::clearData('user_notice', 1);
  88. self::clearData('user_notice_see', 1);
  89. self::clearData('wechat_qrcode', 1);
  90. self::clearData('wechat_message', 1);
  91. self::clearData('store_visit', 1);
  92. self::clearData('store_coupon_user', 1);
  93. self::clearData('store_coupon_issue_user', 1);
  94. self::clearData('store_bargain_user', 1);
  95. self::clearData('store_bargain_user_help', 1);
  96. self::clearData('store_product_reply', 1);
  97. self::clearData('store_product_cate', 1);
  98. self::clearData('routine_qrcode', 1);
  99. self::clearData('routine_form_id', 1);
  100. self::clearData('user_sign', 1);
  101. self::clearData('user_task_finish', 1);
  102. self::clearData('user_level', 1);
  103. self::clearData('user_token', 1);
  104. self::clearData('user_group', 1);
  105. self::clearData('user_visit', 1);
  106. $this->delDirAndFile('./public/uploads/store/comment');
  107. self::clearData('store_product_relation', 1);
  108. return $this->success('清除数据成功!');
  109. }
  110. //清除商城数据
  111. public function storeData()
  112. {
  113. self::clearData('store_coupon', 1);
  114. self::clearData('store_coupon_issue', 1);
  115. self::clearData('store_bargain', 1);
  116. self::clearData('store_combination', 1);
  117. self::clearData('store_product_attr', 1);
  118. self::clearData('store_product_cate', 1);
  119. self::clearData('store_product_attr_result', 1);
  120. self::clearData('store_product_attr_value', 1);
  121. self::clearData('store_product_description', 1);
  122. self::clearData('store_product_rule', 1);
  123. self::clearData('store_seckill', 1);
  124. self::clearData('store_product', 1);
  125. self::clearData('store_visit', 1);
  126. return $this->success('清除数据成功!');
  127. }
  128. //清除商品分类
  129. public function categoryData()
  130. {
  131. self::clearData('store_category', 1);
  132. return $this->success('清除数据成功!');
  133. }
  134. //清除订单数据
  135. public function orderData()
  136. {
  137. self::clearData('store_order', 1);
  138. self::clearData('store_order_cart_info', 1);
  139. self::clearData('store_order_status', 1);
  140. self::clearData('store_pink', 1);
  141. self::clearData('store_cart', 1);
  142. self::clearData('store_order_status', 1);
  143. return $this->success('清除数据成功!');
  144. }
  145. //清除客服数据
  146. public function kefuData()
  147. {
  148. self::clearData('store_service', 1);
  149. $this->delDirAndFile('./public/uploads/store/service');
  150. self::clearData('store_service_log', 1);
  151. return $this->success('清除数据成功!');
  152. }
  153. //清除微信管理数据
  154. public function wechatData()
  155. {
  156. self::clearData('wechat_media', 1);
  157. self::clearData('wechat_reply', 1);
  158. self::clearData('cache', 1);
  159. $this->delDirAndFile('./public/uploads/wechat');
  160. return $this->success('清除数据成功!');
  161. }
  162. //清除所有附件
  163. public function attachmentData()
  164. {
  165. self::clearData('system_attachment', 1);
  166. self::clearData('system_attachment_category', 1);
  167. $this->delDirAndFile('./public/uploads/');
  168. return $this->success('清除上传文件成功!');
  169. }
  170. //清除微信用户
  171. public function wechatuserData()
  172. {
  173. self::clearData('wechat_user', 1);
  174. self::clearData('user', 1);
  175. return $this->success('清除数据成功!');
  176. }
  177. //清除内容分类
  178. public function articledata()
  179. {
  180. self::clearData('article_category', 1);
  181. self::clearData('article', 1);
  182. self::clearData('article_content', 1);
  183. return $this->success('清除数据成功!');
  184. }
  185. //清除系统记录
  186. public function systemdata()
  187. {
  188. self::clearData('system_notice_admin', 1);
  189. self::clearData('system_log', 1);
  190. return $this->success('清除数据成功!');
  191. }
  192. //清除制定表数据
  193. public function clearData($table_name, $status)
  194. {
  195. $table_name = config('database.connections.' . config('database.default'))['prefix'] . $table_name;
  196. if ($status) {
  197. @db::execute('TRUNCATE TABLE ' . $table_name);
  198. } else {
  199. @db::execute('DELETE FROM' . $table_name);
  200. }
  201. }
  202. //递归删除文件
  203. function delDirAndFile($dirName, $subdir = true)
  204. {
  205. if ($handle = @opendir("$dirName")) {
  206. while (false !== ($item = readdir($handle))) {
  207. if ($item != "." && $item != "..") {
  208. if (is_dir("$dirName/$item"))
  209. $this->delDirAndFile("$dirName/$item", false);
  210. else
  211. @unlink("$dirName/$item");
  212. }
  213. }
  214. closedir($handle);
  215. if (!$subdir) @rmdir($dirName);
  216. }
  217. }
  218. /**
  219. * 替换域名方法
  220. * @return mixed
  221. */
  222. public function replaceSiteUrl()
  223. {
  224. list($url) = UtilService::postMore([
  225. ['url','']
  226. ], $this->request, true);
  227. if (!$url)
  228. return $this->fail('请输入需要更换的域名');
  229. if (!verify_domain($url))
  230. return $this->fail('域名不合法');
  231. $siteUrl = sys_config('site_url');
  232. $siteUrlJosn = str_replace('http://', 'http:\\/\\/', $siteUrl);
  233. $valueJosn = str_replace('http://', 'http:\\/\\/', $url);
  234. $prefix = Config::get('database.connections.' . Config::get('database.default') . '.prefix');
  235. $sql = [
  236. "UPDATE `{$prefix}system_attachment` SET `att_dir` = replace(att_dir ,'{$siteUrl}','{$url}'),`satt_dir` = replace(satt_dir ,'{$siteUrl}','{$url}')",
  237. "UPDATE `{$prefix}store_product` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`slider_image` = replace(slider_image ,'{$siteUrl}','{$url}')",
  238. "UPDATE `{$prefix}store_product_attr_value` SET `image` = replace(image ,'{$siteUrl}','{$url}')",
  239. "UPDATE `{$prefix}store_seckill` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrl}','{$url}')",
  240. "UPDATE `{$prefix}store_combination` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrl}','{$url}')",
  241. "UPDATE `{$prefix}store_bargain` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrl}','{$url}')",
  242. "UPDATE `{$prefix}system_config` SET `value` = replace(value ,'{$siteUrlJosn}','{$valueJosn}')",
  243. "UPDATE `{$prefix}article_category` SET `image` = replace(`image` ,'{$siteUrl}','{$url}')",
  244. "UPDATE `{$prefix}article` SET `image_input` = replace(`image_input` ,'{$siteUrl}','{$url}')",
  245. "UPDATE `{$prefix}article_content` SET `content` = replace(`content` ,'{$siteUrl}','{$url}')",
  246. "UPDATE `{$prefix}store_category` SET `pic` = replace(`pic` ,'{$siteUrl}','{$url}')",
  247. "UPDATE `{$prefix}system_group_data` SET `value` = replace(value ,'{$siteUrlJosn}','{$valueJosn}')",
  248. "UPDATE `{$prefix}store_product_description` SET `description`= replace(description,'{$siteUrl}','{$url}')"
  249. ];
  250. try {
  251. foreach ($sql as $item) {
  252. db::execute($item);
  253. }
  254. } catch (\Throwable $e) {
  255. return $this->fail('替换失败,失败原因:' . $e->getMessage());
  256. }
  257. return $this->success('替换成功!');
  258. }
  259. }