157 lines
5.1 KiB
PHP
Executable File
157 lines
5.1 KiB
PHP
Executable File
<?php
|
|
|
|
# @Author: 嗨噜客(三亚) <fm453>
|
|
# @Date: 2022-05-22T07:35:53+08:00
|
|
# @Email: fm453@lukegzs.com
|
|
# @Last modified by: fm453
|
|
# @Last modified time: 2024-07-09T16:01:52+08:00
|
|
# @Copyright: www.hiluker.cn
|
|
|
|
namespace backend\controllers;
|
|
|
|
use Yii;
|
|
use backend\models\AuthItem;
|
|
use backend\models\Menu;
|
|
use backend\models\PasswordForm;
|
|
use yii\data\Pagination;
|
|
use common\models\Member as User;
|
|
use common\models\UserExt;
|
|
use backend\models\AuthAssignment;
|
|
use backend\components\Tree;
|
|
|
|
class MemberController extends Common
|
|
{
|
|
public function actionIndex()
|
|
{
|
|
return $this->render('index');
|
|
}
|
|
|
|
//用户列表
|
|
public function actionList()
|
|
{
|
|
$username = Yii::$app->user->identity->username;
|
|
if (Yii::$app->request->post()) {
|
|
if ($_POST['username']!='') {
|
|
$username = $_POST['username'];
|
|
$data = User::find()->where(['username'=>$username]);
|
|
} elseif ($_POST['mobile']!='') {
|
|
$mobile = $_POST['mobile'];
|
|
$data = User::find()->where(['mobile'=>$mobile]);
|
|
} else {
|
|
$data = User::find();
|
|
}
|
|
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']);
|
|
$user = $data->joinWith('usergroup')->offset($pages->offset)->limit($pages->limit)->all();
|
|
return $this->render('list', [
|
|
'user'=>$user,
|
|
'pages' => $pages
|
|
]);
|
|
} else {
|
|
$data = User::find();
|
|
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']);
|
|
$user = $data->joinWith('usergroup')->offset($pages->offset)->limit($pages->limit)->all();
|
|
}
|
|
return $this->render('list', [
|
|
'user'=>$user,
|
|
'pages' => $pages
|
|
]);
|
|
}
|
|
//新增用户
|
|
public function actionCreate()
|
|
{
|
|
$model = new User();
|
|
if ($model->load(Yii::$app->request->post())) {
|
|
$post = Yii::$app->request->post();
|
|
$model->username = $post['User']['username'];
|
|
$model->mobile = $post['User']['mobile'];
|
|
$model->email = $post['User']['email'];
|
|
$user = User::find()->where(['username'=>$model->username])->all();
|
|
if (!empty($user)) {
|
|
\Yii::$app->getSession()->setFlash('error', '用户名已存在!');
|
|
return $this->redirect(['create']);
|
|
}
|
|
|
|
$user = User::find()->where(['mobile'=>$model->mobile])->all();
|
|
if (!empty($user)) {
|
|
\Yii::$app->getSession()->setFlash('error', '该手机号已使用!');
|
|
return $this->redirect(['create']);
|
|
}
|
|
|
|
$user = User::find()->where(['email'=>$model->email])->all();
|
|
if (!empty($user)) {
|
|
\Yii::$app->getSession()->setFlash('error', '邮箱重复!');
|
|
return $this->redirect(['create']);
|
|
}
|
|
|
|
$model->email = $post['User']['email'];
|
|
$model->avatar = $post['User']['avatar'];
|
|
$model->mobile = $post['User']['mobile'];
|
|
$model->setPassword($post['User']['auth_key']);
|
|
$model->generateAuthKey();
|
|
$model->created_at = time();
|
|
$model->save();
|
|
$user_id = $model->attributes['id']; //获取插入后id
|
|
|
|
return $this->redirect(['list']);
|
|
} else {
|
|
return $this->render('create', [
|
|
'model' => $model,
|
|
'params' => Yii::$app->params
|
|
]);
|
|
}
|
|
}
|
|
|
|
//更新用户
|
|
public function actionUpdate()
|
|
{
|
|
$item_name = Yii::$app->request->get('item_name');
|
|
$id = Yii::$app->request->get('id');
|
|
$model = User::find()->joinWith('usergroup')->where(['id' => $id])->one();
|
|
|
|
|
|
$model1 = $this->findModel($id);
|
|
|
|
if ($model1->load(Yii::$app->request->post())) {
|
|
$post = Yii::$app->request->post();
|
|
//更新密码
|
|
if (!empty($post['User']['auth_key_new'])) {
|
|
$model1->setPassword($post['User']['auth_key_new']);
|
|
$model1->generateAuthKey();
|
|
} else {
|
|
$model1->auth_key = $post['User']['auth_key'];
|
|
}
|
|
$model1->avatar = $post['User']['avatar'];
|
|
$model1->mobile = $post['User']['mobile'];
|
|
|
|
$model1->save($post);
|
|
|
|
return $this->redirect(['user/list']);
|
|
}
|
|
return $this->render('update', [
|
|
'model' => $model,
|
|
]);
|
|
}
|
|
//删除用户
|
|
public function actionDelete($id)
|
|
{
|
|
$connection=Yii::$app->db;
|
|
$transaction=$connection->beginTransaction();
|
|
try {
|
|
$connection->createCommand()->delete("c_user", "id = '$id'")->execute();
|
|
$transaction->commit();
|
|
} catch (Exception $ex) {
|
|
$transaction->rollBack();
|
|
}
|
|
return $this->redirect(['list']);
|
|
}
|
|
|
|
protected function findModel($id)
|
|
{
|
|
if (($model = User::findOne($id)) !== null) {
|
|
return $model;
|
|
} else {
|
|
throw new NotFoundHttpException('The requested page does not exist.');
|
|
}
|
|
}
|
|
}
|