# @Date: 2022-05-22T07:30:22+08:00 # @Email: fm453@lukegzs.com # @Last modified by: fm453 # @Last modified time: 2022-05-22T07:30:22+08:00 # @Copyright: www.hiluker.cn namespace backend\controllers; use Yii; use yii\data\Pagination; use yii\helpers\Url; use addons\models\AcCarBrand; use addons\models\AcCarSeries; class CarseriesController extends Common { //主界面 public function actionIndex() { return $this->render('../layouts/dev', []); } //车型列表 public function actionList() { global $_HI,$_FM; $pid = Yii::$app->session->get('pid'); $session = Yii::$app->session; $model = new AcCarSeries(); $where = $where2 = []; $where2[]='and'; $where2[] = ['in','pid',[0,$pid]]; $post = Yii::$app->request->post(); $search = []; $searchSession = 'carseries::search'; if (isset($_GET['reset']) && $_GET['reset']==1) { $post['search'] = []; } if (isset($post['search'])) { $search = $post['search']; $session->set($searchSession, $search); } elseif ($session->get($searchSession)) { $search = $session->get($searchSession); } $search['title'] = isset($search['title']) ? trim(htmlspecialchars_decode($search['title'])) : ''; if (!empty($search['title'])) { $where2[] = ['LIKE','title',$search['title']]; } $search['brand'] = isset($search['brand_id']) ? trim(htmlspecialchars_decode($search['brand'])) : ''; $search['brand_id'] = isset($search['brand_id']) ? (int)$search['brand_id'] : 0; if (!empty($search['brand_id'])) { $where['brand_id'] = $search['brand_id']; } $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)->indexBy('id')->all(); $status = Yii::$app->params['CommonStatus']; $series = $bids = []; foreach ($res as $r) { $r = $r->toArray(); $series[$r['id']] = $r; $bids[] = $r['brand_id']; } $AcCarBrand = new AcCarBrand(); $brands = []; $_brands = $AcCarBrand->find()->where(['in','id',$bids])->indexBy('id')->all(); foreach ($_brands as $s) { $brands[$s->id] = $s->toArray(); } return $this->render('list', [ 'series' => $series, 'pager' => $pages, 'brands'=>$brands, 'status'=>$status, 'search'=>$search ]); } //车型列表 public function actionSelect() { global $_HI,$_FM; $pid = Yii::$app->session->get('pid'); $session = Yii::$app->session; $model = new AcCarSeries(); $where = $where2 = []; $where2[]='and'; $where2[] = ['in','pid',[0,$pid]]; $post = Yii::$app->request->post(); $search = []; $searchSession = 'carseries::search'; if (isset($_GET['reset']) && $_GET['reset']==1) { $post['search'] = []; } if (isset($post['search'])) { $search = $post['search']; $session->set($searchSession, $search); } elseif ($session->get($searchSession)) { $search = $session->get($searchSession); } $search['title'] = isset($search['title']) ? trim(htmlspecialchars_decode($search['title'])) : ''; if (!empty($search['title'])) { $where2[] = ['LIKE','title',$search['title']]; } // $search['brand'] = isset($search['brand']) ? trim(htmlspecialchars_decode($search['brand'])) : ''; $search['brand'] = isset($search['brand_id']) ? trim(htmlspecialchars_decode($search['brand'])) : ''; $search['brand_id'] = isset($search['brand_id']) ? (int)$search['brand_id'] : 0; if (!empty($search['brand_id'])) { $where['brand_id'] = $search['brand_id']; } $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)->indexBy('id')->all(); $status = Yii::$app->params['CommonStatus']; $series = $bids = []; foreach ($res as $r) { $r = $r->toArray(); $series[$r['id']] = $r; $bids[$r['brand_id']] = $r['brand_id']; } $AcCarBrand = new AcCarBrand(); $brands = []; $_brands = $AcCarBrand->find()->where(['in','id',$bids])->indexBy('id')->all(); foreach ($_brands as $s) { $brands[$s->id] = $s->toArray(); } $callback = !empty(Yii::$app->request->get('callback')) ? Yii::$app->request->get('callback') : 'car'; return $this->render('select', [ 'series' => $series, 'pager' => $pages, 'brands'=>$brands, 'status'=>$status, 'search'=>$search, 'callback'=>$callback ]); } //新增车型 public function actionNew() { global $_HI,$_FM; $status = Yii::$app->params['CommonStatus']; $AcCarSeries = new AcCarSeries(); $AcCarSeries->status_code = 0; return $this->render('modify', ['status'=>$status,'detail'=>$AcCarSeries->toArray()]); } //更新车型 public function actionEdit() { global $_HI,$_FM; $status = Yii::$app->params['CommonStatus']; $model = new AcCarSeries(); $id = Yii::$app->request->get('id'); $pid = Yii::$app->session->get('pid'); $res = $model->find()->where(['id' => $id])->one(); $parmas = []; if ($res) { $res = $res->toArray(); $parmas = json_decode($res['params'], true); } else { $res = []; } //查询对应的品牌 $brand = AcCarBrand::find()->where(['id' => $res['brand_id']])->one(); if ($brand) { $res['brand_title'] = $brand->title; } return $this->render('modify', [ 'detail'=>$res, 'params'=>$parmas, '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 = ['brand_id'=>'brand_id','status_code'=>'status_code','deleted'=>'deleted']; foreach ($cols as $col=>$key) { $data[$col] = isset($post[$key]) ? (int)$post[$key] : 0; } $params = $post['p']; $cols = ['height','length','width','kg','seat']; foreach ($cols as $col) { $params[$col] = isset($params[$col]) ? (int)$params[$col] : 0; } $data['params'] = json_encode($params, JSON_UNESCAPED_UNICODE); $data['create_at'] = time(); $data['update_at'] = $data['create_at']; $data['pid'] = $pid; //格式化车型资料 $AcCarSeries = new AcCarSeries(); foreach ($data as $key=>$val) { $AcCarSeries->$key = $val; } //车型资料入库 if ($id) { $AcCarSeries->id = $id; unset($data['create_at']); $res = $AcCarSeries->updateAll($data, ['id'=>$id]); } else { $res = $AcCarSeries->save(); $id = $AcCarSeries->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 AcCarSeries(); foreach ($data as $key=>$val) { $model->$key = $val; } $model->id = $id; $res = $model->updateAll($data, $where); $return = []; $return['url'] = Url::toRoute('carowner/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 AcCarSeries(); $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)); } }