Skip to content

Commit

Permalink
fecmall安装优化
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyecommerce committed Jan 23, 2020
1 parent 415489b commit 105469f
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 67 deletions.
197 changes: 143 additions & 54 deletions app/appinstall/modules/Database/controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
namespace fecshop\app\appinstall\modules\Database\controllers;

use fecshop\models\mysqldb\AdminUser;
//use fecshop\models\mysqldb\AdminUser;
use Yii;
use yii\base\Exception;
use yii\web\Controller;
Expand All @@ -28,20 +28,29 @@ class ConfigController extends Controller
*/
public function beforeAction($action)
{
try {
if (file_exists('install.lock') && "1" === file_get_contents("install.lock")) {
Yii::$app->response->data = "您的项目可能已安装成功,如果需要重新安装,请手动删除项目目录的 <code>appfront/web/install.lock</code> 文件";
if ($this->action->id != 'complete') {
$installLockFilePath = $this->getInstallLockFilePath();
try {
if (file_exists($installLockFilePath) && "1" === file_get_contents($installLockFilePath)) {
Yii::$app->response->data = "您的项目可能已安装成功,如果需要重新安装,请手动删除项目目录的 <code>".$installLockFilePath."</code> 文件";
return false;
} else {
file_put_contents($installLockFilePath, "0");
}
} catch (\Exception $exception) {
Yii::$app->response->data = $exception->getMessage();
return false;
} else {
file_put_contents("install.lock", "0");
}
} catch (\Exception $exception) {
Yii::$app->response->data = "没有文件读写权限,请赋予 <code>appfront/web</code> 目录可读写权限。" . $exception->getMessage();
return false;
}


return parent::beforeAction($action);
}

protected function getInstallLockFilePath()
{
return Yii::getAlias('@appfront/runtime/install.lock');
}

public function init()
{
Expand All @@ -57,13 +66,6 @@ public function actionIndex()
if ($editForm && $this->checkDatabaseData($editForm)
&& $this->updateDatabaseConfig($editForm)) {
Yii::$app->session->setFlash('database-success', 'Mysql配置成功,写入的配置文件路径为: @common/config/main-local.php');
// 临时缓存超级管理账户资料
Yii::$app->session->set('super_account', [
'username' => $editForm['username'],
'userpassword' => $editForm['userpassword'],
'useremail' => $editForm['useremail'],
]);

// 进行跳转
$homeUrl = Yii::$app->homeUrl;
return $this->redirect($homeUrl . '/database/config/migrate');
Expand All @@ -75,6 +77,29 @@ public function actionIndex()
'editForm' => $editForm,
]);
}

// 安装默认第一步页面
public function actionInitadminuser()
{
$editForm = Yii::$app->request->post('editForm');
if (is_array($editForm) && !empty($editForm)) {
$param = [
'username' => $editForm['username'],
'password' => $editForm['password'],
];
if ($this->installUpdateUser($param)) {
$homeUrl = Yii::$app->homeUrl;
return $this->redirect($homeUrl . '/database/config/complete');

}
}
$errorInfo = $this->getErrorHtml($this->_install_errors);
return $this->render($this->action->id, [
'errorInfo' => $errorInfo,
'editForm' => $editForm,
]);

}

// 数据库migrate页面
public function actionMigrate()
Expand All @@ -84,27 +109,7 @@ public function actionMigrate()
if ($isPost ) {
// 进行数据库初始化
if ($this->runMigrate()) {
// 更新超级管理员账户
$superUserInfo = Yii::$app->session->get('super_account', [
'username' => 'admin',
'userpassword' => 'admin123',
'useremail' => '[email protected]',
]);
/**
* @var $superUser AdminUser
*/
$superUser = AdminUser::findOne(['id' => 2]);
$superUser->username = $superUserInfo['username'];
$superUser->password_hash = Yii::$app->security->generatePasswordHash($superUserInfo['userpassword'], 6);;
$superUser->email = $superUserInfo['useremail'];
$superUser->updated_at = time();
$superUser->updated_at_datetime = date(DATE_ATOM, $superUser->updated_at);
$superUser->save();
Yii::$app->session->remove('super_account');

// 锁定安装向导
file_put_contents('install.lock', "1");


$successInfo = '数据库migrate初始化完成';
$successInfo = $this->getSuccessHtml($successInfo);
//exit;
Expand All @@ -129,6 +134,85 @@ public function actionMigrate()
'errorInfo' => $errorInfo,
]);
}



/*
$superUserInfo = Yii::$app->session->get('super_account', [
'username' => 'admin',
'userpassword' => 'admin123',
'useremail' => '[email protected]',
]);
$param = [
'username' => $superUserInfo['username'],
'password' => $superUserInfo['userpassword'],
'email' => $superUserInfo['useremail'],
];
$this->installUpdateUser($param);
Yii::$app->session->remove('super_account');
*/


/**
* 因为安装部分没有引入services,因此无法使用Yii::$service
* 安装更新User
*/
protected $_install_errors;

public function installUpdateUser($param)
{
// $userFormModelName = '\fecshop\models\mysqldb\adminUser\AdminUserForm';
// list($userFormModelName, $userFormModel) = \Yii::mapGet($userFormModelName);
$userFormModel = new \fecshop\models\mysqldb\adminUser\AdminUserForm;
$model = $userFormModel->findOne(['id' => 2]); // 初始化admin表的user为2
if (!$model) {
//Yii::$service->helper->errors->add('admin user[id=2] is not exist');
$this->_install_errors = 'admin user[id=2] is not exist';
return false;
}

$model->attributes = $param;
if ($model->validate()) {
$model->save();

return true;
} else {
$errors = $model->errors;
if (is_array($errors) && !empty($errors)) {
foreach ($errors as $one) {
$this->_install_errors .= implode(',', $one);
}
}


return false;
}



echo $param['username'];echo $param['password'];
$model->username = $param['username'];
$model->setPassword($param['password']); // = Yii::$app->security->generatePasswordHash($superUserInfo['userpassword'], 6);;
//$model->email = $param['email'];
//$model->updated_at = time();
if ($model->validate()) {
//var_dump($model);
echo 55;
exit;
return $model->save();

return true;
} else {
$errors = $model->errors;
$this->_install_errors = $errors;
//Yii::$service->helper->errors->addByModelErrors($errors);
var_dump($this->_install_errors);
exit;
return false;
}
}

// 产品测试数据添加
public function actionAddtestdata()
Expand All @@ -139,12 +223,12 @@ public function actionAddtestdata()
$errorInfo = Yii::$app->session->getFlash('add-test-data-errors');
$errorInfo = $this->getErrorHtml($errorInfo);
}

return $this->render($this->action->id, [
'errorInfo' => $errorInfo,
'successInfo' => $successInfo,
'initUrl' => Yii::$app->homeUrl . '/database/config/addtestdatainit',
'nextUrl' => Yii::$app->homeUrl . '/database/config/complete',
'nextUrl' => Yii::$app->homeUrl . '/database/config/initadminuser',
//'migrateLog' => $this->_migrateLog
]);

Expand Down Expand Up @@ -180,6 +264,11 @@ public function addProductData()
// 完成页面
public function actionComplete()
{

// 锁定安装向导
$installLockFilePath = $this->getInstallLockFilePath();
file_put_contents($installLockFilePath, "1");

return $this->render($this->action->id, []);
}

Expand Down Expand Up @@ -271,21 +360,21 @@ public function checkDatabaseData($editForm)

return false;
}
if (!$editForm['username']) {
$session->setFlash('database-errors', '超级管理员账户为空');

return false;
}
if (!$editForm['useremail']) {
$session->setFlash('database-errors', '超级账户邮箱为空');

return false;
}
if (!$editForm['userpassword']) {
$session->setFlash('database-errors', '超级账户密码为空');

return false;
}
//if (!$editForm['username']) {
// $session->setFlash('database-errors', '超级管理员账户为空');
//
// return false;
//}
//if (!$editForm['useremail']) {
// $session->setFlash('database-errors', '超级账户邮箱为空');
//
// return false;
// }
//if (!$editForm['userpassword']) {
// $session->setFlash('database-errors', '超级账户密码为空');
//
// return false;
// }

return true;
}
Expand Down
14 changes: 1 addition & 13 deletions app/appinstall/modules/Database/views/config/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<?php echo CRequest::getCsrfInputHtml(); ?>
<div class="form-group">
<label for="name">Mysql数据库Host</label>
<input type="text" class="form-control" value="<?= $editForm['host'] ? $editForm['host'] : 'localhost' ?>"
<input type="text" class="form-control" value="<?= $editForm['host'] ? $editForm['host'] : '127.0.0.1' ?>"
name="editForm[host]" placeholder="Mysql数据库Host,同服务器可填写 127.0.0.1 或 localhost">
</div>

Expand Down Expand Up @@ -41,17 +41,5 @@
placeholder="Mysql数据库密码">
</div>

<div class="form-group">
<label for="name">超级管理员账户</label>
<input type="text" class="form-control" value="<?= $editForm['username'] ? $editForm['username'] : 'admin' ?>"
name="editForm[username]" placeholder="超级管理员账户">
</div>

<div class="form-group">
<label for="name">超级账户密码</label>
<input type="password" class="form-control" required value="<?= $editForm['userpassword'] ?>"
name="editForm[userpassword]" placeholder="超级账户密码">
</div>

<button type="submit" class="btn btn-default">提交</button>
</form>
27 changes: 27 additions & 0 deletions app/appinstall/modules/Database/views/config/initadminuser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use fec\helpers\CRequest;

?>
<h1>后台用户面密码初始化</h1>
<br/>

<?= $errorInfo ?>
<?= $successInfo ?>


<form action="" method="post">
<?php echo CRequest::getCsrfInputHtml(); ?>
<div class="form-group">
<label for="name">超级管理员账户</label>
<input type="text" class="form-control" value="<?= $editForm['username'] ? $editForm['username'] : 'admin' ?>"
name="editForm[username]" placeholder="超级管理员账户">
</div>

<div class="form-group">
<label for="name">超级账户密码</label>
<input type="password" class="form-control" required value="<?= $editForm['password'] ?>"
name="editForm[password]" placeholder="超级账户密码">
</div>
<button type="submit" class="btn btn-default">提交</button>
</form>

0 comments on commit 105469f

Please sign in to comment.