SystemCleardata.php 9.6 KB

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