增加特色服务
This commit is contained in:
@@ -0,0 +1,251 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\admin\controller\Cms\Services;
|
||||
|
||||
use app\admin\BaseController;
|
||||
use think\response\Json;
|
||||
use app\model\Cms\Services;
|
||||
use think\exception\ValidateException;
|
||||
|
||||
/**
|
||||
* 特色服务管理控制器
|
||||
*/
|
||||
class ServicesController extends BaseController
|
||||
{
|
||||
/**
|
||||
* 获取特色服务列表
|
||||
* @return Json
|
||||
*/
|
||||
public function getList(): Json
|
||||
{
|
||||
try {
|
||||
$page = (int)$this->request->param('page', 1);
|
||||
$limit = (int)$this->request->param('limit', 10);
|
||||
$keyword = $this->request->param('keyword', '');
|
||||
$status = $this->request->param('status', '');
|
||||
|
||||
$query = Services::where('delete_time', null)
|
||||
->where('tid', $this->getTenantId());
|
||||
|
||||
// 关键词搜索
|
||||
if (!empty($keyword)) {
|
||||
$query->where('name', 'like', '%' . $keyword . '%');
|
||||
}
|
||||
|
||||
// 状态筛选
|
||||
if ($status !== '') {
|
||||
$query->where('status', (int)$status);
|
||||
}
|
||||
|
||||
$total = $query->count();
|
||||
$list = $query->order('sort', 'asc')
|
||||
->order('id', 'desc')
|
||||
->page($page, $limit)
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'data' => [
|
||||
'list' => $list,
|
||||
'total' => $total
|
||||
]
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '获取失败:' . $e->getMessage(),
|
||||
'data' => []
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加特色服务
|
||||
* @return Json
|
||||
*/
|
||||
public function add(): Json
|
||||
{
|
||||
try {
|
||||
$data = $this->request->param();
|
||||
|
||||
// 验证参数
|
||||
$this->validate($data, [
|
||||
'name|服务名称' => 'require|max:100',
|
||||
'url|服务地址' => 'require|url|max:255',
|
||||
'sort|排序' => 'integer',
|
||||
'status|状态' => 'in:0,1'
|
||||
]);
|
||||
|
||||
$service = new Services();
|
||||
$service->tid = $this->getTenantId();
|
||||
$service->title = $data['title'];
|
||||
$service->url = $data['url'];
|
||||
$service->thumb = $data['thumb'] ?? '';
|
||||
$service->desc = $data['desc'] ?? '';
|
||||
$service->sort = $data['sort'] ?? 0;
|
||||
$service->status = $data['status'] ?? 1;
|
||||
$service->create_time = date('Y-m-d H:i:s');
|
||||
$service->save();
|
||||
|
||||
$this->logSuccess('特色服务', '添加服务', ['id' => $service->id]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '添加成功',
|
||||
'data' => $service->toArray()
|
||||
]);
|
||||
} catch (ValidateException $e) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => $e->getError()
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFail('特色服务', '添加服务', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '添加失败:' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新特色服务
|
||||
* @param int $id
|
||||
* @return Json
|
||||
*/
|
||||
public function update(int $id): Json
|
||||
{
|
||||
try {
|
||||
$data = $this->request->param();
|
||||
|
||||
$service = Services::where('id', $id)
|
||||
->where('tid', $this->getTenantId())
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if (!$service) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '服务不存在'
|
||||
]);
|
||||
}
|
||||
|
||||
// 验证参数
|
||||
if (isset($data['name'])) {
|
||||
$this->validate($data, [
|
||||
'name|服务名称' => 'require|max:100'
|
||||
]);
|
||||
}
|
||||
if (isset($data['url'])) {
|
||||
$this->validate($data, [
|
||||
'url|服务地址' => 'require|url|max:255'
|
||||
]);
|
||||
}
|
||||
|
||||
if (isset($data['title'])) $service->title = $data['title'];
|
||||
if (isset($data['url'])) $service->url = $data['url'];
|
||||
if (isset($data['thumb'])) $service->thumb = $data['thumb'];
|
||||
if (isset($data['desc'])) $service->desc = $data['desc'];
|
||||
if (isset($data['sort'])) $service->sort = $data['sort'];
|
||||
if (isset($data['status'])) $service->status = $data['status'];
|
||||
$service->update_time = date('Y-m-d H:i:s');
|
||||
$service->save();
|
||||
|
||||
$this->logSuccess('特色服务', '更新服务', ['id' => $id]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '更新成功',
|
||||
'data' => $service->toArray()
|
||||
]);
|
||||
} catch (ValidateException $e) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => $e->getError()
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFail('特色服务', '更新服务', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '更新失败:' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除特色服务
|
||||
* @param int $id
|
||||
* @return Json
|
||||
*/
|
||||
public function delete(int $id): Json
|
||||
{
|
||||
try {
|
||||
$service = Services::where('id', $id)
|
||||
->where('tid', $this->getTenantId())
|
||||
->where('delete_time', null)
|
||||
->find();
|
||||
|
||||
if (!$service) {
|
||||
return json([
|
||||
'code' => 404,
|
||||
'msg' => '服务不存在'
|
||||
]);
|
||||
}
|
||||
|
||||
$service->delete();
|
||||
|
||||
$this->logSuccess('特色服务', '删除服务', ['id' => $id]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '删除成功'
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFail('特色服务', '删除服务', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '删除失败:' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除特色服务
|
||||
* @return Json
|
||||
*/
|
||||
public function batchDelete(): Json
|
||||
{
|
||||
try {
|
||||
$ids = $this->request->param('ids', []);
|
||||
|
||||
if (empty($ids)) {
|
||||
return json([
|
||||
'code' => 400,
|
||||
'msg' => '请选择要删除的服务'
|
||||
]);
|
||||
}
|
||||
|
||||
Services::whereIn('id', $ids)
|
||||
->where('tid', $this->getTenantId())
|
||||
->where('delete_time', null)
|
||||
->update(['delete_time' => date('Y-m-d H:i:s')]);
|
||||
|
||||
$this->logSuccess('特色服务', '批量删除服务', ['ids' => $ids]);
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '批量删除成功'
|
||||
]);
|
||||
} catch (\Exception $e) {
|
||||
$this->logFail('特色服务', '批量删除服务', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => '批量删除失败:' . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
use think\facade\Route;
|
||||
|
||||
// 特色服务路由
|
||||
Route::get('services', 'app\admin\controller\Cms\Services\ServicesController@getList');
|
||||
Route::post('services', 'app\admin\controller\Cms\Services\ServicesController@add');
|
||||
Route::put('services/:id', 'app\admin\controller\Cms\Services\ServicesController@update');
|
||||
Route::delete('services/:id', 'app\admin\controller\Cms\Services\ServicesController@delete');
|
||||
@@ -113,22 +113,12 @@ abstract class BaseController
|
||||
* 根据域名获取租户ID(静态方法,方便全局调用)
|
||||
* @return int
|
||||
*/
|
||||
public static function getTenantIdByDomain(): int
|
||||
public static function getTenantIdByDomain(string $baseUrl): int
|
||||
{
|
||||
$request = request();
|
||||
|
||||
// 优先从 Referer 获取来源域名(跨域调用场景)
|
||||
$referer = $request->header('Referer');
|
||||
if ($referer) {
|
||||
$host = parse_url($referer, PHP_URL_HOST);
|
||||
} else {
|
||||
// 否则使用当前访问域名
|
||||
$host = $request->host(true);
|
||||
}
|
||||
|
||||
// 查询域名对应的租户ID
|
||||
$tenantDomain = Db::name('mete_tenant_domain')
|
||||
->where('full_domain', $host)
|
||||
->where('full_domain', $baseUrl)
|
||||
->where('status', 1)
|
||||
->whereNull('delete_time')
|
||||
->find();
|
||||
|
||||
@@ -24,7 +24,12 @@ class NewsCenterController extends BaseController
|
||||
*/
|
||||
public function getCenterNews(): Json
|
||||
{
|
||||
// 从 BaseController 获取当前租户ID
|
||||
$baseUrl = $this->request->get('baseUrl', '');
|
||||
|
||||
if (!empty($baseUrl)) {
|
||||
$this->tenantId = BaseController::getTenantIdByDomain($baseUrl);
|
||||
}
|
||||
|
||||
$tid = $this->getTenantId();
|
||||
|
||||
if (empty($tid)) {
|
||||
|
||||
@@ -17,9 +17,14 @@ class BannerController extends BaseController
|
||||
* 获取 Banner 列表
|
||||
* @return Json
|
||||
*/
|
||||
public function getBanners(): Json
|
||||
public function getBanners()
|
||||
{
|
||||
// 从 BaseController 获取当前租户ID
|
||||
$baseUrl = $this->request->get('baseUrl', '');
|
||||
|
||||
if (!empty($baseUrl)) {
|
||||
$this->tenantId = BaseController::getTenantIdByDomain($baseUrl);
|
||||
}
|
||||
|
||||
$tid = $this->getTenantId();
|
||||
|
||||
if (empty($tid)) {
|
||||
|
||||
@@ -15,6 +15,7 @@ use think\facade\Env;
|
||||
use think\facade\Request;
|
||||
use app\model\Cms\TemplateSiteConfig;
|
||||
use app\model\Cms\Friendlink;
|
||||
use app\model\Tenant\Tenant;
|
||||
|
||||
class Index extends BaseController
|
||||
{
|
||||
@@ -444,9 +445,15 @@ class Index extends BaseController
|
||||
*/
|
||||
public function getHomeData()
|
||||
{
|
||||
$baseUrl = Request::param('baseUrl', '');
|
||||
|
||||
if (empty($baseUrl)) {
|
||||
return json(['code' => 400, 'msg' => '缺少 baseUrl 参数']);
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. 通过域名获取租户ID
|
||||
$tid = BaseController::getTenantIdByDomain();
|
||||
$tid = BaseController::getTenantIdByDomain($baseUrl);
|
||||
|
||||
if (empty($tid)) {
|
||||
return json([
|
||||
@@ -470,12 +477,20 @@ class Index extends BaseController
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 获取联系方式
|
||||
$contact = Tenant::where('id', $tid)
|
||||
->where('status', 1)
|
||||
->where('delete_time', null)
|
||||
->field('contact_phone,contact_email,address,worktime')
|
||||
->find();
|
||||
|
||||
// 4. 合并返回
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'data' => [
|
||||
'normal' => $normalInfos ?: (object) [],
|
||||
'contact' => $contact ?: (object) [],
|
||||
'links' => $friendlinkList
|
||||
]
|
||||
]);
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: Liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace app\model\Cms;
|
||||
|
||||
use think\Model;
|
||||
use think\model\concern\SoftDelete;
|
||||
|
||||
/**
|
||||
* 特色服务模型
|
||||
*/
|
||||
class Services extends Model
|
||||
{
|
||||
// 启用软删除
|
||||
use SoftDelete;
|
||||
|
||||
// 数据库表名
|
||||
protected $name = 'mete_apps_cms_services';
|
||||
|
||||
// 字段类型转换
|
||||
protected $type = [
|
||||
'id' => 'integer',
|
||||
'tid' => 'integer',
|
||||
'title' => 'string',
|
||||
'desc' => 'string',
|
||||
'thumb' => 'string',
|
||||
'url' => 'string',
|
||||
'sort' => 'integer',
|
||||
'create_time' => 'datetime',
|
||||
'update_time' => 'datetime',
|
||||
'delete_time' => 'datetime',
|
||||
];
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user