广州传奇网络
地址:广州市天河区东圃大马路富华楼C座
电话:13808825895
QQ:1564443073
ecshop促销中使用红包激励用户购物,要想炒热活动,红包就需要有物以稀为贵的感觉。有人求有人送,这样红包之间的转赠有助于拉动第二梯队的顾客。但是如果已经把红包添加到自己的账户了怎么办?如果ecshop红包的使用再加上什么限制(比如,一个单只能用一个红包,就够坑爹的),现在来做一个线上转赠红包的功能。
简单的实现一下:
首先了解,红包存放在ecs_user_bonus表中,他的归属区分很简单:通过user_id来决定红包属于谁的。
此功能涉及到四个文件:/js/user.js 、 /user.php、 /includes/lib_transaction.php 、 /themes/default/user_transaction.dwt
第一步:增加模板功能
在/themes/default/user_transaction.dwt中 {$item.status}后面添加
1 |
<!--{if $item.status == "未使用"}--> < form action = "/user.php" name = "handsel_bonus_{$item.bonus_id}" method = "post" onsubmit = "return handsel_bonus_to_user(this)" >< input type = "hidden" name = "act" value = "handsel_bonus" >< input type = "hidden" name = "bonus_id" value = "{$item.bonus_id}" >< input type = "hidden" name = "user_name" value = "" >< input class = "btn_bom" type = "submit" value = "转赠" ></ form > <!--{/if}--> |
代码,然后再增加点按钮的修饰
1 |
.btn_bom{ padding : 5px 10px ; cursor : pointer ;} |
,
这里作用是给处于未被使用状态的红包添加转赠按钮,这里未使用状态直接用文本判断,因为在模板赋值之前已经有过逻辑处理,另外我们转增过程中也会有相应的逻辑处理,因此不会出现bug,真的想完善,又有闲心的可以用语言包代替 本人一向鄙视多语言效果如下
第二步,前端交互增加表单验证
转赠信息提交的表单已经创建,现在需要做验证等相关工作,在/js/user.js中增加
1 |
/* * |
2 |
* 赠送红包 |
3 |
*/ |
4 |
function handsel_bonus_to_user(obj){ |
5 |
var username = prompt( "输入你要转赠红包的账户名" , "" ); |
6 |
var msg= '' ; |
7 |
if (username == null ){ return false ;} |
8 |
if (username.length == 0) |
9 |
{ |
10 |
msg += "用户名不能为空" + '\n' ; |
11 |
} |
12 |
else if (username.match(/^\s*$|^c:\\con\\con$|[%,\'\*\"\s\t\<\>\&\\]/)) |
13 |
{ |
14 |
msg += '用户名不合法' + '\n' ; |
15 |
} |
16 |
else if (username.length < 3) |
17 |
{ |
18 |
msg += '用户名不合法' + '\n' ; |
19 |
} else { |
20 |
obj.user_name.value = username; |
21 |
return true ; |
22 |
} |
23 |
alert(msg); |
24 |
return false ; |
25 |
} |
代码,采用模式窗口接收数据
第三步:接收数据并处理
这一步开始就是核心功能方面的了,在/user.php中增加
1 |
/*赠送红包*/ |
2 |
elseif ( $action == 'handsel_bonus' ){ |
3 |
4 |
include_once (ROOT_PATH . 'includes/lib_transaction.php' ); |
5 |
if (isset( $_REQUEST [ 'bonus_id' ])){ |
6 |
$bonus_id = (int) $_REQUEST [ 'bonus_id' ]; |
7 |
if (handsel_bonus_to_user( $user_id , $bonus_id , $_REQUEST [ 'user_name' ])){ |
8 |
9 |
show_message( "成功转赠红包给" . $_REQUEST [ 'user_name' ], $_LANG [ 'back_up_page' ], 'user.php?act=bonus' , 'info' ); |
10 |
} else { |
11 |
$err ->show( $_LANG [ 'back_up_page' ], 'user.php?act=bonus' ); |
12 |
} |
13 |
} else { |
14 |
15 |
show_message( "请指明要操作的红包" . $_REQUEST [ 'user_name' ], '/user.php?act=bonus' ); |
16 |
} |
17 |
18 |
} |
逻辑代码
第四部:操作数据库,
创建最重要的handsel_bonus_to_user函数,在/includes/lib_transaction.php文件增加函数代码
1 |
/** |
2 |
* 当前用户给指定用户赠送一个指定红包,红包会立即转向对方账号 |
3 |
* |
4 |
* @access public |
5 |
* @param int $user_id 用户ID |
6 |
* @param string $bouns_sn 红包序列号 |
7 |
* @param string $to_user_name 红包序列号接受赠送红包的用户 |
8 |
* |
9 |
* @return boolen $result |
10 |
*/ |
11 |
function handsel_bonus_to_user( $user_id , $bonus_id , $to_user_name ) |
12 |
{ |
13 |
if ( empty ( $user_id )) |
14 |
{ |
15 |
$GLOBALS [ 'err' ]->add( $GLOBALS [ '_LANG' ][ 'not_login' ]); |
16 |
17 |
return false; |
18 |
} |
19 |
20 |
/* 查询红包序列号是否已经存在 */ |
21 |
$sql = "SELECT bonus_id,user_id,order_id,bonus_type_id FROM " . $GLOBALS [ 'ecs' ]->table( 'user_bonus' ) . |
22 |
" WHERE bonus_id = '$bonus_id'" ; |
23 |
$row = $GLOBALS [ 'db' ]->getRow( $sql ); |
24 |
if ( $row ) |
25 |
{ |
26 |
//红包没有被用来购物 |
27 |
if ( $row [ 'order_id' ] == 0) |
28 |
{ |
29 |
30 |
//红包是否属于当前用户 |
31 |
if ( $user_id != $row [ 'user_id' ]){ |
32 |
$GLOBALS [ 'err' ]->add( 'soga,你不拥有此红包' ); |
33 |
return false; |
34 |
} |
35 |
//红包没有被使用 |
36 |
$sql = "SELECT send_end_date, use_end_date " . |
37 |
" FROM " . $GLOBALS [ 'ecs' ]->table( 'bonus_type' ) . |
38 |
" WHERE type_id = '" . $row ['bonus_type_id '] . "' "; |
39 |
40 |
$bonus_time = $GLOBALS [ 'db' ]->getRow( $sql ); |
41 |
42 |
$now = gmtime(); |
43 |
44 |
//超出使用期限 |
45 |
if ( $now > $bonus_time [ 'use_end_date' ]) |
46 |
{ |
47 |
$GLOBALS [ 'err' ]->add( $GLOBALS [ '_LANG' ][ 'bonus_use_expire' ]); |
48 |
return false; |
49 |
} |
50 |
51 |
$sql = "select user_id from " . $GLOBALS [ 'ecs' ]->table( 'users' ) . " where user_name='$to_user_name'" ; |
52 |
$user = $GLOBALS [ 'db' ]->getRow( $sql ); |
53 |
if (! $user ){ |
54 |
$GLOBALS [ 'err' ]->add( '转赠的用户不存在,谢谢好心:)请重新确认用户名!' ); |
55 |
return false; |
56 |
} |
57 |
58 |
$sql = "UPDATE " . $GLOBALS [ 'ecs' ]->table( 'user_bonus' ) . " SET user_id = '" . $user ['user_id ']."' WHERE bonus_id = '$row[bonus_id]' "; |
59 |
$result = $GLOBALS [ 'db' ] ->query( $sql ); |
60 |
if ( $result ) |
61 |
{ |
62 |
return true; |
63 |
} |
64 |
else |
65 |
{ |
66 |
return $GLOBALS [ 'db' ]->errorMsg(); |
67 |
} |
68 |
} |
69 |
else |
70 |
{ |
71 |
//红包被其他人使用过了。 |
72 |
$GLOBALS [ 'err' ]->add( '红包已用来购物,不可再次使用' ); |
73 |
return false; |
74 |
} |
75 |
} |
76 |
else |
77 |
{ |
78 |
//红包不存在 |
79 |
$GLOBALS [ 'err' ]->add( $GLOBALS [ '_LANG' ][ 'bonus_not_exist' ]); |
80 |
return false; |
81 |
} |
82 |
83 |
} |
这样,ecshop商城用户之间就可以互相转赠红包了