Pu Zhibing
2 天以前 41422cb74d137e590fefefa4390cd6eedc02a0fb
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
package com.ruoyi.order.util.douyin;
 
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.douyin.openapi.client.Client;
import com.douyin.openapi.client.models.*;
import com.douyin.openapi.credential.models.Config;
import lombok.extern.slf4j.Slf4j;
 
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List; /**
 * @author zhibing.pu
 * @Date 2025/6/16 10:28
 */
@Slf4j
public class VerifyUtil {
    
    
    /**
     * 验券准备
     * @param code  短链地址
     * @return
     */
    public static CertificatePrepareResponseData certificatePrepare(String code){
        //判断token是否过期
        long now = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        if(ClientTokenUtil.expiration_time < now){
            ClientTokenUtil.getClientToken();
        }
        try {
            //用户短链地址获取长链地址,获取encryptedData参数
            HttpRequest get = HttpUtil.createGet(code);
            HttpResponse execute = get.execute();
            int status = execute.getStatus();
            String encryptedData = "";
            if(302 == status){
                String location = execute.header("Location");
                location = location.substring(location.indexOf("object_id=") + 1);
                location = location.substring(0, location.indexOf("&"));
                encryptedData = location;
            }
            
            Config config = new Config().setClientKey(DouyinConfig.CLIENT_KEY).setClientSecret(DouyinConfig.CLIENT_SECRET); // 改成自己的app_id跟secret
            Client client = new Client(config);
            CertificatePrepareRequest request = new CertificatePrepareRequest();
            request.setAccessToken(ClientTokenUtil.token);
            request.setEncryptedData(encryptedData);
            CertificatePrepareResponse response = client.CertificatePrepare(request);
            CertificatePrepareResponseExtra extra = response.getExtra();
            Integer errorCode = extra.getErrorCode();
            if(0 != errorCode){
                log.error("【抖音】验券准备失败");
                return null;
            }
            return response.getData();
        }catch (Exception e) {
            log.error("【抖音】验券准备失败");
            e.printStackTrace();
        }
        return null;
    }
    
    
    /**
     * 验券
     * @param poiId 抖音门店id
     * @param encryptedCodes    加密券码
     * @return
     */
    public static List<CertificateVerifyResponseDataVerifyResultsItem> certificateVerify(String verify_token, String poiId, List<String> encryptedCodes){
        //判断token是否过期
        long now = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        if(ClientTokenUtil.expiration_time < now){
            ClientTokenUtil.getClientToken();
        }
        try {
            Config config = new Config().setClientKey(DouyinConfig.CLIENT_KEY).setClientSecret(DouyinConfig.CLIENT_SECRET); // 改成自己的app_id跟secret
            Client client = new Client(config);
            CertificateVerifyRequest request = new CertificateVerifyRequest();
            request.setAccessToken(ClientTokenUtil.token);
            request.setVerifyToken(verify_token);
            request.setPoiId(poiId);
            request.setEncryptedCodes(encryptedCodes);
            CertificateVerifyResponse response = client.CertificateVerify(request);
            CertificateVerifyResponseExtra extra = response.getExtra();
            Integer errorCode = extra.getErrorCode();
            if(0 != errorCode){
                log.error("【抖音】验券失败");
                return null;
            }
            CertificateVerifyResponseData data = response.getData();
            List<CertificateVerifyResponseDataVerifyResultsItem> verifyResults = data.getVerifyResults();
            return verifyResults;
        }catch (Exception e) {
            log.error("【抖音】验券失败");
            e.printStackTrace();
        }
        return null;
    }
}