Initial commit,修正

This commit is contained in:
2025-07-13 19:37:38 +08:00
parent 38ce288e27
commit 65bbd06981
12384 changed files with 3985464 additions and 8 deletions
+266
View File
@@ -0,0 +1,266 @@
<?php
/**
* @Author: fm453
* @Date: 2018-04-23 14:23:45
* @Last Modified by: fm453
* @Last Modified time: 2021-09-09 00:17:15
* @Email: fm453@lukegzs.com
*/
//运维人员管理
namespace backend\controllers;
use Yii;
use backend\models\AuthItem;
use backend\models\AuthAssignment;
use backend\models\Menu;
use backend\models\Adminer;
use yii\data\Pagination;
use yii\web\Controller;
class AdminerController extends Controller
{
public function actionIndex()
{
return $this->render('index');
}
//用户列表
public function actionList()
{
$username = Yii::$app->user->identity->username;
$status = [
0 => ['title'=>'禁用','css'=>'default'],
10 => ['title'=>'启用','css'=>'success'],
];
$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 = Adminer::find()->where(['LIKE','username',$username],['<>','status',1]);
}elseif($search['mobile']!=''){
$mobile = $search['mobile'];
$data = Adminer::find()->where(['LIKE','mobile',$mobile],['<>','status',1]);
}else{
$data = Adminer::find()->where(['<>','status',1]);
}
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20','params'=>$search,'page'=>$page]);
$user = $data->joinWith('usergroup')->offset($pages->offset)->limit($pages->limit)->all();
return $this->render('list',[
'user'=>$user,
'search'=>$search,
'status' => $status,
'pages' => $pages
]);
}
//新增用户
public function actionCreate()
{
$model = new Adminer();
$model1 = new AuthItem();
$auth = Yii::$app->authManager;
$item = $auth->getRoles();
$itemArr =array();
foreach($item as $v){
$itemArr[] .= $v->name;
}
foreach($itemArr as $key=>$value)
{
$item_one[$value]=$value;
}
if ($model->load(Yii::$app->request->post())) {
$post = Yii::$app->request->post();
$model->username = $post['Adminer']['username'];
$model->email = $post['Adminer']['email'];
$model->mobile = $post['Adminer']['mobile'];
$user = Adminer::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->nickname = $post['Adminer']['nickname'];
$model->avatar = $post['Adminer']['avatar'];
$model->setPassword($post['Adminer']['auth_key']);
$model->generateAuthKey();
$model->created_at = time();
$model->save();
$user_id = $model->attributes['id']; //获取插入后id
$role = $auth->createRole($post['AuthItem']['name']);//创建角色对象
$auth->assign($role, $user_id);//添加对应关系
return $this->redirect(['list']);
} else {
return $this->render('create', [
'model' => $model,
'model1' => $model1,
'item' => $item_one,
'params' => Yii::$app->params
]);
}
}
//更新用户
public function actionUpdate(){
$item_name = Yii::$app->request->get('item_name');
$id = Yii::$app->request->get('id');
$model = Adminer::find()->joinWith('usergroup')->where(['id' => $id])->one(); //加入了角色组数据
$auth = Yii::$app->authManager;
$item = $auth->getRoles();
$item_one = $itemArr =array();
foreach($item as $v){
$itemArr[] .= $v->name;
}
foreach($itemArr as $key=>$value)
{
$item_one[$value]=$value;
}
$model1 = $this->findModel($id); //仅账号本身数据
if ($model1->load(Yii::$app->request->post())) {
$post = Yii::$app->request->post();
$mobile = $post['Adminer']['mobile'];
if($model->mobile != $mobile){
$user = Adminer::find()->where(['mobile'=>$mobile])->all();
if(!empty($user)){
\Yii::$app->getSession()->setFlash('error', '该手机号已由其他账号绑定!');
return $this->render('update',[
'model' => $model,
'item' => $item_one
]);
}
}
$email = $post['Adminer']['email'];
if($model->email != $email){
$user = Adminer::find()->where(['email'=>$email])->all();
if(!empty($user)){
\Yii::$app->getSession()->setFlash('error', '该邮箱已有其他账号绑定!');
return $this->render('update',[
'model' => $model,
'item' => $item_one
]);
}
}
//更新密码
if(!empty($post['Adminer']['auth_key_new'])){
$model1->setPassword($post['Adminer']['auth_key_new']);
$model1->generateAuthKey();
}else{
$model1->auth_key = $post['Adminer']['auth_key'];
}
$model1->avatar = $post['Adminer']['avatar'];
$model1->mobile = $post['Adminer']['mobile'];
$model->nickname = $post['Adminer']['nickname'];
$model1->save($post);
//分配角色
if(isset($post['AuthAssignment'])){
$role = $auth->createRole($post['AuthAssignment']['item_name']); //创建角色对象
$user_id = $id;//获取用户id
$auth->revokeAll($user_id);
$auth->assign($role, $user_id);//添加对应关系
}
\Yii::$app->getSession()->setFlash('sucess', '账户信息更新成功!');
return $this->redirect(['list']);
}
return $this->render('update',[
'model' => $model,
'item' => $item_one
]);
}
//剔除用户(软删)
public function actionDelete($id)
{
$id = Yii::$app->request->get('id');
$model = $this->findModel($id);
$model->status = 1;
$res = $model->save();
var_dump($res);die;
//剔除权限
$connection=Yii::$app->db;
$transaction=$connection->beginTransaction();
try
{
$connection->createCommand()->delete("auth_assignment", "user_id = '$id'")->execute();
$transaction->commit();
}
catch(Exception $ex)
{
$transaction->rollBack();
}
\Yii::$app->getSession()->setFlash('sucess', '该账户已删除!');
return $this->redirect(['list']);
}
//删除用户
public function actionRemove($id)
{
$connection=Yii::$app->db;
$transaction=$connection->beginTransaction();
try
{
$connection->createCommand()->delete("c_adminer", "id = '$id'")->execute();
$connection->createCommand()->delete("auth_assignment", "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(['adminer/list']);
}
protected function findModel($id)
{
if (($model = Adminer::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
+189
View File
@@ -0,0 +1,189 @@
<?php
/**
* @Author: fm453
* @Date: 2021-09-09 01:20:14
# @Last modified by: Fm453
# @Last modified time: 2021-09-15T00:40:06+08:00
* @Email: fm453@lukegzs.com
*/
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use common\components\HiCookie;
use common\models\Fans;
class FansController extends Controller
{
public function actionIndex()
{
return $this->render('index');
}
//粉丝列表
public function actionList()
{
$username = Yii::$app->user->identity->username;
$cookie = new HiCookie();
$post = Yii::$app->request->post();
$where = $serach = [];
$searchCookie = 'Fans::List';
$Fans = new Fans();
if(isset($_GET['reset']) && $_GET['reset']==1){
$post['search'] = [];
}
if(isset($post['search'])){
$search = $post['search'];
$cookie->set($searchCookie,$search,false);
}elseif($cookie->get($searchCookie)){
$search = $cookie->get($searchCookie);
}
$where[] = 'or';
$search['username'] = isset($search['username']) ? trim(htmlspecialchars_decode($search['username'])) : '';
if($search['username']){
$where[] = ['LIKE','mobile',$search['username']];
}
$search['email'] = isset($search['email']) ? trim(htmlspecialchars_decode($search['email'])) : '';
if($search['email']){
$where[] = ['LIKE','email',$search['email']];
}
//没有搜索条件时,重置一下$where
if(count($where)==1){
$where = [];
}
$data = $Fans->find()->where($where);
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']);
$res = $data->offset($pages->offset)->limit($pages->limit)->all();
$status = $this->getStatus();
return $this->render('list',[
'fans'=>$res,
'search'=>$search,
'count'=>$data->count(),
'status'=>$status,
'pages' => $pages
]);
}
//新增粉丝
public function actionCreate()
{
$model = new Fans();
if ($model->load(Yii::$app->request->post())) {
$post = Yii::$app->request->post();
$model->mobile = $post['Fans']['username'];
$model->email = $post['Fans']['email'];
$user = Fans::find()->where(['mobile'=>$model->mobile])->all();
if(!empty($user)){
\Yii::$app->getSession()->setFlash('error', '该账号已存在!');
return $this->redirect(['create']);
}
$user = Fans::find()->where(['email'=>$model->email])->all();
if(!empty($user)){
\Yii::$app->getSession()->setFlash('error', '邮箱重复!');
return $this->redirect(['create']);
}
$model->setPassword($post['Fans']['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 = $this->getStatus();
if ($model->load(Yii::$app->request->post())) {
$post = Yii::$app->request->post();
$email = $post['Fans']['email'];
if($model->email != $email){
$user = Fans::find()->where(['email'=>$email])->all();
if(!empty($user)){
\Yii::$app->getSession()->setFlash('error', '该邮箱已有其他账号绑定!');
return $this->render('update',[
'model' => $model,
'status'=>$status,
]);
}
}
//更新密码
if(!empty($post['Fans']['auth_key_new'])){
$model->setPassword($post['Fans']['auth_key_new']);
$model->generateAuthKey();
}else{
$model->auth_key = $post['Fans']['auth_key'];
}
$model->email = $post['Fans']['email'];
$model->status = $post['Fans']['status'];
$res = $model->save($post);
\Yii::$app->getSession()->setFlash('sucess', '账户信息更新成功!');
return $this->redirect(['list']);
}
return $this->render('update',[
'model' => $model,
'status'=>$status,
]);
}
//删除粉丝
public function actionRemove($id)
{
$connection=Yii::$app->db;
$transaction=$connection->beginTransaction();
try
{
$connection->createCommand()->delete(Fans::tableName(), "id = '$id'")->execute();
$transaction->commit();
}
catch(Exception $ex)
{
$transaction->rollBack();
}
return $this->redirect(['list']);
}
protected function findModel($id)
{
if (($model = Fans::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
protected function getStatus(){
return $status = [
0=>'禁用',
1=>'未知',
10=>'正常'
];
}
}
+65
View File
@@ -0,0 +1,65 @@
<?php
/**
* @Author: fm453
* @Date: 2018-04-23 14:25:38
# @Last modified by: fm453
# @Last modified time: 2021-09-14T12:16:02+08:00
* @Email: fm453@lukegzs.com
*/
//主入口页
namespace backend\controllers;
use Yii;
use common\models\CLog;
use common\models\User;
use common\models\Member;
use common\models\Fans;
use backend\models\Menu;
use yii\data\Pagination;
use yii\web\Controller;
class IndexController extends Controller
{
// public $enableCsrfValidation = false;
public function actionIndex()
{
return $this->render('index');
}
public function actionWelcome()
{
//登录记录
$query = CLog::find();
$nowTable = CLog::tablename();
$count = $query->count();
$pages = new Pagination(['totalCount' => $count,'pageSize' => '10']);
$log = $query->offset($pages->offset)
->limit($pages->limit)
->orderBy('id desc')
->all();
$sql = "SELECT *,FROM_UNIXTIME(create_time,'%Y-%m') as period,COUNT(*) as times FROM ".$nowTable." GROUP BY period LIMIT 12";
$History = Yii::$app->db->createCommand($sql)->queryAll();
$HistoryMonthStr = '';
$HistoryMonthNum = '';
foreach($History as $val){
$HistoryMonthStr .= "'".$val['period']."',";
$HistoryMonthNum .= $val['times'].",";
}
$HistoryMonthStr = substr($HistoryMonthStr,0,-1);
$HistoryMonthNum = substr($HistoryMonthNum,0,-1);
$keywords = '';
$description = '';
return $this->render('welcome',[
'log' => $log,
'pages' => $pages,
'HistoryMonthStr' => $HistoryMonthStr,
'HistoryMonthNum' => $HistoryMonthNum,
'keywords' => $keywords,
'description' => $description,
]);
}
}
+185
View File
@@ -0,0 +1,185 @@
<?php
/**
* @Author: fm453
* @Date: 2018-04-23 14:27:07
* @Last Modified by: fm453
* @Last Modified time: 2018-04-23 14:28:58
* @Email: fm453@lukegzs.com
*/
namespace backend\controllers;
use Yii;
use backend\models\AuthItem;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\data\Pagination;
use backend\models\ItemForm;
use yii\helpers\Json;
/**
* ItemController implements the CRUD actions for AuthItem model.
*/
class ItemController extends Controller
{
/*public function beforeAction($action)
{
//$action = Yii::$app->controller->action->id;
$action = Yii::$app->controller->module->requestedRoute;
if(\Yii::$app->user->can($action)){
return true;
}else{
//throw new \yii\web\UnauthorizedHttpException('对不起,您现在还没获此操作的权限');
echo '<div style="margin: 100px auto;text-align: center;background-color: #1ab394; color: #ffffff;width: 500px;height: 50px;line-height: 50px;border-radius: 5px;;"><h4>对不起,您现在还没获此操作的权限</h4></div>';
}
}*/
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all AuthItem models.
* @return mixed
*/
public function actionIndex()
{
$model = new AuthItem();
//分页
$curPage = Yii:: $app-> request->get( 'page',1);
$pageSize = 10;
//搜索
$type = Yii:: $app-> request->get( 'type', '');
$value = Yii:: $app-> request->get( 'value', '');
$search = ($type&&$value)?[ 'like',$type,$value]: '';
//查询语句
$query = $model->find()->where(['type'=>1])->orderBy( 'created_at DESC'); //列表只显示角色
$data = $model->getPages($query,$curPage,$pageSize,$search);
$pages = new Pagination([ 'totalCount' =>$data[ 'count'], 'pageSize' => $pageSize]);
return $this->render('index',['pages'=>$pages,'data'=>$data]);
}
public function actionPermission()
{
$model = new AuthItem();
//分页
$curPage = Yii:: $app-> request->get( 'page',1);
$pageSize = 10;
//搜索
$type = Yii:: $app-> request->get( 'type', '');
$value = Yii:: $app-> request->get( 'value', '');
$search = ($type&&$value)?[ 'like',$type,$value]: '';
//查询语句
$query = $model->find()->where(['type'=>2])->orderBy( 'created_at DESC'); //列表只显示角色
$data = $model->getPages($query,$curPage,$pageSize,$search);
$pages = new Pagination([ 'totalCount' =>$data[ 'count'], 'pageSize' => $pageSize]);
return $this->render('index',['pages'=>$pages,'data'=>$data]);
}
/**
* Displays a single AuthItem model.
* @param string $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new AuthItem model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new AuthItem();
//设置场景
$model->setScenario(ItemForm:: SCENARIOS_CREATE);
if ($model->load(Yii::$app->request->post())) {
if($model->validate()){
$model->addItem();
return $this->redirect(['index']);
}
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing AuthItem model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param string $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = new AuthItem();
$model = $model-> getItem($id);
//设置场景
$model->setScenario(AuthItem:: SCENARIOS_UPDATE);
if ($model->load(Yii:: $app-> request->post())) {
if($model->validate()){
$model->updateItem($id);
return $this->redirect([ 'view', 'id' => $model->name]);
}
}
return $this->render( 'update', [
'model' => $model,
]);
}
/**
* Deletes an existing AuthItem model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param string $id
* @return mixed
*/
public function actionDelete()
{
$model = new AuthItem();
$model->setScenario(AuthItem:: SCENARIOS_DELETE);
$model-> name = Yii:: $app-> request->post('id');
$res = $model->romoveItem();
if(!$res){
return Json:: encode(['status'=>false,'msg'=>'删除失败!']);
}
return Json::encode(['status' =>true]);
}
/**
* Finds the AuthItem model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param string $id
* @return AuthItem the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = AuthItem::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
+206
View File
@@ -0,0 +1,206 @@
<?php
/**
* @Author: fm453
* @Date: 2021-09-09 01:20:14
* @Last Modified by: fm453
* @Last Modified time: 2021-09-09 08:54:55
* @Email: fm453@lukegzs.com
*/
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use yii\data\Pagination;
use common\components\HiCookie;
use common\models\Member;
class MemberController extends Controller
{
public function actionIndex()
{
return $this->render('index');
}
//会员列表
public function actionList()
{
$username = Yii::$app->user->identity->username;
$cookie = new HiCookie();
$post = Yii::$app->request->post();
$where = $serach = [];
$searchCookie = 'Member::List';
$Member = new Member();
if(isset($_GET['reset']) && $_GET['reset']==1){
$post['search'] = [];
}
if(isset($post['search'])){
$search = $post['search'];
$cookie->set($searchCookie,$search,false);
}elseif($cookie->get($searchCookie)){
$search = $cookie->get($searchCookie);
}
$where[] = 'or';
$search['username'] = isset($search['username']) ? trim(htmlspecialchars_decode($search['username'])) : '';
if($search['username']){
$where[] = ['LIKE','username',$search['username']];
}
$search['mobile'] = isset($search['mobile']) ? trim(htmlspecialchars_decode($search['mobile'])) : '';
if($search['mobile']){
$where[] = ['LIKE','mobile',$search['mobile']];
}
$search['email'] = isset($search['email']) ? trim(htmlspecialchars_decode($search['email'])) : '';
if($search['email']){
$where[] = ['LIKE','email',$search['email']];
}
//没有搜索条件时,重置一下$where
if(count($where)==1){
$where = [];
}
$data = $Member->find()->where($where);
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']);
$members = $data->offset($pages->offset)->limit($pages->limit)->all();
$status = [
0=>'禁用',
1=>'正常'
];
return $this->render('list',[
'member'=>$members,
'search'=>$search,
'count'=>$data->count(),
'status'=>$status,
'pages' => $pages
]);
}
//新增会员
public function actionCreate()
{
$model = new Member();
if ($model->load(Yii::$app->request->post())) {
$post = Yii::$app->request->post();
$model->username = $post['Member']['username'];
$model->mobile = $post['Member']['mobile'];
$model->email = $post['Member']['email'];
$user = Member::find()->where(['mobile'=>$model->mobile])->all();
if(!empty($user)){
\Yii::$app->getSession()->setFlash('error', '该手机号已使用!');
return $this->redirect(['create']);
}
$user = Member::find()->where(['email'=>$model->email])->all();
if(!empty($user)){
\Yii::$app->getSession()->setFlash('error', '邮箱重复!');
return $this->redirect(['create']);
}
$model->avatar = $post['Member']['avatar'];
$model->setPassword($post['Member']['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 = [
0=>'禁用',
1=>'正常'
];
if ($model->load(Yii::$app->request->post())) {
$post = Yii::$app->request->post();
$mobile = $post['Member']['mobile'];
if($model->mobile != $mobile){
$user = Member::find()->where(['mobile'=>$mobile])->all();
if(!empty($user)){
\Yii::$app->getSession()->setFlash('error', '该手机号已由其他账号绑定!');
return $this->render('update',[
'model' => $model,
'status'=>$_status,
]);
}
}
$email = $post['Member']['email'];
if($model->email != $email){
$user = Member::find()->where(['email'=>$email])->all();
if(!empty($user)){
\Yii::$app->getSession()->setFlash('error', '该邮箱已有其他账号绑定!');
return $this->render('update',[
'model' => $model,
'status'=>$status,
]);
}
}
//更新密码
if(!empty($post['Member']['auth_key_new'])){
$model->setPassword($post['Member']['auth_key_new']);
$model->generateAuthKey();
}else{
$model->auth_key = $post['Member']['auth_key'];
}
$model->avatar = $post['Member']['username'];
$model->avatar = $post['Member']['avatar'];
$model->mobile = $post['Member']['mobile'];
$model->email = $post['Member']['email'];
$model->status = $post['Member']['status'];
$res = $model->save($post);
\Yii::$app->getSession()->setFlash('sucess', '账户信息更新成功!');
return $this->redirect(['list']);
}
return $this->render('update',[
'model' => $model,
'status'=>$status,
]);
}
//删除会员
public function actionRemove($id)
{
$connection=Yii::$app->db;
$transaction=$connection->beginTransaction();
try
{
$connection->createCommand()->delete(Member::tableName(), "id = '$id'")->execute();
$transaction->commit();
}
catch(Exception $ex)
{
$transaction->rollBack();
}
return $this->redirect(['list']);
}
protected function findModel($id)
{
if (($model = Member::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
+174
View File
@@ -0,0 +1,174 @@
<?php
/**
* @Author: fm453
* @Date: 2018-04-23 14:29:02
* @Last Modified by: fm453
* @Last Modified time: 2018-04-23 14:29:07
* @Email: fm453@lukegzs.com
*/
namespace backend\controllers;
use Yii;
use backend\models\Menu;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\models\AuthItem;
use yii\data\Pagination;
/**
* MenuController implements the CRUD actions for Menu model.
*/
class MenuController extends Controller
{
/**
* @inheritdoc
*/
/*public function behaviors()
{
$action = Yii::$app->controller->module->requestedRoute;
if(\Yii::$app->user->can($action)){
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['GET'],
],
],
];
}else{
//throw new \yii\web\UnauthorizedHttpException('对不起,您现在还没获此操作的权限');
echo '<div style="margin: 100px auto;text-align: center;background-color: #1ab394; color: #ffffff;width: 500px;height: 50px;line-height: 50px;border-radius: 5px;;"><h4>对不起,您现在还没获此操作的权限</h4></div>';
}
}*/
/**
* Lists all Menu models.
* @return mixed
*/
public function actionIndex()
{
$menu = new Menu();
$menu = $menu->getMenuList();
return $this->render('index', [
'menu' => $menu
]);
}
/**
* Displays a single Menu model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Menu model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Menu();
$menu = $model->getAllMenu();
$menuArr = array('0'=>"顶级菜单");
foreach($menu as $v){
$menuArr[$v['id']] = $v['name'];
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
//增加菜单同时增加权限
$post = Yii::$app->request->post();
$name = $post['Menu']['route'];
if(!empty($name)){
$auth = Yii::$app->authManager;
$createPost = $auth->createPermission($name);
$createPost->description = '创建了[' . $name. ']权限';
$auth->add($createPost);
//将权限赋给角色
$parent = $auth->createRole('超级管理员'); //创建角色对象
$child = $auth->createPermission($name); //创建权限对象
$auth->addChild($parent, $child); //添加对应关系
}
return $this->redirect(['index']);
} else {
return $this->render('create', [
'model' => $model,
'menuArr' => $menuArr,
]);
}
}
/**
* Updates an existing Menu model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$menu = $model->getAllMenu();
$menuArr = array('0'=>"顶级菜单");
foreach($menu as $v){
$menuArr[$v['id']] = $v['name'];
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['index']);
} else {
return $this->render('update', [
'model' => $model,
'menuArr' => $menuArr,
]);
}
}
/**
* Deletes an existing Menu model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
//通过id找到router
$MenuModel = new Menu();
$name = $MenuModel->getRouteById($id);
//删除菜单同时删除权限
if(!empty($name)){
$model = new AuthItem();
$model->setScenario(AuthItem:: SCENARIOS_DELETE);
$model-> name = $name;
$res = $model->romoveItem();
}
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Menu model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Menu the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Menu::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
+146
View File
@@ -0,0 +1,146 @@
<?php
/**
* @Author: fm453
* @Date: 2018-04-23 14:29:14
* @Last Modified by: fm453
* @Last Modified time: 2021-09-09 10:11:46
* @Email: fm453@lukegzs.com
*/
namespace backend\controllers;
use Yii;
use backend\models\AuthItem;
use backend\models\Menu;
use backend\models\AuthItemChild;
use yii\web\Controller;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* PermissionController implements the CRUD actions for AuthItemChild model.
*/
class PermissionController extends Controller
{
public $enableCsrfValidation = false;
/**
* @inheritdoc
*/
/*public function beforeAction($action)
{
$action = Yii::$app->controller->module->requestedRoute;
if(\Yii::$app->user->can($action)){
return true;
}else{
echo '<div style="margin: 100px auto;text-align: center;background-color: #1ab394; color: #ffffff;width: 500px;height: 50px;line-height: 50px;border-radius: 5px;;"><h4>对不起,您现在还没获此操作的权限</h4></div>';
}
}*/
//给角色配置权限
public function actionSet($id)
{
$auth = Yii::$app->authManager;
$Permission = $auth->getPermissionsByRole($id);
$items = AuthItem::find()->joinWith('permissionName')->where(['type' => '2'])->all();
$menu = Menu::find()->where(['parent'=>'0'])->all();
if (Yii::$app->request->post()) {
$post = Yii::$app->request->post();
$parent = $auth->createRole($post['parent']); //创建角色对象
if(!empty($post['permission'])){
$auth->removeChildren($parent);
foreach($post['permission'] as $v){
$child = $auth->createPermission($v); //创建权限对象
$auth->addChild($parent, $child); //重新增加对应权限
}
}else{
$auth->removeChildren($parent); //权限节点全不选择,清除对应权限
}
return $this->redirect(['permission/set','id'=>$parent->name]);
} else {
return $this->render('set', [
'Permission' => $Permission,
'items'=>$items,
'id'=>$id,
'menu' =>$menu
]);
}
}
/**
* Lists all AuthItemChild models.
* @return mixed
*/
public function actionIndex()
{
$dataProvider = new ActiveDataProvider([
'query' => AuthItemChild::find(),
]);
return $this->render('index', [
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single AuthItemChild model.
* @param string $parent
* @param string $child
* @return mixed
*/
public function actionView($parent, $child)
{
return $this->render('view', [
'model' => $this->findModel($parent, $child),
]);
}
/**
* Creates a new AuthItemChild model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new AuthItemChild();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'parent' => $model->parent, 'child' => $model->child]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Deletes an existing AuthItemChild model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param string $parent
* @param string $child
* @return mixed
*/
public function actionDelete($parent, $child)
{
$this->findModel($parent, $child)->delete();
return $this->redirect(['index']);
}
/**
* Finds the AuthItemChild model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param string $parent
* @param string $child
* @return AuthItemChild the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($parent, $child)
{
if (($model = AuthItemChild::findOne(['parent' => $parent, 'child' => $child])) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
+114
View File
@@ -0,0 +1,114 @@
<?php
/**
* @Author: fm453
* @Date: 2018-04-23 14:29:56
* @Last Modified by: fm453
* @Last Modified time: 2021-09-08 13:28:31
* @Email: fm453@lukegzs.com
*/
namespace backend\controllers;
use Yii;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use backend\models\LoginForm;
use backend\models\Menu;
use backend\components\Helper;
/**
* Site controller
*/
class SiteController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'actions' => ['login', 'error'],
'allow' => true,
],
[
'actions' => ['logout', 'index'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['get'],
],
],
];
}
/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
];
}
public function actionIndex()
{
$user_id=Yii::$app->user->identity->getId();
$user_info = Yii::$app->authManager->getRolesByUser($user_id);
$menu = new Menu();
$menu = $menu->getLeftMenuList();
return $this->render('index',[
'menu' => $menu,
'user_info' => key($user_info)
]);
}
public function actionList()
{
return $this->render('list');
}
public function actionLogin()
{
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
$model->loginLog(); //将登陆记录到日志表
return $this->goBack();
} else {
return $this->render('login', [
'model' => $model,
]);
}
}
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
public function actionError($e){
echo $e;
}
}
+211
View File
@@ -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.');
}
}
}