为ecshop红包增加”转赠”功能
2014-12-17 23:37
197 查看
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}后面添加
代码,然后再增加点按钮的修饰
,
这里作用是给处于未被使用状态的红包添加转赠按钮,这里未使用状态直接用文本判断,因为在模板赋值之前已经有过逻辑处理,另外我们转增过程中也会有相应的逻辑处理,因此不会出现bug,真的想完善,又有闲心的可以用语言包代替本人一向鄙视多语言效果如下
第二步,前端交互增加表单验证
转赠信息提交的表单已经创建,现在需要做验证等相关工作,在/js/user.js中增加
代码,采用模式窗口接收数据
第三步:接收数据并处理
这一步开始就是核心功能方面的了,在/user.php中增加
逻辑代码
第四部:操作数据库,
创建最重要的handsel_bonus_to_user函数,在/includes/lib_transaction.php文件增加函数代码
这样,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 = "returnhandsel_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 | *@accesspublic |
5 | *@paramint$user_id用户ID |
6 | *@paramstring$bouns_sn红包序列号 |
7 | *@paramstring$to_user_name红包序列号接受赠送红包的用户 |
8 | * |
9 | *@returnboolen$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 = "SELECTbonus_id,user_id,order_id,bonus_type_idFROM" . $GLOBALS [ 'ecs' ]->table( 'user_bonus' ). |
22 | "WHEREbonus_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 = "SELECTsend_end_date,use_end_date" . |
37 | "FROM" . $GLOBALS [ 'ecs' ]->table( 'bonus_type' ). |
38 | "WHEREtype_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 = "selectuser_idfrom" . $GLOBALS [ 'ecs' ]->table( 'users' ). "whereuser_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' ). "SETuser_id='" . $user ['user_id ']."' WHEREbonus_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后台增加管理功能页面
- 给ecshop后台增加管理菜单功能页面
- Ecshop商品新增加一个图片上传功能
- ECSHOP增加自动更新缓存的功能
- ECSHOP如何增加红包序列号字符
- ecshop用户中心订单详情增加快递单物流信息查询显示的功能
- 微信自动抢红包的实现(Demo已增加查看TopActivity功能)
- ECShop 增加收藏商品排行榜功能
- [OK]ECSHOP在会员列表页增加按手机查询会员功能
- ecshop功能二次开发之后台管理增加成本价和毛利
- 基于ecshop商城开发优惠券(红包)叠加使用功能
- ecshop 在确认收货时新增加商品评价并送消费积分功能
- ecshop增加直接购买的功能
- 给ecshop后台增加管理功能页面
- 给ecshop后台增加管理功能页面
- 在ecshop 首页中实现增加刷选功能
- ecshop 在确认收货时新增加商品评价并送消费积分功能
- 非常不错的 ecshop增加 浏览过该商品的人还浏览了那些商品 功能
- ecshop后台广告列表中增加图片查看功能
- ecshop增加pc扫描二维码微信支付功能代码