Commit 044c001f by 吕明尚

修改抖音验卷准备逻辑

parent a7988a71
......@@ -4,6 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import share.system.domain.vo.TiktokCouponDto;
import share.system.service.TiktokService;
import java.util.List;
......@@ -14,18 +15,34 @@ public class TiktokController {
@Autowired
private TiktokService tiktokService;
//权限限验证接口,返回client_token,用于调用其他接口,如果不需要,可以不用这个接口
@GetMapping("/oauth/clientToken")
public void clientToken() {
tiktokService.clientToken();
}
//验卷准备接口,返回code,用于调用其他接口
@GetMapping("/certificate/prepare")
public String prepare(String code, String poiId) {
return tiktokService.prepare(code, poiId);
public String prepare(TiktokCouponDto tiktokCouponDto) {
return tiktokService.prepare(tiktokCouponDto);
}
//验卷接口,返回验卷结果
@GetMapping("/certificate/verify")
public String verify(String verifyToken, String poiId, List<String> encryptedCodes) {
return tiktokService.verify(verifyToken, poiId, encryptedCodes);
public String verify(TiktokCouponDto tiktokCouponDto) {
return tiktokService.verify(tiktokCouponDto);
}
//撤销核销接口,返回撤销结果
@GetMapping("/certificate/cancel")
public String cancel(TiktokCouponDto tiktokCouponDto) {
return tiktokService.cancel(tiktokCouponDto);
}
@GetMapping("/certificate/get)")
public String certificateGet() {
return "";
}
}
package share.system.domain.vo;
import lombok.Data;
import java.util.List;
@Data
public class TiktokCouponDto {
//短链接
private String encryptedData;
//劵码
private String code;
//门店id
private String poiId;
//验券准备返回的标识
private String verifyToken;
//验券准备接口返回的加密抖音券码
private List<String> encryptedCodes;
//验卷接口返回 券码一次核销的唯一标识
private String verifyId;
//券码的标识(验券时返回)
private String certificateId;
//验券准备接口返回的加密券码
private String encryptedCode;
}
package share.system.service;
import share.system.domain.vo.TiktokCouponDto;
import java.util.List;
public interface TiktokService {
void clientToken();
String prepare(String code, String poiId);
String prepare(TiktokCouponDto tiktokCouponDto);
String verify(TiktokCouponDto tiktokCouponDto);
String verify(String verifyToken, String poiId, List<String> encryptedCodes);
String cancel(TiktokCouponDto tiktokCouponDto);
}
......@@ -2,6 +2,7 @@ package share.system.service.impl;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpGlobalConfig;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
......@@ -13,6 +14,7 @@ import share.common.core.redis.RedisUtil;
import share.common.enums.ErrorCodeEnum;
import share.common.enums.ReceiptRdeisEnum;
import share.common.enums.VerifyResultsEnum;
import share.system.domain.vo.TiktokCouponDto;
import share.system.service.TiktokService;
import java.util.Date;
......@@ -63,13 +65,25 @@ public class TiktokServiceImpl implements TiktokService {
}
@Override
public String prepare(String code, String poiId) {
public String prepare(TiktokCouponDto tiktokCouponDto) {
String accessToken = obtain();
String url = "https://open.douyin.com/goodlife/v1/fulfilment/certificate/prepare/";
String result = HttpRequest.get(url)
String result = "";
//判断短链接是否以https://v.douyin.com/开头,/结尾
if (tiktokCouponDto.getEncryptedData().startsWith("https://v.douyin.com/") && tiktokCouponDto.getEncryptedData().endsWith("/")) {
String encryptedData = HttpRequest.get(tiktokCouponDto.getEncryptedData()).timeout(HttpGlobalConfig.getTimeout()).execute().body();
String objectId = result.substring(encryptedData.indexOf("object_id=") + "object_id=".length(), encryptedData.indexOf("&amp;"));
result = HttpRequest.get(url)
.contentType("application/json")
.header("access-token", accessToken)
.form("code", code).form("poi_id", poiId).execute().body();
.form("encrypted_data", objectId).form("poi_id", tiktokCouponDto.getPoiId()).execute().body();
} else {
result = HttpRequest.get(url)
.contentType("application/json")
.header("access-token", accessToken)
.form("code", tiktokCouponDto.getCode()).form("poi_id", tiktokCouponDto.getPoiId()).execute().body();
}
JSONObject entries = new JSONObject(result);
JSONObject data = entries.getJSONObject("data");
if (!data.getStr("error_code").equals(ErrorCodeEnum.SUCCESS.getCode())) {
......@@ -79,14 +93,19 @@ public class TiktokServiceImpl implements TiktokService {
return data.getStr("verify_token");
}
public static void main(String[] args) {
String url = "<a href=\"https://www.iesdouyin.com/share/commerce/coupon/I0duaGdVYnp0RitYekxkNm11VHl3WUVCM2wvWXNmNEZDcXZ3aGZDYm9ka3R2VjdualNOT1VGVzMxUkxmdWowTVp2VWVBams1OFAyK3YrMm4yc0lhQm90a0hPb1ZNRFk5V3VxTkhIV3hZTHQyelByMFJ5R2YzSy9FRjZXNDgvMTBXVno1NW5MaFFOYzRQbnFtRExpZGF5YzI4aTdBK3d6dnk3eFBUTVd2VkRRZkM/?schema_type=13&amp;object_id=I0duaGdVYnp0RitYekxkNm11VHl3WUVCM2wvWXNmNEZDcXZ3aGZDYm9ka3R2VjdualNOT1VGVzMxUkxmdWowTVp2VWVBams1OFAyK3YrMm4yc0lhQm90a0hPb1ZNRFk5V3VxTkhIV3hZTHQyelByMFJ5R2YzSy9FRjZXNDgvMTBXVno1NW5MaFFOYzRQbnFtRExpZGF5YzI4aTdBK3d6dnk3eFBUTVd2VkRRZkM&amp;utm_campaign=client_scan_share&amp;app=aweme&amp;utm_medium=ios&amp;tt_from=scan_share&amp;iid=&amp;utm_source=scan_share\">Found</a>.";
//截取中间的object_id=到&amp;之间的字符串
String objectId = url.substring(url.indexOf("object_id=") + "object_id=".length(), url.indexOf("&amp;"));
}
@Override
public String verify(String verifyToken, String poiId, List<String> encryptedCodes) {
public String verify(TiktokCouponDto tiktokCouponDto) {
String accessToken = obtain();
String url = "https://open.douyin.com/goodlife/v1/fulfilment/certificate/verify/";
Map<String, Object> map = new HashMap<>();
map.put("verify_token", verifyToken);
map.put("poi_id", poiId);
map.put("encrypted_codes", encryptedCodes);
map.put("verify_token", tiktokCouponDto.getVerifyToken());
map.put("poi_id", tiktokCouponDto.getPoiId());
map.put("encrypted_codes", tiktokCouponDto.getEncryptedCodes());
JSONObject jsonObject = new JSONObject(map);
String result = HttpRequest.post(url)
.contentType("application/json")
......@@ -100,11 +119,32 @@ public class TiktokServiceImpl implements TiktokService {
} else if (data.getStr("error_code").equals("1208") || data.getJSONObject("verify_results").getStr("result").equals(VerifyResultsEnum.COOKED.getCode())) {
return "验卷成功";
} else if (!data.getStr("error_code").equals(ErrorCodeEnum.SUCCESS.getCode())) {
compensate(verifyToken, poiId, encryptedCodes);
compensate(tiktokCouponDto);
}
return "验卷失败";
}
@Override
public String cancel(TiktokCouponDto tiktokCouponDto) {
String accessToken = obtain();
String url = "https://open.douyin.com/goodlife/v1/fulfilment/certificate/cancel/";
Map<String, Object> map = new HashMap<>();
map.put("verify_id", tiktokCouponDto.getVerifyId());
map.put("certificate_id", tiktokCouponDto.getCertificateId());
JSONObject jsonObject = new JSONObject(map);
String result = HttpRequest.post(url)
.contentType("application/json")
.header("access-token", accessToken)
.body(jsonObject.toString()).execute().body();
JSONObject entries = new JSONObject(result);
JSONObject data = entries.getJSONObject("data");
if (!data.getStr("error_code").equals(ErrorCodeEnum.SUCCESS.getCode())) {
logger.error("抖音撤销核销失败:{}", data.getStr("description"));
throw new RuntimeException(data.getStr("description"));
}
return data.getStr("description");
}
public String obtain() {
String o;
o = redisUtil.get(ReceiptRdeisEnum.TIKTOK_CLIENT_TOKEN.getValue());
......@@ -117,11 +157,11 @@ public class TiktokServiceImpl implements TiktokService {
return sessionKey.getStr("accessToken");
}
public void compensate(String verifyToken, String poiId, List<String> encryptedCodes) {
public void compensate(TiktokCouponDto tiktokCouponDto) {
//间隔5秒
try {
Thread.sleep(5000);
verify(verifyToken, poiId, encryptedCodes);
verify(tiktokCouponDto);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment