ctms/ctms-api/controllers/gm/v1/AlbumsController.php
fm453 4b842ebf3d ADD:添加后台管理端接口;
DEL:删除不必要的头部注释;
FIX:修正若干已知错误;
2025-06-30 09:40:13 +08:00

307 lines
8.8 KiB
PHP
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace api\controllers\gm\v1;
use Yii;
use yii\data\Pagination;
use yii\helpers\Url;
use addons\models\AcCar;
use addons\models\AcAlbums;
use addons\models\AcAlbumPics;
use addons\models\AcAlbumVersion;
//TD:未完成
class AlbumsController extends Common
{
public function beforeAction($action)
{
if (!$this->pid) {
$this->result('您正使用本系统内部接口,禁止非法链接使用!');
}
return parent::beforeAction($action);
}
public function actionIndex()
{
$apis = [
'list'=>'相册列表',
'detail'=>'详情',
'add'=>'添加',
'edit'=>'编辑',
'delete'=>'删除',
'show'=>'显示',
'hide'=>'隐藏'
];
$this->result('您正使用CMTS-GM系统验车相册管理接口', $apis, 200);
}
//相册列表
public function actionList()
{
$s = $this->search();
$res = $s['res'];
if (!$res) {
$this->result('没有查询到相应的数据!', [], 0);
}
$data = $s['query'];
$this->showAlbums($res, $data);
}
public function actionDetail(){
$pid = $this->pid;
$post = $this->postdata;
$id = isset($post['id']) ? $post['id'] : 0;
if ($id <= 0) {
$this->result('查询参数错误!');
}
$model = new AcAlbums();
$detail = $model->findOne($id)->toArray();
if(!$detail) $this->result('未查询到相应数据');
$this->result('查询成功!', $detail, 200);
}
public function actionGetStatus(){
$status = Yii::$app->params['AlbumStatus'];
$this->result('获取相册状态字典', $status, 200);
}
public function actionAdd()
{
}
/*编辑相册*/
public function actionEdit()
{
}
private function search(){
$pid = $this->pid;
$return = [];
$model = new AcAlbums();
$where = [];
$where[]='and';
$where[] = ['=','pid',$pid];
$post = $this->postdata;
$search = $post['search'] ?? [];
//关联指定车辆
$search['car_id'] = isset($search['car_id']) ? (int)$search['car_id'] : '';
$CarModel = new AcCar();
if (empty($search['car_id'])) {
$search['car_id'] = isset($get['car_id']) ? (int)$get['car_id'] : 0;
}
if (!empty($search['car_id'])) {
$where[] = ['=', 'car_id', $search['car_id']];
$car = $CarModel->findOne($search['car_id']);
$search['car_title'] = isset($car->title) ? $car->title : '';
}
//关联指定手机
$search['phone'] = isset($search['phone']) ? trim($search['phone']) : '';
if (!empty($search['phone'])) {
$where[] = ['or', ['LIKE', 'phone1_1', $search['phone']], ['LIKE', 'phone1_2', $search['phone']], ['LIKE', 'phone2_1', $search['phone']], ['LIKE', 'phone2_2', $search['phone']]];
}
//排除软删数据
$deleted = 0;
if (isset($_GET['deleted'])) {
$deleted = isset($_GET['deleted']) ? ((int)$_GET['deleted'] >= 0 ? 1 : 0) : 0;
}
$where[] = ['=', 'deleted', $deleted];
$page = $this->page;
$pageSize = $this->pageSize;
$data = $model->find()->where($where);
$pages = new Pagination(['totalCount' =>$data->count(), 'pageSize' => $pageSize]);
$pages->setPage($page-1, true); //设置分页的当前页面值
$_orderby = 'sign_date DESC,id DESC';
$res = $data->offset($pages->offset)->limit($pages->limit)->orderby($_orderby)->indexBy('id')->all();
return ['res'=>$res,'qeury'=>$data];
}
/*
* 数据更新前的预检查,返回对应关联数据
* 必须确保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 AcAlbums();
$res = $model->findOne($id);
if(!$res) $this->result('未查询到相应数据',[],404);
if($res->pid != $this->pid) $this->result('非本平台数据,不允许操作',[],401);
return $res;
}
/*
* 数据保存前的预检查(查重、参数校验等)
* 要保存的数据[],直接从post中取出
* @op操作类型add,edit……
* @detail, 编辑模式下,要修改的数据对象
* 校验机制:
* 编辑数据时必须用get方式传入参数id并与post进来的数据id进行比对只有一致时才能继续;
* 查重:禁止录入重复数据
* 返回:校验重组后的数据
* */
private function preSave($op,$detail=null)
{
$post = $this->postdata;
$data = [];
$_time = time();
$oid = (int)$post['order_id'] ?? 0;
//格式化数据
$_thumbs = $_logs = [];
//目标键=》POST键
switch ($op) {
case 'add':
if(!$oid || $oid != $post['order_id']) $this->result('未关联运单,禁止操作',[],403);
$AcOrder = new AcOrder();
$order = $AcOrder->findOne($oid);
if (!$order) {
$this->result('未找到关联运单,请检查并重新选择运单!', [], 404);
}
$data['order_id'] = $oid;
$data['create_by'] = $this->user_id;
$data['create_at'] = $_time;
$data['update_at'] = $_time;
break;
case 'edit':
$id = (int)Yii::$app->request->get('id');
$_id = (int)$post['id'];
if($id != $_id) $this->result('id参数不匹配请检查');
if($oid != $detail->order_id) $this->result('不允许更改运单关联,请检查');
if (isset($detail->logs)) {
$_logs = json_decode($detail->logs, TRUE);
}
if (isset($detail->thumbs)) {
$_thumbs = json_decode($detail->thumbs, TRUE);
}
$data['update_at'] = $_time;
break;
}
$data['pid'] = $this->pid;
//格式化数据 为空的项则不修改
$cols = ['pay_from' => '付款方', 'pay_to' => '收款人'];
foreach ($cols as $key => $ti) {
if (isset($post[$key]) && $post[$key] != '') {
$data[$key] = trim($post[$key]);
} else {
$msg = $ti . '不能为空,请检查并填写';
$this->result($msg, [], 100);
}
}
$key = 'paytype';
if (isset($post[$key]) && $post[$key] != '') {
$data[$key] = (int)$post[$key];
} else {
$msg = '支付方式必须选择,请检查';
$this->result($msg, [], 100);
}
$cols = ['money1', 'money2', 'money3']; //金额换算,元角分
$is_minus = FALSE;
//考虑是否有负值情况
if (isset($post['money1']) && (int)$post['money1'] < 0) {
$is_minus = TRUE;
} else if (isset($post['money2']) && (int)$post['money2'] < 0) {
$is_minus = TRUE;
} else if (isset($post['money3']) && (int)$post['money3'] < 0) {
$is_minus = TRUE;
}
foreach ($cols as $key) {
if (isset($post[$key]) && $post[$key] != '') {
$money[$key] = abs((int)$post[$key]); //取绝对值
} else {
$money[$key] = 0;
}
}
//角、分 数值必须个位数
if ($money['money2']) {
$money['money2'] = min($money['money2'], 9);
}
if ($money['money3']) {
$money['money3'] = min($money['money3'], 9);
}
$data['fee'] = 100 * $money['money1'] + 10 * $money['money2'] + $money['money3'];
if ($is_minus) {
$data['fee'] = 0 - $data['fee'];
}
//支付流水号
$key = 'sn';
if (isset($post[$key]) && $post[$key] != '') {
$data[$key] = trim($post[$key]);
}
//图片组处理
$key = 'thumbs';
$imgs = [];
if (isset($post[$key]) && !empty($post[$key])) {
$imgs = $post[$key]; //数组格式
$imgs = array_unique($imgs);
}
if ($imgs) {
if ($_thumbs) {
$imgs = array_merge($imgs, $_thumbs);
$imgs = array_unique($imgs);
}
}
if (!$imgs) {
$msg = '支付凭证截图必须上传,请检查';
$this->result($msg, [], 100);
}
$data[$key] = json_encode($imgs, JSON_UNESCAPED_UNICODE);
//操作日志处理
$key = 'logs';
$logs = [];
if (isset($post[$key]) && $post[$key] != '') {
$data['remark'] = isset($post[$key]) ? htmlspecialchars($post[$key]) : '';
$log = $this->user_info->username . '于' . date('Y-m-d H:i') . '备注-' . htmlspecialchars($post[$key]);
} else {
$log = $this->user_info->username . '于' . date('Y-m-d H:i') . '进行了调整';
}
if ($_logs) {
$logs = $_logs;
}
array_unshift($logs, $log);
$data['cwlogs'] = json_encode($logs, JSON_UNESCAPED_UNICODE);
return $data;
}
/*
* 数据显示格式化
* @res 数据源,查询的结果
* @query 数据表查询实例
* */
private function showAlbums($res,$query){
$datas = [];
foreach ($res as $s) {
$s = $s->toArray();
$datas[$s['id']] = $s;
}
$data = [
'total'=>$query->count(),
'albums' => $datas,
'page'=>$this->page
];
$this->result('相册查询成功!', $data, 200);
}
private function showAlbum($detail){
}
}