578 lines
22 KiB
PHP
578 lines
22 KiB
PHP
<?php
|
||
|
||
/**
|
||
* Created by PhpStorm.
|
||
* Author:
|
||
* Date: 2017/3/3
|
||
* Time: 下午2:29
|
||
*/
|
||
|
||
namespace app\frontend\modules\goods\controllers;
|
||
|
||
use app\common\components\ApiController;
|
||
use app\common\components\UploadVerificationApiController;
|
||
use app\common\helpers\Cache;
|
||
use app\common\models\comment\CommentConfig;
|
||
use app\common\models\Goods;
|
||
use app\common\models\Member;
|
||
use app\common\models\OrderGoods;
|
||
use app\common\services\MiniFileLimitService;
|
||
use app\frontend\models\Order;
|
||
use app\frontend\modules\goods\models\Comment;
|
||
use Carbon\Carbon;
|
||
use Illuminate\Support\Facades\Cookie;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Yunshop\StoreCashier\common\models\StoreOrder;
|
||
|
||
class CommentController extends UploadVerificationApiController
|
||
{
|
||
public $commentConfig;
|
||
// 前端评论时,图片不能超过5张
|
||
const COMMENT_IMAGE_COUNT = 5;
|
||
|
||
public function preAction()
|
||
{
|
||
$this->commentConfig = CommentConfig::getSetConfig();
|
||
|
||
parent::preAction(); // TODO: Change the autogenerated stub
|
||
}
|
||
|
||
|
||
|
||
public function getComment()
|
||
{
|
||
app('db')->cacheSelect = true;
|
||
$goodsId = \YunShop::request()->goods_id;
|
||
$pageSize = 20;
|
||
|
||
$list = Comment::getCommentsByGoods($goodsId)
|
||
->where(function ($query) {
|
||
$query->where('has_default_good_reputation',0)
|
||
->orWhere(function($query2){
|
||
$query2->where('has_default_good_reputation',1)->where('additional_comment_id','!=',0);//默认好评的评价需要追评才显示
|
||
});
|
||
})
|
||
->paginate($pageSize);
|
||
$memberLevel = \app\common\models\MemberLevel::uniacid()->select('id', 'level_name')->get()->toArray();
|
||
$levelList = [];
|
||
foreach ($memberLevel as $item) {
|
||
$levelList[$item['id']] = $item['level_name'];
|
||
}
|
||
if ($list) {
|
||
foreach ($list as &$item) {
|
||
$item->reply_count = $item->hasManyReply->count('id');
|
||
$item->head_img_url = $item->head_img_url ? replace_yunshop(yz_tomedia($item->head_img_url)) : yz_tomedia(\Setting::get('shop.shop.logo'));
|
||
$item->level_name = $levelList[$item->level_set] ?? $item->hasOneMember->yzMember->level->level_name ?? \Setting::get('shop.member.level_name') ?? "普通会员";
|
||
}
|
||
//对评论图片进行处理,反序列化并组装完整图片url
|
||
$list = $list->toArray();
|
||
foreach ($list['data'] as &$item) {
|
||
$item['nick_name'] = substrCut2($item['nick_name']);
|
||
self::unSerializeImage($item);
|
||
}
|
||
// $list['favorable_rate'] = $this->favorableRate($goodsId);
|
||
return $this->successJson('获取评论数据成功!', $list);
|
||
}
|
||
return $this->errorJson('未检测到评论数据!', $list);
|
||
}
|
||
|
||
/*
|
||
* 获取商品好评率
|
||
*/
|
||
public function favorableRate($id)
|
||
{
|
||
$total = OrderGoods::where('goods_id',$id)->sum('id');
|
||
$level_comment = \app\common\models\Comment::where(['goods_id' => $id])->sum('level');
|
||
$comment = \app\common\models\Comment::where(['goods_id' => $id])->sum('id');
|
||
$mark = bcmul($total,5,2);//总评分
|
||
$no_comment = bcmul(bcsub($total,$comment,2) ,5,2);//未评分
|
||
$have_comment = bcmul(bcdiv(bcadd($level_comment,$no_comment,2),$mark,2),100,2);//最终好评率
|
||
return $have_comment.'%';
|
||
|
||
}
|
||
|
||
/**
|
||
* 评论页面
|
||
* @return \Illuminate\Http\JsonResponse
|
||
*/
|
||
public function createCommentPage()
|
||
{
|
||
$is_score_latitude = CommentConfig::isScoreLatitude();
|
||
|
||
return $this->successJson('success',[
|
||
'is_score_latitude' => $is_score_latitude
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 追评页面
|
||
*/
|
||
public function appendCommentPage()
|
||
{
|
||
$orderId = \YunShop::request()->order_id ?: 0;// 0为后台虚拟评论
|
||
$goodsId = \YunShop::request()->goods_id;
|
||
$order_goods_id = \YunShop::request()->order_goods_id;//订单商品表ID,主要用于区分商品规格
|
||
|
||
if (!$orderId || !$goodsId) return $this->errorJson('获取评论失败!未检测到评论ID!');
|
||
|
||
$comment_model = Comment::uniacid()
|
||
->select('id','goods_id','content','images','level','order_goods_id')
|
||
->with([
|
||
'hasOneGoods' => function($query) {
|
||
$query->select(['id','title','thumb','price']);
|
||
}
|
||
])
|
||
->where('type', 1)
|
||
->where('order_id', $orderId)
|
||
->where('goods_id', $goodsId);
|
||
|
||
//存在order_goods_id为此次更新后评论/追评的数据
|
||
if ($comment_model->first()->order_goods_id != 0) {
|
||
$comment_model->where('order_goods_id',$order_goods_id);
|
||
}
|
||
|
||
$comment = $comment_model->first();
|
||
if ($comment) {
|
||
$comment->toArray();
|
||
} else {
|
||
return $this->errorJson('获取评论失败!!');
|
||
}
|
||
|
||
self::unSerializeImage($comment);
|
||
|
||
return $this->successJson('success',[
|
||
'comment' => $comment
|
||
]);
|
||
}
|
||
|
||
//评论
|
||
public function createComment()
|
||
{
|
||
$key = \YunShop::request()->order_id.'create_comment_'.\YunShop::app()->getMemberId();
|
||
if (Cache::has($key)) {
|
||
return $this->errorJson('请求太过频繁!!');
|
||
}
|
||
Cache::put($key, 1, 1);
|
||
|
||
if (request()->input('ysi') && app('plugins')->isEnabled('ys-system')) {
|
||
\Yunshop\YsSystem\common\AccountBindState::editWhetherSave(request()->input('ysi'));
|
||
}
|
||
|
||
|
||
$commentConfig = $this->commentConfig;
|
||
$commentModel = new \app\common\models\Comment();
|
||
$ingress = request()->ingress;
|
||
$member = Member::getUserInfos(\YunShop::app()->getMemberId())->first();
|
||
if (!$member) {
|
||
return $this->errorJson('评论失败!未检测到会员数据!');
|
||
}
|
||
$commentStatus = '1';
|
||
|
||
$comment = [
|
||
'order_id' => \YunShop::request()->order_id,
|
||
'goods_id' => \YunShop::request()->goods_id,
|
||
'content' => \YunShop::request()->content,
|
||
'level' => \YunShop::request()->level,
|
||
'order_goods_id' => \YunShop::request()->order_goods_id ?: 0
|
||
];
|
||
|
||
//评分纬度
|
||
if ($commentConfig->is_score_latitude && \YunShop::request()->score_latitude) {
|
||
$comment['score_latitude'] = json_encode(\YunShop::request()->score_latitude);
|
||
}
|
||
|
||
//审核
|
||
if ($commentConfig->is_comment_audit) {
|
||
$comment['audit_status'] = $commentModel::wait_audit;
|
||
}
|
||
|
||
if ($ingress && !empty($comment['content'])) {
|
||
$check_result = (new MiniFileLimitService())->checkMsg($comment['content']);
|
||
if ($check_result['errcode'] != 0) {
|
||
return $this->errorJson('输入信息含有违法违规内容');
|
||
}
|
||
}
|
||
|
||
if (!$comment['order_id']) {
|
||
return $this->errorJson('评论失败!未检测到订单ID!');
|
||
}
|
||
if (!$comment['goods_id']) {
|
||
return $this->errorJson('评论失败!未检测到商品ID!');
|
||
}
|
||
if (!$comment['content']) {
|
||
return $this->errorJson('评论失败!未检测到评论内容!');
|
||
}
|
||
if (!$comment['level']) {
|
||
return $this->errorJson('评论失败!未检测到评论等级!');
|
||
}
|
||
|
||
if (\YunShop::request()->images) {
|
||
$comment['images'] = json_decode(\YunShop::request()->images);
|
||
if (is_array($comment['images'])) {
|
||
if (count($comment['images']) > self::COMMENT_IMAGE_COUNT) {
|
||
return $this->errorJson('评论失败!评论图片不能多于5张!');
|
||
}
|
||
$comment['images'] = serialize($comment['images']);
|
||
} else {
|
||
return $this->errorJson('评论失败!评论图片数据不正确!');
|
||
}
|
||
} else {
|
||
$comment['images'] = serialize([]);
|
||
}
|
||
|
||
$plugin_id = \app\common\models\Order::select(['uid','plugin_id'])->where('id',$comment['order_id'])->first();
|
||
|
||
|
||
if ($member->uid != $plugin_id->uid) {
|
||
return $this->errorJson('评论失败!该订单不属于您!');
|
||
}
|
||
|
||
$is_open =app('plugins')->isEnabled('store-cashier');
|
||
if ($is_open){
|
||
$store_id = StoreOrder::select('store_id')->where('order_id',$comment['order_id'])->first();
|
||
}
|
||
|
||
|
||
$commentModel->setRawAttributes($comment);
|
||
$commentModel->plugin_id = $plugin_id->plugin_id;
|
||
$commentModel->plugin_table_id = $is_open ? $store_id->store_id : null;
|
||
$commentModel->uniacid = \YunShop::app()->uniacid;
|
||
$commentModel->uid = $member->uid;
|
||
$commentModel->nick_name = $member->nickname;
|
||
$commentModel->head_img_url = $member->avatar;
|
||
$commentModel->type = '1';
|
||
$res = $this->insertComment($commentModel, $commentStatus);
|
||
|
||
if(!is_null($event_arr = \app\common\modules\shop\ShopConfig::current()->get('after_comment_log'))){
|
||
foreach ($event_arr as $v){
|
||
$class = array_get($v, 'class');
|
||
$function = array_get($v, 'function');
|
||
$class::$function(request());
|
||
}
|
||
}
|
||
|
||
return $res;
|
||
}
|
||
|
||
//追评
|
||
public function appendComment()
|
||
{
|
||
$commentConfig = $this->commentConfig;
|
||
if (!$commentConfig->is_additional_comment) {
|
||
return $this->errorJson('未开启追评功能!');
|
||
}
|
||
$commentModel = new \app\common\models\Comment();
|
||
$ingress = request()->ingress;
|
||
$member = Member::getUserInfos(\YunShop::app()->getMemberId())->first();
|
||
if (!$member) {
|
||
return $this->errorJson('追加评论失败!未检测到会员数据!');
|
||
}
|
||
$commentStatus = '2';
|
||
$id = \YunShop::request()->id;
|
||
$append = $commentModel::find($id);
|
||
if (!$append) {
|
||
return $this->errorJson('追加评论失败!未检测到评论数据!');
|
||
}
|
||
|
||
$comment = [
|
||
'order_id' => $append->order_id,
|
||
'goods_id' => $append->goods_id,
|
||
'content' => \YunShop::request()->content,
|
||
'comment_id' => $append->id,
|
||
'plugin_id' => $append->plugin_id,
|
||
'plugin_table_id' => $append->plugin_table_id,
|
||
'order_goods_id' => \YunShop::request()->order_goods_id
|
||
];
|
||
|
||
//审核
|
||
if ($commentConfig->is_comment_audit) {
|
||
$comment['audit_status'] = $commentModel::wait_audit;
|
||
}
|
||
|
||
if ($ingress && !empty($comment['content'])) {
|
||
$check_result = (new MiniFileLimitService())->checkMsg($comment['content']);
|
||
if ($check_result['errcode'] != 0) {
|
||
return $this->errorJson('输入信息含有违法违规内容');
|
||
}
|
||
}
|
||
|
||
if (!$comment['content']) {
|
||
return $this->errorJson('追加评论失败!未检测到评论内容!');
|
||
}
|
||
|
||
if (\YunShop::request()->images) {
|
||
$comment['images'] = json_decode(\YunShop::request()->images);
|
||
if (is_array($comment['images'])) {
|
||
if (count($comment['images']) > self::COMMENT_IMAGE_COUNT) {
|
||
return $this->errorJson('追加评论失败!评论图片不能多于5张!');
|
||
}
|
||
$comment['images'] = serialize($comment['images']);
|
||
} else {
|
||
return $this->errorJson('追加评论失败!评论图片数据不正确!');
|
||
}
|
||
} else {
|
||
$comment['images'] = serialize([]);
|
||
}
|
||
|
||
$commentModel->setRawAttributes($comment);
|
||
|
||
$commentModel->uniacid = \YunShop::app()->uniacid;
|
||
$commentModel->uid = $member->uid;
|
||
$commentModel->nick_name = $member->nickname;
|
||
$commentModel->head_img_url = $member->avatar;
|
||
$commentModel->reply_id = $append->uid;
|
||
$commentModel->reply_name = $append->nick_name;
|
||
$commentModel->type = '3';
|
||
|
||
return $this->insertComment($commentModel, $commentStatus, $id);
|
||
|
||
}
|
||
|
||
//回复
|
||
public function replyComment()
|
||
{
|
||
$commentModel = new \app\common\models\Comment();
|
||
$member = Member::getUserInfos(\YunShop::app()->getMemberId())->first();
|
||
if (!$member) {
|
||
return $this->errorJson('回复评论失败!未检测到会员数据!');
|
||
}
|
||
|
||
$id = \YunShop::request()->id;
|
||
$reply = $commentModel::find($id);
|
||
if (!$reply) {
|
||
return $this->errorJson('回复评论失败!未检测到评论数据!');
|
||
}
|
||
|
||
$comment = [
|
||
'order_id' => $reply->order_id,
|
||
'goods_id' => $reply->goods_id,
|
||
'content' => \YunShop::request()->content,
|
||
'comment_id' => $reply->comment_id ? $reply->comment_id : $reply->id,
|
||
];
|
||
if (!$comment['content']) {
|
||
return $this->errorJson('回复评论失败!未检测到评论内容!');
|
||
}
|
||
|
||
// if (isset($comment['images'] ) && is_array($comment['images'])) {
|
||
// $comment['images'] = serialize($comment['images']);
|
||
// } else {
|
||
// $comment['images'] = serialize([]);
|
||
// }
|
||
if (\YunShop::request()->images) {
|
||
$comment['images'] = json_decode(\YunShop::request()->images);
|
||
if (is_array($comment['images'])) {
|
||
if (count($comment['images']) > self::COMMENT_IMAGE_COUNT) {
|
||
return $this->errorJson('追加评论失败!评论图片不能多于5张!');
|
||
}
|
||
$comment['images'] = serialize($comment['images']);
|
||
} else {
|
||
return $this->errorJson('追加评论失败!评论图片数据不正确!');
|
||
}
|
||
} else {
|
||
$comment['images'] = serialize([]);
|
||
}
|
||
|
||
$commentModel->setRawAttributes($comment);
|
||
|
||
$commentModel->uniacid = \YunShop::app()->uniacid;
|
||
$commentModel->uid = $member->uid;
|
||
$commentModel->nick_name = substrCut2($member->nickname);
|
||
$commentModel->head_img_url = $member->avatar;
|
||
$commentModel->reply_id = $reply->uid;
|
||
$commentModel->reply_name = $reply->nick_name;
|
||
$commentModel->type = '2';
|
||
return $this->insertComment($commentModel);
|
||
|
||
}
|
||
|
||
/**
|
||
* @param $commentModel
|
||
* @param string $commentStatus 评论状态:1-已评论,2-已追评
|
||
* @param int $additional_comment_id 追评ID(用于主评论标识搜索方便)
|
||
* @return \Illuminate\Http\JsonResponse
|
||
*/
|
||
public function insertComment($commentModel, $commentStatus = '', $additional_comment_id = 0)
|
||
{
|
||
$validator = $commentModel->validator($commentModel->getAttributes());
|
||
if ($validator->fails()) {
|
||
//检测失败
|
||
return $this->errorJson($validator->messages());
|
||
} else {
|
||
//数据保存
|
||
if ($commentModel->save()) {
|
||
Goods::updatedComment($commentModel->goods_id);
|
||
|
||
if ($commentStatus) {
|
||
//评论
|
||
if ($commentStatus == 1) {
|
||
$updateData = ['comment_status' => $commentStatus, 'comment_id' => $commentModel->id];
|
||
} else {
|
||
//追评不更新ID
|
||
$updateData = ['comment_status' => $commentStatus];
|
||
}
|
||
|
||
if ($commentModel->order_goods_id) {
|
||
OrderGoods::where('id',$commentModel->order_goods_id)->update($updateData);
|
||
} else {
|
||
OrderGoods::where('order_id', $commentModel->order_id)
|
||
->where('goods_id', $commentModel->goods_id)
|
||
->update($updateData);
|
||
}
|
||
}
|
||
|
||
//不需要审核的时候,主评论添加追评ID 否则再审核通过的时候再添加
|
||
if ($additional_comment_id && $commentModel->id && !$this->commentConfig->is_comment_audit) {
|
||
Comment::updatedAdditionalCommentId($additional_comment_id,$commentModel->id);
|
||
}
|
||
|
||
if (!empty($commentModel->images)) {
|
||
$commentModel->images = unserialize($commentModel->images);
|
||
}
|
||
|
||
return $this->successJson('评论成功!',$commentModel);
|
||
} else {
|
||
return $this->errorJson('评论失败!');
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
public function getOrderGoodsComment()
|
||
{
|
||
$commentId = \YunShop::request()->comment_id;//评论主键ID
|
||
$orderId = \YunShop::request()->order_id ?: 0;// 0为后台虚拟评论
|
||
$goodsId = \YunShop::request()->goods_id;
|
||
$order_goods_id = \YunShop::request()->order_goods_id;
|
||
if (!$orderId && !$goodsId) return $this->errorJson('获取评论失败!未检测到评论ID!');
|
||
|
||
// 0
|
||
if(empty($orderId)){
|
||
$with = [
|
||
'hasManyReply'=>function($query) {
|
||
$query->where('type', 2)->where('is_show',1);
|
||
},
|
||
'hasOneGoods' => function($query) {
|
||
$query->select(['id','title','thumb','price']);
|
||
},
|
||
'hasOneMember' => function ($query) {
|
||
$query->with(['yzMember' => function ($query2) {
|
||
$query2->select('member_id', 'level_id');
|
||
}])->select('uid');
|
||
}
|
||
];
|
||
if (app('plugins')->isEnabled('live-install')){
|
||
$with['hasOneLiveInstallComment'] = function ($query){
|
||
$query->select('id','comment_id','worker_score');
|
||
};
|
||
}
|
||
$comment_model = Comment::uniacid()
|
||
->with($with)
|
||
->where('type', 1)
|
||
->where('order_id', 0)
|
||
->where('goods_id', $goodsId);
|
||
|
||
//防止之前的评论找不到
|
||
if ($comment_model->first()->order_goods_id != 0) {
|
||
$comment_model->where('order_goods_id',$order_goods_id);
|
||
}
|
||
if ($commentId) {
|
||
$comment_model->where('id',$commentId);
|
||
}
|
||
$comment = $comment_model->first();
|
||
$comment['has_one_order_goods'] = $comment['hasOneGoods'];
|
||
$comment['has_one_order_goods']['total'] = 1;
|
||
$comment['has_one_order_goods']['thumb'] = yz_tomedia($comment['has_one_order_goods']['thumb']);
|
||
$comment['head_img_url'] = yz_tomedia($comment['head_img_url']);
|
||
$comment['has_one_order_goods']['goods_id'] = $comment['goods_id'];
|
||
unset($comment['hasOneGoods']);
|
||
}else{
|
||
$with = [
|
||
'hasManyReply'=>function($query) {
|
||
$query->where('type', 2)->where('is_show',1);
|
||
},
|
||
'hasOneOrderGoods' => function($query) use ($goodsId) {
|
||
$query->select(['id','title','thumb','price', 'goods_id', 'order_id', 'total'])->where('goods_id', $goodsId);
|
||
},
|
||
'hasOneMember' => function ($query) {
|
||
$query->with(['yzMember' => function ($query2) {
|
||
$query2->select('member_id', 'level_id');
|
||
}])->select('uid');
|
||
}
|
||
];
|
||
$comment_model = Comment::uniacid()
|
||
->with($with)
|
||
->where('type', 1)
|
||
->where('goods_id', $goodsId)
|
||
->where('order_id', $orderId);
|
||
|
||
//防止之前的评论找不到
|
||
if ($comment_model->first()->order_goods_id != 0) {
|
||
$comment_model->where('order_goods_id',$order_goods_id);
|
||
}
|
||
if ($commentId) {
|
||
$comment_model->where('id',$commentId);
|
||
}
|
||
$comment = $comment_model->first();
|
||
}
|
||
|
||
if ($comment) {
|
||
// 将图片字段反序列化
|
||
if (!is_array($comment)) {
|
||
$comment = $comment->toArray();
|
||
}
|
||
$arrComment = $comment;
|
||
if (!is_null($event_arr = \app\common\modules\shop\ShopConfig::current()->get('frontend_comment_detail'))) {
|
||
foreach ($event_arr as $v) {
|
||
$class = array_get($v, 'class');
|
||
$function = array_get($v, 'function');
|
||
$res = $class::$function($arrComment);
|
||
foreach ($res as $vv) {
|
||
$arrComment[$vv['key']] = $vv;
|
||
}
|
||
}
|
||
}
|
||
foreach ($arrComment['has_many_reply'] as $k => $v) {
|
||
$arrComment['has_many_reply'][$k]['nick_name'] = substrCut2($v['nick_name']);
|
||
$arrComment['has_many_reply'][$k]['reply_name'] = substrCut2($v['reply_name']);
|
||
}
|
||
|
||
$memberLevel = \app\common\models\MemberLevel::uniacid()->select('id', 'level_name')->get()->toArray();
|
||
$levelList = [];
|
||
foreach ($memberLevel as $item) {
|
||
$levelList[$item['id']] = $item['level_name'];
|
||
}
|
||
|
||
$arrComment['nick_name'] = substrCut2($arrComment['nick_name']);
|
||
|
||
$arrComment['level_name'] = $levelList[$arrComment['level_set']] ?? $levelList[$arrComment['has_one_member']['yz_member']['level_id']] ?? \Setting::get('shop.member.level_name') ?? "普通会员";
|
||
|
||
self::unSerializeImage($arrComment);
|
||
return $this->successJson('获取评论数据成功!', $arrComment);
|
||
}
|
||
return $this->errorJson('未检测到评论数据!');
|
||
}
|
||
|
||
// 反序列化图片
|
||
public static function unSerializeImage(&$arrComment)
|
||
{
|
||
$arrComment['images'] = unserialize($arrComment['images']);
|
||
foreach ($arrComment['images'] as &$image) {
|
||
$image = yz_tomedia($image);
|
||
}
|
||
if ($arrComment['append']) {
|
||
$arrComment['append']['images'] = unserialize($arrComment['append']['images']);
|
||
foreach ($arrComment['append']['images'] as &$image) {
|
||
$image = yz_tomedia($image);
|
||
}
|
||
}
|
||
if ($arrComment['has_many_reply']) {
|
||
foreach ($arrComment['has_many_reply'] as &$comment) {
|
||
$comment['images'] = unserialize($comment['images']);
|
||
foreach ($comment['images'] as &$image) {
|
||
$image = yz_tomedia($image);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
} |