初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller\app\complaint;
|
||||
|
||||
use app\common\controller\Api;
|
||||
|
||||
/**
|
||||
* 投诉
|
||||
*/
|
||||
class Complaint extends Api
|
||||
{
|
||||
protected $noNeedLogin = ['category', 'config'];
|
||||
protected $noNeedRight = ['*'];
|
||||
|
||||
protected $categoryModel = null;
|
||||
protected $recordModel = null;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->categoryModel = new \app\admin\model\app\complaint\Category;
|
||||
$this->recordModel = new \app\admin\model\app\complaint\Record;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取投诉功能配置
|
||||
*/
|
||||
public function config()
|
||||
{
|
||||
$this->success('获取成功', [
|
||||
'status' => $this->getComplaintStatus()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取投诉类型树
|
||||
*/
|
||||
public function category()
|
||||
{
|
||||
if ($this->getComplaintStatus() !== '1') {
|
||||
$this->success('投诉功能已关闭', []);
|
||||
}
|
||||
$pid = $this->request->post('pid', 0);
|
||||
$list = $this->categoryModel
|
||||
->where(['uniacid' => UNIACID, 'pid' => $pid, 'status' => 1])
|
||||
->field('id,pid,title,description')
|
||||
->order('weigh desc,id asc')
|
||||
->select();
|
||||
|
||||
$data = [];
|
||||
foreach ($list as $item) {
|
||||
$row = $item->toArray();
|
||||
$row['has_children'] = $this->categoryModel
|
||||
->where(['uniacid' => UNIACID, 'pid' => $row['id'], 'status' => 1])
|
||||
->count() > 0 ? 1 : 0;
|
||||
$data[] = $row;
|
||||
}
|
||||
|
||||
$this->success('获取成功', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 提交投诉
|
||||
*/
|
||||
public function submit()
|
||||
{
|
||||
if ($this->getComplaintStatus() !== '1') {
|
||||
$this->error('投诉功能已关闭');
|
||||
}
|
||||
|
||||
$categoryId = $this->request->post('category_id', 0);
|
||||
$content = trim($this->request->post('content', ''));
|
||||
$images = $this->request->post('images/a', []);
|
||||
$targetType = $this->request->post('target_type', '');
|
||||
$targetId = $this->request->post('target_id', 0);
|
||||
$targetTitle = $this->request->post('target_title', '');
|
||||
|
||||
if (!$categoryId) {
|
||||
$this->error('请选择投诉类型');
|
||||
}
|
||||
if ($content === '') {
|
||||
$this->error('请填写投诉内容');
|
||||
}
|
||||
if (mb_strlen($content) > 200) {
|
||||
$this->error('投诉内容不能超过200字');
|
||||
}
|
||||
$images = array_values(array_filter($images));
|
||||
if (count($images) > 4) {
|
||||
$this->error('证据截图最多上传4张');
|
||||
}
|
||||
|
||||
$lastComplaint = $this->recordModel
|
||||
->where(['uniacid' => UNIACID, 'user_id' => $this->auth->id])
|
||||
->where('createtime', '>', time() - 300)
|
||||
->find();
|
||||
if ($lastComplaint) {
|
||||
$this->error('投诉提交过于频繁,请5分钟后再试');
|
||||
}
|
||||
|
||||
$category = $this->categoryModel->where(['id' => $categoryId, 'uniacid' => UNIACID, 'status' => 1])->find();
|
||||
if (!$category) {
|
||||
$this->error('投诉类型不存在');
|
||||
}
|
||||
|
||||
$data = [
|
||||
'uniacid' => UNIACID,
|
||||
'user_id' => $this->auth->id,
|
||||
'category_id' => $categoryId,
|
||||
'category_path' => $this->buildCategoryPath($categoryId),
|
||||
'target_type' => $targetType,
|
||||
'target_id' => intval($targetId),
|
||||
'target_title' => $targetTitle,
|
||||
'content' => $content,
|
||||
'images' => json_encode(array_values($images), JSON_UNESCAPED_UNICODE),
|
||||
'status' => 0,
|
||||
'createtime' => time(),
|
||||
'updatetime' => time(),
|
||||
];
|
||||
|
||||
$id = $this->recordModel->insertGetId($data);
|
||||
$this->success('提交成功', ['id' => $id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取投诉功能开关状态
|
||||
*/
|
||||
protected function getComplaintStatus()
|
||||
{
|
||||
$config = \app\common\model\app\Config::getConfig('complaint');
|
||||
if (!is_array($config) || !isset($config['status'])) {
|
||||
return '1';
|
||||
}
|
||||
return (string)$config['status'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建投诉类型路径
|
||||
*/
|
||||
protected function buildCategoryPath($categoryId)
|
||||
{
|
||||
$names = [];
|
||||
while ($categoryId) {
|
||||
$category = $this->categoryModel->where(['id' => $categoryId, 'uniacid' => UNIACID])->find();
|
||||
if (!$category) {
|
||||
break;
|
||||
}
|
||||
array_unshift($names, $category['title']);
|
||||
$categoryId = intval($category['pid']);
|
||||
}
|
||||
return implode(' / ', $names);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user