Initial commit,修正
This commit is contained in:
Executable
+211
@@ -0,0 +1,211 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @Author: fm453
|
||||
* @Date: 2021-09-08 17:26:29
|
||||
* @Last Modified by: fm453
|
||||
* @Last Modified time: 2021-09-09 03:12:35
|
||||
* @Email: fm453@lukegzs.com
|
||||
*/
|
||||
namespace backend\controllers;
|
||||
|
||||
use Yii;
|
||||
use yii\web\Controller;
|
||||
use yii\data\Pagination;
|
||||
use common\models\User;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
|
||||
public function actionIndex()
|
||||
{
|
||||
return $this->render('index');
|
||||
}
|
||||
|
||||
//用户列表
|
||||
public function actionList()
|
||||
{
|
||||
$username = Yii::$app->user->identity->username;
|
||||
$search = [];
|
||||
$search['username'] = Yii::$app->request->get('username');
|
||||
$search['mobile'] = Yii::$app->request->get('mobile');
|
||||
$page = (int)Yii::$app->request->get('page');
|
||||
$page = max(1,$page)-1;
|
||||
if (Yii::$app->request->post('search')) {
|
||||
$search = Yii::$app->request->post('search');
|
||||
}
|
||||
if($search['username']!=''){
|
||||
$username = $search['username'];
|
||||
$data = User::find()->where(['LIKE','username',$username],['>','status',0]);
|
||||
}elseif($search['mobile']!=''){
|
||||
$mobile = $search['mobile'];
|
||||
$data = User::find()->where(['LIKE','mobile',$mobile],['>','status',0]);
|
||||
}else{
|
||||
$data = User::find()->where(['>','status',0]);
|
||||
}
|
||||
|
||||
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20','params'=>$search,'page'=>$page]);
|
||||
$user = $data->offset($pages->offset)->limit($pages->limit)->all();
|
||||
$status = User::status();
|
||||
return $this->render('list',[
|
||||
'user'=>$user,
|
||||
'search'=>$search,
|
||||
'status' => $status,
|
||||
'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->avatar = $post['User']['avatar'];
|
||||
$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(){
|
||||
$id = Yii::$app->request->get('id');
|
||||
$model = $this->findModel($id);
|
||||
$status = User::status();
|
||||
$_status = [];
|
||||
foreach($status as $k => $v){
|
||||
$_status[$k] = $v['title'];
|
||||
}
|
||||
|
||||
if ($model->load(Yii::$app->request->post())) {
|
||||
$post = Yii::$app->request->post();
|
||||
$mobile = $post['User']['mobile'];
|
||||
if($model->mobile != $mobile){
|
||||
$user = User::find()->where(['mobile'=>$mobile])->all();
|
||||
if(!empty($user)){
|
||||
\Yii::$app->getSession()->setFlash('error', '该手机号已由其他账号绑定!');
|
||||
return $this->render('update',[
|
||||
'model' => $model,
|
||||
'userstatus'=>$_status,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$email = $post['User']['email'];
|
||||
if($model->email != $email){
|
||||
$user = User::find()->where(['email'=>$email])->all();
|
||||
if(!empty($user)){
|
||||
\Yii::$app->getSession()->setFlash('error', '该邮箱已有其他账号绑定!');
|
||||
return $this->render('update',[
|
||||
'model' => $model,
|
||||
'userstatus'=>$_status,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
//更新密码
|
||||
if(!empty($post['User']['auth_key_new'])){
|
||||
$model->setPassword($post['User']['auth_key_new']);
|
||||
$model->generateAuthKey();
|
||||
}else{
|
||||
$model->auth_key = $post['User']['auth_key'];
|
||||
}
|
||||
$model->avatar = $post['User']['avatar'];
|
||||
$model->mobile = $post['User']['mobile'];
|
||||
$model->email = $post['User']['email'];
|
||||
$model->status = $post['User']['status'];
|
||||
$model->sms_left = $post['User']['sms_left'];
|
||||
|
||||
$res = $model->save($post);
|
||||
\Yii::$app->getSession()->setFlash('sucess', '账户信息更新成功!');
|
||||
|
||||
return $this->redirect(['list']);
|
||||
}
|
||||
return $this->render('update',[
|
||||
'model' => $model,
|
||||
'userstatus'=>$_status,
|
||||
]);
|
||||
}
|
||||
|
||||
//移除用户(软删除)
|
||||
public function actionDelete($id)
|
||||
{
|
||||
$id = Yii::$app->request->get('id');
|
||||
$model = $this->findModel($id);
|
||||
$model->status = 0;
|
||||
$model->save();
|
||||
return $this->redirect(['list']);
|
||||
}
|
||||
//删除用户
|
||||
public function actionRemove($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']);
|
||||
}
|
||||
|
||||
//启停用户
|
||||
public function actionStatus(){
|
||||
$id = Yii::$app->request->get('id');
|
||||
$id = (int)$id;
|
||||
if($id>0){
|
||||
$status = Yii::$app->request->get('status');
|
||||
$model = $this->findModel($id);
|
||||
$model->status = $status;
|
||||
$res = $model->save();
|
||||
if(!$res){
|
||||
\Yii::$app->getSession()->setFlash('error', '用户状态更新失败!');
|
||||
}
|
||||
}
|
||||
|
||||
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.');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user