DefaultController.php 3.4 KB
<?php

namespace app\ht\modules\my\controllers;

use Yii;
use yii\base\Exception;
use yii\web\NotFoundHttpException;
use app\ht\controllers\BaseController;
use app\ht\helpers\Password;
use common\models\SysUser           as SysUserModel;
use common\models\SysUserProfile    as SysUserProfileModel;

/**
 * 账号设置
 */
class DefaultController extends BaseController
{
    /**
     * 账号设置
     */
    public function actionIndex()
    {
        $model = $this->getUser();

        return $this->render('index', [
            'model' => $model,
        ]);
    }

    /**
     * 账号设置
     */
    public function actionMyIndex()
    {
        $model = $this->getUser();

        return $this->render('my-index', [
            'model' => $model,
        ]);
    }

    /**
     * 更新执行动作
     */
    public function actionDoUpdate()
    {
        $post = Yii::$app->request->post();
        if (empty($post)) {
            return;
        }

        $realname = isset($post['realname']) ? $post['realname'] : '';
        $email = isset($post['email']) ? $post['email'] : '';
        $mobile = isset($post['mobile']) ? $post['mobile'] : '';

        $model = $this->getUser();

        $transaction = Yii::$app->db->beginTransaction();
        try {
            $model->save();
            if ($model->profile) {
                $profile = $model->profile;
            } else {
                $profile = Yii::createObject(SysUserProfileModel::className());
                $profile->admin_user_id = $model->admin_user_id;
            }
            $profile->realname = $realname;
            $profile->email = $email;
            $profile->mobile = $mobile;
            $profile->save();
            $transaction->commit();
        } catch (Exception $e) {
            Yii::$app->session->setFlash('danger', '数据保存失败,请重新操作');
            $transaction->rollBack();

            return $this->redirect(['index']);
        }

        Yii::$app->session->setFlash('success', '账号更新成功');



        return $this->redirect(['index']);
    }

    /**
     * 账号设置
     */
    public function actionPassword()
    {
        return $this->render('password', [
        ]);
    }

    /**
     * 更新密码执行动作
     */
    public function actionDoPassword()
    {
        $post = Yii::$app->request->post();
        if (empty($post)) {
            return;
        }

        $user = $this->getUser();
        // 判断原密码
        if (!Password::validate($post['old_password'], $user->password_hash)) {
            Yii::$app->session->setFlash('danger', '原密码不对,请重新输入');
            return $this->redirect(['password']);
        }

        // 新密码确认
        if ($post['password'] != $post['password_confirm']) {
            Yii::$app->session->setFlash('danger', '新密码和确认密码不一致,请重新输入');
            return $this->redirect(['password']);
        }

        $user->password = $post['password'];
        $user->save();

        Yii::$app->session->setFlash('success', '密码更新成功');

        return $this->redirect(['password']);
    }


    protected function getUser()
    {
        $user = Yii::createObject(SysUserModel::className());
        if (($model = $user::findOne(Yii::$app->getUser()->id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('访问页面不存在');
        }
    }
}