forked from faint32/benhu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lottery.php
471 lines (441 loc) · 16.5 KB
/
lottery.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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<?php
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
if ((DEBUG_MODE & 2) != 2)
{
$smarty->caching = true;
}
/*------------------------------------------------------ */
//-- INPUT
/*------------------------------------------------------ */
if (empty($_REQUEST['act']))
{
$act = 'list';
}
else
{
$act=$_REQUEST['act'];
}
/*------------------------------------------------------ */
//-- PROCESSOR
/*------------------------------------------------------ */
$cache_id = $_CFG['lang'] . '-' . $act . '-' . $_SESSION['user_rank'];
$cache_id = sprintf('%X', crc32($cache_id));
if ($act == 'list')
{
assign_template();
// if (!$smarty->is_cached('lottery.dwt', $cache_id))
{
$smarty->assign('page_title', "XX商城-抽奖总动员-免费大奖等你拿"); // 页面标题
$smarty->assign('ur_here', "免费抽奖"); // 当前位置
$smarty->assign('user_id', "-1");
$smarty->assign('totalwin', gettotallu()); // 当前位置
if ($_SESSION['user_id'] > 0)
{
$sql = "SELECT is_validated,email FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_id='".$_SESSION['user_id'] ."'";
$row = $GLOBALS['db']->getRow($sql);
$smarty->assign('hash', $row['is_validated']);
$email_name = substr($row['email'],strpos($row['email'],'@')+1);
if($email_name == 'gmail.com')
{
$email_url = 'http://gmail.com';
}
else if($email_name == 'hotmail.com')
{
$email_url = 'http://www.hotmail.com';
}
else
{
$email_url = 'http://mail.'. $email_name;
}
$smarty->assign('email_url', $email_url);
$smarty->assign('user_info', get_user_info());
$smarty->assign('user_id', $_SESSION['user_id']);
$smarty->assign('user_name', $_SESSION['user_name']);
$smarty->assign('credit', getusercredit($_SESSION['user_id']));
$sql="select * from ".$GLOBALS['ecs']->table('lottery_user')." where applystatus=0 and user_id='".$_SESSION['user_id']."' limit 1";
$row = $GLOBALS['db']->getRow($sql);
if($row)
{
$smarty->assign('fetch_rewards', "lottery.php?act=fetch_rewards&id=".$row['id']);
}
}
$smarty->assign('lotterylist1', getlotterylist(1));
$smarty->assign('lotterylist2', getlotterylist(2));
$smarty->assign('lotterylist3', getlotterylist(3));
$smarty->assign('winlist', getwinlist());
$smarty->assign('helps', get_shop_help()); // 网店帮助
/* links */
$links = index_get_links();
$smarty->assign('img_links', $links['img']);
$smarty->assign('txt_links', $links['txt']);
$smarty->assign('data_dir', DATA_DIR); // 数据目录
}
$smarty->assign('nav', '5');
$smarty->display('lottery.dwt');//, $cache_id);
}
elseif ($_REQUEST['act'] == 'try')
{
require(ROOT_PATH . 'includes/cls_json.php');
$json = new JSON;
$result = array('error' => 0, 'message' => '', 'content' => '', 'id' => 0, 'credit' => 0,'win'=>0);
$_REQUEST['cmt'] = json_str_iconv($_REQUEST['cmt']);
$cmt = $json->decode($_REQUEST['cmt']);
$id = $cmt->id;
$pro = $cmt->pro;
$winnum = $cmt->winnum;
$user_id = $cmt->user_id;
$point = $cmt->point;
if($pro <= 0 || $winnum <= 0 || $winnum > $pro || $user_id < 1)
{
$result['error'] = 1;
$result['message'] = "服务器感到很大压力,出问题了.";
}
else
{
//检测用户是否拥有足够的积分
if(checkuserpoint($user_id, $point) == "-1")
{
$result['error'] = -1;
$content = "<br/><br/><br/><font size=\"+5\" color=\"#0033CC\">你的积分不足拉,快去挣取积分吧...</font><br/><br/><br/><br/>";
}
else
{
srand(seed());
$randnum = rand(1, $pro);
if($randnum == $winnum)//中奖了
{
//先检查库存,再减少库存
$num = ReduceNumber($id, $user_id);
if($num == "0")
{
$randnum = $randnum + 1;
//$content = "你的号码:".$randnum."<br/>";
$result['error'] = 1;
$content = "没有中奖唉~";
}
else
{
//$content = "你的号码:".$randnum."<br/>";
$result['error'] = 0;
$result['wid'] = $num;
$content = "恭喜您中奖了!!写下您获奖感言索取奖品吧..<br/>
";
}
clear_cache_files('lottery');
}
else
{
//$content = "你的号码:".$randnum."<br/>";
$result['error'] = 1;
$content = "没有中奖唉~";
}
$content .="<br/>";
$sql = "update ".$GLOBALS['ecs']->table('lottery')." set click=click+1 where id='".$id."'";
$GLOBALS['db']->query($sql);
}
$result['credit'] = getusercredit($user_id);
// $result['content'] = $content;
$result['message'] = $content;
$result['id'] = $id;
}
die($json->encode($result));
}
elseif ($_REQUEST['act'] == 'view')
{
$page = empty($_REQUEST['page']) ? 1 : intval($_REQUEST['page']);
$size = 10;
$record_count = intval(gettotallu());
$page_count = $record_count > 0 ? intval(ceil($record_count / $size)) : 1;
if ($page < 1)
{
$page = 1;
}
if ($page > $page_count)
{
$page = $page_count;
}
$pager = get_pager('lottery.php', array('act'=>'view'), $record_count, $page, $size);
$smarty->assign('pager', $pager);
assign_template();
$smarty->assign('page_title', "ec抽奖网-抽奖总动员-免费大奖等你拿"); // 页面标题
$smarty->assign('ur_here', "获奖名单"); // 当前位置
$smarty->assign('winlist', getwinlist($size, $page));
$smarty->display('lottery_winlist.dwt');
}
elseif ($_REQUEST['act'] == 'fetch_rewards')
{
$page = empty($_REQUEST['page']) ? 1 : intval($_REQUEST['page']);
$size = 10;
$record_count = intval(gettotallu());
$page_count = $record_count > 0 ? intval(ceil($record_count / $size)) : 1;
if ($page < 1)
{
$page = 1;
}
if ($page > $page_count)
{
$page = $page_count;
}
$pager = get_pager('lottery.php', array('act'=>'view'), $record_count, $page, $size);
$smarty->assign('pager', $pager);
assign_template();
$smarty->assign('page_title', "ec抽奖网-抽奖总动员-免费大奖等你拿"); // 页面标题
$smarty->assign('ur_here', "提交获奖感言"); // 当前位置
$smarty->assign('winlist', getwinlist($size, $page));
$id = empty($_REQUEST['id']) ? 0 : intval($_REQUEST['id']);
if($id > 0)
{
$user_id = $_SESSION['user_id'];
$sql="select * from ".$GLOBALS['ecs']->table('lottery_user')." where id='$id' and user_id='$user_id' and applystatus=0";
$row = $GLOBALS['db']->getRow($sql);
if($row)
{
// if($row['speech'] == "" || $row['speech'] == "无" || $row['address']=="")
{
// if($row['address']=="")
{
$sql = "select lu.*,l.shop_price,l.goods_id,(select goods_name from ".$GLOBALS['ecs']->table('goods')." as g where g.goods_id=l.goods_id) as goods_name ".
",(select goods_thumb from ".$GLOBALS['ecs']->table('goods')." as g where g.goods_id=l.goods_id) as goods_thumb ".
",(select user_name from ".$GLOBALS['ecs']->table('users')." as u where u.user_id=lu.user_id) as user_name ".
" from ".$GLOBALS['ecs']->table('lottery_user') . "as lu left join " .$GLOBALS['ecs']->table('lottery') . " as l on l.id=lu.lid where lu.id='$id' limit 1";
$row = $GLOBALS['db']->getRow($sql);
if($row)
{
$smarty->assign('goods_name', $row['goods_name']);
$smarty->assign('goods_thumb', get_image_path($row['goods_id'], $row['goods_thumb'], true));
$smarty->assign('shop_price', $row['shop_price']);
$smarty->assign('goods_id', $row['goods_id']);
$smarty->assign('speech', "1");
$smarty->assign('id', $id);
$smarty->assign('goods_url', build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']));
/* 取得国家列表、商店所在国家、商店所在国家的省列表 */
$smarty->assign('country_list', get_regions());
$smarty->assign('province_list', get_regions(1, 1));
$smarty->assign('city_list', get_regions(2, 0));
$smarty->assign('district_list', get_regions(3, 0));
}
}
}
}
}
$smarty->display('lottery_winlist.dwt');
}
elseif ($_REQUEST['act'] == 'submitspeech')
{
require(ROOT_PATH . 'includes/cls_json.php');
$json = new JSON;
$result = array('error' => 0, 'message' => '', 'content' => '提交失败1.0!');
$_REQUEST['cmt'] = json_str_iconv($_REQUEST['cmt']);
$cmt = $json->decode($_REQUEST['cmt']);
$user_id = $_SESSION['user_id'];
$id = $cmt->id;
$sql="select applystatus from ".$GLOBALS['ecs']->table('lottery_user')." where id=".$id;
$applystatus = $GLOBALS['db']->getOne($sql);
if($applystatus == 1)
{
$result = array('error' => 0, 'message' => '', 'content' => '已提交,请勿重复提交!');
}
else
{
if(submitmyspeech($cmt, $user_id))
{
$result = array('error' => 1, 'message' => '', 'content' => '提交成功!请到“我的订单”查询中奖产品.');
}
else
{
$result = array('error' => 0, 'message' => '', 'content' => '提交失败!');
}
}
die($json->encode($result));
}
/**
*获取抽奖商品
*$lg 所在组别
*/
function getlotterylist($type = 0)
{
if($type > 0)
{
$sql = "SELECT l.*,(select goods_name from ".$GLOBALS['ecs']->table('goods')." as g where g.goods_id=l.goods_id) as goods_name ".
",(select goods_thumb from ".$GLOBALS['ecs']->table('goods')." as g where g.goods_id=l.goods_id) as goods_thumb ".
"FROM ".$GLOBALS['ecs']->table('lottery'). " as l left join ".$GLOBALS['ecs']->table('lottery_group')." as lg on l.lg=lg.id where l.status=1 and (l.total-l.outnum)>0 and lg.type='".$type."'";
}
else
{
$sql = "SELECT l.*,(select goods_name from ".$GLOBALS['ecs']->table('goods')." as g where g.goods_id=l.goods_id) as goods_name ".
",(select goods_thumb from ".$GLOBALS['ecs']->table('goods')." as g where g.goods_id=l.goods_id) as goods_thumb ".
"FROM ".$GLOBALS['ecs']->table('lottery'). " as l left join ".$GLOBALS['ecs']->table('lottery_group')." as lg on l.lg=lg.id where l.status=1 and (l.total-l.outnum)>0";
}
$res = $GLOBALS['db']->getAll($sql);
$lottery_list = array();
foreach ($res AS $row)
{
$row['shop_price'] = price_format($row['shop_price']);
$row['thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
$row['url'] = build_uri('goods', array('gid' => $row['goods_id']), $row['goods_name']);
$row['proper'] = round(floatval(1.00/$row['pro'])*100,3);
$lottery_list[] = $row;
}
return $lottery_list;
}
/**
*获取中奖列表
*/
function getwinlist($size = 3, $page = 1)
{
$sql = "select lu.*,l.goods_id,l.shop_price,(select goods_name from ".$GLOBALS['ecs']->table('goods')." as g where g.goods_id=l.goods_id) as goods_name ".
",(select goods_thumb from ".$GLOBALS['ecs']->table('goods')." as g where g.goods_id=l.goods_id) as goods_thumb ".
",(select user_name from ".$GLOBALS['ecs']->table('users')." as u where u.user_id=lu.user_id) as user_name ".
" from ".$GLOBALS['ecs']->table('lottery_user') . "as lu left join " .$GLOBALS['ecs']->table('lottery') . " as l on l.id=lu.lid where 1=1 order by lu.id desc ";
$res = $GLOBALS['db']->selectLimit($sql, $size, ($page - 1) * $size);
$win_list = array();
while ($row = $GLOBALS['db']->fetchRow($res))
{
$row['wintime'] = local_date("Y-m-d H:i:s", $row['wintime']);
$row['gettime'] = local_date("Y-m-d H:i:s", $row['gettime']);
$row['goods_thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);
$row['speech'] = $row['speech'] == "" ? "无" : $row['speech'];
$win_list[] = $row;
}
return $win_list;
}
/**
*中奖了,,减少库存,,加入中奖列表,返回0表示抽奖完毕了
*/
function ReduceNumber($id, $user_id)
{
//先检查库存是否正确
$sql = "select 1 from ".$GLOBALS['ecs']->table('lottery')." where id='".$id."' and (total-outnum)>0 and status=1";
$num = $GLOBALS['db']->getOne($sql);
if($num)
{
$sql = "update ".$GLOBALS['ecs']->table('lottery')." set outnum=outnum+1 where id='".$id."'";
$GLOBALS['db']->query($sql);
$sql = "insert into ".$GLOBALS['ecs']->table('lottery_user')." (lid, user_id, status, remark,speech,wintime) values('".$id."','".$user_id."','0','','无','".local_strtotime(date("Y-m-d H:i:s", time()+8*3600))."')";
$GLOBALS['db']->query($sql);
$id = $GLOBALS['db']->insert_id();
//是否还有抽奖数量
$sql = "select 1 from ".$GLOBALS['ecs']->table('lottery')." where id='".$id."' and total=outnum";
$num = $GLOBALS['db']->getOne($sql);
if($num)
{
$sql = "update ".$GLOBALS['ecs']->table('lottery')." set status=2 where id='".$id."'";
$GLOBALS['db']->query($sql);
}
return $id;
}
else
{
return "0";
}
}
/**
*检测用户是否拥有足够的积分
*/
function checkuserpoint($user_id, $point)
{
$sql = "select 1 from ".$GLOBALS['ecs']->table('users')." where user_id='$user_id' and pay_points>=$point";
$num = $GLOBALS['db']->getOne($sql);
if($num)
{
//足够的话要减少积分
$sql = "update ".$GLOBALS['ecs']->table('users')." set pay_points=pay_points-$point where user_id='$user_id'";
$GLOBALS['db']->query($sql);
return "1";
}
return "-1";
}
/**
*获取用户拥有的积分
*/
function getusercredit($user_id)
{
$sql = "select pay_points from ".$GLOBALS['ecs']->table('users')." where user_id='$user_id'";
return $GLOBALS['db']->getOne($sql);
}
/**
*获取中奖用户总数
*/
function gettotallu()
{
$sql = "select count(*) from ".$GLOBALS['ecs']->table('lottery_user');
return $GLOBALS['db']->getOne($sql);
}
/**
*提交获奖感言
*/
function submitmyspeech($cmt, $user_id)
{
require_once(ROOT_PATH . 'includes/lib_order.php');
$id = $cmt->id;
$speech = $cmt->speech;
$address = $cmt->address;
$tel = $cmt->tel;
$name = $cmt->name;
$province = $cmt->province;
$city = $cmt->city;
$district = $cmt->district;
$email = $cmt->email;
$goods_id = $cmt->goods_id;
//下订单
$order_sn = get_order_sn();
$shipping_id = 8;
$shipping_name = "圆通快递";
$shipping_fee = 0.00;
$order_amount = $shipping_fee;
$pay_id = 1;
$pay_name = "余额支付";
$sql = 'INSERT INTO '.$GLOBALS['ecs']->table('order_info').' (`order_sn`, `user_id`,`consignee`, `country`, `province`,`city`, `district`, `address`, '.
'`tel`, `email`,`shipping_id`, `shipping_name`,`pay_id`, `pay_name`, `how_oos`, `goods_amount`,'.
' `shipping_fee`,`order_amount`,`referer`, `add_time`) VALUES ("'.
$order_sn.'",'.$user_id.',"'.$name.'",1,'.$province.','.$city.','.$district.',"'.$address.'","'.$tel.'","'.$email.'",'.$shipping_id.',"'.$shipping_name.'",'.
$pay_id.',"'.$pay_name.'","等待所有商品备齐后再发",0.00,'.$shipping_fee.','.$order_amount.',"本站",'.gmtime().')';
$GLOBALS['db']->query($sql);
$sql = 'select max(order_id) from '.$GLOBALS['ecs']->table('order_info');
$order_id = $GLOBALS['db']->getOne($sql);
$sql = 'select * from '.$GLOBALS['ecs']->table('goods').' where goods_id = '.$goods_id;
$goods = $GLOBALS['db']->getRow($sql); //获奖商品信息
$sql = 'INSERT INTO '.$GLOBALS['ecs']->table('order_goods').' (`order_id`, `goods_id`, `goods_name`, `goods_sn`, `market_price`, `goods_price`, `is_real`) VALUES ('.
$order_id.','.$goods_id.',"'.$goods['goods_name'].'(抽奖获奖品)","'.$goods['goods_sn'].'",'.$goods['market_price'].',0.00,1)';
$GLOBALS['db']->query($sql);
$province = get_region_name($province);
$city = get_region_name($city);
$district = get_region_name($district);
$address = $name.','.$address.','.$email;
$address1 = $province.$city.$district;
//修改获奖状态为已申请
$sql = "update ".$GLOBALS['ecs']->table('lottery_user').
" set speech='$speech', address1='$address1',address='$address', tel='$tel' ,order_id = '$order_id',gettime='".gmtime()."',ip = '".real_ip().
"',applystatus=1 where id='$id' and user_id='$user_id'";
$GLOBALS['db']->query($sql);
return "1";
}
//seed用户自定义函数以微秒作为种子
function seed()
{
list($msec, $sec) = explode(' ', microtime());
return (float) $sec;
}
function index_get_links()
{
$sql = 'SELECT link_logo, link_name, link_url FROM ' . $GLOBALS['ecs']->table('friend_link') . ' ORDER BY show_order';
$res = $GLOBALS['db']->getAll($sql);
$links['img'] = $links['txt'] = array();
foreach ($res AS $row)
{
if (!empty($row['link_logo']))
{
$links['img'][] = array('name' => $row['link_name'],
'url' => $row['link_url'],
'logo' => $row['link_logo']);
}
else
{
$links['txt'][] = array('name' => $row['link_name'],
'url' => $row['link_url']);
}
}
return $links;
}
?>