增加产品模块

This commit is contained in:
2026-03-20 19:48:32 +08:00
parent 14c305f518
commit b3a25ceb82
17 changed files with 916 additions and 469 deletions
@@ -0,0 +1,188 @@
<?php
declare(strict_types=1);
namespace app\admin\controller\Cms\Products;
use app\admin\BaseController;
use think\response\Json;
use app\model\Cms\Products;
use think\exception\ValidateException;
/**
* 特色产品管理控制器
*/
class ProductsController extends BaseController
{
/**
* 获取特色产品列表
* @return Json
*/
public function productsList(): Json
{
try {
$page = (int)$this->request->param('page', 1);
$limit = (int)$this->request->param('limit', 10);
$keyword = $this->request->param('keyword', '');
$query = Products::where('delete_time', null)
->where('tid', $this->getTenantId());
// 关键词搜索
if (!empty($keyword)) {
$query->where('title', 'like', '%' . $keyword . '%');
}
$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 addProducts(): Json
{
try {
$data = $this->request->param();
$product = new Products();
$product->tid = $this->getTenantId();
$product->title = $data['title'];
$product->url = $data['url'];
$product->thumb = $data['thumb'] ?? '';
$product->desc = $data['desc'] ?? '';
$product->content = $data['content'] ?? '';
$product->sort = $data['sort'] ?? 0;
$product->create_time = date('Y-m-d H:i:s');
$product->save();
$this->logSuccess('特色产品', '添加产品', ['id' => $product->id]);
return json([
'code' => 200,
'msg' => '添加成功',
'data' => $product->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 editProducts(int $id): Json
{
try {
$data = $this->request->param();
$product = Products::where('id', $id)
->where('tid', $this->getTenantId())
->where('delete_time', null)
->find();
if (!$product) {
return json([
'code' => 404,
'msg' => '产品不存在'
]);
}
if (isset($data['title'])) $product->title = $data['title'];
if (isset($data['url'])) $product->url = $data['url'];
if (isset($data['thumb'])) $product->thumb = $data['thumb'];
if (isset($data['desc'])) $product->desc = $data['desc'];
if (isset($data['sort'])) $product->sort = $data['sort'];
$product->update_time = date('Y-m-d H:i:s');
$product->save();
$this->logSuccess('特色产品', '更新产品', ['id' => $id]);
return json([
'code' => 200,
'msg' => '更新成功',
'data' => $product->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 deleteProducts(int $id): Json
{
try {
$product = Products::where('id', $id)
->where('tid', $this->getTenantId())
->where('delete_time', null)
->find();
if (!$product) {
return json([
'code' => 404,
'msg' => '产品不存在'
]);
}
$product->delete();
$this->logSuccess('特色产品', '删除产品', ['id' => $id]);
return json([
'code' => 200,
'msg' => '删除成功'
]);
} catch (\Exception $e) {
$this->logFail('特色产品', '删除产品', $e->getMessage());
return json([
'code' => 500,
'msg' => '删除失败:' . $e->getMessage()
]);
}
}
}
@@ -18,13 +18,12 @@ class ServicesController extends BaseController
* 获取特色服务列表
* @return Json
*/
public function getList(): Json
public function servicesList(): 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());
@@ -34,11 +33,6 @@ class ServicesController extends BaseController
$query->where('name', 'like', '%' . $keyword . '%');
}
// 状态筛选
if ($status !== '') {
$query->where('status', (int)$status);
}
$total = $query->count();
$list = $query->order('sort', 'asc')
->order('id', 'desc')
@@ -67,19 +61,11 @@ class ServicesController extends BaseController
* 添加特色服务
* @return Json
*/
public function add(): Json
public function addServices(): 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'];
@@ -87,7 +73,6 @@ class ServicesController extends BaseController
$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();
@@ -117,7 +102,7 @@ class ServicesController extends BaseController
* @param int $id
* @return Json
*/
public function update(int $id): Json
public function editServices(int $id): Json
{
try {
$data = $this->request->param();
@@ -134,24 +119,11 @@ class ServicesController extends BaseController
]);
}
// 验证参数
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();
@@ -181,7 +153,7 @@ class ServicesController extends BaseController
* @param int $id
* @return Json
*/
public function delete(int $id): Json
public function deleteServices(int $id): Json
{
try {
$service = Services::where('id', $id)
@@ -213,39 +185,4 @@ class ServicesController extends BaseController
}
}
/**
* 批量删除特色服务
* @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()
]);
}
}
}
+8
View File
@@ -0,0 +1,8 @@
<?php
use think\facade\Route;
// 特色产品路由
Route::get('productsList', 'app\admin\controller\Cms\Products\ProductsController@productsList');
Route::post('addProducts', 'app\admin\controller\Cms\Products\ProductsController@addProducts');
Route::put('editProducts/:id', 'app\admin\controller\Cms\Products\ProductsController@editProducts');
Route::delete('deleteProducts/:id', 'app\admin\controller\Cms\Products\ProductsController@deleteProducts');
+4 -4
View File
@@ -2,7 +2,7 @@
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');
Route::get('servicesList', 'app\admin\controller\Cms\Services\ServicesController@servicesList');
Route::post('addServices', 'app\admin\controller\Cms\Services\ServicesController@addServices');
Route::put('editServices/:id', 'app\admin\controller\Cms\Services\ServicesController@editServices');
Route::delete('deleteServices/:id', 'app\admin\controller\Cms\Services\ServicesController@deleteServices');
+25 -6
View File
@@ -15,6 +15,8 @@ use think\facade\Env;
use think\facade\Request;
use app\model\Cms\TemplateSiteConfig;
use app\model\Cms\Friendlink;
use app\model\Cms\Services;
use app\model\Cms\Products;
use app\model\Tenant\Tenant;
class Index extends BaseController
@@ -452,7 +454,7 @@ class Index extends BaseController
}
try {
// 1. 通过域名获取租户ID
// 通过域名获取租户ID
$tid = BaseController::getTenantIdByDomain($baseUrl);
if (empty($tid)) {
@@ -463,12 +465,28 @@ class Index extends BaseController
]);
}
// 2. 获取站点基础信息 (normalinfos)
// 获取站点基础信息 (normalinfos)
$normalInfos = SystemSiteSetting::where('tid', $tid)
->field('sitename,logo,logow,ico,description,copyright,companyname,icp,companyintroduction')
->find();
// 3. 获取友情链接列表
// 获取特色服务列表
$servicesList = Services::where('delete_time', null)
->where('tid', $tid)
->order('sort', 'asc')
->field('id,title,desc,thumb,url')
->select()
->toArray();
// 获取企业产品列表
$productsList = Products::where('delete_time', null)
->where('tid', $tid)
->order('sort', 'asc')
->field('id,title,desc,content,thumb,url')
->select()
->toArray();
// 获取友情链接列表
$friendlinkList = Friendlink::where('delete_time', null)
->where('tid', $tid)
->where('status', 1)
@@ -484,17 +502,18 @@ class Index extends BaseController
->field('contact_phone,contact_email,address,worktime')
->find();
// 4. 合并返回
// 合并返回
return json([
'code' => 200,
'msg' => 'success',
'data' => [
'normal' => $normalInfos ?: (object) [],
'normal' => $normalInfos ?: (object) [],
'contact' => $contact ?: (object) [],
'services' => $servicesList,
'products' => $productsList,
'links' => $friendlinkList
]
]);
} catch (\Exception $e) {
return json([
'code' => 500,
+7 -5
View File
@@ -13,6 +13,9 @@ Route::get(':page', 'app\index\controller\Index@page')
// --- 模板初始化接口 ---
Route::get('init', 'app\index\controller\Index@init');
// --- Banner 路由 ---
Route::get('getBanners', 'app\index\controller\BannerController@getBanners');
// --- 前端其他数据路由 ---
Route::get('footerdata', 'app\index\controller\Index@getFooterData');
Route::get('companyInfos', 'app\index\controller\Index@getCompanyInfos');
@@ -21,13 +24,12 @@ Route::get('homeData', 'app\index\controller\Index@getHomeData');
// --- 客户需求路由 ---
// --- 文章列表路由 ---
// --- 新闻中心列表路由 ---
Route::get('getCenterNews', 'app\index\controller\Article\NewsCenterController@getCenterNews');
// --- Banner 路由 ---
Route::get('getBanners', 'app\index\controller\BannerController@getBanners');
// --- 文章互动路由 ---
// --- 新闻中心互动路由 ---
Route::post('articleViews/:id', 'app\index\controller\Article\ArticleController@articleViews');
Route::post('articleLikes/:id', 'app\index\controller\Article\ArticleController@articleLikes');
Route::post('articleUnlikes/:id', 'app\index\controller\Article\ArticleController@articleUnlikes');
+44
View File
@@ -0,0 +1,44 @@
<?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 Products extends Model
{
// 启用软删除
use SoftDelete;
// 数据库表名
protected $name = 'mete_apps_cms_products';
// 字段类型转换
protected $type = [
'id' => 'integer',
'tid' => 'integer',
'title' => 'string',
'desc' => 'string',
'content' => 'string',
'thumb' => 'string',
'url' => 'string',
'sort' => 'integer',
'create_time' => 'datetime',
'update_time' => 'datetime',
'delete_time' => 'datetime',
];
}