ADD:添加后台管理端接口;

DEL:删除不必要的头部注释;
FIX:修正若干已知错误;
This commit is contained in:
2025-07-13 19:40:10 +08:00
parent 0a45e55cca
commit 270b881437
66 changed files with 8618 additions and 8129 deletions
+217 -274
View File
@@ -1,17 +1,9 @@
<?php
# @Author: 嗨噜客(三亚) <fm453>
# @Date: 2022-05-22T07:30:22+08:00
# @Email: fm453@lukegzs.com
# @Last modified by: fm453
# @Last modified time: 2024-08-09T09:17:27+08:00
# @Copyright: www.hiluker.cn
namespace api\controllers\gm\v1;
use Yii;
use yii\data\Pagination;
use yii\helpers\Url;
use addons\models\AcCarBrand;
use addons\models\AcCarSeries;
@@ -20,320 +12,271 @@
//主界面
public function actionIndex()
{
return $this->render('../layouts/dev', []);
$apis = [
'list'=>'车型列表',
'detail'=>'详情',
'add'=>'添加',
'edit'=>'编辑',
'delete'=>'删除',
'show'=>'显示',
'hide'=>'隐藏',
'ajax'=>'ajax操作'
];
$this->result('您正使用CMTS-GM系统品牌车型管理接口!', $apis, 200);
}
//车型列表
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);
} else if ($session->get($searchSession)) {
$search = $session->get($searchSession);
}
$s = $this->search();
$res = $s['res'];
if (!$res) {
$this->result('没有查询到相应的数据!', [], 0);
}
$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
]);
$return = [];
$return['code'] = 200;
$return['msg'] = '车型查询成功!';
$return['data']= [
'total'=>$s['query']->count(),
'series' => $res,
'brands' => $s['brands'],
'page'=>$this->page
];
$this->result($return['msg'], $return['data'], $return['code']);
}
//车型列表
public function actionSelect()
public function actionDetail()
{
global $_HI, $_FM;
$pid = Yii::$app->session->get('pid');
$session = Yii::$app->session;
$id = (int)Yii::$app->request->get('id');
if(!$id) $this->result('请求错误,未携带ID参数');
$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);
} else if ($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
]);
$res = $model->findOne($id)->toArray();
if(!$res) $this->result('未查询到相应数据');
$this->result('车型信息查询完成', $res, 200);
}
//新增车型
public function actionNew()
public function actionAdd()
{
global $_HI, $_FM;
$status = Yii::$app->params['CommonStatus'];
$AcCarSeries = new AcCarSeries();
$AcCarSeries->status_code = 0;
return $this->render('modify', ['status' => $status, 'detail' => $AcCarSeries->toArray()]);
$post = $this->postdata;
$data = [];
//格式化数据 为空的项则不修改
$data['title'] = trim($post['title']);
if(!$data['title']) $this->result('请传入车型名称');
$data['brand_id'] = (int)$post['brand_id'];
if(!$data['brand_id']) $this->result('请传入所属品牌');
$data['params'] = $post['params'] ?? [];
$data['status_code'] = 1; //状态代码:1可用
$data['create_at'] = time();
$data['update_at'] = $data['create_at'];
//数据预检查、编排
$data = $this->preSave($data,'add');
if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100);
//保存资料
$model = new AcCarSeries();
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()
{
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 = [];
}
$detail = $this->preUpdate();
$post = $this->postdata;
//查询对应的品牌
$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;
//格式化数据 为空的项则不修改
$data['title'] = trim($post['title']);
if(!$data['title']) $this->result('请传入车型名称');
$data['brand_id'] = (int)$post['brand_id'];
if(!$data['brand_id']) $this->result('请传入所属品牌');
//格式化车型资料
$AcCarSeries = new AcCarSeries();
foreach ($data as $key => $val) {
$AcCarSeries->$key = $val;
}
$data['params'] = $post['params'] ?? [];
$data['status_code'] = 1; //状态代码:1可用
$data['update_at'] = time();
$data = $this->preSave($data,'edit');
if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100);
//车型资料入库
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();
}
//保存资料
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($id)
public function actionDelete()
{
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;
$detail = $this->preUpdate();
$data = [];
$data['update_at'] = time();
$data['deleted'] = 1;
$data['deleted'] = $detail->deleted + 1;
$model = new AcCarSeries();
foreach ($data as $key => $val) {
$model->$key = $val;
}
//保存资料
foreach ($data as $key=>$val) {
$detail->$key = $val;
}
$res = $detail->save();
$msg = '车型数据删除失败!';
if(!$res) $this->result($msg, [], 100);
$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));
$msg = '车型数据删除成功!';
$this->result($msg,[], 200);
}
public function actionAjax()
{
$detail = $this->preUpdate();
$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));
}
$msg = '';
$errorCode = 0;
$detail->update_at = time();
switch ($get['do']) {
case 'hide':
$data['status_code'] = 0;
$model->id = $id;
$res = $model->updateAll($data, $where);
$detail->status_code = 0;
$res = $detail->save();
if ($res) {
$return['msg'] = '车型隐藏标记成功';
$return['errorcode'] = 200;
$return['data'] = ['css' => 'info'];
$msg = '车型隐藏标记成功';
$errorCode = 200;
} else {
$return['msg'] = '车型隐藏标记失败';
$return['errorcode'] = 0;
$msg = '车型隐藏标记失败';
}
break;
case 'show':
$data['status_code'] = 1;
$model->id = $id;
$res = $model->updateAll($data, $where);
$detail->status_code = 1;
$res = $detail->save();
if ($res) {
$return['msg'] = '车型显示标记成功';
$return['errorcode'] = 200;
$return['data'] = ['css' => 'default'];
$msg = '车型显示标记成功';
$errorCode = 200;
} else {
$return['msg'] = '车型显示标记失败';
$return['errorcode'] = 0;
$msg = '车型显示标记失败';
}
break;
default:
break;
}
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
$this->result($msg,[], $errorCode);
}
private function search(){
$pid = $this->pid;
$return = [];
$model = new AcCarSeries();
$where = $where2 = [];
$where2[] = 'and';
// $where['pid'] = $pid;
$post = $this->postdata;
$search = $post['search'] ?? [];
$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_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' => $this->pageSize]);
$res = $data->offset($pages->offset)->limit($pages->limit)->indexBy('id')->all();
$series = $bids = [];
foreach ($res as &$s) {
$s = $s->toArray();
$brands[$s['id']] = $s;
}
$AcCarBrand = new AcCarBrand();
$brands = [];
$_brands = $AcCarBrand->find()->where(['in', 'id', $bids])->indexBy('id')->all();
unset($s);
foreach ($_brands as $s) {
$brands[$s->id] = $s->toArray();
}
return ['res'=>$res,'query'=>$data,'brands'=>$brands];
}
//数据更新前的预检查,返回对应关联数据
private function preUpdate()
{
$id = (int)Yii::$app->request->get('id');
if(!$id) $this->result('请求错误,未携带ID参数');
$post = $this->postdata;
if($post['id'] != $id) $this->result('传参与请求数据不匹配',[],403);
$model = new AcCarSeries();
$res = $model->findOne($id);
if(!$res) $this->result('未查询到相应数据',[],404);
return $res;
}
/*
* 数据保存前的预检查(查重、参数校验等)
* @data,要保存的数据[]
* @op,操作类型(add,edit……)
* 校验机制:
* 编辑数据时,必须用get方式传入参数id,并与post进来的数据id进行比对,只有一致时才能继续;
* 查重:禁止录入重复数据
* 返回:校验重组后的data
* */
private function preSave($data,$op)
{
$title = trim($data['title']);
$id = 0;
$model = new AcCarBrand();
switch ($op) {
case 'add':
$hasOne = $model->find()->where(['title'=>$title])->one();
if ($hasOne) {
$this->result('品牌名称重复,请检查重录');
}
break;
case 'edit':
$id = (int)$data['id'];
$origin = $model->findOne($id);
if(!$origin) $this->result('指定id('.$id.')的数据不存在,请检查');
$hasOne = $model->find()->select('id')->where(['title'=>$title,'id'<>$id])->indexBy('id')->one();
if ($hasOne) {
$this->result('品牌名称重复,请检查重录');
}
break;
}
//处理车型相关参数(数组内检查完成后,转json)
$params = $data['params'];
$arr = ['length','width','height','kg'];
foreach ($arr as $v) {
$params[$v] = isset($params[$v]) ? (int)$params[$v] : 0;
}
$arr = ['gas'=>'汽油','diesel'=>'柴油','e-power'=>'纯电','hybrid'=>'混动'];
$arr_keys = array_keys($arr);
//设置默认能源类型为 汽油
$energy = $params['energy'] ?? '';
if(!empty($energy) && array_key_exists($energy,$arr_keys)){
$params['energy'] = $energy;
}else{
$params['energy'] = 'gas';
}
$data['params'] = json_encode($params, JSON_UNESCAPED_UNICODE);
return $data;
}
}