Comment.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace app\store\model\sharing;
  3. use app\common\model\sharing\Comment as CommentModel;
  4. /**
  5. * 商品评价模型
  6. * Class Comment
  7. * @package app\store\model\sharing
  8. */
  9. class Comment extends CommentModel
  10. {
  11. /**
  12. * 软删除
  13. * @return false|int
  14. */
  15. public function setDelete()
  16. {
  17. return $this->save(['is_delete' => 1]);
  18. }
  19. /**
  20. * 获取评价总数量
  21. * @return int|string
  22. */
  23. public function getCommentTotal()
  24. {
  25. return $this->where(['is_delete' => 0])->count();
  26. }
  27. /**
  28. * 更新记录
  29. * @param $data
  30. * @return bool
  31. * @throws \think\exception\PDOException
  32. */
  33. public function edit($data)
  34. {
  35. // 开启事务
  36. $this->startTrans();
  37. try {
  38. // 删除评价图片
  39. $this->image()->delete();
  40. // 添加评论图片
  41. isset($data['images']) && $this->addCommentImages($data['images']);
  42. // 是否为图片评价
  43. $data['is_picture'] = !$this->image()->select()->isEmpty();
  44. // 更新评论记录
  45. $this->allowField(true)->save($data);
  46. $this->commit();
  47. return true;
  48. } catch (\Exception $e) {
  49. $this->error = $e->getMessage();
  50. $this->rollback();
  51. return false;
  52. }
  53. }
  54. /**
  55. * 添加评论图片
  56. * @param $images
  57. * @return int
  58. */
  59. private function addCommentImages($images)
  60. {
  61. $data = array_map(function ($image_id) {
  62. return [
  63. 'image_id' => $image_id,
  64. 'wxapp_id' => self::$wxapp_id
  65. ];
  66. }, $images);
  67. return $this->image()->saveAll($data);
  68. }
  69. /**
  70. * 获取评价列表
  71. * @return \think\Paginator
  72. * @throws \think\exception\DbException
  73. */
  74. public function getList()
  75. {
  76. return $this->with(['user', 'orderM', 'OrderGoods'])
  77. ->where('is_delete', '=', 0)
  78. ->order(['sort' => 'asc', 'create_time' => 'desc'])
  79. ->paginate(15, false, [
  80. 'query' => request()->request()
  81. ]);
  82. }
  83. }