CommentController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. <?php
  2. namespace app\backend\modules\goods\controllers;
  3. use app\backend\modules\uploadVerificate\UploadVerificationBaseController;
  4. use app\common\helpers\Url;
  5. use app\common\models\comment\CommentConfig;
  6. use app\common\models\Goods;
  7. use app\common\models\Member;
  8. use app\backend\modules\goods\models\Comment;
  9. use app\backend\modules\goods\services\CommentService;
  10. use app\common\components\BaseController;
  11. use app\common\helpers\PaginationHelper;
  12. use app\common\models\MemberLevel;
  13. /**
  14. * Created by PhpStorm.
  15. * Author: 芸众商城 www.yunzshop.com
  16. * Date: 2017/2/27
  17. * Time: 下午5:09
  18. */
  19. class CommentController extends UploadVerificationBaseController
  20. {
  21. /**
  22. * 评论设置
  23. */
  24. public function index()
  25. {
  26. return view('goods.comment.index')->render();
  27. }
  28. /**
  29. * 评论列表
  30. */
  31. public function list()
  32. {
  33. return view('goods.comment.list')->render();
  34. }
  35. /**
  36. * 审核列表
  37. */
  38. public function audit()
  39. {
  40. return view('goods.comment.audit')->render();
  41. }
  42. //评论设置数据&保存
  43. public function saveSet()
  44. {
  45. $data = request()->form;
  46. $config_data = CommentConfig::getSetConfig();
  47. if ($data) {
  48. if ($config_data) {
  49. $res = CommentConfig::find($config_data['id']);
  50. $res->delete();
  51. }
  52. $res = new CommentConfig();
  53. $resData = [
  54. 'uniacid' => \YunShop::app()->uniacid,
  55. 'is_comment_audit' => $data['is_comment_audit'],
  56. 'is_default_good_reputation' => $data['is_default_good_reputation'],
  57. 'is_order_comment_entrance' => $data['is_order_comment_entrance'],
  58. 'is_additional_comment' => $data['is_additional_comment'],
  59. 'is_score_latitude' => $data['is_score_latitude'],
  60. 'top_sort' => $data['top_sort'],
  61. 'is_order_detail_comment_show' => $data['is_order_detail_comment_show'],
  62. ];
  63. $res->fill($resData);
  64. $res->save();
  65. }
  66. return $this->successJson('success',[
  67. 'data' => $config_data
  68. ]);
  69. }
  70. /**
  71. * 更改评论状态
  72. * @return \Illuminate\Http\JsonResponse
  73. */
  74. public function changeCommentStatus()
  75. {
  76. $comment_id = request()->comment_id;
  77. $type = request()->type;
  78. switch ($type) {
  79. case 'show':
  80. $column = 'is_show';
  81. break;
  82. case 'top':
  83. $column = 'is_top';
  84. break;
  85. default:
  86. $column = '';
  87. }
  88. if (!$column || !$comment_id) {
  89. return $this->errorJson('参数错误');
  90. }
  91. $commentModel = Comment::uniacid()->find($comment_id);
  92. $commentModel->$column = request()->$column;
  93. $commentModel->save();
  94. return $this->successJson('success');
  95. }
  96. //修改审核状态
  97. public function changeAuditStatus()
  98. {
  99. $comment_id = request()->comment_id;
  100. $commentModel = Comment::uniacid()->find($comment_id);
  101. if ($commentModel->audit_status != Comment::wait_audit) {
  102. return $this->errorJson('该评论状态不可审核');
  103. }
  104. $commentModel->audit_status = Comment::pass_audit;
  105. //追评审核
  106. if ($commentModel->type == 3) {
  107. $commentModel::updatedAdditionalCommentId($commentModel->comment_id,$comment_id);
  108. }
  109. $commentModel->save();
  110. \Log::info('后台通过评论审核',\YunShop::app()->uid);
  111. return $this->successJson('success');
  112. }
  113. public function commentData()
  114. {
  115. $pageSize = 10;
  116. $search = CommentService::Search(request()->search);
  117. if (isset(request()->type) && request()->type == 'audit') {
  118. $list = Comment::getComments(request()->search,'audit')->where('audit_status',Comment::wait_audit)->paginate($pageSize)->toArray();
  119. } else {
  120. $list = Comment::getComments(request()->search)->whereIn('audit_status',[Comment::not_audit,Comment::pass_audit])->paginate($pageSize)->toArray();
  121. }
  122. foreach ($list['data'] as &$item) {
  123. if (request()->type == 'audit' && $item['type'] == 3) {
  124. $item['level'] = 5;//追评默认好评
  125. }
  126. $item['head_img_url'] = yz_tomedia($item['head_img_url']);
  127. $item['goods']['thumb'] = yz_tomedia($item['goods']['thumb']);
  128. }
  129. $data = [
  130. 'list' => $list,
  131. 'total' => $list['total'],
  132. 'search' => $search,
  133. ];
  134. return $this->successJson('ok', $data);
  135. }
  136. public function editView()
  137. {
  138. return view('goods.comment.info', ['id' => request()->id, 'goods_id' => request()->goods_id, 'default_level' => \Setting::get('shop.member')['level_name']?:'普通会员','levels' => $this->getLevels()])->render();
  139. }
  140. /**
  141. * 添加评论
  142. */
  143. public function addComment()
  144. {
  145. $goods_id = request()->goods_id;
  146. $goods = [];
  147. if (!empty($goods_id)) {
  148. $goods = Goods::getGoodsById($goods_id);
  149. if (!$goods) {
  150. return $this->message('未找到此商品或该商品已被删除', Url::absoluteWeb('goods.comment.index'));
  151. }
  152. $goods = $goods->toArray();
  153. }
  154. $commentModel = new Comment();
  155. $commentModel->goods_id = $goods_id;
  156. $requestComment = request()->comment;
  157. if ($requestComment) {
  158. $requestComment['goods_id'] = $goods_id;
  159. $comment_time = time();
  160. if ( $requestComment['time_state'] && $requestComment['comment_time'] > 0){
  161. $comment_time = $requestComment['comment_time'] / 1000;
  162. }
  163. unset($requestComment['time_state']);
  164. unset($requestComment['comment_time']);
  165. if (!CommentConfig::isScoreLatitude()) {
  166. unset($requestComment['score_latitude']);
  167. } else {
  168. //insert方法不走模型,手动转换json
  169. $requestComment['score_latitude'] = json_encode($requestComment['score_latitude']);
  170. }
  171. //将数据赋值到model
  172. $commentModel->setRawAttributes($requestComment);
  173. //其他字段赋值
  174. $commentModel->uniacid = \YunShop::app()->uniacid;
  175. if (empty($commentModel->nick_name)) {
  176. $commentModel->nick_name = Member::getRandNickName()->nickname;
  177. }
  178. if (empty($commentModel->head_img_url)) {
  179. $commentModel->head_img_url = Member::getRandAvatar()->avatar;
  180. }
  181. $commentModel = CommentService::comment($commentModel);
  182. //字段检测
  183. $validator = $commentModel->validator($commentModel->getAttributes());
  184. if ($validator->fails()) {
  185. $this->errorJson($validator->messages());
  186. } else {
  187. $commentData = $commentModel->getAttributes();
  188. $commentData['created_at'] = $comment_time;
  189. $commentData['updated_at'] = $comment_time;
  190. //数据保存
  191. // if ($commentModel->save()) {
  192. if ($commentModel->insert($commentData)) {
  193. Goods::updatedComment($commentModel->goods_id);
  194. //显示信息并跳转
  195. return $this->successJson('评论创建成功');
  196. } else {
  197. $this->errorJson('评论创建失败');
  198. }
  199. }
  200. }
  201. $goods['thumb'] = yz_tomedia($goods['thumb']);
  202. $data = [
  203. 'comment' => $commentModel,
  204. 'goods' => $goods,
  205. 'is_score_latitude' => CommentConfig::isScoreLatitude()
  206. ];
  207. return $this->successJson('ok', $data);
  208. // return view('goods.comment.info', [
  209. // 'comment' => $commentModel,
  210. // 'goods' => $goods
  211. // ])->render();
  212. }
  213. public function searchGoodsV2()
  214. {
  215. $keyword = request()->keyword;
  216. $goods = Goods::select('id', 'title', 'thumb')
  217. ->where('title', 'like', '%' . $keyword . '%')
  218. ->where('status', 1)
  219. ->get();
  220. if (!$goods->isEmpty()) {
  221. $goods = set_medias($goods->toArray(), array('thumb', 'share_icon'));
  222. }
  223. $data = [
  224. 'goods' => $goods,
  225. 'exchange' => request()->exchange,
  226. ];
  227. return $this->successJson('ok', $data);
  228. // return view('goods.query', [
  229. // 'goods' => $goods,
  230. // 'exchange' => \YunShop::request()->exchange,
  231. // ])->render();
  232. }
  233. public function searchGoods()
  234. {
  235. $keyword = \YunShop::request()->keyword;
  236. $goods = Goods::select('id', 'title', 'thumb')
  237. ->where('title', 'like', '%' . $keyword . '%')
  238. ->where('status', 1)
  239. ->get();
  240. if (!$goods->isEmpty()) {
  241. $goods = set_medias($goods->toArray(), array('thumb', 'share_icon'));
  242. }
  243. return view('goods.query', [
  244. 'goods' => $goods,
  245. 'exchange' => \YunShop::request()->exchange,
  246. ])->render();
  247. }
  248. /**
  249. * 修改评论
  250. */
  251. public function updated()
  252. {
  253. $id = request()->id;
  254. $commentModel = Comment::getComment($id)->first();
  255. if (!$commentModel) {
  256. return $this->errorJson('无此记录或已被删除');
  257. }
  258. $requestComment = request()->comment;
  259. if ($requestComment) {
  260. $goods_id = $commentModel->goods_id;
  261. if (!empty(request()->goods_id) && request()->goods_id != $goods_id){
  262. if (!$goods = Goods::getGoodsById(request()->goods_id)) return $this->errorJson('选择的商品不存在或已删除');
  263. $goods_id = $goods->id;
  264. }
  265. $comment_time = 0;
  266. if ($requestComment['time_state'] && $requestComment['comment_time'] > 0){
  267. $comment_time = $requestComment['comment_time'] / 1000;
  268. }
  269. unset($requestComment['time_state']);
  270. unset($requestComment['comment_time']);
  271. if (!CommentConfig::isScoreLatitude()) {
  272. unset($requestComment['score_latitude']);
  273. } else {
  274. //insert方法不走模型,手动转换json
  275. $requestComment['score_latitude'] = json_encode($requestComment['score_latitude']);
  276. }
  277. //将数据赋值到model
  278. $commentModel->setRawAttributes($requestComment);
  279. if (empty($commentModel->nick_name)) {
  280. $commentModel->nick_name = Member::getRandNickName()->nick_name;
  281. }
  282. if (empty($commentModel->head_img_url)) {
  283. $commentModel->head_img_url = Member::getRandAvatar()->avatar;
  284. }
  285. $commentModel->images = isset($commentModel->images) && is_array($commentModel->images) ? serialize($commentModel->images) : serialize([]);
  286. if ($comment_time) $commentModel->created_at = $comment_time;
  287. $commentModel->goods_id = $goods_id;
  288. //字段检测
  289. $validator = $commentModel->validator($commentModel->getAttributes());
  290. if ($validator->fails()) {
  291. $this->errorJson($validator->messages());
  292. } else {
  293. //数据保存
  294. if ($commentModel->save()) {
  295. //显示信息并跳转
  296. return $this->successJson('评论保存成功');
  297. } else {
  298. $this->errorJson('评论保存失败');
  299. }
  300. }
  301. }
  302. $commentModel['head_img_url_url'] = yz_tomedia($commentModel['head_img_url']);
  303. $commentModel['images'] = unserialize($commentModel['images']);
  304. $imgs = $commentModel['images'];
  305. foreach ($imgs as &$item) {
  306. $item = yz_tomedia($item);
  307. }
  308. $commentModel['images_url'] = $imgs;
  309. $commentModel['comment_time'] = is_numeric($commentModel['created_at']) ? : strtotime($commentModel['created_at']);
  310. $goods = Goods::getGoodsById($commentModel->goods_id);
  311. $goods['thumb'] = yz_tomedia($goods['thumb']);
  312. $data = [
  313. 'id' => $id,
  314. 'comment' => $commentModel,
  315. 'goods' => $goods,
  316. 'score_latitude' => $commentModel['score_latitude'],
  317. 'is_score_latitude' => CommentConfig::isScoreLatitude()
  318. ];
  319. return $this->successJson('ok', $data);
  320. // return view('goods.comment.info', [
  321. // 'id' => $id,
  322. // 'comment' => $commentModel,
  323. // 'goods' => $goods
  324. // ])->render();
  325. }
  326. public function replyView()
  327. {
  328. return view('goods.comment.reply', ['id' => request()->id,'page_type' => request()->page_type])->render();
  329. }
  330. /**
  331. * 评论回复
  332. */
  333. public function reply()
  334. {
  335. $id = intval(request()->id);
  336. $commentModel = Comment::getComment($id)->first();
  337. if (!$commentModel) {
  338. return $this->errorJson('无此记录或已被删除');
  339. }
  340. if (request()->reply) {
  341. return $this->createReply();
  342. }
  343. $commentModel = $commentModel->toArray();
  344. $goods = Goods::getGoodsById($commentModel['goods_id']);
  345. $commentModel['images'] = unserialize($commentModel['images']);
  346. foreach ($commentModel['images'] as $key=>$item) {
  347. $commentModel['images'][$key] = yz_tomedia($item);
  348. }
  349. foreach ($commentModel['has_many_reply'] as &$item) {
  350. $item['images'] = unserialize($item['images']);
  351. foreach ($item['images'] as &$it) {
  352. $it = yz_tomedia($it);
  353. }
  354. }
  355. $goods['thumb'] = yz_tomedia($goods['thumb']);
  356. $after_content = Comment::getAfterContent($commentModel['id']);
  357. if ($after_content) {
  358. $after_content = $after_content->toArray();
  359. $after_content['images'] = unserialize($after_content['images']);
  360. foreach ($after_content['images'] as &$image) {
  361. $image = yz_tomedia($image);
  362. }
  363. }
  364. if ($commentModel['type'] == 3) {
  365. $commentModel['level'] = 5;//追评默认好评
  366. }
  367. $data = [
  368. 'comment' => $commentModel,
  369. 'goods' => $goods,
  370. 'page_type' => request()->page_type,
  371. 'after_content' => $after_content,
  372. 'score_latitude' => $commentModel['score_latitude'],
  373. ];
  374. if(!is_null($comment_detail_arr = \app\common\modules\shop\ShopConfig::current()->get('comment_detail_data'))){
  375. foreach ($comment_detail_arr as $v){
  376. $class = array_get($v, 'class');
  377. $function = array_get($v, 'function');
  378. if ($other_data = $class::$function($commentModel)){
  379. $data[$other_data['key']] = $other_data;
  380. }
  381. }
  382. }
  383. return $this->successJson('ok', $data);
  384. // return view('goods.comment.reply', [
  385. // 'comment' => $commentModel,
  386. // 'goods' => $goods
  387. // ])->render();
  388. }
  389. public function createReply()
  390. {
  391. $id = intval(request()->id);
  392. $commentModel = new Comment;
  393. $requestReply = request()->reply;
  394. if ($requestReply) {
  395. //主评论状态修改
  396. $commentStatusModel = Comment::uniacid()->find($id);
  397. $commentStatusModel->is_show = request()->is_show;
  398. $commentStatusModel->is_top = request()->is_top;
  399. $commentStatusModel->save();
  400. //内容为空
  401. if (empty($requestReply['reply_content'])) {
  402. return $this->successJson('修改状态成功');
  403. }
  404. $member = Member::getMemberById($requestReply['reply_id']);
  405. $requestReply = CommentService::reply($requestReply, $member);
  406. //将数据赋值到model
  407. $commentModel->setRawAttributes($requestReply);
  408. $validator = $commentModel->validator($commentModel->getAttributes());
  409. //字段检测
  410. if ($validator->fails()) {
  411. return $this->errorJson($validator->messages());
  412. } else {
  413. //数据保存
  414. if (Comment::saveComment($commentModel->getAttributes())) {
  415. //显示信息并跳转
  416. // return $this->message('评论回复保存成功', Url::absoluteWeb('goods.comment.reply', ['id' => $id]));
  417. return $this->successJson('评论回复保存成功');
  418. } else {
  419. return $this->errorJson('评论回复保存失败');
  420. }
  421. }
  422. }
  423. }
  424. /**
  425. * 删除评论
  426. */
  427. public function deleted()
  428. {
  429. $comment = Comment::getComment(request()->id);
  430. if (!$comment) {
  431. return $this->errorJson('无此评论或已经删除');
  432. }
  433. $result = Comment::daletedComment(request()->id);
  434. if ($result) {
  435. return $this->successJson('删除评论成功');
  436. } else {
  437. return $this->errorJson('删除评论失败');
  438. }
  439. }
  440. private function getLevels()
  441. {
  442. $levels = MemberLevel::uniacid()
  443. ->select('id', 'level', 'level_name')
  444. ->get();
  445. return $levels;
  446. }
  447. }