Comment.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\common\model;
  3. use app\common\library\Service;
  4. use app\common\exception\CommentException;
  5. use app\common\library\Auth;
  6. use fast\Tree;
  7. use think\Exception;
  8. use think\Model;
  9. use think\Validate;
  10. /**
  11. * 模型
  12. */
  13. class Comment extends Model
  14. {
  15. // 表名
  16. protected $name = 'shop_comment';
  17. // 开启自动写入时间戳字段
  18. protected $autoWriteTimestamp = 'int';
  19. // 定义时间戳字段名
  20. protected $createTime = 'createtime';
  21. protected $updateTime = 'updatetime';
  22. // 追加属性
  23. protected $append = [];
  24. public function getImagesAttr($value, $data)
  25. {
  26. $imagesArr = explode(',', $data['images'] ?? '');
  27. foreach ($imagesArr as $index => &$item) {
  28. $item && $item = cdnurl($item, true);
  29. }
  30. return array_filter($imagesArr);
  31. }
  32. public function setImagesAttr($value, $data)
  33. {
  34. return is_array($value) ? implode(',', $value) : $value;
  35. }
  36. public function User()
  37. {
  38. return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT');
  39. }
  40. public function Manage()
  41. {
  42. return $this->belongsTo('\app\admin\model\Admin', 'user_id', 'id', [], 'LEFT');
  43. }
  44. public function Goods()
  45. {
  46. return $this->belongsTo('Goods', 'goods_id', 'id', [], 'LEFT');
  47. }
  48. /**
  49. * 获取评论列表
  50. */
  51. public static function getCommentList($params)
  52. {
  53. $goods_id = empty($params['goods_id']) ? 0 : $params['goods_id'];
  54. $pid = empty($params['pid']) ? 0 : $params['pid'];
  55. $condition = empty($params['condition']) ? '' : $params['condition'];
  56. $fragment = empty($params['fragment']) ? 'comments' : $params['fragment'];
  57. $row = empty($params['row']) ? 10 : (int)$params['row'];
  58. $orderby = empty($params['orderby']) ? 'nums' : $params['orderby'];
  59. $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']);
  60. $pagesize = empty($params['pagesize']) ? $row : $params['pagesize'];
  61. $page = empty($params['page']) ? 1 : (int)$params['page'];
  62. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  63. $where = [
  64. 'status' => 'normal'
  65. ];
  66. if ($goods_id !== '') {
  67. $where['goods_id'] = $goods_id;
  68. }
  69. if ($pid !== '') {
  70. $where['pid'] = $pid;
  71. }
  72. $order = $orderby == 'rand' ? 'rand()' : (in_array($orderby, ['pid', 'id', 'createtime', 'updatetime']) ? "{$orderby} {$orderway}" : "id {$orderway}");
  73. $config = [
  74. 'type' => '\\addons\\shop\\library\\Bootstrap',
  75. 'var_page' => 'cp',
  76. 'fragment' => $fragment,
  77. 'page' => $page
  78. ];
  79. $list = self::with(['user', 'reply' => function ($query) {
  80. $query->with(['manage' => function ($user) {
  81. $user->field('id,nickname');
  82. }]);
  83. }])
  84. ->where($where)
  85. ->where($condition)
  86. ->order($order)
  87. ->paginate($pagesize, false, $config);
  88. return $list;
  89. }
  90. //好评度
  91. public static function degree($goods_id)
  92. {
  93. $total = self::where('goods_id', $goods_id)->where('pid', 0)->where('status', 'normal')->sum('star');
  94. $favorable = self::where('goods_id', $goods_id)->where('pid', 0)->where('status', 'normal')->where('star', '>', 3)->sum('star');
  95. if (!$total || !$favorable) {
  96. return 100;
  97. }
  98. return bcmul(bcdiv($favorable, $total, 2), 100);
  99. }
  100. public function reply()
  101. {
  102. return $this->hasMany('Comment', 'pid', 'id')->where('status', 'normal');
  103. }
  104. }