pid) { $this->result('您正使用本系统内部接口,禁止非法链接使用!'); } return parent::beforeAction($action); } //主界面 public function actionIndex() { $apis = [ 'list'=>'车辆列表', 'detail'=>'详情', 'add'=>'添加', 'edit'=>'编辑', 'delete'=>'删除', 'show'=>'显示', 'hide'=>'隐藏', 'ajax'=>'ajax操作' ]; $this->result('您正使用CMTS-GM系统车辆管理接口!', $apis, 200); } //车辆列表 public function actionList() { $s = $this->search(); $res = $s['res']; if (!$res) { $this->result('没有查询到相应的数据!', [], 0); } $return = []; $return['code'] = 200; $return['msg'] = '车辆查询成功!'; $return['data']= [ 'total'=>$s['query']->count(), 'cars' => $res, 'series' => $s['series'], 'brands' => $s['brands'], 'owners' => $s['owners'], 'users' => $s['users'], 'page'=>$this->page ]; $this->result($return['msg'], $return['data'], $return['code']); } public function actionDetail() { $id = (int)Yii::$app->request->get('id'); if(!$id) $this->result('请求错误,未携带ID参数'); $model = new AcCar(); $res = $model->findOne($id)->toArray(); if(!$res) $this->result('未查询到相应数据'); $car = $this->showCar($res); $this->result('车辆信息查询完成', $car, 200); } public function actionAdd() { //数据预检查、编排 $data = $this->preSave('add'); if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100); //保存资料 $model = new AcCar(); foreach ($data as $key=>$val) { $model->$key = $val; } $res = $model->save(); $msg = '数据保存失败!'; if(!$res) $this->result($msg, [], 100); $msg = '数据保存成功!'; $return = []; $return['id']= $model->attributes['id']; //获取插入后id; $this->result($msg,$return, 200); } //编辑客车资料 public function actionEdit() { $detail = $this->preUpdate(); //数据预检查、编排 $data = $this->preSave('edit'); if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100); //保存资料 foreach ($data as $key=>$val) { $detail->$key = $val; } $res = $detail->save(); $msg = '数据编辑失败!'; if(!$res) $this->result($msg, [], 100); $msg = '数据编辑成功!'; $return = []; $return['data']= $detail; $this->result($msg,$return, 200); } //删除车辆 public function actionDelete() { $detail = $this->preUpdate(); $data = []; $data['update_at'] = time(); $data['deleted'] = $detail->deleted + 1; //保存资料 foreach ($data as $key=>$val) { $detail->$key = $val; } $res = $detail->save(); $msg = '车型数据删除失败!'; if(!$res) $this->result($msg, [], 100); $msg = '车型数据删除成功!'; $this->result($msg,[], 200); } public function actionAjax() { $detail = $this->preUpdate(); $get = Yii::$app->request->get(); $msg = ''; $errorCode = 0; $detail->update_at = time(); switch ($get['do']) { case 'hide': $detail->status_code = 0; $res = $detail->save(); if ($res) { $msg = '车型隐藏标记成功'; $errorCode = 200; } else { $msg = '车型隐藏标记失败'; } break; case 'show': $detail->status_code = 1; $res = $detail->save(); if ($res) { $msg = '车型显示标记成功'; $errorCode = 200; } else { $msg = '车型显示标记失败'; } break; default: break; } $this->result($msg,[], $errorCode); } private function search(){ $pid = $this->pid; $return = []; $model = new AcCar(); $where = $where2 = []; $where['pid'] = $pid; $post = $this->postdata; $search = $post['search'] ?? []; $search['title'] = isset($search['title']) ? trim(htmlspecialchars_decode($search['title'])) : ''; if (!empty($search['title'])) { $where2[] = 'or'; $where2[] = ['LIKE', 'car_no', $search['title']]; $where2[] = ['LIKE', 'num_left', $search['title']]; $where2[] = ['LIKE', 'num_frame', $search['title']]; } $search['brand_id'] = isset($search['brand_id']) ? (int)$search['brand_id'] : 0; if (!empty($search['brand_id'])) { $where['brand_id'] = $search['brand_id']; } $search['series_id'] = isset($search['series_id']) ? (int)$search['series_id'] : 0; if (!empty($search['series_id'])) { $where['series_id'] = $search['series_id']; } $where['deleted'] = 0; $data = $model->find()->where($where); if ($search['title']) { $data = $data->andwhere($where2); } $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $this->pageSize]); $res = $data->offset($pages->offset)->limit($pages->limit)->orderby('id DESC')->all(); $oids = $mids = $bids = $sids = []; if ($res) { foreach ($res as $r) { $oids[] = $r->owner_id; $mids[] = $r->mid; $bids[] = $r->brand_id; $sids[] = $r->series_id; } } $AcCarOwner = new AcCarOwner(); $where = []; $where['pid'] = $pid; $_owners = $AcCarOwner->find()->where($where)->andwhere(['in', 'id', $oids])->all(); $owners = []; foreach ($_owners as $s) { $owners[$s->id] = $s->toArray(); } $User = new User(); $where = []; $_users = $User->find()->where($where)->andwhere(['in', 'id', $mids])->all(); $users = []; foreach ($_users as $s) { $users[$s->id] = $s->toArray(); } $AcUserExt = new AcUserExt(); $userExts = $AcUserExt->find()->where(['pid' => $pid, 'deleted' => 0])->andWhere(['in', 'mid', $mids])->all(); $exts = []; if ($userExts) { foreach ($userExts as $v) { $exts[$v->mid][$v->key] = $v->value; } if ($exts) { foreach ($exts as $i => $ext) { $_owner = $users[$i] ? $users[$i] : []; if ($ext['title']) { $_owner['owner_title'] = $ext['title']; } if ($ext['nickname']) { $_owner['owner_title'] = $ext['nickname']; } if ($ext['realname']) { $_owner['owner_title'] = $ext['realname']; } $users[$i]['username'] = $_owner['owner_title'] . '【' . $users[$s->id]['mobile'] . '】'; } } } $AcCarSeries = new AcCarSeries(); $where = []; $series = []; $_series = $AcCarSeries->find()->where($where)->andwhere(['in', 'id', $sids])->all(); if ($_series) { foreach ($_series as $r) { $series[$r->id] = $r->toArray(); } } $AcCarBrand = new AcCarBrand(); $where = []; $_brands = $AcCarBrand->find()->where($where)->andwhere(['in', 'id', $bids])->all(); if ($_brands) { foreach ($_brands as $r) { $brands[$r->id] = $r->toArray(); } } $cars = []; if ($res) { foreach ($res as $r) { $car = $r->toArray(); $car['numbers'] = $car['num_p'] . $car['num_area'] . $car['num_left']; $car['numbers'] = $car['num_p'] ? $car['numbers'] : $car['num_frame']; $cars[$r->id] = $car; } } return ['res'=>$cars,'query'=>$data,'owners'=>$owners,'users'=>$users,'brands'=>$brands,'series'=>$series]; } /* * 数据更新前的预检查,返回对应关联数据 * 必须确保get与post数据中均包含需更新的数据id且一致 * */ private function preUpdate() { $id = (int)Yii::$app->request->get('id'); if(!$id) $this->result('请求错误,未携带ID参数'); $post = $this->postdata; if($post['id'] != $id) $this->result('传参id与请求数据不匹配',[],403); $model = new AcCar(); $res = $model->findOne($id); if(!$res) $this->result('未查询到相应数据',[],404); return $res; } /* * 数据保存前的预检查(查重、参数校验等) * 要保存的数据[],直接从post中取出 * @op,操作类型(add,edit……) * 校验机制: * 编辑数据时,必须用get方式传入参数id,并与post进来的数据id进行比对,只有一致时才能继续; * 查重:禁止录入重复数据 * 返回:校验重组后的数据 * */ private function preSave($op) { $post = $this->postdata; $car_no = trim($post['car_no']) ?? '';//车牌号 $num_frame = trim($post['num_frame']) ?? '';//车架号 if(!$car_no && !$num_frame) $this->result('请传入车牌/架号'); $data = []; //格式化数据 //目标键=》POST键 $cols = ['title' => 'title', 'num_frame' => 'num_frame', 'num_p' => 'num_p', 'num_area' => 'num_area', 'num_left' => 'num_left','car_no'=>'car_no']; foreach ($cols as $col => $key) { $data[$col] = isset($post[$key]) ? trim($post[$key]) : ''; } $model = new AcCar(); switch ($op) { case 'add': if(!$num_frame){ $error = "车牌号已存在"; $hasOne = $model->find()->where(['deleted'=>0,'pid'=>$this->pid,'car_no'=>$car_no])->one(); }else{ $error = "车架/牌号已存在"; $hasOne = $model->find()->where(['deleted'=>0,'pid'=>$this->pid])->andWhere(['OR',['LIKE', 'car_no', $car_no],['LIKE', 'num_frame', $num_frame]])->one(); } if ($hasOne) { $this->result($error); } $data['create_at'] = time(); $data['update_at'] = $data['create_at']; break; case 'edit': $id = (int)Yii::$app->request->get('id'); $_id = (int)$post['id']; if(!$id != $_id) $this->result('id参数不匹配,请检查'); $hasOne = $model->find()->select('id')->where(['car_no'=>$car_no,'id'<>$id,'pid'=>$this->pid,'deleted'=>0])->indexBy('id')->one(); if ($hasOne) { $this->result('车牌号重复,请检查'); } $data['update_at'] = time(); break; } $data['pid'] = $this->pid; $cols = ['brand_id' => 'brand_id', 'series_id' => 'series_id', 'owner_id' => 'owner_id', 'status_code' => 'status_code', 'deleted' => 'deleted']; foreach ($cols as $col => $key) { $data[$col] = isset($post[$key]) ? (int)$post[$key] : 0; } $cols = ['remark' => 'editor']; foreach ($cols as $col => $key) { $data[$col] = isset($post[$key]) ? htmlspecialchars($post[$key]) : ''; } //填补mid参数 if($data['owner_id']){ $AcCarOwner = new AcCarOwner(); $owner = $AcCarOwner->findOne($data['owner_id']); if ($owner) { $data['mid'] = $owner->mid; } } return $data; } //格式化显示车辆信息 @car 车辆信息原始数据 [] private function showCar($car){ $oid = $car['owner_id']; $mid = $car['mid']; $bid = $car['brand_id']; $sid = $car['series_id']; $AcCarSeries = new AcCarSeries(); $series = $AcCarSeries->findOne($sid); $car['series'] = $series; $AcCarBrand = new AcCarBrand(); $brand = $AcCarBrand->findOne($bid); $car['brand'] = $brand; $AcCarOwner = new AcCarOwner(); $owner = $AcCarOwner->findOne($oid); $car['owner'] = $owner; $CUser = new User(); $user = $CUser->findOne($mid)->toArray(); $AcUserExt = new AcUserExt(); $userExts = $AcUserExt->find()->where(['pid' => $this->pid, 'deleted' => 0,'mid'=>$mid])->all(); $ext = []; foreach ($userExts as $v) { $ext[$v->key] = $v->value; } if ($ext['title']) { $user['owner_title'] = $ext['title']; } if ($ext['nickname']) { $user['owner_title'] = $ext['nickname']; } if ($ext['realname']) { $user['owner_title'] = $ext['realname']; } $car['user'] = $user; $car['numbers'] = $car['num_p'] . $car['num_area'] . $car['num_left']; $car['numbers'] = $car['num_p'] ? $car['numbers'] : $car['num_frame']; return $car; } }