ShoproHelp.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. namespace addons\shopro\console;
  3. use think\Db;
  4. use Exception;
  5. use think\console\input\Argument;
  6. use think\console\input\Option;
  7. use app\admin\model\shopro\Admin;
  8. use think\Queue;
  9. class ShoproHelp extends Command
  10. {
  11. protected $input = null;
  12. protected $output = null;
  13. /**
  14. * 支持的命令列表
  15. */
  16. protected $commands = [
  17. ['code' => "0", 'name' => 'cancel', 'desc' => '取消'],
  18. ['code' => "1", 'name' => 'all', 'desc' => 'shopro 帮助工具列表'],
  19. // ['code' => "2", 'name' => 'open_debug', 'desc' => '开启 debug'],
  20. // ['code' => "3", 'name' => 'close_debug', 'desc' => '关闭 debug'],
  21. ['code' => "4", 'name' => 'admin_reset_password', 'desc' => '重置管理员密码'],
  22. ['code' => "5", 'name' => 'admin_clear_login_fail', 'desc' => '清除管理员登录锁定状态'],
  23. // ['code' => "6", 'name' => 'database_notes_md', 'desc' => '生成 markdown 格式的数据库字典'],
  24. // ['code' => "7", 'name' => 'database_notes_pdf', 'desc' => '生成 pdf 格式的数据库字典'],
  25. // ['code' => "8", 'name' => 'update_composer', 'desc' => '更新 composer 包'],
  26. ['code' => "9", 'name' => 'queue', 'desc' => '检查队列状态'],
  27. // ['code' => "10", 'name' => 'clear_cache', 'desc' => '清空缓存']
  28. ];
  29. /**
  30. * 帮助命令配置
  31. */
  32. protected function configure()
  33. {
  34. $this->setName('shopro:help')
  35. ->addArgument('code', Argument::OPTIONAL, "请输入操作编号")
  36. ->setDescription('shopro 帮助命令');
  37. }
  38. /**
  39. * 全部命令集合
  40. */
  41. public function all()
  42. {
  43. $this->output->newLine();
  44. $this->output->writeln("shopro 帮助命令行");
  45. $this->output->writeln('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
  46. $this->output->newLine();
  47. if (!is_dir(RUNTIME_PATH . 'storage')) {
  48. @mkdir(RUNTIME_PATH . 'storage', 0755, true);
  49. }
  50. foreach ($this->commands as $command) {
  51. $this->output->writeln("[" . $command['code'] . "] " . $command['desc']);
  52. }
  53. $this->output->newLine();
  54. $code = $this->output->ask(
  55. $this->input,
  56. '请输入命令代号',
  57. '0'
  58. );
  59. $this->choose($code);
  60. }
  61. /**
  62. * 打开调试模式
  63. */
  64. // public function openDebug()
  65. // {
  66. // $this->setEnvFilePath();
  67. // $this->setEnvVar('APP_DEBUG', 'true');
  68. // $this->output->writeln("debug 开启成功");
  69. // return true;
  70. // }
  71. // /**
  72. // * 关闭调试模式
  73. // */
  74. // public function closeDebug()
  75. // {
  76. // $this->setEnvFilePath();
  77. // $this->setEnvVar('APP_DEBUG', 'false');
  78. // $this->output->writeln("debug 关闭成功");
  79. // return true;
  80. // }
  81. /**
  82. * 重置管理员密码
  83. */
  84. public function adminResetPassword()
  85. {
  86. $this->output->newLine();
  87. $this->output->writeln("重置管理员密码");
  88. $this->output->writeln('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
  89. $this->output->newLine();
  90. $username = $this->output->ask(
  91. $this->input,
  92. '请输入要重置的管理员账号'
  93. );
  94. $admin = null;
  95. if ($username) {
  96. $admin = Admin::where('username', $username)->find();
  97. }
  98. if (!$admin) {
  99. $this->output->error("请输入正确的管理员账号");
  100. return false;
  101. }
  102. $password = $this->output->ask(
  103. $this->input,
  104. '请输入要设置的密码[6-16]'
  105. );
  106. if (empty($password) || strlen($password) < 6 || strlen($password) > 16) {
  107. $this->output->error("请输入正确的密码");
  108. return false;
  109. }
  110. // 重置密码
  111. $admin->salt = $admin->salt ?: mt_rand(1000, 9999);
  112. $admin->password = md5(md5($password) . $admin->salt);
  113. $admin->save();
  114. $this->output->writeln("账号 [" . $admin->username . "] 的密码重置成功");
  115. return true;
  116. }
  117. /**
  118. * 清除管理员登录失败锁定状态
  119. */
  120. public function adminClearLoginFail()
  121. {
  122. $this->output->newLine();
  123. $this->output->writeln("清除管理员登录锁定状态");
  124. $this->output->writeln('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
  125. $this->output->newLine();
  126. $username = $this->output->ask(
  127. $this->input,
  128. '请输入要清除的管理员账号'
  129. );
  130. $admin = null;
  131. if ($username) {
  132. $admin = Admin::where('username', $username)->find();
  133. }
  134. if (!$admin) {
  135. $this->output->error("请输入正确的管理员账号");
  136. return false;
  137. }
  138. $admin->loginfailure = 0;
  139. $admin->save();
  140. $this->output->writeln("账号 [" . $admin->username . "] 锁定状态清除成功");
  141. return true;
  142. }
  143. /**
  144. * 检查队列状态
  145. */
  146. public function queue()
  147. {
  148. @unlink(RUNTIME_PATH . 'storage/queue/shopro.log');
  149. @unlink(RUNTIME_PATH . 'storage/queue/shopro-high.log');
  150. $queue = config('queue');
  151. $connector = $queue['connector'] ?? 'sync';
  152. if ($connector == 'sync') {
  153. $this->output->error("队列驱动不可以使用 sync,请选择 database 或者 redis 配置");
  154. return false;
  155. }
  156. $this->output->writeln('当前队列驱动为:' . $connector);
  157. $this->output->writeln('正在添加测试队列...');
  158. $this->output->newLine();
  159. // 添加验证队列
  160. Queue::push('\addons\shopro\job\Test@shopro', [], 'shopro'); // 普通队列
  161. Queue::push('\addons\shopro\job\Test@shoproHigh', [], 'shopro-high'); // 高优先级队列
  162. $this->output->writeln('测试队列添加成功');
  163. $this->output->writeln('请检查 ' . str_replace('\\', '/', RUNTIME_PATH . 'storage/queue') . '目录下是否存在如下文件,并且文件内容为当前测试的时间');
  164. $this->output->newLine();
  165. $this->output->writeln(str_replace('\\', '/', RUNTIME_PATH . 'storage/queue') . 'shopro.log // 如果没有该文件,则普通优先级队列未监听');
  166. $this->output->writeln(str_replace('\\', '/', RUNTIME_PATH . 'storage/queue') . 'shopro-high.log // 如果没有该文件,则高优先级队列未监听');
  167. $this->output->newLine();
  168. return true;
  169. }
  170. }