GoodsController.php
4.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
117
<?php
namespace app\goods\controller;
use app\goods\model\GoodsModel;
use cmf\controller\HomeBaseController;
use think\Db;
class GoodsController extends HomeBaseController
{
public function goodsList()
{
$request = request();
$cid = $request->param('cid');
$category = [];
if (!$request->isAjax()) {
$category = $this->getCategory($cid);
$cid = $category[0]['id'];
}
$goods = new GoodsModel;
$goodsList = $goods->getGoods(null, $cid);
if ($request->isAjax()) {
$html = '';
if (!empty($goodsList)) {
foreach ($goodsList as $item) {
if ($item['caid'] == 2) {
$integral = '购买可获得' . $item['price'] * 1.5 . '积分';
} else {
$integral = '该商品无积分奖励';
}
$html .= '
<a href="' . url('goods/Goods/detail', ['id' => $item['id']]) . '">
<div class="container_list">
<div class="text_img">
<img src="' . $item['thumb'] . '" alt="">
</div>
<div class="container_info">
<p class="info_name">
' . $item['name'] . '
</p>
<p class="info_num">
' . $item['intro'] . '
</p>
<div class="people">
<p class="people_weight">
' . $integral . '
</p>
<p class="people_money">¥<span class="money">' . $item['price'] . '</span></p>
</div>
</div>
</div>
</a>
';
}
} else {
$html = '<div style="width: 100%; text-align: center; font-size: small">暂无商品</div>';
}
$this->success('', '', $html);
} else {
return $this->fetch(':list', [
'category' => $category,
'goodsList' => $goodsList
]);
}
}
public function detail()
{
$id = request()->param('id');
$data = Db::name('zj_goods')->alias('g')
->field('thumb,g.name,g.intro,g.price,g.content,g.more,c.cid as caid')
->join('zj_category c', 'c.id=g.cid')
->where(['g.id' => $id])->find();
$data['more'] = json_decode($data['more'], true);
foreach ($data['more'] as $k => $v) {
$data['photo'][$k]['photo'] = cmf_get_image_url($v['url']);
}
if ($data['caid'] == 2) {
$data['integral'] = '购买可获得' . $data['price'] * 1.5 . '积分';
} else {
$data['integral'] = '该商品无积分奖励';
}
$data['content'] = html_entity_decode($data['content']);
$total = Db::name('zj_cart')->alias('c')
->field('sum(num) as total')
->join('zj_goods g', 'c.gid=g.id')
->where(['uid' => session('user.id'), 'is_sta' => 1])
->select();
if ($total[0]['total'] == '') {
$total = 0;
} else {
$total = $total[0]['total'];
}
return $this->fetch(':detail', [
'total' => $total,
'data' => $data,
'id' => $id,
'comment' => $this->getGoodsComment($id)
]);
}
private function getCategory($cid)
{
return Db::name('zj_category')->field('id,name')->where(['cid' => $cid, 'delete_time' => ['eq', 0]])->select()->toArray();
}
public function getGoodsComment($gid)
{
return Db::name('zj_evaluate')->alias('e')
->field('u.user_nickname,avatar,e.content,e.create_time')
->join('user u', 'u.id=e.uid')
->where(['gid' => $gid])
->order('create_time DESC')
->select()
->toArray();
}
}