coupon-admin/app/controller/manager/Comment.php

273 lines
8.2 KiB
PHP

<?php
namespace app\controller\manager;
use app\exception\RepositoryException;
use app\model\Comment as CommentModel;
use app\repository\AccountRepository;
use Exception;
use think\facade\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\exception\ValidateException;
use think\response\Json;
use think\response\View;
use think\facade\Config;
/**
* 评论
*
* Class Comment
* @package app\controller\manager
*/
class Comment extends Base
{
protected function initialize()
{
parent::initialize();
Config::load("extra/wechat", "wechat");
}
/**
* 审核
*
* @return Json|View
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws Exception
*/
public function examine()
{
$id = input('id/d', 0);
$state = input('state/d', 0);
if (!in_array($state, array_keys(CommentModel::allState()))) {
return $this->json(4001, '状态错误');
}
if (!$info = CommentModel::findById($id)) {
return $this->json(4001, '记录不存在');
}
if ($this->request->isPost()) {
$data = ['state' => $state];
if ($state == CommentModel::state_hide) {
$data['is_delete'] = CommentModel::COMMON_ON;
}
$info->save($data);
return $this->json();
}
$disabled = CommentModel::getAllChildrenIds($id);
$disabled[] = $id;
$this->data['item'] = $info;
return $this->view();
}
/**
* 恢复
*
* @return Json|View
* @throws DataNotFoundException
* @throws DbException
* @throws ModelNotFoundException
* @throws Exception
*/
public function recovery()
{
$id = input('id/d', 0);
if (!$info = CommentModel::findById($id)) {
return $this->json(4001, '记录不存在');
}
if ($this->request->isPost()) {
$data = ["is_delete"=>CommentModel::COMMON_OFF];
$info->save($data);
return $this->json();
}
}
/**
* 加入黑名单
* @return Json
*/
public function addBlack()
{
$id = input("id/d");
$info = CommentModel::findById($id);
if (empty($info)) {
return $this->error('记录不存在');
}
if ($this->request->isPost()) {
if (empty($info)) {
return $this->json(4001, '记录不存在');
}
$time = input("time/d", 0, "abs");
if ($time <= 0) {
return $this->json(4001, "时间输入错误");
}
$account = AccountRepository::getInstance()->findOneByWhere(["user_code" => $info["user_code"]]);
if (empty($account)) {
return $this->json(4001, "用户不存在");
}
$timeSecond = $time * 60;
Db::startTrans();
try {
//如果之前有黑名单设置 如果黑名单时间未过期
if (!empty($account["blank_time"]) && strtotime($account["blank_time"]) && strtotime($account["blank_time"]) > time()) {
$account->save(["blank_time" => date("Y-m-d H:i:s", (strtotime($account["blank_time"]) + $timeSecond)), "blank_total" => (($account["blank_total"] ?? 0) + $time)]);
} else {
$account->save(["blank_time" => date("Y-m-d H:i:s", (time() + $timeSecond)), "blank_total" => (($account["blank_total"] ?? 0) + $time)]);
}
$info->save(["state" => CommentModel::state_hide]);
Db::commit();
return $this->json();
} catch (RepositoryException $e) {
Db::rollback();
return $this->json(5001, "失败");
} catch (Exception $e) {
Db::rollback();
return $this->json(5002, "失败");
}
}
$this->data["id"] = $id;
return $this->view();
}
/**
* 彻底删除
* @return Json
*/
public function del()
{
if (!$this->request->isPost()) {
return $this->json(4000, '非法请求');
}
$ids = $this->request->param('ids/a', []);
CommentModel::destroy($ids);
return $this->json();
}
/**
* 列表
*
* @return Json|View
* @throws Exception
*/
public function index()
{
$stateArray = CommentModel::allState();
$typeArray = CommentModel::allType();
if ($this->request->isPost()) {
$keyword = $this->request->param('keyword/s',);
$state = $this->request->param('state/d', "-1");
$type = $this->request->param('type/d', -1);
$page = $this->request->param('page/d', 1);
$size = $this->request->param('size/d', 30);
$whereMap = [
["comment.is_delete", "=", CommentModel::COMMON_OFF],
["comment.state", "in", [CommentModel::state_default,CommentModel::state_success]],
];
$orders = ['comment.id' => 'desc'];
if (!empty($keyword)) {
$whereMap[] = ['account.nick_name|comment.comment', 'like', "%$keyword%"];
}
if ($type >= 0) {
$whereMap[] = ['comment.type', '=', $type];
}
if (isset($stateArray[$state])) {
$whereMap[] = ['comment.state', '=', $state];
}
$list = CommentModel::findList($whereMap, [], $page, $size, function ($q) {
return $q->withJoin("account");
}, $orders);
$list["list"]->each(function ($item){
if($item->type == CommentModel::type_text){
$data = json_decode($item->comment,true);
if(!empty($data)){
$dataStr = '';
foreach ($data as $ditem){
if($ditem["msgType"] == CommentModel::msg_type_emoji){
$dataStr .= "<img class='emoji' src='/static/" . $ditem['msgImage']."'/>";
}else{
$dataStr.=$ditem['msgCont'];
}
}
$item->comment = $dataStr;
}
}
});
return $this->json(0, 'success', $list);
}
$this->data["state"] = $stateArray;
$this->data["type"] = $typeArray;
return $this->view();
}
/**
* 列表
*
* @return Json|View
* @throws Exception
*/
public function recycleBin()
{
if ($this->request->isPost()) {
$keyword = $this->request->param('keyword/s',);
$page = $this->request->param('page/d', 1);
$size = $this->request->param('size/d', 30);
$whereMap = [["comment.is_delete", "=", CommentModel::COMMON_ON]];
if (!empty($keyword)) {
$whereMap[] = ['account.nick_name|comment.comment', 'like', "%$keyword%"];
}
$orders = ['comment.id' => 'desc'];
$list = CommentModel::findList($whereMap, [], $page, $size, function ($q) {
return $q->withJoin("account");
}, $orders);
$list["list"]->each(function ($item){
if($item->type == CommentModel::type_text){
$data = json_decode($item->comment,true);
if(!empty($data)){
$dataStr = '';
foreach ($data as $ditem){
if($ditem["msgType"] == CommentModel::msg_type_emoji){
$dataStr .= "<img class='emoji' src='/static/" . $ditem['msgImage']."'/>";
}else{
$dataStr.=$ditem['msgCont'];
}
}
$item->comment = $dataStr;
}
}
});
return $this->json(0, 'success', $list);
}
return $this->view();
}
}