caipan_shop_admin/app/traits/account/FeedbackTrait.php

126 lines
3.6 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace app\traits\account;
use app\validate\FeedbackValidate;
use app\model\{
Feedback,
FeedbackType
};
use app\exception\TraitException;
use think\Collection;
use think\Exception;
/**
* 投诉与意见关联
* Trait FeedbackTrait
* @package app\traits\account
*/
trait FeedbackTrait
{
/**
* 投诉与意见分类
*/
public function feedbackTypes()
{
try {
return (new FeedbackType)->select();
} catch (\Exception $e) {
return new Collection();
}
}
/**
* 添加投诉与意见
* 限制每个用户每日每个问题类型最多提交3次
*
* @param int $accountId
* @param array $data 内容参数 ['type_id', 'content', 'user_name', 'user_phone']
* @throws TraitException
*/
public function addFeedback(int $accountId, array $data)
{
try {
$validate = new FeedbackValidate();
if (!$validate->scene('add')->check($data)) {
throw new TraitException($validate->getError());
}
$allowTypeIds = $this->feedbackTypes()->column('id');
if (!in_array($data['type_id'], $allowTypeIds)) {
throw new TraitException('暂不支持该问题类型信息的提交');
}
$btStart = date('Y-m-d 00:00:00');
$btEnd = date('Y-m-d 23:59:59');
$hasCount = Feedback::where('account_id', $accountId)
->where('type_id', $data['type_id'])
->whereBetweenTime('created_at', $btStart, $btEnd)
->count();
if ($hasCount >= 3) {
throw new TraitException('同一问题类型每日最多可提交3次请勿频繁提交谢谢您的反馈');
}
Feedback::create([
'account_id' => $accountId,
'type_id' => $data['type_id'],
'content' => $data['content'],
'user_name' => $data['user_name'],
'user_phone' => $data['user_phone'],
'ip' => request()->ip(),
'status' => Feedback::STATUS_WAITING,
'created_at' => date('Y-m-d H:i:s'),
]);
} catch (TraitException $e) {
throw $e;
} catch (\Exception $e) {
throw new TraitException('反馈信息提交失败!');
}
}
/**
* 反馈意见列表
*
* @param array $where
* @param array $fields
* @param int $page
* @param int $size
* @param callable|null $callback
* @param array $orders
* @return array
*/
public function feedbackList(array $where = [], array $fields = [], int $page = 1, int $size = 10, callable $callback = null, array $orders = []): array
{
try {
return Feedback::findList($where, $fields, $page, $size, $callback, $orders);
} catch (\Exception $e) {
return [
'total' => 0,
'current' => $page,
'size' => $size,
'list' => new Collection(),
];
}
}
/**
* 更新反馈意见的状态为已处理
*
* @param array $ids
* @return bool
*/
public function feedbacksToDone(array $ids): bool
{
try {
Feedback::whereIn('id', $ids)->update([
'status' => Feedback::STATUS_DONE,
'updated_at' => date('Y-m-d H:i:s')
]);
} catch (\Exception $e) {
return false;
}
return true;
}
}