feat: 添加留言记录(联系我们)分页/导出接口

This commit is contained in:
2025-03-13 11:54:42 +08:00
parent 825ea45eb7
commit 694199ccd4
5 changed files with 147 additions and 2 deletions

View File

@@ -0,0 +1,75 @@
<?php
declare (strict_types = 1);
namespace app\admin\controller\v1;
use app\admin\model\v1\LeaveMessageModel;
/**
* 留言记录(联系我们)控制器
*/
class LeaveMessage
{
// 分页
public function index()
{
$param = request()->param([
'created_at',
'page/d' => 1,
'size/d' => 10
]);
$msgs = LeaveMessageModel::withoutField([
'language_id',
'user_agent'
])
->withSearch(['created_at'], [
'created_at' => !empty($param['created_at']) ? explode(',', $param['created_at']) : null
])
->language(request()->lang_id)
->order(['id' => 'desc'])
->paginate([
'list_rows' => $param['size'],
'page' => $param['page']
]);
return success('获取成功', $msgs);
}
// 导出
public function export()
{
$schema = [
'id' => 'ID',
'name' => '姓名',
'email' => '邮箱',
'ip' => 'IP',
'content' => '留言内容',
'created_at' => '提交时间'
];
// 获取留言导出数据
$msgs = $this->getLeaveMessageExportData();
// 导出
return xlsx_writer($msgs, $schema);
}
// 获取留言导出数据
private function getLeaveMessageExportData()
{
$param = request()->param([
'created_at'
]);
return LeaveMessageModel::withoutField([
'language_id',
'user_agent'
])
->withSearch(['created_at'], [
'created_at' => !empty($param['created_at']) ? explode(',', $param['created_at']) : null
])
->language(request()->lang_id)
->order(['id' => 'desc'])
->select();
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare (strict_types = 1);
namespace app\admin\model\v1;
use app\common\model\LeaveMessageBaseModel;
/**
* 留言记录(联系我们)模型
* @mixin \think\Model
*/
class LeaveMessageModel extends LeaveMessageBaseModel
{
// 根据语言查询
public function scopeLanguage($query, $value)
{
$query->where('language_id', '=', $value);
}
// 按添加时间搜索
public function searchCreatedAtAttr($query, $value, $data)
{
if (is_null($value)) return;
if (is_array($value)) {
if (count($value) > 1) {
$query->whereBetweenTime('created_at', $value[0], $value[1]);
} else {
$query->whereTime('created_at', '>=', $value[0]);
}
}
}
}

View File

@@ -519,6 +519,15 @@ Route::group('v1', function () {
Route::delete('delete/:id', 'Faq/delete');
});
// 反馈管理 - 留言记录(联系我们)
Route::group('leavemsg', function() {
// 留言记录(联系我们)分页
Route::get('index', 'LeaveMessage/index');
// 留言记录(联系我们)导出
Route::get('export', 'LeaveMessage/export');
});
// 配置项列表
Route::group('config', function() {
// 配置分组

View File

@@ -0,0 +1,29 @@
<?php
declare (strict_types = 1);
namespace app\common\model;
/**
* 留言记录(联系我们)模型
* @mixin \think\Model
*/
class LeaveMessageBaseModel extends BaseModel
{
// 表名
protected $name = 'leave_message';
// 主键
protected $pk = 'id';
// 字段信息
protected $schema = [
'id' => 'int',
'language_id' => 'int',
'name' => 'string',
'email' => 'string',
'content' => 'string',
'ip' => 'string',
'user_agent' => 'string',
'created_at' => 'datetime',
];
}

View File

@@ -2,7 +2,7 @@
use think\migration\Migrator;
class CreateContactUs extends Migrator
class CreateLeaveMessage extends Migrator
{
/**
* Change Method.
@@ -27,7 +27,7 @@ class CreateContactUs extends Migrator
*/
public function change()
{
$table = $this->table('contact_us', ['engine' => 'MyISAM', 'comment' => '联系我们表']);
$table = $this->table('leave_message', ['engine' => 'MyISAM', 'comment' => '留言记录(联系我们表']);
$table->addColumn('language_id', 'integer', ['signed' => false, 'null' => false, 'comment' => '语言ID'])
->addColumn('name', 'string', ['limit' => 64, 'null' => false, 'comment' => '姓名'])
->addColumn('email', 'string', ['limit' => 128, 'null' => false, 'comment' => '邮箱'])