You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.9 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace console\controllers;
  3. use Yii;
  4. use yii\console\Controller;
  5. use yii\helpers\Console;
  6. use yii\db\Query;
  7. use yii\helpers\ArrayHelper;
  8. use backend\models\User;
  9. /**
  10. * Description of RbacsetController
  11. *
  12. * @author blobt
  13. */
  14. class InitController extends Controller {
  15. public function actionIndex() {
  16. echo "index\n";
  17. Category::deleteAll();
  18. for ($i = 0; $i < 1000000; $i++) {
  19. $model = new Category();
  20. $model->attributes = [
  21. "cat_name" => "这是一个测试分类{$i}",
  22. "icon" => 'fa',
  23. "icon_type" => 1,
  24. "sort_order" => $i,
  25. "created_at" => time() - rand(10000, 1000000)
  26. ];
  27. $model->save();
  28. }
  29. }
  30. public function actionCreateAdmin($password = NULL) {
  31. $auth = Yii::$app->authManager;
  32. while ($password == NULL) {
  33. $password = $this->prompt("\n\n请输入admin用户密码(最少6位任意字符):");
  34. if (strlen($password) < 6) {
  35. $password = NULL;
  36. continue;
  37. }
  38. break;
  39. }
  40. $this->createAdmin($password);
  41. }
  42. /**
  43. * 数据迁移
  44. * @param sring $migrationPath 数据迁移代码目录
  45. */
  46. private function migrate($migrationPath = "@app/migrations") {
  47. $migrate = Yii::createObject("yii\console\controllers\MigrateController", ["action", $this]);
  48. $migrate->interactive = false;
  49. $migrate->migrationPath = $migrationPath;
  50. $migrate->runAction("up", []);
  51. }
  52. /**
  53. * TODO没有完善
  54. * 清空数据库
  55. */
  56. public function actionClearDb() {
  57. Yii::$app->db->createCommand("SET FOREIGN_KEY_CHECKS = 0;")->execute();
  58. $dbname = explode('=', explode(';', Yii::$app->db->dsn)[1])[1];
  59. $sql = "SELECT CONCAT('drop table ',table_name,';') FROM information_schema.`TABLES` WHERE table_schema='{$dbname}';";
  60. $sqls = Yii::$app->db->createCommand($sql)->queryColumn();
  61. foreach ($sqls as $dropSql) {
  62. Yii::$app->db->createCommand($dropSql)->execute();
  63. }
  64. }
  65. /**
  66. * 创建管理员
  67. * @param string $password 管理员密码
  68. */
  69. private function createAdmin($password) {
  70. $auth = Yii::$app->authManager;
  71. $model = User::findOne("username = 'admin");
  72. if (empty($model)) {
  73. $model = new User();
  74. }
  75. $model->username = "admin";
  76. $model->email = "admin@kcshop.store";
  77. $model->setPassword($password);
  78. $model->generateAuthKey();
  79. $model->status = User::STATUS_ACTIVE;
  80. $model->created_at = $model->updated_at = time();
  81. if ($model->save()) {
  82. //$oRole = $auth->getRole("超级管理员");
  83. //if ($auth->assign($oRole, $model->id)) {
  84. $this->stdout("Create admin success!\n", Console::FG_GREEN);
  85. //}
  86. }
  87. }
  88. }