Comment.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. /**
  5. * 商品评价模型
  6. * Class Comment
  7. * @package app\common\model
  8. */
  9. class Comment extends BaseModel
  10. {
  11. protected $name = 'comment';
  12. /**
  13. * 所属订单
  14. * @return \think\model\relation\BelongsTo
  15. */
  16. public function orderM()
  17. {
  18. return $this->belongsTo('Order');
  19. }
  20. /**
  21. * 订单商品
  22. * @return \think\model\relation\BelongsTo
  23. */
  24. public function OrderGoods()
  25. {
  26. return $this->belongsTo('OrderGoods');
  27. }
  28. /**
  29. * 关联用户表
  30. * @return \think\model\relation\BelongsTo
  31. */
  32. public function user()
  33. {
  34. return $this->belongsTo('User');
  35. }
  36. /**
  37. * 关联评价图片表
  38. * @return \think\model\relation\HasMany
  39. */
  40. public function image()
  41. {
  42. return $this->hasMany('CommentImage')->order(['id' => 'asc']);
  43. }
  44. /**
  45. * 评价详情
  46. * @param $comment_id
  47. * @return Comment|null
  48. * @throws \think\exception\DbException
  49. */
  50. public static function detail($comment_id)
  51. {
  52. return self::get($comment_id, ['user', 'orderM', 'OrderGoods', 'image.file']);
  53. }
  54. /**
  55. * 更新记录
  56. * @param $data
  57. * @return bool
  58. */
  59. public function edit($data)
  60. {
  61. return $this->transaction(function () use ($data) {
  62. // 删除评价图片
  63. $this->image()->delete();
  64. // 添加评论图片
  65. isset($data['images']) && $this->addCommentImages($data['images']);
  66. // 是否为图片评价
  67. $data['is_picture'] = !$this->image()->select()->isEmpty();
  68. // 更新评论记录
  69. return $this->allowField(true)->save($data);
  70. });
  71. }
  72. /**
  73. * 添加评论图片
  74. * @param $images
  75. * @return int
  76. */
  77. private function addCommentImages($images)
  78. {
  79. $data = array_map(function ($image_id) {
  80. return [
  81. 'image_id' => $image_id,
  82. 'wxapp_id' => self::$wxapp_id
  83. ];
  84. }, $images);
  85. return $this->image()->saveAll($data);
  86. }
  87. /**
  88. * 获取评价列表
  89. * @return \think\Paginator
  90. * @throws \think\exception\DbException
  91. */
  92. public function getList()
  93. {
  94. return $this->with(['user', 'orderM', 'OrderGoods'])
  95. ->where('is_delete', '=', 0)
  96. ->order(['sort' => 'asc', 'create_time' => 'desc'])
  97. ->paginate(15, false, [
  98. 'query' => request()->request()
  99. ]);
  100. }
  101. }