admin
2020-05-19 744594ef1a2f530fc3e86ea9dc48b62247f79420
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
package com.yeshi.fanli.util;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Component;
import org.yeshi.utils.JsonUtil;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yeshi.fanli.dto.pdd.PDDGoodsDetail;
import com.yeshi.fanli.entity.common.ImageInfo;
import com.yeshi.fanli.entity.jd.JDGoods;
import com.yeshi.fanli.entity.taobao.PidUser;
import com.yeshi.fanli.entity.taobao.TaoBaoGoodsBrief;
import com.yeshi.fanli.entity.taobao.TaoBaoShopInfo;
import com.yeshi.fanli.entity.taobao.TaoBaoUnionConfig;
import com.yeshi.fanli.exception.taobao.TaobaoGoodsDownException;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.service.inter.config.ConfigService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoShopService;
import com.yeshi.fanli.service.inter.taobao.TaoBaoUnionConfigService;
import com.yeshi.fanli.util.jd.JDApiUtil;
import com.yeshi.fanli.util.pinduoduo.PinDuoDuoApiUtil;
import com.yeshi.fanli.util.taobao.TaoBaoCouponUtil;
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
import com.yeshi.fanli.util.taobao.TaoKeApiUtil;
 
import net.sf.json.JSONArray;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;
 
//抢红包采用的redis
@Component
public class RedisManager {
 
    @Resource
    private JedisPool jedisPool;
 
    @Resource
    private TaoBaoShopService taoBaoShopService;
 
    @Resource
    private TaoBaoUnionConfigService taoBaoUnionConfigService;
 
    @Resource
    private ConfigService configService;
 
    /**
     * 缓存字符串
     * 
     * @param key
     * @param value
     */
    private void setString(String key, String value) {
        Jedis jedis = jedisPool.getResource();
        SetParams params=new SetParams().nx().ex(60);
        jedis.set(key, value, params);
        try {
            jedis.set(key, value);
        } finally {
            jedis.close();
        }
 
    }
 
    /**
     * 删除某个键值
     * 
     * @param key
     * @param value
     */
    private void removeKey(String key) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.del(key);
        } finally {
            jedis.close();
        }
 
    }
 
    /**
     * 缓存字符串
     * 
     * @param key
     * @param value
     * @param seconds
     *            -缓存时间(s)
     */
    private void setString(String key, String value, int seconds) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.setex(key, seconds, value);
        } finally {
            jedis.close();
        }
    }
 
    private String getString(String key) {
        Jedis jedis = jedisPool.getResource();
        try {
            return jedis.get(key);
        } finally {
            jedis.close();
        }
    }
 
    public void increase(String key) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.incr(key);
        } finally {
            jedis.close();
        }
    }
 
    public void expire(String key, int seconds) {
        Jedis jedis = jedisPool.getResource();
        try {
            jedis.expire(key, seconds);
        } finally {
            jedis.close();
        }
    }
 
    public void cacheCommonString(String key, String value, int seconds) {
        setString(key, value, seconds);
    }
 
    public void cacheCommonString(String key, String value) {
        setString(key, value);
    }
 
    public String getCommonString(String key) {
        return getString(key);
    }
 
    public void removeCommonString(String key) {
        removeKey(key);
    }
 
    /**
     * 临时存储淘宝的商品详情
     * 
     * @param goods
     */
    public void saveTaoBaoGoodsBriefTemp(TaoBaoGoodsBrief goods) {
        if (goods == null)
            return;
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoGoodsTemp, goods.getAuctionId() + "");
        if (Constant.IS_OUTNET) {
            // 暂存4个小时的分享
            cacheCommonString(key, JsonUtil.getSimpleGson().toJson(goods), 60 * 60 * 4);
        }
    }
 
    public TaoBaoGoodsBrief getTaoBaoGoodsTemp(Long auctionId) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoGoodsTemp, auctionId + "");
        String value = getCommonString(key);
        if (!StringUtil.isNullOrEmpty(value)) {
            return JsonUtil.getSimpleGson().fromJson(value, TaoBaoGoodsBrief.class);
        }
        return null;
    }
 
    /**
     * 删除缓存
     * 
     * @param auctionId
     */
    public void deleteTaoBaoGoodsBrief(Long auctionId) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoGoods, auctionId + "");
        if (Constant.IS_OUTNET)
            removeKey(key);
    }
 
    public TaoBaoGoodsBrief getTaoBaoGoodsBrief(long auctionId) throws TaobaoGoodsDownException {
        long startTime = System.currentTimeMillis();
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoGoods, auctionId + "");
        String value = "";
        if (Constant.IS_OUTNET)
            value = getCommonString(key);
 
        if (StringUtil.isNullOrEmpty(value)) {
            TaoBaoGoodsBrief goods = null;
 
            goods = TaoKeApiUtil.searchGoodsDetail(auctionId);
 
            if (goods != null)
                // 缓存20分钟
                if (Constant.IS_OUTNET)
                cacheCommonString(key, JsonUtil.getSimpleGson().toJson(goods), 60 * 20);
            LogHelper.test(auctionId + "-获取商品详情耗时:" + (System.currentTimeMillis() - startTime));
            return goods;
        } else {// 直接取缓存
            return JsonUtil.getSimpleGson().fromJson(value, TaoBaoGoodsBrief.class);
        }
    }
 
    public List<ImageInfo> getTaoBaoGoodsDetailImgs(long auctionId) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoGoodsDetailimgs, auctionId + "");
        String value = "";
        if (Constant.IS_OUTNET)
            value = getCommonString(key);
 
        if (StringUtil.isNullOrEmpty(value)) {
            List<ImageInfo> list = null;
            try {
                list = TaoBaoUtil.getTBDetailImageWithSizev2(auctionId, configService.getTaoBaoProxyIP());
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (list == null || list.size() == 0) {
                list = TaoBaoUtil.getTBDetailImageWithSize(auctionId);
            }
            if (list != null && list.size() > 0)
                // 缓存1天
                if (Constant.IS_OUTNET)
                cacheCommonString(key, JsonUtil.getSimpleGson().toJson(list), 60 * 60 * 24);
            return list;
        } else {// 直接取缓存
            List<ImageInfo> imgList = new Gson().fromJson(value, new TypeToken<List<ImageInfo>>() {
            }.getType());
            return imgList;
        }
    }
 
    public String getXCXCouponToken(TaoBaoGoodsBrief tb) {
        List<TaoBaoUnionConfig> configList = taoBaoUnionConfigService.getConfigByTypeCache(PidUser.TYPE_FANLI_ANDROID);
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoCoupleXCX, tb.getAuctionId() + "");
        String value = "";
        if (Constant.IS_OUTNET)
            value = getCommonString(key);
        if (StringUtil.isNullOrEmpty(value)) {
            value = TaoKeApiUtil.getTKToken(tb.getPictUrl(), tb.getTitle(), TaoBaoCouponUtil
                    .getCoupleUrl(tb.getCouponActivityId(), configList.get(0).getDefaultPid(), tb.getAuctionId() + ""));
            if (value != null)
                // 缓存20分钟
                if (Constant.IS_OUTNET)
                cacheCommonString(key, value, 60 * 20);
            return value;
        } else {// 直接取缓存
            return value;
        }
    }
 
    /**
     * IP访问频率限制 适用于 小程序,H5,WEB 同一IP,5s内限制请求100次
     * 
     * @param ip
     */
    public boolean ipFrequencyLimit(String ip, String apiName) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.emptyKey, ip + "-" + StringUtil.Md5(apiName));
        Jedis jedis = jedisPool.getResource();
        try {
            long count = jedis.incr(key);
            if (count == 1)
                jedis.expire(key, 5);
            if (count >= 10)
                return true;
            else
                return false;
        } finally {
            jedis.close();
        }
    }
 
    public boolean frequencyLimit(String key, int timeS, int num) {
        key = RedisKeyEnum.getRedisKey(RedisKeyEnum.frequencyLimit, key);
        Jedis jedis = jedisPool.getResource();
        try {
            long count = jedis.incr(key);
            if (count == 1)
                jedis.expire(key, timeS);
            if (count >= num)
                return true;
            else
                return false;
        } finally {
            jedis.close();
        }
    }
 
    /**
     * 获取淘宝图片
     * 
     * @param auctionId
     * @return
     */
    public List<String> getTBImg(Long auctionId) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoImgs, auctionId + "");
        String value = "";
        if (Constant.IS_OUTNET)
            value = getCommonString(key);
        if (StringUtil.isNullOrEmpty(value)) {
            List<String> list = TaoBaoUtil.getTbImg(auctionId + "");
            if (Constant.IS_OUTNET && list != null && list.size() > 0) {
                value = new Gson().toJson(list);
                cacheCommonString(key, value, 60 * 60 * 2);
            }
            return list;
        } else {
            JSONArray array = JSONArray.fromObject(value);
            List<String> list = new ArrayList<>();
            for (int i = 0; i < array.size(); i++) {
                list.add(array.getString(i));
            }
            return list;
        }
    }
 
    /**
     * 获取淘宝店铺信息
     * 
     * @param shopTitle
     * @param sellerId
     * @return
     */
    public TaoBaoShopInfo getTBShopInfo(String shopTitle, Long sellerId, Long auctionId) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoShop, sellerId +"");
        String value = "";
        if (Constant.IS_OUTNET)
            value = getCommonString(key);
 
        if (StringUtil.isNullOrEmpty(value)) {
            TaoBaoGoodsBrief goods = new TaoBaoGoodsBrief();
            goods.setShopTitle(shopTitle);
            goods.setSellerId(sellerId);
            goods.setAuctionId(auctionId);
            TaoBaoShopInfo info = taoBaoShopService.getTaoBaoShopInfo(goods);
 
            if (info != null) {
                String shopUrl = info.getShopUrl();
                if (shopUrl != null && shopUrl.contains("tmall://page.tm/shop")) {
                    shopUrl = "http://store.taobao.com/shop/view_shop.htm?user_number_id=" + sellerId;
                    info.setShopUrl(shopUrl);
                }
            }
 
            if (Constant.IS_OUTNET && info != null) {
                value = new Gson().toJson(info);
                cacheCommonString(key, value, 60 * 60 * 2);
            }
 
            return info;
        } else {
            TaoBaoShopInfo info = new Gson().fromJson(value, TaoBaoShopInfo.class);
 
            String shopUrl = info.getShopUrl();
            if (shopUrl != null && shopUrl.contains("tmall://page.tm/shop")) {
                shopUrl = "http://store.taobao.com/shop/view_shop.htm?user_number_id=" + sellerId;
                info.setShopUrl(shopUrl);
            }
 
            return info;
        }
    }
 
    /**
     * 是否限制发送短信
     * 
     * @param phone
     * @param type
     * @return
     */
    public boolean isSmsFrequencyLimit(String phone, int type) {
        if (!Constant.IS_OUTNET)
            return false;
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMS,  phone + "-" + type);
        String value = getCommonString(key);
        if (StringUtil.isNullOrEmpty(value))
            return false;
        else
            return true;
    }
 
    /**
     * 设置短信60s倒计时
     * 
     * @param phone
     * @param type
     */
    public void sendSms(String phone, int type) {
        if (!Constant.IS_OUTNET)
            return;
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMS,  phone + "-" + type);
        setString(key, "1", 10);
    }
 
    public void clearSMSFrequencyLimit(String phone, int type) {
        if (!Constant.IS_OUTNET)
            return;
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMS,  phone + "-" + type);
        removeKey(key);
    }
 
    /**
     * 保存验证码
     * 
     * @param phone
     * @param type
     * @param code
     */
 
    public void saveSMSVCode(String phone, int type, String code) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + type);
        // 保存2分钟
        setString(key, code, 120);
    }
 
    /**
     * 
     * @param phone
     * @param type
     * @return
     */
    public String getSMSVCode(String phone, int type) {
        // if (!Constant.IS_OUTNET)
        // return "";
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + type);
        // 保存2分钟
        return getString(key);
    }
 
    /**
     * 清除掉验证码
     * 
     * @param phone
     * @param type
     * @param code
     */
    public void clearSMSVCode(String phone, int type) {
        // if (!Constant.IS_OUTNET)
        // return;
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSVCode, phone + "-" + type);
        removeKey(key);
    }
 
    /**
     * 保存绑定支付宝短信验证码的正确性
     */
    public void saveBindAlipayAccountSMSState(String phone) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSStateAlipay, phone + "" );
        // 验证后十分钟有效
        setString(key, "1", 10 * 60);
    }
 
    /**
     * 绑定支付宝发送的短信是否在有效期内
     * 
     * @param phone
     * @return
     */
    public boolean isBindAlipayAccountSMSStateValid(String phone) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.SMSStateAlipay, phone + "" );
        return !StringUtil.isNullOrEmpty(getString(key));
    }
 
    /**
     * 缓存短连接1分钟
     * 
     * @param uid
     * @param shortlink
     */
    public void setInviteShortLink(long uid, String shortlink) {
        String value = "";
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.inviteShortLink, uid + "" );
        if (Constant.IS_OUTNET) {
 
            value = getCommonString(key);
 
            if (StringUtil.isNullOrEmpty(value)) {
                cacheCommonString(key, shortlink, 60);
            }
        }
 
    }
 
    /**
     * 获取用户短连接
     * 
     * @param uid
     * @return
     */
    public String getInviteShortLink(long uid) {
        String value = "";
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.inviteShortLink, uid + "" );
        if (Constant.IS_OUTNET)
            value = getCommonString(key);
 
        return value;
    }
 
    /**
     * 保存淘口令
     * 
     * @param auctionId
     * @param token
     */
    public void saveCommonTaoToken(Long auctionId, String token) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoTokenCommon, auctionId + "" );
        if (Constant.IS_OUTNET) {
            if (!StringUtil.isNullOrEmpty(token)) {
                // 口令缓存10天
                cacheCommonString(key, token, 60 * 60 * 24 * 10);
            }
        }
    }
 
    /**
     * 获取用户短连接
     * 
     * @param uid
     * @return
     */
    public String getCommonTaoToken(Long auctionId) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoTokenCommon, auctionId + "" );
        if (Constant.IS_OUTNET) {
            return getCommonString(key);
        }
        return null;
    }
 
    /**
     * 保存淘礼金的口令
     * 
     * @param url
     * @param token
     */
    public void saveTLJToken(String url, String token) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoTokenTLJ, StringUtil.Md5(url));
        if (Constant.IS_OUTNET) {
            if (!StringUtil.isNullOrEmpty(token)) {
                // 口令缓存10天
                cacheCommonString(key, token, 60 * 60 * 24 * 10);
            }
        }
    }
 
    /**
     * 获取淘礼金口令
     * 
     * @param url
     * @return
     */
    public String getTLJToken(String url) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.taobaoTokenTLJ, StringUtil.Md5(url));
        if (Constant.IS_OUTNET) {
            return getCommonString(key);
        }
        return null;
    }
 
    /**
     * 保存对象
     * 
     * @param T
     * @param key
     * @param seconds
     */
    public void saveObj(Object clazz, String key, Integer seconds) {
        if (clazz == null)
            return;
        String value = new Gson().toJson(clazz);
        if (seconds != null)
            cacheCommonString(key, value, seconds);
        else
            cacheCommonString(key, value);
    }
 
    /**
     * 保存列表
     * 
     * @param clazzList
     * @param key
     * @param seconds
     */
    public <T> T saveObjList(List<T> clazzList, String key, Integer seconds) {
        if (clazzList == null)
            return null;
        String value = new Gson().toJson(clazzList);
        if (seconds != null)
            cacheCommonString(key, value, seconds);
        else
            cacheCommonString(key, value);
        return null;
    }
 
    /**
     * 获取对象
     * 
     * @param clazz
     * @param key
     * @return
     */
    public Class<?> getObj(Class<?> clazz, String key) {
        String value = getCommonString(key);
        return (Class<?>) new Gson().fromJson(value, clazz);
    }
 
    /**
     * 获取列表
     * 
     * @param clazz
     * @param key
     * @return
     */
    public <T> List<T> getObjList(Class<T> clazz, String key) {
        String value = getCommonString(key);
        return JsonUtil.jsonToList(value, clazz);
    }
 
    public JDGoods getJDGoods(long goodsId) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.jingDongGoods, goodsId +"");
        String value = getCommonString(key);
        if (StringUtil.isNullOrEmpty(value)) {
            JDGoods jdGoods = JDApiUtil.queryGoodsDetail(goodsId);
            if (jdGoods == null) {
                jdGoods = JDApiUtil.getGoodsDetail(goodsId);
            }
 
            // 缓存20分钟
            if (jdGoods != null)
                cacheCommonString(key, JsonUtil.getSimpleGson().toJson(jdGoods), 60 * 20);
 
            return jdGoods;
        } else {// 直接取缓存
            return JsonUtil.getSimpleGson().fromJson(value, JDGoods.class);
        }
    }
 
    public PDDGoodsDetail getPDDGoodsDetail(long goodsId) {
        String key = RedisKeyEnum.getRedisKey(RedisKeyEnum.pinDuoDuoGoods, goodsId +"");
 
        String value = getCommonString(key);
        if (StringUtil.isNullOrEmpty(value)) {
            PDDGoodsDetail pddGoods = PinDuoDuoApiUtil.getGoodsDetail(goodsId);
 
            // 缓存20分钟
            if (pddGoods != null)
                cacheCommonString(key, JsonUtil.getSimpleGson().toJson(pddGoods), 60 * 20);
 
            return pddGoods;
        } else {// 直接取缓存
            return JsonUtil.getSimpleGson().fromJson(value, PDDGoodsDetail.class);
        }
    }
 
}