请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

扫码时扫一次码,后台运行了两次,是什么问题呢

2018-03-04 13:59:24.608[http-nio-8080-exec-1]DEBUGc.h.w.dao.Dao_UserProductMap.insertUserProductMap-==> Parameters: 1(Long), 34(Long), 1(Long), 5(Long), 2018-03-04 13:59:24.543(Timestamp), 1(Integer)

2018-03-04 13:59:24.618[http-nio-8080-exec-3]DEBUGc.h.w.dao.Dao_UserProductMap.insertUserProductMap-==> Parameters: 1(Long), 34(Long), 1(Long), 5(Long), 2018-03-04 13:59:24.54(Timestamp), 1(Integer)

2018-03-04 13:59:24.648[http-nio-8080-exec-1]DEBUGc.h.w.dao.Dao_UserProductMap.insertUserProductMap-<==    Updates: 1

2018-03-04 13:59:24.648[http-nio-8080-exec-3]DEBUGc.h.w.dao.Dao_UserProductMap.insertUserProductMap-<==    Updates: 1


正在回答

插入代码

5回答

翔仔 2018-03-04 19:43:06

同学这个去重有很多种方法 只要奏效即可 学到这个章节希望同学能锻炼出举一反三的能力,主观题自由作答,同学可以多理解上面利用session去重或者别的去重方案去解决,去重的核心就是何为重,怎么确定操作来自同一个请求转发两次 我们有什么属性能证明其唯一(userid awardid):)

0 回复 有任何疑惑可以回复我~
  • 提问者 杰哥大大 #1
    还是没弄好,基础太差了,哎_(:з」∠)_
    回复 有任何疑惑可以回复我~ 2018-03-04 21:14:11
  • 翔仔 回复 提问者 杰哥大大 #2
    操作员扫码的时候是有user_id的吧,并且操作的时候是有当前的时间吧?那第一次来的时候,你就把user_id和当前的时间存入到session里面 key是user_id 值是当前时间。下次来的时候如果是相同的user_id 则取出session里对应user_id的值也就是上次的时间值,如果间隔小于1秒那么就是重复访问,就直接return就好了。说的应该是比较明确了,在对应的controller方法里面添加。希望同学下次能够自行解决此类问题,这也是锻炼你将来的能力,不然以后还会是一个问题
    回复 有任何疑惑可以回复我~ 2018-03-04 21:36:07
  • 提问者 杰哥大大 回复 翔仔 #3
    给表里的useid和createtime进行联合索引,然后controller层用try..adduserproductmap方法..catch..return成功后页面,这样修改可以吗?
    回复 有任何疑惑可以回复我~ 2018-03-05 02:19:49
提问者 杰哥大大 2018-03-04 16:25:22

按照那个调了一下还是不太懂,时间戳也不太懂的用。。。应该如何修改?

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
/**
     * 添加消费记录
     */
    @Override
    @Transactional
    public UserProductMapExecution addUserProductMap(UserProductMap userProductMap)
            throws UserProductMapOperationException {
        // 空值判断,主要确保顾客Id,店铺Id以及操作员Id非空
        if (userProductMap != null && userProductMap.getUser().getUserId() != null
                && userProductMap.getShop().getShopId() != null && userProductMap.getOperator().getUserId() != null) {
            // 设定默认值
            userProductMap.setCreateTime(new Date());
            try {
                // 添加消费记录
                int effectedNum = userProductMapDao.insertUserProductMap(userProductMap);
                if (effectedNum <= 0) {
                    throw new UserProductMapOperationException("添加消费记录失败");
                }
                // 若本次消费能够积分
                if (userProductMap.getPoint() != null && userProductMap.getPoint() > 0) {
                    // 查询该顾客是否在店铺消费过
                    UserShopMap userShopMap = userShopMapDao.queryUserShopMap(userProductMap.getUser().getUserId(),
                            userProductMap.getShop().getShopId());
                    if (userShopMap != null && userShopMap.getUserShopId() != null) {
                        // 若之前消费过,即有过积分记录,则进行总积分的更新操作
                        userShopMap.setPoint(userShopMap.getPoint() + userProductMap.getPoint());
                        effectedNum = userShopMapDao.updateUserShopMapPoint(userShopMap);
                        if (effectedNum <= 0) {
                            throw new UserProductMapOperationException("更新积分信息失败");
                        }
                    else {
                        // 在店铺没有过消费记录,添加一条店铺积分信息(就跟初始化会员一样)
                        userShopMap = compactUserShopMap4Add(userProductMap.getUser().getUserId(),
                                userProductMap.getShop().getShopId(), userProductMap.getPoint());
                        effectedNum = userShopMapDao.insertUserShopMap(userShopMap);
                        if (effectedNum <= 0) {
                            throw new UserProductMapOperationException("积分信息创建失败");
                        }
                    }
                }
                return new UserProductMapExecution(UserProductMapStateEnum.SUCCESS, userProductMap);
            catch (Exception e) {
                throw new UserProductMapOperationException("添加授权失败:" + e.toString());
            }
        else {
            return new UserProductMapExecution(UserProductMapStateEnum.NULL_USERPRODUCT_INFO);
        }
    }


0 回复 有任何疑惑可以回复我~
翔仔 2018-03-04 15:16:37

还有一个思路可以在session里用key记录一个用户id,value是访问微信链接回传的时间戳,然后每次回传回来的时候如果是同一个用户,则取出value与当前时间戳判断,如果小于一秒,则认为是重复访问,则丢弃本次操作并更新该key的value为现在时间。这样就能避免两次重复操作。不知道同学能否理解

0 回复 有任何疑惑可以回复我~
  • 提问者 杰哥大大 #1
    不太懂...
    回复 有任何疑惑可以回复我~ 2018-03-04 16:03:24
  • 提问者 杰哥大大 #2
    在controller的adduserproductmap里改吗?
    回复 有任何疑惑可以回复我~ 2018-03-04 19:11:31
翔仔 2018-03-04 13:46:35

用IP的对吗 两次不影响就没事 我这边就没影响 确实有时候会有两次

0 回复 有任何疑惑可以回复我~
  • 提问者 杰哥大大 #1
    用natapp内网穿透的二级域名,获取openid,token,insert消费记录,update什么的那些sql语句也跟着这样运行了两次,结果一扫码出现了重复的记录......
    回复 有任何疑惑可以回复我~ 2018-03-04 14:24:36
  • 翔仔 回复 提问者 杰哥大大 #2
    有可能是回发了两次,我用正常域名没有遇到这类问题呢,因为是微信那边的重发机制,这边没办法做出控制呢,可以看看是否在哪里重复访问了两次url,或者换成正规的域名,如果是学习目的的话先暂时忽略掉吧
    回复 有任何疑惑可以回复我~ 2018-03-04 15:12:23
  • 提问者 杰哥大大 回复 翔仔 #3
    检查了代码,可能是微信回发了两次,感觉只能弄个做个去重方法,积分*0.5....,扫奖品领取的二维码却只有回发了一次,我也搞不懂
    回复 有任何疑惑可以回复我~ 2018-03-04 15:20:28
翔仔 2018-03-04 13:22:34

是公网ip还是域名 ip可能由于验证什么的会有两次回发 这是微信本身的问题 不影响使用

0 回复 有任何疑惑可以回复我~
  • 提问者 杰哥大大 #1
    但是sql语句也运行了两次
    回复 有任何疑惑可以回复我~ 2018-03-04 13:24:27
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号