GoodsComment.php
3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
namespace addons\shopro\model;
use think\Model;
use traits\model\SoftDelete;
/**
* 商品评价
*/
class GoodsComment extends Model
{
use SoftDelete;
// 表名,不含前缀
protected $name = 'shopro_goods_comment';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
// 追加属性
protected $append = [
];
public static $typeAll = [
'all' => ['code' => 'all', 'name' => '全部'],
'images' => ['code' => 'images', 'name' => '有图'],
'good' => ['code' => 'good', 'name' => '好评'],
'moderate' => ['code' => 'moderate', 'name' => '中评'],
'bad' => ['code' => 'bad', 'name' => '差评'],
];
public function scopeImages($query) {
return $query->whereNotNull('images')->where('images', '<>', '');
}
public function scopeGood($query) {
return $query->where('level', 'in', [5, 4]);
}
public function scopeModerate($query) {
return $query->where('level', 'in', [3, 2]);
}
public function scopeBad($query) {
return $query->where('level', 1);
}
public static function getList ($params) {
extract($params);
$per_page = $per_page ?? 10;
$goodsComments = self::with(['user' => function ($query) {
$query->field('id, nickname, avatar');
}])->where('goods_id', $goods_id);
if ($type != 'all' && isset(self::$typeAll[$type])) {
$goodsComments = $goodsComments->{$type}();
}
$goodsComments = $goodsComments
->paginate($per_page)
->each(function($v){
$v->video = $v->video ? cdnurl($v->video,true) : ''; //格式化视频地址
$v->createtime = date('Y-m-d',$v->createtime); //格式化评价时间
});
return $goodsComments;
}
public static function getMyList ($params) {
$user = User::info();
extract($params);
$per_page = $per_page ?? 10;
$goodsComments = self::with(['user' => function ($query) {
$query->field('id, nickname, avatar');
}])->where('user_id', $user->id);
$goodsComments = $goodsComments
->paginate($per_page)
->each(function($v){
$v->video = $v->video ? cdnurl($v->video,true) : ''; //格式化视频地址
$v->createtime = date('Y-m-d',$v->createtime); //格式化评价时间
});
return $goodsComments;
}
public function getImagesAttr($value, $data)
{
$imagesArray = [];
if (!empty($value)) {
$imagesArray = explode(',', $value);
foreach ($imagesArray as &$v) {
$v = cdnurl($v, true);
}
return $imagesArray;
}
return $imagesArray;
}
public function user()
{
return $this->belongsTo(\addons\shopro\model\User::class, 'user_id', 'id');
}
}