GoodsController.php
3.4 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
<?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']==1){
$integral='购买可获得'.$item['price']*2 .'积分';
}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')->field('thumb,name,intro,price,content')->where(['id' => $id])->find();
$total = Db::name('zj_cart')->field('sum(num) as total')->where(['uid' => session('user.id')])->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])->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();
}
}