SystemCleardata.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. $this->delDirAndFile('./public/uploads/store/comment');
  52. self::clearData('store_product_relation', 1);
  53. return Json::successful('清除数据成功!');
  54. }
  55. //清除商城数据
  56. public function storeData()
  57. {
  58. self::clearData('store_coupon', 1);
  59. self::clearData('store_coupon_issue', 1);
  60. self::clearData('store_bargain', 1);
  61. self::clearData('store_combination', 1);
  62. self::clearData('store_product_attr', 1);
  63. self::clearData('store_product_attr_result', 1);
  64. self::clearData('store_product_attr_value', 1);
  65. self::clearData('store_product_description', 1);
  66. self::clearData('store_product_reply', 1);
  67. self::clearData('store_seckill', 1);
  68. self::clearData('store_product', 1);
  69. self::clearData('store_visit', 1);
  70. return Json::successful('清除数据成功!');
  71. }
  72. //清除产品分类
  73. public function categoryData()
  74. {
  75. self::clearData('store_category', 1);
  76. self::clearData('store_product_cate', 1);
  77. return Json::successful('清除数据成功!');
  78. }
  79. //清除订单数据
  80. public function orderData()
  81. {
  82. self::clearData('store_order', 1);
  83. self::clearData('store_order_cart_info', 1);
  84. self::clearData('store_order_status', 1);
  85. self::clearData('store_pink', 1);
  86. self::clearData('store_cart', 1);
  87. self::clearData('store_order_status', 1);
  88. return Json::successful('清除数据成功!');
  89. }
  90. //清除客服数据
  91. public function kefuData()
  92. {
  93. self::clearData('store_service', 1);
  94. $this->delDirAndFile('./public/uploads/store/service');
  95. self::clearData('store_service_log', 1);
  96. return Json::successful('清除数据成功!');
  97. }
  98. //清除微信管理数据
  99. public function wechatData()
  100. {
  101. self::clearData('wechat_media', 1);
  102. self::clearData('wechat_reply', 1);
  103. self::clearData('cache', 1);
  104. $this->delDirAndFile('./public/uploads/wechat');
  105. return Json::successful('清除数据成功!');
  106. }
  107. //清除所有附件
  108. public function uploadData()
  109. {
  110. self::clearData('system_attachment', 1);
  111. self::clearData('system_attachment_category', 1);
  112. $this->delDirAndFile('./public/uploads/');
  113. return Json::successful('清除上传文件成功!');
  114. }
  115. //清除微信用户
  116. public function wechatuserData()
  117. {
  118. self::clearData('wechat_user', 1);
  119. self::clearData('user', 1);
  120. return Json::successful('清除数据成功!');
  121. }
  122. //清除内容分类
  123. public function articledata()
  124. {
  125. self::clearData('article_category', 1);
  126. self::clearData('article', 1);
  127. self::clearData('article_content', 1);
  128. return Json::successful('清除数据成功!');
  129. }
  130. //清除系统记录
  131. public function systemdata()
  132. {
  133. self::clearData('system_notice_admin', 1);
  134. self::clearData('system_log', 1);
  135. return Json::successful('清除数据成功!');
  136. }
  137. /**
  138. * 清除数据
  139. * @param int $type
  140. * @throws \Exception
  141. */
  142. public function undata($type = 1)
  143. {
  144. switch ((int)$type) {
  145. case 1:
  146. $fileImage = \app\admin\model\system\SystemAttachment::where('module_type', 2)->field(['att_dir', 'satt_dir'])->select();
  147. foreach ($fileImage as $image) {
  148. if ($image['att_dir'] && ($imagePath = strstr($image['att_dir'], 'uploads')) !== false) {
  149. if (is_file($imagePath))
  150. unlink($imagePath);
  151. unset($imagePath);
  152. }
  153. if ($image['satt_dir'] && ($imagePath = strstr($image['satt_dir'], 'uploads')) !== false) {
  154. if (is_file($imagePath))
  155. unlink($imagePath);
  156. unset($imagePath);
  157. }
  158. }
  159. \app\admin\model\system\SystemAttachment::where('module_type', 2)->delete();
  160. @unlink('uploads/follow/follow.jpg');//删除海报二维码
  161. break;
  162. case 2:
  163. StoreProduct::where('is_del', 1)->delete();
  164. break;
  165. case 3:
  166. $value = $this->request->param('value');
  167. if (!$value)
  168. return Json::fail('请输入需要更换的域名');
  169. if (!verify_domain($value))
  170. return Json::fail('域名不合法');
  171. $siteUrl = SystemConfigService::get('site_url', true);
  172. $siteUrlJosn = str_replace('http://', 'http:\\\/\\\/', $siteUrl);
  173. $valueJosn = str_replace('http://', 'http:\\\/\\\/', $value);
  174. $prefix = Config::get('database.connections.' . Config::get('database.default') . '.prefix');
  175. $sql = [
  176. "UPDATE `{$prefix}system_attachment` SET `att_dir` = replace(`att_dir` ,'{$siteUrl}','{$value}'),`satt_dir` = replace(`satt_dir` ,'{$siteUrl}','{$value}')",
  177. "UPDATE `{$prefix}store_product` SET `image` = replace(`image` ,'{$siteUrl}','{$value}'),`slider_image` = replace(`slider_image` ,'{$siteUrlJosn}','{$valueJosn}')",
  178. "UPDATE `{$prefix}store_product_attr_value` SET `image` = replace(`image` ,'{$siteUrl}','{$value}')",
  179. "UPDATE `{$prefix}store_seckill` SET `image` = replace(`image` ,'{$siteUrl}','{$value}'),`images` = replace(`images`,'{$siteUrlJosn}','{$valueJosn}')",
  180. "UPDATE `{$prefix}store_combination` SET `image` = replace(`image` ,'{$siteUrl}','{$value}'),`images` = replace(`images`,'{$siteUrlJosn}','{$valueJosn}')",
  181. "UPDATE `{$prefix}store_bargain` SET `image` = replace(`image` ,'{$siteUrl}','{$value}'),`images` = replace(`images`,'{$siteUrlJosn}','{$valueJosn}')",
  182. "UPDATE `{$prefix}system_config` SET `value` = replace(`value` ,'{$siteUrlJosn}','{$valueJosn}')",
  183. "UPDATE `{$prefix}article_category` SET `image` = replace(`image` ,'{$siteUrl}','{$value}')",
  184. "UPDATE `{$prefix}article` SET `image_input` = replace(`image_input` ,'{$siteUrl}','{$value}')",
  185. "UPDATE `{$prefix}article_content` SET `content` = replace(`content` ,'{$siteUrl}','{$value}')",
  186. "UPDATE `{$prefix}store_category` SET `pic` = replace(`pic` ,'{$siteUrl}','{$value}')",
  187. "UPDATE `{$prefix}system_group_data` SET `value` = replace(`value` ,'{$siteUrlJosn}','{$valueJosn}')",
  188. "UPDATE `{$prefix}store_product_description` SET `description`= replace(`description`,'{$siteUrl}','{$value}')"
  189. ];
  190. try {
  191. foreach ($sql as $item) {
  192. db::execute($item);
  193. }
  194. } catch (\Throwable $e) {
  195. return Json::fail('替换失败,失败原因:' . $e->getMessage());
  196. }
  197. return Json::success('替换成功!');
  198. break;
  199. }
  200. return Json::successful('清除数据成功!');
  201. }
  202. //清除制定表数据
  203. public function clearData($table_name, $status)
  204. {
  205. $table_name = config('database.connections.' . config('database.default'))['prefix'] . $table_name;
  206. if ($status) {
  207. @db::execute('TRUNCATE TABLE ' . $table_name);
  208. } else {
  209. @db::execute('DELETE FROM' . $table_name);
  210. }
  211. }
  212. //递归删除文件
  213. function delDirAndFile($dirName, $subdir = true)
  214. {
  215. if ($handle = @opendir("$dirName")) {
  216. while (false !== ($item = readdir($handle))) {
  217. if ($item != "." && $item != "..") {
  218. if (is_dir("$dirName/$item"))
  219. $this->delDirAndFile("$dirName/$item", false);
  220. else
  221. @unlink("$dirName/$item");
  222. }
  223. }
  224. closedir($handle);
  225. if (!$subdir) @rmdir($dirName);
  226. }
  227. }
  228. }