PHP仿微信发红包领红包效果

2025-05-29 0 61

近期项目需要在聊天的基础上新增红包功能,需求:仿微信(不含留言),但只能使用余额发红包。于是多次使用微信红包,了解各种交互界面及业务需求,如展示信息、分类(个人,群普通,群拼手气)、个数限制(100)、金额限制(200)、过期时间(24小时)等等,然后着手开发,下面提及的基本全是提供给app端的接口,毕竟我是phper。

一、设计数据表如下

?

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
create table `red_packet` (

`id` int(10) unsigned not null auto_increment,

`user_id` int(10) unsigned not null default '0' comment '用户id',

`for_id` int(10) unsigned not null default '0' comment '发放对象(用户或群id)',

`pay_status` tinyint(1) unsigned not null default '0' comment '支付状态:0未支付,1已支付',

`type` tinyint(1) unsigned not null default '0' comment '类型:1、个人,2、群普通,3、群拼手气',

`intro` varchar(255) not null default '' comment '简介',

`number` tinyint(1) unsigned not null default '0' comment '个数',

`total_money` decimal(10,2) unsigned not null default '0.0' comment '总金额',

`single_money` decimal(10,2) unsigned not null default '0.0' comment '单个红包金额(群拼手气时为0)',

`return_money` decimal(10,2) unsigned not null default '0.0' comment '退还金额',

`is_cli_handle` tinyint(1) unsigned not null default '0' comment '是否经过cli退款处理:0否,1是',

`expend_time` mediumint(1) unsigned not null default '0' comment '领取消耗时间',

`add_time` int(10) unsigned not null default '0' comment '创建时间',

`pay_time` int(10) unsigned not null default '0' comment '支付时间',

primary key (`id`),

key `user_id` (`user_id`),

key `pay_status` (`pay_status`),

key `pay_time` (`pay_time`)

) engine=innodb default charset=utf8 comment='红包发放表';

create table `red_packet_log` (

`id` int(10) unsigned not null auto_increment,

`rp_id` int(10) unsigned not null default '0' comment '红包id',

`user_id` int(10) unsigned not null default '0' comment '领取人id',

`money` decimal(10,2) unsigned not null default '0.0' comment '领取金额',

`is_good` tinyint(1) unsigned not null default '0' comment '是否手气最佳:0否,1是',

`add_time` int(10) unsigned not null default '0' comment '添加时间',

`update_time` int(10) unsigned not null default '0' comment '领取时间',

primary key (`id`),

key `rp_id` (`rp_id`)

) engine=innodb default charset=utf8 comment='红包领取日志表';

二、发红包

PHP仿微信发红包领红包效果PHP仿微信发红包领红包效果

由于支付成功之后,红包就马上发到聊天界面了,所以在左图“塞钱进红包”时,就把红包信息插入 red_packet 表(支付状态未支付),并分配好金额、计算手气打乱后插入 red_packet_log 表(领取人和领取时间为空),右图“确认支付”成功之后,更新 red_packet 表的支付状态,然后发出红包

三、领红包(这里只针对群红包进行分析)

PHP仿微信发红包领红包效果

PHP仿微信发红包领红包效果

红包的各种前提校验请自己脑补,这里说一个抢群红包的并发问题(群里的几十个人抢几个红包),引入mq来解决。在发红包的时候,先把红包个数依次写入mq,比如发3个红包,就依次写入1、2、3。抢红包的时候从mq取值,取得到数字说明你是第几个抢到红包,对应 red_packet_log 表里的第几个红包,接下来的就是更新 red_packet_log 表的领取人和领取时间,以及余额加钱以及记流水等业务处理了,然后返回领取结果;取不到数字的当然就说明没有抢到红包,直接出“手慢了”的界面。前期有考虑把 red_packet_log 表的主键写入mq,可以省去排序拿第几条log记录,但这样会让“领取消耗时间”这个字段的更新更加麻烦;采用mq存数字,则可以直接比对是否是最后一个红包(取到的数字等与红包个数),然后更新消耗时间。

微信红包的领取结果页(即查看手气页)有很多种:单个和群结果不一样,发红包的人和领红包的人看到的也不一样,单个和群红包过期之后提示不一样等等,这里不一一列举,基本都是根据界面查数据库而已。

四、需求变更,新增第三方支付

说到第三方支付,就要提及同步和异步回调,还有回调时间差。app端在同步回调成功的时候,就会把红包发出去了(app端的支付同步回调是直接调用callback的),如果此时异步回调慢了一两秒,那么用户就会抢到这个支付状态为0的红包。如果说让app端调用长连接接口去查异步回调是否已经成功,再发出红包,则用户体验比较差。

?

1

2

3

4

5

6

7
# 引入中间状态

alter table `red_packet`

modify column `pay_status` tinyint(1) unsigned not null default 0 comment '支付状态:0未支付,1已支付,2等待到账' after `for_id`,

add column `pay_type` tinyint(1) not null default 0 comment '支付方式:0未知,1支付宝,2微信,3银联' after `pay_status`,

add column `trade_no` varchar(30) not null default '' comment '第三方支付交易号' after `pay_type`;

alter table `red_packet_log`

add column `is_into_account` tinyint(1) unsigned not null default 0 comment '是否到账:0否,1是' after `is_good`;

用户抢到红包的时候,根据 pay_status 来决定 is_into_account 的值;

同步回调到app端时,调用接口把支付状态 pay_status 变为2;

异步回调到服务端时,则把支付状态 pay_status 变为1,并查出 is_into_account=1 的 red_packet_log 记录进行处理。

但是上面这三步都要对 red_packet 的查询进行 for update 操作,不然会有执行时间和顺序问题,导致部分 red_packet_log 记录未到账 is_into_account=0;另外锁机制还会使得用户抢红包时变得很慢,因为要等锁释放。

改进如下:(全程不 for update)

用户抢到红包的时候,根据 pay_status 来决定 is_into_account 的值;

同步回调到app端时,调用接口把支付状态 pay_status 变为2;

异步回调到服务端时,则把支付状态 pay_status 变为1,并把红包id(red_packet主键)放入mq;

后台自动脚本,从mq拿到红包id之后,把该红包 is_into_account=0 的记录进行处理,然后再延迟5秒把红包id再次写入mq,进行二次处理,确保数据全部到账。

五、红包过期退还

这里就一个自动脚本,根据 red_packet 表的 pay_time 判断是否超过24小时且没领完的钱,退回用户余额。

原文链接:http://www.cnblogs.com/chanAndy/p/php.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP仿微信发红包领红包效果 https://www.kuaiidc.com/95835.html

相关文章

发表评论
暂无评论