Util.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. $this->import($filePath);
  56. break;
  57. }
  58. $output->info('执行成功');
  59. }
  60. protected function replaceSiteUrl(string $url, string $siteUrl)
  61. {
  62. $siteUrlJosn = str_replace('http://', 'http:\\\/\\\/', $siteUrl);
  63. $siteUrlJosn = str_replace('https://', 'https:\\\/\\\/', $siteUrlJosn);
  64. $valueJosn = str_replace('http://', 'http:\\\/\\\/', $url);
  65. $valueJosn = str_replace('https://', 'https:\\\/\\\/', $valueJosn);
  66. $prefix = Config::get('database.connections.' . Config::get('database.default') . '.prefix');
  67. $sql = [
  68. "UPDATE `{$prefix}article` SET `image_input` = replace(`image_input` ,'{$siteUrl}','{$url}')",
  69. "UPDATE `{$prefix}article_category` SET `image` = replace(`image` ,'{$siteUrl}','{$url}')",
  70. "UPDATE `{$prefix}article_content` SET `content` = replace(`content` ,'{$siteUrl}','{$url}')",
  71. "UPDATE `{$prefix}diy` SET `value` = replace(value ,'{$siteUrlJosn}','{$valueJosn}'),`default_value` = replace(default_value ,'{$siteUrlJosn}','{$valueJosn}')",
  72. "UPDATE `{$prefix}luck_lottery` SET `image` = replace(image ,'{$siteUrlJosn}','{$valueJosn}')",
  73. "UPDATE `{$prefix}luck_prize` SET `image` = replace(image ,'{$siteUrlJosn}','{$valueJosn}')",
  74. "UPDATE `{$prefix}store_bargain` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJosn}','{$valueJosn}')",
  75. "UPDATE `{$prefix}store_category` SET `pic` = replace(`pic` ,'{$siteUrl}','{$url}'),`big_pic` = replace(`big_pic` ,'{$siteUrl}','{$url}')",
  76. "UPDATE `{$prefix}store_combination` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJosn}','{$valueJosn}')",
  77. "UPDATE `{$prefix}store_integral` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJosn}','{$valueJosn}')",
  78. "UPDATE `{$prefix}store_product` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`slider_image` = replace(slider_image ,'{$siteUrlJosn}','{$valueJosn}'),`recommend_image` = replace(recommend_image ,'{$siteUrlJosn}','{$valueJosn}')",
  79. "UPDATE `{$prefix}store_product_attr_result` SET `result` = replace(result ,'{$siteUrl}','{$url}')",
  80. "UPDATE `{$prefix}store_product_attr_value` SET `image` = replace(image ,'{$siteUrl}','{$url}')",
  81. "UPDATE `{$prefix}store_product_description` SET `description`= replace(description,'{$siteUrl}','{$url}')",
  82. "UPDATE `{$prefix}store_seckill` SET `image` = replace(image ,'{$siteUrl}','{$url}'),`images` = replace(images,'{$siteUrlJosn}','{$valueJosn}')",
  83. "UPDATE `{$prefix}system_attachment` SET `att_dir` = replace(att_dir ,'{$siteUrl}','{$url}'),`satt_dir` = replace(satt_dir ,'{$siteUrl}','{$url}')",
  84. "UPDATE `{$prefix}system_config` SET `value` = replace(value ,'{$siteUrlJosn}','{$valueJosn}')",
  85. "UPDATE `{$prefix}system_group_data` SET `value` = replace(value ,'{$siteUrlJosn}','{$valueJosn}')",
  86. "UPDATE `{$prefix}user` SET `avatar` = replace(avatar ,'{$siteUrlJosn}','{$valueJosn}')",
  87. ];
  88. return Db::transaction(function () use ($sql) {
  89. try {
  90. foreach ($sql as $item) {
  91. Db::execute($item);
  92. }
  93. } catch (\Throwable $e) {
  94. throw new AdminException(400612, ['msg' => $e->getMessage()]);
  95. }
  96. });
  97. }
  98. /**
  99. * 导入数据
  100. * @param string $filePath
  101. * @return mixed
  102. * @author 等风来
  103. * @email 136327134@qq.com
  104. * @date 2023/4/26
  105. */
  106. protected function import(string $filePath)
  107. {
  108. $preg = '/\{+[a-zA-Z0-9]+\}/';
  109. $res = file_get_contents(app()->getRootPath() . $filePath);
  110. $data = json_decode($res, true)['apiCollection'][0]['items'];
  111. $route = [];
  112. foreach ($data as $item) {
  113. foreach ($item['items'] as $value) {
  114. if (isset($value['api'])) {
  115. $path = str_replace('//', '/', str_replace('{}', '', $value['api']['path']));
  116. $paramePath = $this->getPathValue($value['api']['parameters']['path'] ?? []);
  117. if (strstr($path, ':') !== false) {
  118. $path = str_replace(':', '', $path);
  119. }
  120. if (preg_match_all($preg, $path, $matches)) {
  121. $paramePathMatche = [];
  122. if (isset($matches[0]) && $matches[0]) {
  123. foreach ($matches[0] as $v) {
  124. $paramePathMatche[] = str_replace(['{', '}'], ['<', '>'], $v);
  125. }
  126. }
  127. if ($paramePathMatche) {
  128. $paramePath = implode('/', $paramePathMatche);
  129. }
  130. }
  131. if ($path[0] === '/') {
  132. $path = substr($path, 1);
  133. }
  134. $route[] = [
  135. 'method' => strtoupper($value['api']['method']),
  136. 'path' => $path . $paramePath,
  137. 'request_type' => $value['api']['requestBody']['type'],
  138. 'request' => $this->getRequest($value['api']['requestBody']['parameters'] ?? []),
  139. 'response' => $this->getResponse($value['api']['responses'][0]['jsonSchema']['properties'] ?? []),
  140. ];
  141. }
  142. }
  143. }
  144. return app()->make(SystemRouteServices::class)->importData($route);
  145. }
  146. /**
  147. * 获取请求返回数据
  148. * @param array $options
  149. * @param string $parentId
  150. * @return array
  151. * @author 等风来
  152. * @email 136327134@qq.com
  153. * @date 2023/4/26
  154. */
  155. protected function getResponse(array $options, $parentId = '')
  156. {
  157. $response = [];
  158. foreach ($options as $key => $option) {
  159. $id = uniqid();
  160. $response[] = [
  161. 'attribute' => $key,
  162. 'type' => $option['type'],
  163. 'trip' => '',
  164. 'id' => $id,
  165. 'parentId' => $parentId,
  166. ];
  167. if (isset($option['properties'])) {
  168. $response = array_merge($response, $this->getResponse($option['properties'], $id));
  169. }
  170. }
  171. return $response;
  172. }
  173. /**
  174. * 获取请求数据
  175. * @param array $options
  176. * @return array
  177. * @author 等风来
  178. * @email 136327134@qq.com
  179. * @date 2023/4/26
  180. */
  181. protected function getRequest(array $options)
  182. {
  183. $request = [];
  184. foreach ($options as $option) {
  185. $request[] = [
  186. 'attribute' => $option['name'],
  187. 'type' => $option['type'] === 'text' ? 'string' : $option['type'],
  188. 'must' => 0,
  189. 'trip' => $option['description'],
  190. 'id' => $option['id'],
  191. ];
  192. }
  193. return $request;
  194. }
  195. /**
  196. * 处理path路径
  197. * @param array $options
  198. * @return string
  199. * @author 等风来
  200. * @email 136327134@qq.com
  201. * @date 2023/4/26
  202. */
  203. protected function getPathValue(array $options)
  204. {
  205. $path = [];
  206. foreach ($options as $option) {
  207. if (strstr($option['name'], '?') !== false) {
  208. $option['name'] = str_replace('?', '', $option['name']) . '?';
  209. }
  210. $path[] = '<' . $option['name'] . '>';
  211. }
  212. return implode('/', $path);
  213. }
  214. }