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
+273 -236
View File
@@ -1,19 +1,13 @@
<?php
# @Author: 嗨噜客(三亚) <fm453>
# @Date: 2022-04-24T20:28:47+08:00
# @Email: fm453@lukegzs.com
# @Last modified by: fm453
# @Last modified time: 2024-08-09T09:00:19+08:00
# @Copyright: www.hiluker.cn
namespace api\controllers\gm\v1;
use Yii;
use yii\data\Pagination;
use addons\models\AcEmployee;
use addons\models\AcOrderPre;
use addons\models\AcStore;
use Yii;
use yii\data\Pagination;
class OrderpreController extends Common
{
@@ -30,9 +24,17 @@
$apis = [
'list' => '搜索询单',
'detail' => '询单详情',
'create' => '创建询单'
'get-status'=>'获取询价单状态字典',
'add' => '创建询单',
'edit'=>'编辑',
'cancel'=>'取消',
'delete'=>'删除',
'ajax'=>[
'show'=>'显示',
'hide'=>'隐藏'
]
];
$this->result('您正使用CMTS系统询价单管理接口!', $apis, 200);
$this->result('您正使用CMTS-GM系统询价单管理接口!', $apis, 200);
}
//订单列表
@@ -43,56 +45,201 @@
if (!$res) {
$this->result('没有查询到相应的数据!', [], 0);
}
$_res = $this->showOrders($res);
$data = $s['data'];
$this->showOrder($res, $data);
$return = [
'total' => $data->count(),
'orders' => $_res['orders'],
'employees' => $_res['employees'],
'stores' => $_res['stores'],
'page' => $this->page
];
$this->result('订单查询成功',$return,200);
}
private function search()
public function actionDetail()
{
$order = $this->preUpdate();
$detail = $this->showOrder($order);
$this->result('查询成功!', $detail, 200);
}
public function actionGetStatus()
{
$status = Yii::$app->params['OrderPreStatus'];
$this->result('查询成功!', $status, 200);
}
public function actionCancel()
{
$order = $this->preUpdate();
$res = $this->userPrivilege($this->user_info,'orderPreEdit',$order);
if(!$res) $this->result('您没有操作权限!',[],403);
if ($order->status_code <= 0 || $order->status_code >= 99) {
$this->result('订单已取消或已经完成,不允许操作!',[],403);
}
$order->status_code = -1;
$order->save();
$this->result('已取消!', $order, 200);
}
/**
* 创建咨询单
* @mid [int] 平台租户身份
* @uid [int] 前端用户身份
*/
public function actionAdd()
{
//数据预检查、编排
$data = $this->preSave('add');
if(!$data) $this->result('数据预检查未通过,保存失败', $data, 100);
//保存资料
$model = new AcOrderPre();
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);
}
/**
* 更新咨询单
* @mid [int] 平台租户身份
* @uid [int] 前端用户身份
*/
public function actionEdit()
{
$detail = $this->preUpdate();
$res = $this->userPrivilege($this->user_info,'orderPreEdit',$detail);
if(!$res) $this->result('您没有操作权限!',[],403);
//数据预检查、编排
$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();
$res = $this->userPrivilege($this->user_info,'orderPreDelete',$detail);
if(!$res) $this->result('您没有操作权限!',[],403);
if ($detail->deleted != 0) {
$this->result('数据已被删除过,操作无效', [], 400);
}
$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;
$_time = time();
$detail->update_at = $_time;
$res = true;
switch ($get['do']) {
case 'hide':
if ($detail->status_code != 0) {
$detail->status_code = 0;
$res = $detail->save();
}
if ($res) {
$msg = '记录隐藏标记成功';
$errorCode = 200;
} else {
$msg = '记录隐藏标记失败';
$errorCode = 0;
}
break;
default:
break;
}
$this->result($msg, [], $errorCode);
}
/*onlyMe,预置搜索条件,仅查看本人运单*/
private function search($onlyMe=false)
{
$pid = $this->pid;
$return = [];
$model = new AcOrderPre();
$where = $orwhere = [];
$where = [];
$where[] = 'and';
$where[] = ['=', 'pid', $pid];
$post = $this->postdata;
$get = yii::$app->request->get();
$search = isset($post['search']) ? $post['search'] : [];
//预置搜索条件,只允许查询与自己完全相关的订单
$where[] = ['=', 'from_uid', $this->user_id];
$search['phone'] = $this->user_info->mobile;
$where[] = ['=', 'from_mobile', $search['phone']];
if (isset($search['carno']) && !empty($search['carno'])) {
$search['carno'] = trim($search['carno']);
$where[] = ['LIKE', 'car_no', $search['carno']];
if($onlyMe){
//预置搜索条件,只允许查询与自己完全相关的订单
$where[] = ['=', 'from_uid', $this->user_id];
$search['phone'] = $this->user_info->mobile;
}
if (isset($search['phone']) && !empty($search['phone'])) {
$search['phone'] = trim($search['phone']);
$where[] = ['=', 'from_mobile', $search['phone']];
}
if (isset($search['carno']) && !empty($search['carno'])) {
$search['carno'] = trim($search['carno']);
$where[] = ['LIKE', 'car_no', $search['carno']];
}
if (isset($search['city']) && !empty($search['city'])) {
$search['city'] = trim($search['city']);
$where[] = ['LIKE', 'aim_city', $search['city']];
}
$search['employee'] = isset($search['employee']) ? (int)$search['employee'] : '';
$EmployeeModel = new AcEmployee();
$search['employee'] = isset($search['employee']) ? (int)$search['employee'] : 0;
if (!empty($search['employee'])) {
$where[] = ['=', 'employee_id', $search['employee']];
$employee = $EmployeeModel->findOne($search['employee']);
$search['employee_title'] = isset($employee->name) ? $employee->name : '';
}
$search['store_id'] = isset($search['store_id']) ? (int)$search['store_id'] : '';
if (empty($search['sotre_id'])) {
$search['store_id'] = isset($search['store_id']) ? (int)$search['store_id'] : 0;
if (empty($search['store_id'])) {
$search['store_id'] = isset($get['store_id']) ? (int)$get['store_id'] : 0;
}
$StoreModel = new AcStore();
if (!empty($search['store_id'])) {
$where[] = ['=', 'store_id', $search['store_id']];
$store = $StoreModel->findOne($search['store_id']);
$search['store_title'] = isset($store->title) ? $store->title : '';
}
$search['start_province'] = isset($search['start_province']) ? trim($search['start_province']) : '';
@@ -119,28 +266,107 @@
if (isset($search['status_code']) && $search['status_code'] != 'all') {
$search['status_code'] = (int)$search['status_code'];
$where[] = ['=', 'status_code', $search['status_code']];
} else {
unset($search['status_code']);
}
$deleted = 0;
$where[] = ['=', 'deleted', $deleted];
$where[] = ['=', 'deleted', 0];
$page = $this->page;
$pageSize = $this->pageSize;
$data = $model->find()->where($where)->andwhere($orwhere);
$data = $model->find()->where($where);
$pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
$pages->setPage($page - 1, TRUE); //设置分页的当前页面值
$_orderby = 'update_at DESC,id DESC';
$_orderby = 'update_at DESC,date ASC,id DESC';
$res = $data->offset($pages->offset)->limit($pages->limit)->orderby($_orderby)->all();
return ['res' => $res, 'data' => $data];
}
private function showOrder($orderRes, $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 AcOrderPre();
$res = $model->findOne($id);
if(!$res) $this->result('未查询到相应数据',[],404);
if($res->pid != $this->pid) $this->result('非本平台数据,不允许操作',[],401);
return $res;
}
/*
* 数据保存前的预检查(查重、参数校验等)
* 要保存的数据[],直接从post中取出
* @op,操作类型(add,edit……)
* 校验机制:
* 编辑数据时,必须用get方式传入参数id,并与post进来的数据id进行比对,只有一致时才能继续;
* 查重:禁止录入重复数据
* 返回:校验重组后的数据
* */
private function preSave($op)
{
$post = $this->postdata;
$data = [];
$_time = time();
//格式化数据
//目标键=》POST键
switch ($op) {
case 'add':
$data['from_uid'] = $this->user_id;
$data['status_code'] = 1; //订单处于创建待审核状态
$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参数不匹配,请检查');
$data['update_at'] = $_time;
break;
}
$data['pid'] = $this->pid;
$cols = ['start_province' => 'start_province', 'start_city' => 'start_city', 'aim_province' => 'aim_province', 'aim_city' => 'aim_city',
'car_title' => 'car_title', 'car_no' => 'car_no', 'price' => 'price',
'bind' => 'bind', 'receptionist' => "receptionist",
'from_name' => 'from_name', 'from_mobile' => 'from_mobile',
];
foreach ($cols as $col => $key) {
$data[$col] = isset($post[$key]) ? trim($post[$key]) : '';
}
$cols = ['car_value' => 'car_value', 'from_gender' => 'from_gender','from_mid'=>'from_mid','status_code'=>'status_code'];
foreach ($cols as $col => $key) {
$data[$col] = isset($post[$key]) ? (int)$post[$key] : 0;
}
$data['employee_id'] = isset($post['employee_id']) ? (int)$post['employee_id'] : 0;
$data['store_id'] = isset($post['store_id']) ? (int)$post['store_id'] : 0;
$cols = ['remark' => 'remark', 'car' => 'car'];
foreach ($cols as $col => $key) {
if (isset($post[$key]) && $post[$key] != '') {
$data[$col] = htmlspecialchars($post[$key]);
}
}
/*@date: 1649952000000*/
$data['date'] = isset($post['plan_date']) ? $post['plan_date'] / 1000 : strtotime('+7 day');
// 计划时间,默认7天后
return $data;
}
/*
订单数据显示
*/
private function showOrders($orderRes)
{
$status = Yii::$app->params['OrderPreStatus'];
$cids = $sids = $eids = $mids = [];
$sids = $eids = [];
if ($orderRes) {
foreach ($orderRes as $r) {
$sids[$r->store_id] = $r->store_id;
@@ -148,9 +374,6 @@
}
}
$where = [];
$where['pid'] = Yii::$app->session->get('pid');
$model = new AcStore();
$_stores = $model->find()->where(['in', 'id', $sids])->all();
$stores = [];
@@ -189,33 +412,13 @@
}
unset($s);
$return = [];
$return['code'] = 200;
$return['msg'] = '订单查询成功!';
$return['data'] = [
'total' => $data->count(),
'orders' => $orders,
'employees' => $employees,
'stores' => $stores,
'page' => $this->page
];
exit(json_encode($return, JSON_UNESCAPED_UNICODE));
$data = ['orders'=>$orders,'employees'=>$employees,'stores'=>$stores];
return $data;
}
public function actionDetail()
private function showOrder($order)
{
$pid = $this->pid;
$status = Yii::$app->params['OrderStatus'];
$post = $this->postdata;
$id = isset($post['id']) ? $post['id'] : 0;
if ($id <= 0) {
$this->result('查询参数错误!');
}
$AcOrderPre = new AcOrderPre();
$order = $AcOrderPre->findOne($id);
$detail = $order->toArray();
if ($detail['store_id']) {
$model = new AcStore();
$store = $model->findOne($detail['store_id']);
@@ -233,8 +436,8 @@
}
$sn = '00000000000'; //11位
$sn = substr($sn, 0, 11 - strlen($id));
$sn .= $id;
$sn = substr($sn, 0, 11 - strlen($order->id));
$sn .= $order->id;
$detail['sn'] = $detail['sn'] ? $detail['sn'] : $sn;
$weekdays = [0 => '日', 1 => '一', 2 => '二', 3 => '三', 4 => '四', 5 => '五', 6 => '六'];
@@ -247,172 +450,6 @@
foreach ($unsets as $us) {
unset($detail[$us]);
}
$this->result('查询成功!', $detail, 200);
}
public function actionCancel()
{
$pid = $this->pid;
$status = Yii::$app->params['OrderStatus'];
$post = $this->postdata;
$id = isset($post['id']) ? $post['id'] : 0;
if ($id <= 0) {
$this->result('未指定订单!');
}
$AcOrderPre = new AcOrderPre();
$order = $AcOrderPre->findOne($id);
if ($order->from_uid != $this->user_id) {
$this->result('非本人订单,不允许操作!');
}
if ($order->status_code <= 0 || $order->status_code >= 99) {
$this->result('订单已取消或已经完成,不允许操作!');
}
$order->status_code = -1;
$order->save();
$this->result('已取消!', $order->toArray(), 200);
}
public function actionCreate()
{
/**
* 创建咨询单
* @mid [int] 平台租户身份
* @uid [int] 前端用户身份
*/
$get = Yii::$app->request->get();
$post = $this->postdata;
$pid = $this->pid;
//格式化数据 为空的项则不修改
$data['from_uid'] = $this->user_id;
//目标键=》POST键
$cols = ['start_province' => 'start_province', 'start_city' => 'start_city', 'aim_province' => 'aim_province', 'aim_city' => 'aim_city',
'car_title' => 'car_title', 'car_no' => 'car_no', 'price' => 'price',
'bind' => 'bind', 'receptionist' => "receptionist",
'from_name' => 'from_name', 'from_mobile' => 'from_mobile',
];
foreach ($cols as $col => $key) {
if (isset($post[$key]) && $post[$key] != '') {
$data[$col] = trim($post[$key]);
}
}
$cols = ['car_value' => 'car_value', 'from_gender' => 'from_gender'];
foreach ($cols as $col => $key) {
$data[$col] = 0;
if (isset($post[$key]) && $post[$key] != '') {
$data[$col] = (int)$post[$key];
}
}
$cols = ['remark' => 'remark', 'car' => 'car'];
foreach ($cols as $col => $key) {
if (isset($post[$key]) && $post[$key] != '') {
$data[$col] = htmlspecialchars($post[$key]);
}
}
/*@date: 1649952000000*/
$data['date'] = isset($post['plan_date']) ? $post['plan_date'] / 1000 : strtotime('+7 day');
// 计划时间,默认7天后
$mid = isset($post['mid']) ? (int)$post['mid'] : 0;
$data['from_mid'] = $mid;
$data['status_code'] = 1; //订单处于创建待审核状态
$data['create_at'] = time();
$data['update_at'] = $data['create_at'];
$data['pid'] = $pid;
$data['employee_id'] = isset($post['employee_id']) ? (int)$post['employee_id'] : 0;
$data['store_id'] = isset($post['store_id']) ? (int)$post['store_id'] : 0;
//保存资料
$AcOrderPre = new AcOrderPre();
foreach ($data as $key => $val) {
$AcOrderPre->$key = $val;
}
$res = $AcOrderPre->save();
$id = $AcOrderPre->attributes['id']; //获取插入后id
if ($res) {
$this->result('询单保存成功!', ['id' => $id], 200);
} else {
$this->result('询单保存失败!', [], 100);
}
}
/*
订单数据显示格式化
*/
public function actionEdit()
{
/**
* 更新咨询单
* @mid [int] 平台租户身份
* @uid [int] 前端用户身份
*/
$get = Yii::$app->request->get();
$post = $this->postdata;
$id = isset($post['id']) ? (int)$post['id'] : 0;
if (!$id) {
$this->result('未指定询价单!');
}
$model = new AcOrderPre();
$detail = $model->findOne($id);
if (!$detail) {
$this->result('询价单信息不存在!');
}
if ($detail->pid != $this->pid) {
$this->result('非法操作!');
}
if ($detail->from_uid != $this->user_id) {
$this->result('非法操作,请检查订单归属!');
}
//格式化数据 为空的项则不修改
//目标键=》POST键
$cols = ['start_province' => 'start_province', 'start_city' => 'start_city', 'aim_province' => 'aim_province', 'aim_city' => 'aim_city',
'car_title' => 'car_title', 'car_no' => 'car_no',
'from_name' => 'from_name', 'from_mobile' => 'from_mobile',
];
foreach ($cols as $col => $key) {
if (isset($post[$key]) && $post[$key] != '') {
$detail->$col = trim($post[$key]);
}
}
$cols = ['car_value' => 'car_value', 'from_gender' => 'from_gender'];
foreach ($cols as $col => $key) {
if (isset($post[$key]) && $post[$key] != '') {
$detail->$col = (int)$post[$key];
}
}
$cols = ['remark' => 'remark'];
foreach ($cols as $col => $key) {
if (isset($post[$key]) && $post[$key] != '') {
$detail->$col = htmlspecialchars($post[$key]);
}
}
$detail->date = isset($post['plan_date']) ? $post['plan_date'] / 1000 : $detail->date;
$detail->status_code = 1; //标记订单处于创建待审核状态
$detail->update_at = time();
//保存资料
$res = $detail->save();
if ($res) {
$this->result('询单保存成功!', ['id' => $id], 200);
} else {
$this->result('询单保存失败!', [], 100);
}
return $detail;
}
}