ctms/admins/controllers/FansController.php
2025-04-10 23:19:13 +08:00

190 lines
5.6 KiB
PHP
Executable File

<?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=>'正常'
];
}
}