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.

103 lines
2.9 KiB

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