Util.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace crmeb\command;
  3. use app\services\system\log\SystemFileInfoServices;
  4. use app\services\system\SystemRouteServices;
  5. use crmeb\exceptions\AdminException;
  6. use think\console\Command;
  7. use think\console\Input;
  8. use think\console\input\Argument;
  9. use think\console\input\Option;
  10. use think\console\Output;
  11. use think\facade\Config;
  12. use think\facade\Db;
  13. class Util extends Command
  14. {
  15. protected function configure()
  16. {
  17. $this->setName('util')
  18. ->addArgument('type', Argument::REQUIRED, '类型replace/route/file/apifox')
  19. ->addOption('h', null, Option::VALUE_REQUIRED, '替换成当前域名')
  20. ->addOption('u', null, Option::VALUE_REQUIRED, '替换的域名')
  21. ->addOption('a', null, Option::VALUE_REQUIRED, '应用名')
  22. ->addOption('f', null, Option::VALUE_REQUIRED, '导入文件路径,文件只能在项目根目录下或者根目录下的其他文件夹内')
  23. ->setDescription('工具类');
  24. }
  25. protected function execute(Input $input, Output $output)
  26. {
  27. $type = $input->getArgument('type');
  28. switch ($type) {
  29. case 'replace':
  30. $host = $input->getOption('h');
  31. $url = $input->getOption('u');
  32. if (!$host) {
  33. return $output->error('缺少替换域名');
  34. }
  35. if (!$url) {
  36. return $output->error('缺少替换的域名');
  37. }
  38. $this->replaceSiteUrl($url, $host);
  39. break;
  40. case 'route':
  41. $appName = $input->getOption('a');
  42. if (!$appName) {
  43. return $output->error('缺少应用名称');
  44. }
  45. app()->make(SystemRouteServices::class)->syncRoute($appName);
  46. break;
  47. case 'file':
  48. app()->make(SystemFileInfoServices::class)->syncfile();
  49. break;
  50. case 'apifox':
  51. $filePath = $input->getOption('f');
  52. if (!$filePath) {
  53. return $output->error('缺少导入文件地址');
  54. }
  55. app()->make(SystemRouteServices::class)->import($filePath);
  56. break;
  57. }
  58. $output->info('执行成功');
  59. }
  60. protected function replaceSiteUrl(string $url, string $siteUrl)
  61. {
  62. // 解析站点 URL 的协议
  63. $siteUrlScheme = parse_url($siteUrl)['scheme'];
  64. // 将站点 URL 中的协议替换为 JSON 格式
  65. $siteUrlJson = str_replace($siteUrlScheme . '://', $siteUrlScheme . ':\\\/\\\/', $siteUrl);
  66. // 获取当前 URL 的协议
  67. $urlScheme = parse_url($url)['scheme'];
  68. // 将当前 URL 中的协议替换为 JSON 格式
  69. $urlJson = str_replace($urlScheme . '://', $urlScheme . ':\\\/\\\/', $url);
  70. // 获取数据库表前缀
  71. $prefix = Config::get('database.connections.' . Config::get('database.default') . '.prefix');
  72. // 构建 SQL 语句数组
  73. $sql = [
  74. "UPDATE `{$prefix}agent_level` SET `image` = replace(`image` ,'{$siteUrl}','{$url}')",
  75. "UPDATE `{$prefix}agreement` SET `content` = replace(content ,'{$siteUrl}','{$url}')",
  76. "UPDATE `{$prefix}article` SET `image_input` = replace(`image_input` ,'{$siteUrl}','{$url}')",
  77. "UPDATE `{$prefix}article_category` SET `image` = replace(`image` ,'{$siteUrl}','{$url}')",
  78. "UPDATE `{$prefix}article_content` SET `content` = replace(`content` ,'{$siteUrl}','{$url}')",
  79. "UPDATE `{$prefix}cache` SET `result` = replace(result ,'{$siteUrlJson}','{$urlJson}')",
  80. "UPDATE `{$prefix}delivery_service` SET `avatar` = replace(`avatar` ,'{$siteUrl}','{$url}')",
  81. "UPDATE `{$prefix}division_agent_apply` SET `images` = replace(images ,'{$siteUrlJson}','{$urlJson}')",
  82. "UPDATE `{$prefix}diy` SET `value` = replace(value ,'{$siteUrlJson}','{$urlJson}'),`default_value` = replace(default_value ,'{$siteUrlJson}','{$urlJson}')",
  83. "UPDATE `{$prefix}live_anchor` SET `cover_img` = replace(`cover_img` ,'{$siteUrl}','{$url}')",
  84. "UPDATE `{$prefix}live_goods` SET `cover_img` = replace(`cover_img` ,'{$siteUrl}','{$url}')",
  85. "UPDATE `{$prefix}live_room` SET `cover_img` = replace(`cover_img` ,'{$siteUrl}','{$url}'),`share_img` = replace(`share_img` ,'{$siteUrl}','{$url}')",
  86. "UPDATE `{$prefix}luck_lottery` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`content` = replace(content ,'{$siteUrl}','{$url}')",
  87. "UPDATE `{$prefix}luck_prize` SET `image` = replace(image ,'{$siteUrl}','{$url}')",
  88. "UPDATE `{$prefix}member_card_batch` SET `qrcode` = replace(qrcode ,'{$siteUrl}','{$url}')",
  89. "UPDATE `{$prefix}member_right` SET `image` = replace(image ,'{$siteUrl}','{$url}')",
  90. "UPDATE `{$prefix}out_interface` SET `return_params` = replace(return_params ,'{$siteUrl}','{$url}'),`request_example` = replace(request_example ,'{$siteUrl}','{$url}'),`return_example` = replace(return_example ,'{$siteUrl}','{$url}')",
  91. "UPDATE `{$prefix}qrcode` SET `url` = replace(url ,'{$siteUrl}','{$url}'),`qrcode_url` = replace(qrcode_url ,'{$siteUrl}','{$url}')",
  92. "UPDATE `{$prefix}store_bargain` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJson}','{$urlJson}')",
  93. "UPDATE `{$prefix}store_category` SET `pic` = replace(`pic` ,'{$siteUrl}','{$url}'),`big_pic` = replace(`big_pic` ,'{$siteUrl}','{$url}')",
  94. "UPDATE `{$prefix}store_combination` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJson}','{$urlJson}')",
  95. "UPDATE `{$prefix}store_integral` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJson}','{$urlJson}')",
  96. "UPDATE `{$prefix}store_integral_order` SET `image` = replace(`image` ,'{$siteUrl}','{$url}')",
  97. "UPDATE `{$prefix}store_order_cart_info` SET `cart_info` = replace(cart_info ,'{$siteUrlJson}','{$urlJson}')",
  98. "UPDATE `{$prefix}store_order_refund` SET `refund_img` = replace(refund_img ,'{$siteUrl}','{$url}'),`cart_info` = replace(cart_info,'{$siteUrlJson}','{$urlJson}')",
  99. "UPDATE `{$prefix}store_pink` SET `avatar` = replace(`avatar` ,'{$siteUrl}','{$url}')",
  100. "UPDATE `{$prefix}store_product` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`slider_image` = replace(slider_image ,'{$siteUrlJson}','{$urlJson}'),`recommend_image` = replace(recommend_image ,'{$siteUrl}','{$url}')",
  101. "UPDATE `{$prefix}store_product_attr_result` SET `result` = replace(result ,'{$siteUrlJson}','{$urlJson}')",
  102. "UPDATE `{$prefix}store_product_attr_value` SET `image` = replace(image ,'{$siteUrl}','{$url}')",
  103. "UPDATE `{$prefix}store_product_description` SET `description`= replace(description,'{$siteUrl}','{$url}')",
  104. "UPDATE `{$prefix}store_product_reply` SET `avatar` = replace(avatar ,'{$siteUrl}','{$url}'),`pics` = replace(pics,'{$siteUrlJson}','{$urlJson}')",
  105. "UPDATE `{$prefix}store_seckill` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJson}','{$urlJson}')",
  106. "UPDATE `{$prefix}store_service` SET `avatar` = replace(avatar ,'{$siteUrl}','{$url}')",
  107. "UPDATE `{$prefix}store_service_log` SET `msn` = replace(msn ,'{$siteUrlJson}','{$urlJson}')",
  108. "UPDATE `{$prefix}store_service_record` SET `avatar` = replace(avatar ,'{$siteUrl}','{$url}'),`message` = replace(message,'{$siteUrlJson}','{$urlJson}')",
  109. "UPDATE `{$prefix}system_admin` SET `head_pic` = replace(head_pic ,'{$siteUrl}','{$url}')",
  110. "UPDATE `{$prefix}system_attachment` SET `att_dir` = replace(att_dir ,'{$siteUrl}','{$url}'),`satt_dir` = replace(satt_dir ,'{$siteUrl}','{$url}')",
  111. "UPDATE `{$prefix}system_config` SET `value` = replace(value ,'{$siteUrl}','{$url}')",
  112. "UPDATE `{$prefix}system_config` SET `value` = replace(value ,'{$siteUrlJson}','{$urlJson}')",
  113. "UPDATE `{$prefix}system_group_data` SET `value` = replace(value ,'{$siteUrlJson}','{$urlJson}')",
  114. "UPDATE `{$prefix}system_store` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`oblong_image` = replace(oblong_image ,'{$siteUrl}','{$url}')",
  115. "UPDATE `{$prefix}system_store_staff` SET `avatar` = replace(avatar ,'{$siteUrl}','{$url}')",
  116. "UPDATE `{$prefix}system_user_level` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`icon` = replace(icon ,'{$siteUrl}','{$url}')",
  117. "UPDATE `{$prefix}user` SET `avatar` = replace(avatar ,'{$siteUrl}','{$url}')",
  118. "UPDATE `{$prefix}user_extract` SET `qrcode_url` = replace(qrcode_url ,'{$siteUrl}','{$url}')",
  119. "UPDATE `{$prefix}wechat_qrcode` SET `image` = replace(image ,'{$siteUrl}','{$url}')",
  120. "UPDATE `{$prefix}wechat_user` SET `headimgurl` = replace(headimgurl ,'{$siteUrl}','{$url}')",
  121. ];
  122. // 执行 SQL 语句
  123. return $this->transaction(function () use ($sql) {
  124. try {
  125. foreach ($sql as $item) {
  126. Db::execute($item);
  127. }
  128. } catch (\Throwable $e) {
  129. throw new AdminException(400612, ['msg' => $e->getMessage()]);
  130. }
  131. });
  132. }
  133. }