Comment.php 2.2 KB

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