wangtengyu
2018-12-07 f459412e0dac4ed94106da043b4c6f8576bfe496
commit | author | age
3e083b 1 <?php
B 2
3 /**
4  *  团购商品前台文件
5  */
6
7 define('IN_ECS', true);
8
9 require(dirname(__FILE__) . '/includes/init.php');
10
11 if ((DEBUG_MODE & 2) != 2)
12 {
13     $smarty->caching = true;
14 }
15
16 /*------------------------------------------------------ */
17 //-- act 操作项的初始化
18 /*------------------------------------------------------ */
19 if (empty($_REQUEST['act']))
20 {
21     $_REQUEST['act'] = 'list';
22 }
23
24 /*------------------------------------------------------ */
25 //-- 团购商品 --> 团购活动商品列表
26 /*------------------------------------------------------ */
27 if ($_REQUEST['act'] == 'list')
28 {
29     /* 取得团购活动总数 */
30     $count = group_buy_count();
31     if ($count > 0)
32     {
33         /* 取得每页记录数 */
34         $size = isset($_CFG['page_size']) && intval($_CFG['page_size']) > 0 ? intval($_CFG['page_size']) : 10;
35
36         /* 计算总页数 */
37         $page_count = ceil($count / $size);
38
39         /* 取得当前页 */
40         $page = isset($_REQUEST['page']) && intval($_REQUEST['page']) > 0 ? intval($_REQUEST['page']) : 1;
41         $page = $page > $page_count ? $page_count : $page;
42
43         /* 缓存id:语言 - 每页记录数 - 当前页 */
44         $cache_id = $_CFG['lang'] . '-' . $size . '-' . $page;
45         $cache_id = sprintf('%X', crc32($cache_id));
46     }
47     else
48     {
49         /* 缓存id:语言 */
50         $cache_id = $_CFG['lang'];
51         $cache_id = sprintf('%X', crc32($cache_id));
52     }
53
54     /* 如果没有缓存,生成缓存 */
55     if (!$smarty->is_cached('group_buy_list.dwt', $cache_id))
56     {
57         if ($count > 0)
58         {
59             /* 取得当前页的团购活动 */
60             $gb_list = group_buy_list($size, $page);
61             $smarty->assign('gb_list',  $gb_list);
62
63             /* 设置分页链接 */
64             $pager = get_pager('group_buy.php', array('act' => 'list'), $count, $page, $size);
65             $smarty->assign('pager', $pager);
66         }
67
68         /* 模板赋值 */
69         $smarty->assign('cfg', $_CFG);
70         assign_template();
71         $position = assign_ur_here();
72         $smarty->assign('page_title', $position['title']);    // 页面标题
73         $smarty->assign('ur_here',    $position['ur_here']);  // 当前位置
74         $smarty->assign('categories', get_categories_tree()); // 分类树
75         $smarty->assign('helps',      get_shop_help());       // 网店帮助
76         $smarty->assign('top_goods',  get_top10());           // 销售排行
77         $smarty->assign('promotion_info', get_promotion_info());
78         $smarty->assign('feed_url',         ($_CFG['rewrite'] == 1) ? "feed-typegroup_buy.xml" : 'feed.php?type=group_buy'); // RSS URL
79
80         assign_dynamic('group_buy_list');
81     }
82
83     /* 显示模板 */
84     $smarty->display('group_buy_list.dwt', $cache_id);
85 }
86
87 /*------------------------------------------------------ */
88 //-- 团购商品 --> 商品详情
89 /*------------------------------------------------------ */
90 elseif ($_REQUEST['act'] == 'view')
91 {
92     /* 取得参数:团购活动id */
93     $group_buy_id = isset($_REQUEST['id']) ? intval($_REQUEST['id']) : 0;
94     if ($group_buy_id <= 0)
95     {
96         ecs_header("Location: ./\n");
97         exit;
98     }
99
100     /* 取得团购活动信息 */
101     $group_buy = group_buy_info($group_buy_id);
102
103     if (empty($group_buy))
104     {
105         ecs_header("Location: ./\n");
106         exit;
107     }
108 //    elseif ($group_buy['is_on_sale'] == 0 || $group_buy['is_alone_sale'] == 0)
109 //    {
110 //        header("Location: ./\n");
111 //        exit;
112 //    }
113
114     /* 缓存id:语言,团购活动id,状态,(如果是进行中)当前数量和是否登录 */
115     $cache_id = $_CFG['lang'] . '-' . $group_buy_id . '-' . $group_buy['status'];
116     if ($group_buy['status'] == GBS_UNDER_WAY)
117     {
118         $cache_id = $cache_id . '-' . $group_buy['valid_goods'] . '-' . intval($_SESSION['user_id'] > 0);
119     }
120     $cache_id = sprintf('%X', crc32($cache_id));
121
122     /* 如果没有缓存,生成缓存 */
123     if (!$smarty->is_cached('group_buy_goods.dwt', $cache_id))
124     {
125         $group_buy['gmt_end_date'] = $group_buy['end_date'];
126         $smarty->assign('group_buy', $group_buy);
127
128         /* 取得团购商品信息 */
129         $goods_id = $group_buy['goods_id'];
130         $goods = goods_info($goods_id);
131         if (empty($goods))
132         {
133             ecs_header("Location: ./\n");
134             exit;
135         }
136         $goods['url'] = build_uri('goods', array('gid' => $goods_id), $goods['goods_name']);
137         $smarty->assign('gb_goods', $goods);
138
139         /* 取得商品的规格 */
140         $properties = get_goods_properties($goods_id);
141         $smarty->assign('specification', $properties['spe']); // 商品规格
142
143         //模板赋值
144         $smarty->assign('cfg', $_CFG);
145         assign_template();
146
147         $position = assign_ur_here(0, $goods['goods_name']);
148         $smarty->assign('page_title', $position['title']);    // 页面标题
149         $smarty->assign('ur_here',    $position['ur_here']);  // 当前位置
150
151         $smarty->assign('categories', get_categories_tree()); // 分类树
152         $smarty->assign('helps',      get_shop_help());       // 网店帮助
153         $smarty->assign('top_goods',  get_top10());           // 销售排行
154         $smarty->assign('promotion_info', get_promotion_info());
155         assign_dynamic('group_buy_goods');
156     }
157
158     //更新商品点击次数
159     $sql = 'UPDATE ' . $ecs->table('goods') . ' SET click_count = click_count + 1 '.
160            "WHERE goods_id = '" . $group_buy['goods_id'] . "'";
161     $db->query($sql);
162
163     $smarty->assign('now_time',  gmtime());           // 当前系统时间
164     $smarty->display('group_buy_goods.dwt', $cache_id);
165 }
166
167 /*------------------------------------------------------ */
168 //-- 团购商品 --> 购买
169 /*------------------------------------------------------ */
170
171 elseif ($_REQUEST['act'] == 'buy')
172 {
173     /* 查询:判断是否登录 */
174     if ($_SESSION['user_id'] <= 0)
175     {
176         show_message($_LANG['gb_error_login'], '', '', 'error');
177     }
178
179     /* 查询:取得参数:团购活动id */
180     $group_buy_id = isset($_POST['group_buy_id']) ? intval($_POST['group_buy_id']) : 0;
181     if ($group_buy_id <= 0)
182     {
183         ecs_header("Location: ./\n");
184         exit;
185     }
186
187     /* 查询:取得数量 */
188     $number = isset($_POST['number']) ? intval($_POST['number']) : 1;
189     $number = $number < 1 ? 1 : $number;
190
191     /* 查询:取得团购活动信息 */
192     $group_buy = group_buy_info($group_buy_id, $number);
193     if (empty($group_buy))
194     {
195         ecs_header("Location: ./\n");
196         exit;
197     }
198
199     /* 查询:检查团购活动是否是进行中 */
200     if ($group_buy['status'] != GBS_UNDER_WAY)
201     {
202         show_message($_LANG['gb_error_status'], '', '', 'error');
203     }
204
205     /* 查询:取得团购商品信息 */
206     $goods = goods_info($group_buy['goods_id']);
207     if (empty($goods))
208     {
209         ecs_header("Location: ./\n");
210         exit;
211     }
212
213     /* 查询:判断数量是否足够 */
214     if (($group_buy['restrict_amount'] > 0 && $number > ($group_buy['restrict_amount'] - $group_buy['valid_goods'])) || $number > $goods['goods_number'])
215     {
216         show_message($_LANG['gb_error_goods_lacking'], '', '', 'error');
217     }
218
219     /* 查询:取得规格 */
220     $specs = '';
221     foreach ($_POST as $key => $value)
222     {
223         if (strpos($key, 'spec_') !== false)
224         {
225             $specs .= ',' . intval($value);
226         }
227     }
228     $specs = trim($specs, ',');
229
230     /* 查询:如果商品有规格则取规格商品信息 配件除外 */
231     if ($specs)
232     {
233         $_specs = explode(',', $specs);
234         $product_info = get_products_info($goods['goods_id'], $_specs);
235     }
236
237     empty($product_info) ? $product_info = array('product_number' => 0, 'product_id' => 0) : '';
238
239     /* 查询:判断指定规格的货品数量是否足够 */
240     if ($specs && $number > $product_info['product_number'])
241     {
242         show_message($_LANG['gb_error_goods_lacking'], '', '', 'error');
243     }
244
245     /* 查询:查询规格名称和值,不考虑价格 */
246     $attr_list = array();
247     $sql = "SELECT a.attr_name, g.attr_value " .
248             "FROM " . $ecs->table('goods_attr') . " AS g, " .
249                 $ecs->table('attribute') . " AS a " .
250             "WHERE g.attr_id = a.attr_id " .
251             "AND g.goods_attr_id " . db_create_in($specs);
252     $res = $db->query($sql);
253     while ($row = $db->fetchRow($res))
254     {
255         $attr_list[] = $row['attr_name'] . ': ' . $row['attr_value'];
256     }
257     $goods_attr = join(chr(13) . chr(10), $attr_list);
258
259     /* 更新:清空购物车中所有团购商品 */
260     include_once(ROOT_PATH . 'includes/lib_order.php');
261     clear_cart(CART_GROUP_BUY_GOODS);
262
263     /* 更新:加入购物车 */
264     $goods_price = $group_buy['deposit'] > 0 ? $group_buy['deposit'] : $group_buy['cur_price'];
265     $cart = array(
266         'user_id'        => $_SESSION['user_id'],
267         'session_id'     => SESS_ID,
268         'goods_id'       => $group_buy['goods_id'],
269         'product_id'     => $product_info['product_id'],
270         'goods_sn'       => addslashes($goods['goods_sn']),
271         'goods_name'     => addslashes($goods['goods_name']),
272         'market_price'   => $goods['market_price'],
273         'goods_price'    => $goods_price,
274         'goods_number'   => $number,
275         'goods_attr'     => addslashes($goods_attr),
276         'goods_attr_id'  => $specs,
277         'is_real'        => $goods['is_real'],
278         'extension_code' => addslashes($goods['extension_code']),
279         'parent_id'      => 0,
280         'rec_type'       => CART_GROUP_BUY_GOODS,
281         'is_gift'        => 0
282     );
283     $db->autoExecute($ecs->table('cart'), $cart, 'INSERT');
284
285     /* 更新:记录购物流程类型:团购 */
286     $_SESSION['flow_type'] = CART_GROUP_BUY_GOODS;
287     $_SESSION['extension_code'] = 'group_buy';
288     $_SESSION['extension_id'] = $group_buy_id;
289
290     /* 进入收货人页面 */
291     ecs_header("Location: ./flow.php?step=consignee\n");
292     exit;
293 }
294
295 /* 取得团购活动总数 */
296 function group_buy_count()
297 {
298     $now = gmtime();
299     $sql = "SELECT COUNT(*) " .
300             "FROM " . $GLOBALS['ecs']->table('goods_activity') .
301             "WHERE act_type = '" . GAT_GROUP_BUY . "' " .
302             "AND start_time <= '$now' AND is_finished < 3";
303
304     return $GLOBALS['db']->getOne($sql);
305 }
306
307 /**
308  * 取得某页的所有团购活动
309  * @param   int     $size   每页记录数
310  * @param   int     $page   当前页
311  * @return  array
312  */
313 function group_buy_list($size, $page)
314 {
315     /* 取得团购活动 */
316     $gb_list = array();
317     $now = gmtime();
318     $sql = "SELECT b.*, IFNULL(g.goods_thumb, '') AS goods_thumb, b.act_id AS group_buy_id, ".
319                 "b.start_time AS start_date, b.end_time AS end_date " .
320             "FROM " . $GLOBALS['ecs']->table('goods_activity') . " AS b " .
321                 "LEFT JOIN " . $GLOBALS['ecs']->table('goods') . " AS g ON b.goods_id = g.goods_id " .
322             "WHERE b.act_type = '" . GAT_GROUP_BUY . "' " .
323             "AND b.start_time <= '$now' AND b.is_finished < 3 ORDER BY b.act_id DESC";
324     $res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
325     while ($group_buy = $GLOBALS['db']->fetchRow($res))
326     {
327         $ext_info = unserialize($group_buy['ext_info']);
328         $group_buy = array_merge($group_buy, $ext_info);
329
330         /* 格式化时间 */
331         $group_buy['formated_start_date']   = local_date($GLOBALS['_CFG']['time_format'], $group_buy['start_date']);
332         $group_buy['formated_end_date']     = local_date($GLOBALS['_CFG']['time_format'], $group_buy['end_date']);
333
334         /* 格式化保证金 */
335         $group_buy['formated_deposit'] = price_format($group_buy['deposit'], false);
336
337         /* 处理价格阶梯 */
338         $price_ladder = $group_buy['price_ladder'];
339         if (!is_array($price_ladder) || empty($price_ladder))
340         {
341             $price_ladder = array(array('amount' => 0, 'price' => 0));
342         }
343         else
344         {
345             foreach ($price_ladder as $key => $amount_price)
346             {
347                 $price_ladder[$key]['formated_price'] = price_format($amount_price['price']);
348             }
349         }
350         $group_buy['price_ladder'] = $price_ladder;
351
352         /* 处理图片 */
353         if (empty($group_buy['goods_thumb']))
354         {
355             $group_buy['goods_thumb'] = get_image_path($group_buy['goods_id'], $group_buy['goods_thumb'], true);
356         }
357         /* 处理链接 */
358         $group_buy['url'] = build_uri('group_buy', array('gbid'=>$group_buy['group_buy_id']));
359         /* 加入数组 */
360         $gb_list[] = $group_buy;
361     }
362
363     return $gb_list;
364 }
365
366 ?>