CommentController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/3
  6. * Time: 下午2:29
  7. */
  8. namespace app\frontend\modules\goods\controllers;
  9. use app\common\components\ApiController;
  10. use app\common\components\UploadVerificationApiController;
  11. use app\common\helpers\Cache;
  12. use app\common\models\comment\CommentConfig;
  13. use app\common\models\Goods;
  14. use app\common\models\Member;
  15. use app\common\models\OrderGoods;
  16. use app\common\services\MiniFileLimitService;
  17. use app\frontend\models\Order;
  18. use app\frontend\modules\goods\models\Comment;
  19. use Carbon\Carbon;
  20. use Illuminate\Support\Facades\Cookie;
  21. use Illuminate\Support\Facades\DB;
  22. use Yunshop\StoreCashier\common\models\StoreOrder;
  23. class CommentController extends UploadVerificationApiController
  24. {
  25. public $commentConfig;
  26. // 前端评论时,图片不能超过5张
  27. const COMMENT_IMAGE_COUNT = 5;
  28. public function preAction()
  29. {
  30. $this->commentConfig = CommentConfig::getSetConfig();
  31. parent::preAction(); // TODO: Change the autogenerated stub
  32. }
  33. public function getComment()
  34. {
  35. app('db')->cacheSelect = true;
  36. $goodsId = \YunShop::request()->goods_id;
  37. $pageSize = 20;
  38. $list = Comment::getCommentsByGoods($goodsId)
  39. ->where(function ($query) {
  40. $query->where('has_default_good_reputation',0)
  41. ->orWhere(function($query2){
  42. $query2->where('has_default_good_reputation',1)->where('additional_comment_id','!=',0);//默认好评的评价需要追评才显示
  43. });
  44. })
  45. ->paginate($pageSize);
  46. $memberLevel = \app\common\models\MemberLevel::uniacid()->select('id', 'level_name')->get()->toArray();
  47. $levelList = [];
  48. foreach ($memberLevel as $item) {
  49. $levelList[$item['id']] = $item['level_name'];
  50. }
  51. if ($list) {
  52. foreach ($list as &$item) {
  53. $item->reply_count = $item->hasManyReply->count('id');
  54. $item->head_img_url = $item->head_img_url ? replace_yunshop(yz_tomedia($item->head_img_url)) : yz_tomedia(\Setting::get('shop.shop.logo'));
  55. $item->level_name = $levelList[$item->level_set] ?? $item->hasOneMember->yzMember->level->level_name ?? \Setting::get('shop.member.level_name') ?? "普通会员";
  56. }
  57. //对评论图片进行处理,反序列化并组装完整图片url
  58. $list = $list->toArray();
  59. foreach ($list['data'] as &$item) {
  60. $item['nick_name'] = substrCut($item['nick_name']);
  61. self::unSerializeImage($item);
  62. }
  63. // $list['favorable_rate'] = $this->favorableRate($goodsId);
  64. return $this->successJson('获取评论数据成功!', $list);
  65. }
  66. return $this->errorJson('未检测到评论数据!', $list);
  67. }
  68. /*
  69. * 获取商品好评率
  70. */
  71. public function favorableRate($id)
  72. {
  73. $total = OrderGoods::where('goods_id',$id)->sum('id');
  74. $level_comment = \app\common\models\Comment::where(['goods_id' => $id])->sum('level');
  75. $comment = \app\common\models\Comment::where(['goods_id' => $id])->sum('id');
  76. $mark = bcmul($total,5,2);//总评分
  77. $no_comment = bcmul(bcsub($total,$comment,2) ,5,2);//未评分
  78. $have_comment = bcmul(bcdiv(bcadd($level_comment,$no_comment,2),$mark,2),100,2);//最终好评率
  79. return $have_comment.'%';
  80. }
  81. /**
  82. * 评论页面
  83. * @return \Illuminate\Http\JsonResponse
  84. */
  85. public function createCommentPage()
  86. {
  87. $is_score_latitude = CommentConfig::isScoreLatitude();
  88. return $this->successJson('success',[
  89. 'is_score_latitude' => $is_score_latitude
  90. ]);
  91. }
  92. /**
  93. * 追评页面
  94. */
  95. public function appendCommentPage()
  96. {
  97. $orderId = \YunShop::request()->order_id ?: 0;// 0为后台虚拟评论
  98. $goodsId = \YunShop::request()->goods_id;
  99. $order_goods_id = \YunShop::request()->order_goods_id;//订单商品表ID,主要用于区分商品规格
  100. if (!$orderId || !$goodsId) return $this->errorJson('获取评论失败!未检测到评论ID!');
  101. $comment_model = Comment::uniacid()
  102. ->select('id','goods_id','content','images','level','order_goods_id')
  103. ->with([
  104. 'hasOneGoods' => function($query) {
  105. $query->select(['id','title','thumb','price']);
  106. }
  107. ])
  108. ->where('type', 1)
  109. ->where('order_id', $orderId)
  110. ->where('goods_id', $goodsId);
  111. //存在order_goods_id为此次更新后评论/追评的数据
  112. if ($comment_model->first()->order_goods_id != 0) {
  113. $comment_model->where('order_goods_id',$order_goods_id);
  114. }
  115. $comment = $comment_model->first();
  116. if ($comment) {
  117. $comment->toArray();
  118. } else {
  119. return $this->errorJson('获取评论失败!!');
  120. }
  121. self::unSerializeImage($comment);
  122. return $this->successJson('success',[
  123. 'comment' => $comment
  124. ]);
  125. }
  126. //评论
  127. public function createComment()
  128. {
  129. $key = \YunShop::request()->order_id.'create_comment_'.\YunShop::app()->getMemberId();
  130. if (Cache::has($key)) {
  131. return $this->errorJson('请求太过频繁!!');
  132. }
  133. Cache::put($key, 1, 1);
  134. if (request()->input('ysi') && app('plugins')->isEnabled('ys-system')) {
  135. \Yunshop\YsSystem\common\AccountBindState::editWhetherSave(request()->input('ysi'));
  136. }
  137. $commentConfig = $this->commentConfig;
  138. $commentModel = new \app\common\models\Comment();
  139. $ingress = request()->ingress;
  140. $member = Member::getUserInfos(\YunShop::app()->getMemberId())->first();
  141. if (!$member) {
  142. return $this->errorJson('评论失败!未检测到会员数据!');
  143. }
  144. $commentStatus = '1';
  145. $comment = [
  146. 'order_id' => \YunShop::request()->order_id,
  147. 'goods_id' => \YunShop::request()->goods_id,
  148. 'content' => \YunShop::request()->content,
  149. 'level' => \YunShop::request()->level,
  150. 'order_goods_id' => \YunShop::request()->order_goods_id ?: 0
  151. ];
  152. //评分纬度
  153. if ($commentConfig->is_score_latitude && \YunShop::request()->score_latitude) {
  154. $comment['score_latitude'] = json_encode(\YunShop::request()->score_latitude);
  155. }
  156. //审核
  157. if ($commentConfig->is_comment_audit) {
  158. $comment['audit_status'] = $commentModel::wait_audit;
  159. }
  160. if ($ingress && !empty($comment['content'])) {
  161. $check_result = (new MiniFileLimitService())->checkMsg($comment['content']);
  162. if ($check_result['errcode'] != 0) {
  163. return $this->errorJson('输入信息含有违法违规内容');
  164. }
  165. }
  166. if (!$comment['order_id']) {
  167. return $this->errorJson('评论失败!未检测到订单ID!');
  168. }
  169. if (!$comment['goods_id']) {
  170. return $this->errorJson('评论失败!未检测到商品ID!');
  171. }
  172. if (!$comment['content']) {
  173. return $this->errorJson('评论失败!未检测到评论内容!');
  174. }
  175. if (!$comment['level']) {
  176. return $this->errorJson('评论失败!未检测到评论等级!');
  177. }
  178. if (\YunShop::request()->images) {
  179. $comment['images'] = json_decode(\YunShop::request()->images);
  180. if (is_array($comment['images'])) {
  181. if (count($comment['images']) > self::COMMENT_IMAGE_COUNT) {
  182. return $this->errorJson('评论失败!评论图片不能多于5张!');
  183. }
  184. $comment['images'] = serialize($comment['images']);
  185. } else {
  186. return $this->errorJson('评论失败!评论图片数据不正确!');
  187. }
  188. } else {
  189. $comment['images'] = serialize([]);
  190. }
  191. $plugin_id = \app\common\models\Order::select(['uid','plugin_id'])->where('id',$comment['order_id'])->first();
  192. if ($member->uid != $plugin_id->uid) {
  193. return $this->errorJson('评论失败!该订单不属于您!');
  194. }
  195. $is_open =app('plugins')->isEnabled('store-cashier');
  196. if ($is_open){
  197. $store_id = StoreOrder::select('store_id')->where('order_id',$comment['order_id'])->first();
  198. }
  199. $commentModel->setRawAttributes($comment);
  200. $commentModel->plugin_id = $plugin_id->plugin_id;
  201. $commentModel->plugin_table_id = $is_open ? $store_id->store_id : null;
  202. $commentModel->uniacid = \YunShop::app()->uniacid;
  203. $commentModel->uid = $member->uid;
  204. $commentModel->nick_name = $member->nickname;
  205. $commentModel->head_img_url = $member->avatar;
  206. $commentModel->type = '1';
  207. $res = $this->insertComment($commentModel, $commentStatus);
  208. if(!is_null($event_arr = \app\common\modules\shop\ShopConfig::current()->get('after_comment_log'))){
  209. foreach ($event_arr as $v){
  210. $class = array_get($v, 'class');
  211. $function = array_get($v, 'function');
  212. $class::$function(request());
  213. }
  214. }
  215. return $res;
  216. }
  217. //追评
  218. public function appendComment()
  219. {
  220. $commentConfig = $this->commentConfig;
  221. if (!$commentConfig->is_additional_comment) {
  222. return $this->errorJson('未开启追评功能!');
  223. }
  224. $commentModel = new \app\common\models\Comment();
  225. $ingress = request()->ingress;
  226. $member = Member::getUserInfos(\YunShop::app()->getMemberId())->first();
  227. if (!$member) {
  228. return $this->errorJson('追加评论失败!未检测到会员数据!');
  229. }
  230. $commentStatus = '2';
  231. $id = \YunShop::request()->id;
  232. $append = $commentModel::find($id);
  233. if (!$append) {
  234. return $this->errorJson('追加评论失败!未检测到评论数据!');
  235. }
  236. $comment = [
  237. 'order_id' => $append->order_id,
  238. 'goods_id' => $append->goods_id,
  239. 'content' => \YunShop::request()->content,
  240. 'comment_id' => $append->id,
  241. 'plugin_id' => $append->plugin_id,
  242. 'plugin_table_id' => $append->plugin_table_id,
  243. 'order_goods_id' => \YunShop::request()->order_goods_id
  244. ];
  245. //审核
  246. if ($commentConfig->is_comment_audit) {
  247. $comment['audit_status'] = $commentModel::wait_audit;
  248. }
  249. if ($ingress && !empty($comment['content'])) {
  250. $check_result = (new MiniFileLimitService())->checkMsg($comment['content']);
  251. if ($check_result['errcode'] != 0) {
  252. return $this->errorJson('输入信息含有违法违规内容');
  253. }
  254. }
  255. if (!$comment['content']) {
  256. return $this->errorJson('追加评论失败!未检测到评论内容!');
  257. }
  258. if (\YunShop::request()->images) {
  259. $comment['images'] = json_decode(\YunShop::request()->images);
  260. if (is_array($comment['images'])) {
  261. if (count($comment['images']) > self::COMMENT_IMAGE_COUNT) {
  262. return $this->errorJson('追加评论失败!评论图片不能多于5张!');
  263. }
  264. $comment['images'] = serialize($comment['images']);
  265. } else {
  266. return $this->errorJson('追加评论失败!评论图片数据不正确!');
  267. }
  268. } else {
  269. $comment['images'] = serialize([]);
  270. }
  271. $commentModel->setRawAttributes($comment);
  272. $commentModel->uniacid = \YunShop::app()->uniacid;
  273. $commentModel->uid = $member->uid;
  274. $commentModel->nick_name = $member->nickname;
  275. $commentModel->head_img_url = $member->avatar;
  276. $commentModel->reply_id = $append->uid;
  277. $commentModel->reply_name = $append->nick_name;
  278. $commentModel->type = '3';
  279. return $this->insertComment($commentModel, $commentStatus, $id);
  280. }
  281. //回复
  282. public function replyComment()
  283. {
  284. $commentModel = new \app\common\models\Comment();
  285. $member = Member::getUserInfos(\YunShop::app()->getMemberId())->first();
  286. if (!$member) {
  287. return $this->errorJson('回复评论失败!未检测到会员数据!');
  288. }
  289. $id = \YunShop::request()->id;
  290. $reply = $commentModel::find($id);
  291. if (!$reply) {
  292. return $this->errorJson('回复评论失败!未检测到评论数据!');
  293. }
  294. $comment = [
  295. 'order_id' => $reply->order_id,
  296. 'goods_id' => $reply->goods_id,
  297. 'content' => \YunShop::request()->content,
  298. 'comment_id' => $reply->comment_id ? $reply->comment_id : $reply->id,
  299. ];
  300. if (!$comment['content']) {
  301. return $this->errorJson('回复评论失败!未检测到评论内容!');
  302. }
  303. // if (isset($comment['images'] ) && is_array($comment['images'])) {
  304. // $comment['images'] = serialize($comment['images']);
  305. // } else {
  306. // $comment['images'] = serialize([]);
  307. // }
  308. if (\YunShop::request()->images) {
  309. $comment['images'] = json_decode(\YunShop::request()->images);
  310. if (is_array($comment['images'])) {
  311. if (count($comment['images']) > self::COMMENT_IMAGE_COUNT) {
  312. return $this->errorJson('追加评论失败!评论图片不能多于5张!');
  313. }
  314. $comment['images'] = serialize($comment['images']);
  315. } else {
  316. return $this->errorJson('追加评论失败!评论图片数据不正确!');
  317. }
  318. } else {
  319. $comment['images'] = serialize([]);
  320. }
  321. $commentModel->setRawAttributes($comment);
  322. $commentModel->uniacid = \YunShop::app()->uniacid;
  323. $commentModel->uid = $member->uid;
  324. $commentModel->nick_name = $member->nickname;
  325. $commentModel->head_img_url = $member->avatar;
  326. $commentModel->reply_id = $reply->uid;
  327. $commentModel->reply_name = $reply->nick_name;
  328. $commentModel->type = '2';
  329. return $this->insertComment($commentModel);
  330. }
  331. /**
  332. * @param $commentModel
  333. * @param string $commentStatus 评论状态:1-已评论,2-已追评
  334. * @param int $additional_comment_id 追评ID(用于主评论标识搜索方便)
  335. * @return \Illuminate\Http\JsonResponse
  336. */
  337. public function insertComment($commentModel, $commentStatus = '', $additional_comment_id = 0)
  338. {
  339. $validator = $commentModel->validator($commentModel->getAttributes());
  340. if ($validator->fails()) {
  341. //检测失败
  342. return $this->errorJson($validator->messages());
  343. } else {
  344. //数据保存
  345. if ($commentModel->save()) {
  346. Goods::updatedComment($commentModel->goods_id);
  347. if ($commentStatus) {
  348. //评论
  349. if ($commentStatus == 1) {
  350. $updateData = ['comment_status' => $commentStatus, 'comment_id' => $commentModel->id];
  351. } else {
  352. //追评不更新ID
  353. $updateData = ['comment_status' => $commentStatus];
  354. }
  355. if ($commentModel->order_goods_id) {
  356. OrderGoods::where('id',$commentModel->order_goods_id)->update($updateData);
  357. } else {
  358. OrderGoods::where('order_id', $commentModel->order_id)
  359. ->where('goods_id', $commentModel->goods_id)
  360. ->update($updateData);
  361. }
  362. }
  363. //不需要审核的时候,主评论添加追评ID 否则再审核通过的时候再添加
  364. if ($additional_comment_id && $commentModel->id && !$this->commentConfig->is_comment_audit) {
  365. Comment::updatedAdditionalCommentId($additional_comment_id,$commentModel->id);
  366. }
  367. if (!empty($commentModel->images)) {
  368. $commentModel->images = unserialize($commentModel->images);
  369. }
  370. return $this->successJson('评论成功!',$commentModel);
  371. } else {
  372. return $this->errorJson('评论失败!');
  373. }
  374. }
  375. }
  376. public function getOrderGoodsComment()
  377. {
  378. // $commentId = \YunShop::request()->comment_id;//评论主键ID 部分插件订单列表接口没有关联has_one_comment
  379. $orderId = \YunShop::request()->order_id ?: 0;// 0为后台虚拟评论
  380. $goodsId = \YunShop::request()->goods_id;
  381. $order_goods_id = \YunShop::request()->order_goods_id;
  382. if (!$orderId || !$goodsId) return $this->errorJson('获取评论失败!未检测到评论ID!');
  383. // 0
  384. if(empty($orderId)){
  385. $with = [
  386. 'hasManyReply'=>function($query) {
  387. $query->where('type', 2)->where('is_show',1);
  388. },
  389. 'hasOneGoods' => function($query) {
  390. $query->select(['id','title','thumb','price']);
  391. },
  392. 'hasOneMember' => function ($query) {
  393. $query->with(['yzMember' => function ($query2) {
  394. $query2->select('member_id', 'level_id');
  395. }])->select('uid');
  396. }
  397. ];
  398. if (app('plugins')->isEnabled('live-install')){
  399. $with['hasOneLiveInstallComment'] = function ($query){
  400. $query->select('id','comment_id','worker_score');
  401. };
  402. }
  403. $comment_model = Comment::uniacid()
  404. ->with($with)
  405. ->where('type', 1)
  406. ->where('order_id', 0)
  407. ->where('goods_id', $goodsId);
  408. //防止之前的评论找不到
  409. if ($comment_model->first()->order_goods_id != 0) {
  410. $comment_model->where('order_goods_id',$order_goods_id);
  411. }
  412. $comment = $comment_model->first();
  413. $comment['has_one_order_goods'] = $comment['hasOneGoods'];
  414. $comment['has_one_order_goods']['total'] = 1;
  415. $comment['has_one_order_goods']['thumb'] = yz_tomedia($comment['has_one_order_goods']['thumb']);
  416. $comment['head_img_url'] = yz_tomedia($comment['head_img_url']);
  417. unset($comment['hasOneGoods']);
  418. }else{
  419. $with = [
  420. 'hasManyReply'=>function($query) {
  421. $query->where('type', 2)->where('is_show',1);
  422. },
  423. 'hasOneOrderGoods' => function($query) use ($goodsId) {
  424. $query->where('goods_id', $goodsId);
  425. },
  426. 'hasOneMember' => function ($query) {
  427. $query->with(['yzMember' => function ($query2) {
  428. $query2->select('member_id', 'level_id');
  429. }])->select('uid');
  430. }
  431. ];
  432. $comment_model = Comment::uniacid()
  433. ->with($with)
  434. ->where('type', 1)
  435. ->where('goods_id', $goodsId)
  436. ->where('order_id', $orderId);
  437. //防止之前的评论找不到
  438. if ($comment_model->first()->order_goods_id != 0) {
  439. $comment_model->where('order_goods_id',$order_goods_id);
  440. }
  441. $comment = $comment_model->first();
  442. }
  443. if ($comment) {
  444. // 将图片字段反序列化
  445. if (!is_array($comment)) {
  446. $comment = $comment->toArray();
  447. }
  448. $arrComment = $comment;
  449. if (!is_null($event_arr = \app\common\modules\shop\ShopConfig::current()->get('frontend_comment_detail'))) {
  450. foreach ($event_arr as $v) {
  451. $class = array_get($v, 'class');
  452. $function = array_get($v, 'function');
  453. $res = $class::$function($arrComment);
  454. foreach ($res as $vv) {
  455. $arrComment[$vv['key']] = $vv;
  456. }
  457. }
  458. }
  459. foreach ($arrComment['has_many_reply'] as $k => $v) {
  460. $arrComment['has_many_reply'][$k]['nick_name'] = substrCut($v['nick_name']);
  461. $arrComment['has_many_reply'][$k]['reply_name'] = substrCut($v['reply_name']);
  462. }
  463. $memberLevel = \app\common\models\MemberLevel::uniacid()->select('id', 'level_name')->get()->toArray();
  464. $levelList = [];
  465. foreach ($memberLevel as $item) {
  466. $levelList[$item['id']] = $item['level_name'];
  467. }
  468. $arrComment['nick_name'] = substrCut($arrComment['nick_name']);
  469. $arrComment['level_name'] = $levelList[$arrComment['level_set']] ?? $levelList[$arrComment['has_one_member']['yz_member']['level_id']] ?? \Setting::get('shop.member.level_name') ?? "普通会员";
  470. self::unSerializeImage($arrComment);
  471. return $this->successJson('获取评论数据成功!', $arrComment);
  472. }
  473. return $this->errorJson('未检测到评论数据!');
  474. }
  475. // 反序列化图片
  476. public static function unSerializeImage(&$arrComment)
  477. {
  478. $arrComment['images'] = unserialize($arrComment['images']);
  479. foreach ($arrComment['images'] as &$image) {
  480. $image = yz_tomedia($image);
  481. }
  482. if ($arrComment['append']) {
  483. $arrComment['append']['images'] = unserialize($arrComment['append']['images']);
  484. foreach ($arrComment['append']['images'] as &$image) {
  485. $image = yz_tomedia($image);
  486. }
  487. }
  488. if ($arrComment['has_many_reply']) {
  489. foreach ($arrComment['has_many_reply'] as &$comment) {
  490. $comment['images'] = unserialize($comment['images']);
  491. foreach ($comment['images'] as &$image) {
  492. $image = yz_tomedia($image);
  493. }
  494. }
  495. }
  496. }
  497. }