-
Notifications
You must be signed in to change notification settings - Fork 5
/
group_buy.php
375 lines (323 loc) · 13 KB
/
group_buy.php
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
<?php
/**
* ECSHOP 团购商品前台文件
* ============================================================================
* * 版权所有 2005-2012 上海商派网络科技有限公司,并保留所有权利。
* 网站地址: http://www.ecshop.com;
* ----------------------------------------------------------------------------
* 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和
* 使用;不允许对程序代码以任何形式任何目的的再发布。
* ============================================================================
* $Author: liubo $
* $Id: group_buy.php 17217 2011-01-19 06:29:08Z liubo $
*/
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
if ((DEBUG_MODE & 2) != 2)
{
$smarty->caching = true;
}
/*------------------------------------------------------ */
//-- act 操作项的初始化
/*------------------------------------------------------ */
if (empty($_REQUEST['act']))
{
$_REQUEST['act'] = 'list';
}
/*------------------------------------------------------ */
//-- 团购商品 --> 团购活动商品列表
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
/* 取得团购活动总数 */
$count = group_buy_count();
if ($count > 0)
{
/* 取得每页记录数 */
$size = isset($_CFG['page_size']) && intval($_CFG['page_size']) > 0 ? intval($_CFG['page_size']) : 10;
/* 计算总页数 */
$page_count = ceil($count / $size);
/* 取得当前页 */
$page = isset($_REQUEST['page']) && intval($_REQUEST['page']) > 0 ? intval($_REQUEST['page']) : 1;
$page = $page > $page_count ? $page_count : $page;
/* 缓存id:语言 - 每页记录数 - 当前页 */
$cache_id = $_CFG['lang'] . '-' . $size . '-' . $page;
$cache_id = sprintf('%X', crc32($cache_id));
}
else
{
/* 缓存id:语言 */
$cache_id = $_CFG['lang'];
$cache_id = sprintf('%X', crc32($cache_id));
}
/* 如果没有缓存,生成缓存 */
if (!$smarty->is_cached('group_buy_list.dwt', $cache_id))
{
if ($count > 0)
{
/* 取得当前页的团购活动 */
$gb_list = group_buy_list($size, $page);
$smarty->assign('gb_list', $gb_list);
/* 设置分页链接 */
$pager = get_pager('group_buy.php', array('act' => 'list'), $count, $page, $size);
$smarty->assign('pager', $pager);
}
/* 模板赋值 */
$smarty->assign('cfg', $_CFG);
assign_template();
$position = assign_ur_here();
$smarty->assign('page_title', $position['title']); // 页面标题
$smarty->assign('ur_here', $position['ur_here']); // 当前位置
$smarty->assign('categories', get_categories_tree()); // 分类树
$smarty->assign('helps', get_shop_help()); // 网店帮助
$smarty->assign('top_goods', get_top10()); // 销售排行
$smarty->assign('promotion_info', get_promotion_info());
$smarty->assign('feed_url', ($_CFG['rewrite'] == 1) ? "feed-typegroup_buy.xml" : 'feed.php?type=group_buy'); // RSS URL
assign_dynamic('group_buy_list');
}
/* 显示模板 */
$smarty->display('group_buy_list.dwt', $cache_id);
}
/*------------------------------------------------------ */
//-- 团购商品 --> 商品详情
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'view')
{
/* 取得参数:团购活动id */
$group_buy_id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
if ($group_buy_id <= 0)
{
ecs_header("Location: ./\n");
exit;
}
/* 取得团购活动信息 */
$group_buy = group_buy_info($group_buy_id);
if (empty($group_buy))
{
ecs_header("Location: ./\n");
exit;
}
// elseif ($group_buy['is_on_sale'] == 0 || $group_buy['is_alone_sale'] == 0)
// {
// header("Location: ./\n");
// exit;
// }
/* 缓存id:语言,团购活动id,状态,(如果是进行中)当前数量和是否登录 */
$cache_id = $_CFG['lang'] . '-' . $group_buy_id . '-' . $group_buy['status'];
if ($group_buy['status'] == GBS_UNDER_WAY)
{
$cache_id = $cache_id . '-' . $group_buy['valid_goods'] . '-' . intval($_SESSION['user_id'] > 0);
}
$cache_id = sprintf('%X', crc32($cache_id));
/* 如果没有缓存,生成缓存 */
if (!$smarty->is_cached('group_buy_goods.dwt', $cache_id))
{
$group_buy['gmt_end_date'] = $group_buy['end_date'];
$smarty->assign('group_buy', $group_buy);
/* 取得团购商品信息 */
$goods_id = $group_buy['goods_id'];
$goods = goods_info($goods_id);
if (empty($goods))
{
ecs_header("Location: ./\n");
exit;
}
$goods['url'] = build_uri('goods', array('gid' => $goods_id), $goods['goods_name']);
$smarty->assign('gb_goods', $goods);
/* 取得商品的规格 */
$properties = get_goods_properties($goods_id);
$smarty->assign('specification', $properties['spe']); // 商品规格
//模板赋值
$smarty->assign('cfg', $_CFG);
assign_template();
$position = assign_ur_here(0, $goods['goods_name']);
$smarty->assign('page_title', $position['title']); // 页面标题
$smarty->assign('ur_here', $position['ur_here']); // 当前位置
$smarty->assign('categories', get_categories_tree()); // 分类树
$smarty->assign('helps', get_shop_help()); // 网店帮助
$smarty->assign('top_goods', get_top10()); // 销售排行
$smarty->assign('promotion_info', get_promotion_info());
assign_dynamic('group_buy_goods');
}
//更新商品点击次数
$sql = 'UPDATE ' . $ecs->table('goods') . ' SET click_count = click_count + 1 '.
"WHERE goods_id = '" . $group_buy['goods_id'] . "'";
$db->query($sql);
$smarty->assign('now_time', gmtime()); // 当前系统时间
$smarty->display('group_buy_goods.dwt', $cache_id);
}
/*------------------------------------------------------ */
//-- 团购商品 --> 购买
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'buy')
{
/* 查询:判断是否登录 */
if ($_SESSION['user_id'] <= 0)
{
show_message($_LANG['gb_error_login'], '', '', 'error');
}
/* 查询:取得参数:团购活动id */
$group_buy_id = isset($_POST['group_buy_id']) ? intval($_POST['group_buy_id']) : 0;
if ($group_buy_id <= 0)
{
ecs_header("Location: ./\n");
exit;
}
/* 查询:取得数量 */
$number = isset($_POST['number']) ? intval($_POST['number']) : 1;
$number = $number < 1 ? 1 : $number;
/* 查询:取得团购活动信息 */
$group_buy = group_buy_info($group_buy_id, $number);
if (empty($group_buy))
{
ecs_header("Location: ./\n");
exit;
}
/* 查询:检查团购活动是否是进行中 */
if ($group_buy['status'] != GBS_UNDER_WAY)
{
show_message($_LANG['gb_error_status'], '', '', 'error');
}
/* 查询:取得团购商品信息 */
$goods = goods_info($group_buy['goods_id']);
if (empty($goods))
{
ecs_header("Location: ./\n");
exit;
}
/* 查询:判断数量是否足够 */
if (($group_buy['restrict_amount'] > 0 && $number > ($group_buy['restrict_amount'] - $group_buy['valid_goods'])) || $number > $goods['goods_number'])
{
show_message($_LANG['gb_error_goods_lacking'], '', '', 'error');
}
/* 查询:取得规格 */
$specs = '';
foreach ($_POST as $key => $value)
{
if (strpos($key, 'spec_') !== false)
{
$specs .= ',' . intval($value);
}
}
$specs = trim($specs, ',');
/* 查询:如果商品有规格则取规格商品信息 配件除外 */
if ($specs)
{
$_specs = explode(',', $specs);
$product_info = get_products_info($goods['goods_id'], $_specs);
}
empty($product_info) ? $product_info = array('product_number' => 0, 'product_id' => 0) : '';
/* 查询:判断指定规格的货品数量是否足够 */
if ($specs && $number > $product_info['product_number'])
{
show_message($_LANG['gb_error_goods_lacking'], '', '', 'error');
}
/* 查询:查询规格名称和值,不考虑价格 */
$attr_list = array();
$sql = "SELECT a.attr_name, g.attr_value " .
"FROM " . $ecs->table('goods_attr') . " AS g, " .
$ecs->table('attribute') . " AS a " .
"WHERE g.attr_id = a.attr_id " .
"AND g.goods_attr_id " . db_create_in($specs);
$res = $db->query($sql);
while ($row = $db->fetchRow($res))
{
$attr_list[] = $row['attr_name'] . ': ' . $row['attr_value'];
}
$goods_attr = join(chr(13) . chr(10), $attr_list);
/* 更新:清空购物车中所有团购商品 */
include_once(ROOT_PATH . 'includes/lib_order.php');
clear_cart(CART_GROUP_BUY_GOODS);
/* 更新:加入购物车 */
$goods_price = $group_buy['deposit'] > 0 ? $group_buy['deposit'] : $group_buy['cur_price'];
$cart = array(
'user_id' => $_SESSION['user_id'],
'session_id' => SESS_ID,
'goods_id' => $group_buy['goods_id'],
'product_id' => $product_info['product_id'],
'goods_sn' => addslashes($goods['goods_sn']),
'goods_name' => addslashes($goods['goods_name']),
'market_price' => $goods['market_price'],
'goods_price' => $goods_price,
'goods_number' => $number,
'goods_attr' => addslashes($goods_attr),
'goods_attr_id' => $specs,
'is_real' => $goods['is_real'],
'extension_code' => addslashes($goods['extension_code']),
'parent_id' => 0,
'rec_type' => CART_GROUP_BUY_GOODS,
'is_gift' => 0
);
$db->autoExecute($ecs->table('cart'), $cart, 'INSERT');
/* 更新:记录购物流程类型:团购 */
$_SESSION['flow_type'] = CART_GROUP_BUY_GOODS;
$_SESSION['extension_code'] = 'group_buy';
$_SESSION['extension_id'] = $group_buy_id;
/* 进入收货人页面 */
ecs_header("Location: ./flow.php?step=consignee\n");
exit;
}
/* 取得团购活动总数 */
function group_buy_count()
{
$now = gmtime();
$sql = "SELECT COUNT(*) " .
"FROM " . $GLOBALS['ecs']->table('goods_activity') .
"WHERE act_type = '" . GAT_GROUP_BUY . "' " .
"AND start_time <= '$now' AND is_finished < 3";
return $GLOBALS['db']->getOne($sql);
}
/**
* 取得某页的所有团购活动
* @param int $size 每页记录数
* @param int $page 当前页
* @return array
*/
function group_buy_list($size, $page)
{
/* 取得团购活动 */
$gb_list = array();
$now = gmtime();
$sql = "SELECT b.*, IFNULL(g.goods_thumb, '') AS goods_thumb, b.act_id AS group_buy_id, ".
"b.start_time AS start_date, b.end_time AS end_date " .
"FROM " . $GLOBALS['ecs']->table('goods_activity') . " AS b " .
"LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS g ON b.goods_id = g.goods_id " .
"WHERE b.act_type = '" . GAT_GROUP_BUY . "' " .
"AND b.start_time <= '$now' AND b.is_finished < 3 ORDER BY b.act_id DESC";
$res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
while ($group_buy = $GLOBALS['db']->fetchRow($res))
{
$ext_info = unserialize($group_buy['ext_info']);
$group_buy = array_merge($group_buy, $ext_info);
/* 格式化时间 */
$group_buy['formated_start_date'] = local_date($GLOBALS['_CFG']['time_format'], $group_buy['start_date']);
$group_buy['formated_end_date'] = local_date($GLOBALS['_CFG']['time_format'], $group_buy['end_date']);
/* 格式化保证金 */
$group_buy['formated_deposit'] = price_format($group_buy['deposit'], false);
/* 处理价格阶梯 */
$price_ladder = $group_buy['price_ladder'];
if (!is_array($price_ladder) || empty($price_ladder))
{
$price_ladder = array(array('amount' => 0, 'price' => 0));
}
else
{
foreach ($price_ladder as $key => $amount_price)
{
$price_ladder[$key]['formated_price'] = price_format($amount_price['price']);
}
}
$group_buy['price_ladder'] = $price_ladder;
/* 处理图片 */
if (empty($group_buy['goods_thumb']))
{
$group_buy['goods_thumb'] = get_image_path($group_buy['goods_id'], $group_buy['goods_thumb'], true);
}
/* 处理链接 */
$group_buy['url'] = build_uri('group_buy', array('gbid'=>$group_buy['group_buy_id']));
/* 加入数组 */
$gb_list[] = $group_buy;
}
return $gb_list;
}
?>