文件
yjyf/server/application/admin/controller/Service.php
YuanWen 361dffc7d8 feat: 新增师傅端功能及服务增项管理模块
- 新增师傅端路由和页面配置,切换登录后默认跳转到师傅大厅
- 新增服务分类图标上传功能和增项服务管理模块
- 新增订单、申诉、结算等模型关联关系及后台控制器
- 新增师傅端TabBar组件和消息页面跳转逻辑
- 新增文件上传接口和服务计算逻辑
- 新增各类SVG图标和空状态提示
2026-11-03 15:45:21 +08:00

233 行
8.0 KiB
PHP

<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use Exception;
/**
* 服务项目与商品管理
*
* @icon fa fa-circle-o
*/
class Service extends Backend
{
/**
* Service模型对象
* @var \app\admin\model\Service
*/
protected $model = null;
protected $relationSearch = true;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\Service;
$this->view->assign("priceTypeList", $this->model->getPriceTypeList());
$this->view->assign("statusList", $this->model->getStatusList());
// 获取全部可用的公共增项服务列表
$addonsPool = Db::name('service_addon')
->where('status', 'normal')
->order('weigh desc, id asc')
->select();
$this->view->assign("addonsPool", $addonsPool);
}
public function index()
{
$this->request->filter(['strip_tags', 'trim']);
if (false === $this->request->isAjax()) {
return $this->view->fetch();
}
if ($this->request->request('keyField')) {
return $this->selectpage();
}
[$where, $sort, $order, $offset, $limit] = $this->buildparams();
$list = $this->model
->with(['cat'])
->where($where)
->order($sort, $order)
->paginate($limit);
$result = ['total' => $list->total(), 'rows' => $list->items()];
return json($result);
}
/**
* 添加服务商品
*/
public function add()
{
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
// 处理工项类型与增项配置
$params = $this->formatServiceConfig($params);
$result = false;
Db::startTrans();
try {
$result = $this->model->allowField(true)->save($params);
$serviceId = $this->model->id;
// 处理规格列表
$this->syncSpecs($serviceId, $this->request->post('specs/a', []));
Db::commit();
} catch (ValidateException|PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success();
} else {
$this->error(__('No rows were inserted'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
return $this->view->fetch();
}
/**
* 编辑服务商品(工项价格、规格价格、增项服务绑定)
*/
public function edit($ids = null)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
$this->error(__('You have no permission'));
}
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
// 处理工项类型与增项配置
$params = $this->formatServiceConfig($params);
$result = false;
Db::startTrans();
try {
$result = $row->allowField(true)->save($params);
// 同步处理规格列表
$this->syncSpecs($row->id, $this->request->post('specs/a', []));
Db::commit();
} catch (ValidateException|PDOException|Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success();
} else {
$this->error(__('No rows were updated'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
// 解析工项与增项配置以供视图回显
$serviceTypes = [];
if (!empty($row->service_types)) {
$serviceTypes = is_array($row->service_types) ? $row->service_types : json_decode($row->service_types, true);
}
$addons = [];
if (!empty($row->addons)) {
$addons = is_array($row->addons) ? $row->addons : json_decode($row->addons, true);
}
// 获取已有规格
$specs = Db::name('service_spec')
->where('service_id', $row->id)
->where('status', 'normal')
->order('weigh desc, id asc')
->select();
$this->view->assign("row", $row);
$this->view->assign("serviceTypes", $serviceTypes ?: []);
$this->view->assign("assignedAddons", $addons ?: []);
$this->view->assign("specs", $specs ?: []);
return $this->view->fetch();
}
/**
* 格式化工项与增项输入数据
*/
protected function formatServiceConfig($params)
{
// 工项类型 JSON
if (isset($params['service_types_raw'])) {
$params['service_types'] = is_string($params['service_types_raw']) ? $params['service_types_raw'] : json_encode($params['service_types_raw'], JSON_UNESCAPED_UNICODE);
unset($params['service_types_raw']);
}
// 增项服务 JSON
if (isset($params['addons_raw'])) {
$params['addons'] = is_string($params['addons_raw']) ? $params['addons_raw'] : json_encode($params['addons_raw'], JSON_UNESCAPED_UNICODE);
unset($params['addons_raw']);
}
return $params;
}
/**
* 同步商品规格表
*/
protected function syncSpecs($serviceId, $specsInput)
{
if (empty($serviceId)) return;
$now = time();
$keepIds = [];
if (!empty($specsInput) && is_array($specsInput)) {
foreach ($specsInput as $item) {
$name = trim(isset($item['name']) ? $item['name'] : '');
if ($name === '') continue;
$specPrice = isset($item['base_price']) ? (float)$item['base_price'] : 0.00;
$specDesc = isset($item['description']) ? trim($item['description']) : '';
$specId = isset($item['id']) ? (int)$item['id'] : 0;
if ($specId > 0) {
Db::name('service_spec')->where('id', $specId)->where('service_id', $serviceId)->update([
'name' => $name,
'base_price' => $specPrice,
'description' => $specDesc,
'updatetime' => $now,
]);
$keepIds[] = $specId;
} else {
$newId = Db::name('service_spec')->insertGetId([
'service_id' => $serviceId,
'name' => $name,
'base_price' => $specPrice,
'description' => $specDesc,
'weigh' => 0,
'status' => 'normal',
'createtime' => $now,
'updatetime' => $now,
]);
$keepIds[] = $newId;
}
}
}
// 将未在列表中的历史规格软下架或删除
$delQuery = Db::name('service_spec')->where('service_id', $serviceId);
if (!empty($keepIds)) {
$delQuery->whereNotIn('id', $keepIds);
}
$delQuery->delete();
}
}