SystemCleardata.php 12 KB

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