Item.php
9.95 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
use Action\WebAction;
use Product\ItemModel;
use Index\HomeModel;
use WebPlugin\Helpers;
use LibModels\Web\Product\FavoriteData;
class ItemController extends WebAction
{
public function indexAction()
{
$this->setWebNavHeader();
$productId = $this->param('productId','');
if (!is_numeric($productId)) {
$this->error();
}
$goodsId = $this->param('goodsId','');
if (!is_numeric($goodsId)) {
$this->error();
}
$uid = $this->getUid();
$vipLevel = -1;
if(!empty($this->_vip)) {
$vipLevel = Helpers::getVipLevel($this->_vip);
}
//获取商品信息
$productInfo = ItemModel::getProductInfo($productId, $goodsId, $uid, $vipLevel);
if(empty($productInfo)) {
$this->error();
}
$sortNames = array();
$navs = ItemModel::getSortNav($productInfo['goodsInfo']['smallSortId']);
foreach($navs as $nav) {
$sortNames[] = $nav['name'];
}
$seo = $this->getSeoByGoodsInfo($productInfo['goodsInfo'], $navs);
$this->setTitle($seo['title'], true, '|');
$this->setKeywords($seo['keywords']);
$this->setDescription($seo['description'], true);
$this->saveCookie($goodsId, $productInfo['goodsInfo']['skn']);
//获取商品尺寸相关
$sizeInfo = ItemModel::getSizeInfo($productInfo['goodsInfo']['skn'], $productInfo['goodsInfo']['maxSortId']);
$data = array('productDetailPage' => true,
'detail'=> array(),
'statGoodsInfo'=> array_merge(
array('fullSortName'=> implode('-', $sortNames)),
$productInfo['statGoodsInfo']),
);
//导航
$data['detail']['pathNav'] = array_merge(
array(HomeModel::getHomeChannelNav()),
$navs,
array(array('name' => $productInfo['goodsInfo']['name']))
);
$data['detail']['latestWalk'] = 5;
$data['detail'] += $productInfo + $sizeInfo;
$this->_view->display('index', $data);
}
/**
* 显示商品信息
*
* @param int productId 商品ID
* @param int productSkn 商品SKN
*/
public function showAction()
{
$this->setWebNavHeader();
$productSkn = $this->param('productSkn','');
if (!is_numeric($productSkn)) {
$this->error();
}
$sortNames = array();
$uid = $this->getUid();
$vipLevel = -1;
if(!empty($this->_vip)) {
$vipLevel = Helpers::getVipLevel($this->_vip);
}
//获取商品信息
$productInfo = ItemModel::getProductInfo(null, null, $uid, $vipLevel, $productSkn);
if(empty($productInfo)) {
$this->error();
}
$navs = ItemModel::getSortNav($productInfo['goodsInfo']['smallSortId']);
foreach($navs as $nav) {
$sortNames[] = $nav['name'];
}
$seo = $this->getSeoByGoodsInfo($productInfo['goodsInfo'], $navs);
$this->setTitle($seo['title'], true, '|');
$this->setKeywords($seo['keywords']);
$this->setDescription($seo['description'], true);
//获取商品尺寸相关
$sizeInfo = ItemModel::getSizeInfo($productInfo['goodsInfo']['skn'], $productInfo['goodsInfo']['maxSortId']);
$data = array('productDetailPage' => true,
'detail'=> array(),
'statGoodsInfo'=> array_merge(array('fullSortName'=> implode('-', $sortNames)),
$productInfo['statGoodsInfo']),
);
//导航
$data['detail']['pathNav'] = array_merge(
array(HomeModel::getHomeChannelNav()),
$navs,
array(array('name' => $productInfo['goodsInfo']['name']))
);
$data['detail']['latestWalk'] = 5;
$data['detail'] += $productInfo + $sizeInfo;
$data['uid'] = $uid;
$data['productId'] = $productInfo['goodsInfo']['productId'];
$this->_view->display('index', $data);
}
/**
* 收藏商品
*
* return jsonp
*/
public function togglecollectAction()
{
$uid = $this->getUid();
$productId = $this->post('productId');
$type = $this->post('type', 'add');//add 添加,canncel取消
$result = array('code'=> 400 ,'message' =>'收藏失败');
if(!empty($uid) && is_numeric($productId)) {
if($type == 'add') {
$result = FavoriteData::addUidProductFav($uid, $productId);
if($result['code'] == 413) {
$result['message'] = '该商品已经收藏';
}
} else if($type == 'cancel') {
$result = FavoriteData::delUidProductFav($uid, $productId);
}
} else if(empty($uid)) {
$result['code'] = 403;
$result['message'] = '用户没有登录';
$result['data'] = array('url'=> Helpers::url('/signin.html', array()));
}
$this->echoJson($result);
}
/**
* 获取评论
*/
public function commentsAction()
{
$productId = $this->get('productId', 0);
$page = $this->get('page', 1);
$size = $this->get('size', 10);
$data = ItemModel::getComments($productId, $page, $size);
$this->echoJson(
array(
'code' => 200,
'data' => $data
)
);
}
/**
* 获取咨询列表
*/
public function consultsAction()
{
$uid = $this->getUid();
$productId = $this->get('productId', 0);
$page = $this->get('page', 1);
$size = $this->get('size', 10);
$data = ItemModel::getConsults($uid, $productId, $page, $size);
$this->echoJson(
array(
'code' => 200,
'data' => $data
)
);
}
/**
* 获取热区图
*/
public function hotareaAction()
{
$productId = $this->get('productId', 0);
$data = ItemModel::getProductHotArea($productId);
$this->_view->display('hotarea', array('hotArea'=> $data));
}
/**
* 添加咨询操作
*
* @return json
*/
public function addconsultAction()
{
$result = array('code' => 400, 'message' => '请输入咨询内容', 'data' => '');
if ($this->isAjax()) {
$uid = $this->getUid();
$productId = $this->post('productId', 0);//商品ID
$content = $this->post('content');//咨询内容
if(!empty($content) && !empty($uid)){
$result = ItemModel::addConsult($uid, $productId, $content);
} else if(empty($content)) {
$result['code'] = 400;
$result['message'] = '请输入咨询内容';
}
else if(empty($uid)) {
$result['code'] = 403;
$result['message'] = '用户没有登录';
$result['data'] = array('url'=> Helpers::url('/signin.html', array()));
}
}
$this->echoJson($result);
}
/**
* 保存商品ID
*
* @param string $productId
* @param string $skn
* @param string $goodsId
*/
private function saveCookie($goodsId, $skn)
{
$browseList = $this->getCookie('_browse');
$browseSknList = $this->getCookie('_browseskn');
$goodsList = array();
$goodsSknList = array();
//有一个空,都清除
if(empty($browseList) || empty($browseSknList)) {
$browseList = '';
$browseSknList = '';
}
if (!empty($browseList)) {
$goodsList = explode(',', $browseList);
}
if(!empty($browseSknList)) {
$goodsSknList = explode(',', $browseSknList);
}
//处理存在
if(in_array($goodsId, $goodsList) || in_array($skn . '-' . $goodsId, $goodsSknList)) {
$key = array_search($goodsId, $goodsList);
if($key !== false) {
unset($goodsList[$key]);
}
$key = array_search($skn . '-' . $goodsId, $goodsSknList);
if($key !== false) {
unset($goodsSknList[$key]);
}
}
array_unshift($goodsList, $goodsId);
array_unshift($goodsSknList, $skn . '-' . $goodsId);
$browse = implode(',', array_slice($goodsList, 0, 30));
$browseSkn = implode(',', array_slice($goodsSknList, 0, 30));
$this->setCookie('_browse', $browse, 2000000000);
$this->setCookie('_browseskn', $browseSkn, 2000000000);
}
/**
* 获取seo信息
*
* @param array $goodsInfo
* @param array $navs
* @return array
*/
private function getSeoByGoodsInfo(&$goodsInfo, &$navs)
{
$title = '';
$keywords = '';
$brandName = '';
$sortName = '';
if(!empty($goodsInfo['brandName'])) {
$title = $goodsInfo['brandName'] .' ';
$brandName = $goodsInfo['brandName'];
}
if(!empty($navs) && isset($navs[1]['name'])) {
$sortName = $navs[1]['name'];
$title.= $navs[1]['name'].'|';
}
$title .= $goodsInfo['name'].'正品 ';
$keywords = $brandName.$sortName.','.$brandName.'官网专卖店,'. $brandName.'官方授权店,'.
$brandName.'正品,'.$brandName.'打折,'.$brandName.'折扣店,'.$brandName.'真品,'.$brandName.'代购';
$description = empty($goodsInfo['shareDesc']) ? $goodsInfo['name'] : $goodsInfo['shareDesc'];
return array('title' => $title, 'keywords'=> $keywords, 'description' =>$description);
}
}