# @Date: 2022-05-22T07:28:54+08:00 # @Email: fm453@lukegzs.com # @Last modified by: fm453 # @Last modified time: 2022-05-22T07:29:12+08:00 # @Copyright: www.hiluker.cn namespace backend\controllers; use Yii; use yii\data\Pagination; use yii\helpers\Url; use addons\models\AcCarBrand; class CarbrandController extends Common { //主界面 public function actionIndex() { return $this->render('../layouts/dev', []); } //车品牌列表 public function actionList() { $pid = Yii::$app->session->get('pid'); $model = new AcCarBrand(); $where = $where2 = []; $where2[]='and'; $where['pid'] = $pid; $post = Yii::$app->request->post(); $search = []; $search['title'] = isset($post['title']) ? trim(htmlspecialchars_decode($post['title'])) : ''; if (!empty($search['title'])) { $where2[] = ['LIKE','title',$search['title']]; } $where['deleted'] = 0; $data = $model->find()->where($where); if ($search['title']) { $data = $data->andwhere($where2); } $pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']); $res = $data->offset($pages->offset)->limit($pages->limit)->all(); $brands=[]; foreach ($res as $s) { $s = $s->toArray(); $brands[$s['id']] = $s; } $status = Yii::$app->params['CommonStatus']; return $this->render('list', [ 'brands' => $brands, 'pager' => $pages, 'status'=>$status, 'search'=>$search ]); } //车品牌列表 public function actionSelect() { global $_HI,$_FM; $pid = Yii::$app->session->get('pid'); $model = new AcCarBrand(); $where = $where2 = []; $where2[]='and'; $where['pid'] = $pid; $post = Yii::$app->request->post(); $search = []; $search['title'] = isset($post['title']) ? trim(htmlspecialchars_decode($post['title'])) : ''; if (!empty($search['title'])) { $where2[] = ['LIKE','title',$search['title']]; } $where['deleted'] = 0; $data = $model->find()->where($where); if ($search['title']) { $data = $data->andwhere($where2); } $pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => '20']); $res = $data->offset($pages->offset)->limit($pages->limit)->all(); $brands=[]; foreach ($res as $s) { $s = $s->toArray(); $brands[$s['id']] = $s; } $status = Yii::$app->params['CommonStatus']; $callback = !empty(Yii::$app->request->get('callback')) ? Yii::$app->request->get('callback') : 'car'; return $this->render('select', [ 'brands' => $brands, 'pager' => $pages, 'status'=>$status, 'search'=>$search, 'callback'=>$callback ]); } //新增车品牌 public function actionNew() { global $_HI,$_FM; $status = Yii::$app->params['CommonStatus']; $AcCarBrand = new AcCarBrand(); $AcCarBrand->status_code = 0; return $this->render('modify', ['status'=>$status,'detail'=>$AcCarBrand->toArray()]); } //更新车品牌 public function actionEdit() { global $_HI,$_FM; $model = new AcCarBrand(); $id = Yii::$app->request->get('id'); $pid = Yii::$app->session->get('pid'); $res = $model->find()->where(['id' => $id])->one(); if ($res) { $res = $res->toArray(); } else { $res = []; } $status = Yii::$app->params['CommonStatus']; return $this->render('modify', [ 'detail'=>$res, 'status'=>$status, 'id'=>$id, ]); } //保存车品牌资料 public function actionSave() { $post = Yii::$app->request->post(); $id = (int)$post['id']; $pid = Yii::$app->session->get('pid'); //格式化数据 $data = []; $data['title'] = trim($post['title']); //目标键=》POST键 $cols = ['status_code'=>'status_code','deleted'=>'deleted']; foreach ($cols as $col=>$key) { $data[$col] = isset($post[$key]) ? (int)$post[$key] : 0; } $data['create_at'] = time(); $data['update_at'] = $data['create_at']; $data['pid'] = $pid; //格式化资料 $AcCarBrand = new AcCarBrand(); foreach ($data as $key=>$val) { $AcCarBrand->$key = $val; } //资料入库 if ($id) { $AcCarBrand->id = $id; unset($data['create_at']); $res = $AcCarBrand->updateAll($data, ['id'=>$id]); } else { $res = $AcCarBrand->save(); $id = $AcCarBrand->attributes['id']; //获取插入后id } if ($res) { \Yii::$app->getSession()->setFlash('success', '车主资料保存成功!'); return $this->redirect(['edit','id'=>$id]); } else { \Yii::$app->getSession()->setFlash('warning', '车主资料保存失败!'); return $this->goBack(); } } //删除用户 public function actionDelete($id) { global $_HI,$_FM; $get = Yii::$app->request->get(); $post = Yii::$app->request->post(); $id = (int)$get['id']; $data = $where = []; $where['pid'] = Yii::$app->session->get('pid'); $where['id'] = $id; $data['update_at'] = time(); $data['deleted'] = 1; $model = new AcCarBrand(); foreach ($data as $key=>$val) { $model->$key = $val; } $model->id = $id; $res = $model->updateAll($data, $where); $return = []; $return['url'] = Url::toRoute('carbrand/list'); $return['timeout'] = 3; //3秒后自动跳转 $return['status'] = 0; if ($res) { $return['msg'] = '删除成功'; $return['errorcode'] = 200; $return['data'] = $id; } else { $return['msg'] = '删除失败'; $return['errorcode'] = 0; } exit(json_encode($return, JSON_UNESCAPED_UNICODE)); } public function actionAjax() { $get = Yii::$app->request->get(); $post = Yii::$app->request->post(); $id = (int)$get['id']; $data = $where = []; $where['pid'] = Yii::$app->session->get('pid'); $where['id'] = $id; $data['update_at'] = time(); $model = new AcCarBrand(); $res = $model->find()->where(['id' => $id])->one(); $return = []; $return['timeout'] = 1; //3秒后自动跳转 $return['status_code'] = 0; $return['ajax'] = 1; if ($res) { $res = $res->toArray(); } else { $return['msg'] = '品牌数据不存在'; $return['errorcode'] = 404; exit(json_encode($return, JSON_UNESCAPED_UNICODE)); } switch ($get['do']) { case 'hide': $data['status_code'] = 0; $model->id = $id; $res = $model->updateAll($data, $where); if ($res) { $return['msg'] = '品牌隐藏标记成功'; $return['errorcode'] = 200; $return['data'] = ['css'=>'info']; } else { $return['msg'] = '品牌隐藏标记失败'; $return['errorcode'] = 0; } break; case 'show': $data['status_code'] = 1; $model->id = $id; $res = $model->updateAll($data, $where); if ($res) { $return['msg'] = '品牌显示标记成功'; $return['errorcode'] = 200; $return['data'] = ['css'=>'default']; } else { $return['msg'] = '品牌显示标记失败'; $return['errorcode'] = 0; } break; default: break; } exit(json_encode($return, JSON_UNESCAPED_UNICODE)); } }