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

207 lines
6.5 KiB
PHP
Executable File

<?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.');
}
}
}