AccountRpcService.java
3.8 KB
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
package cn.fw.freya.service.rpc;
import cn.fw.freya.common.BusinessException;
import cn.fw.freya.common.PathConstant;
import cn.fw.freya.enums.AccountTypeEnum;
import cn.fw.freya.model.dto.rpc.AccountDto;
import cn.fw.freya.service.data.AccountService;
import cn.fw.freya.utils.RequestUtil;
import cn.fw.freya.utils.http.HttpConfig;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.*;
/**
* @author kurisu
* @date 2021-11-30 14:56
* @description 账号RPC服务, 上报失效账号, 从服务器拉取有效账号
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class AccountRpcService {
@Getter
@Value("${freya.url}")
private String baseUrl;
private final AccountService accountService;
/**
* 账号cookie失效, 上报服务器
*
* @param accountNo 账号
* @param type 账户类型(1:快手, 2:抖音, 3:懂车帝, 4:Bilibili)
* @return 结果
*/
public boolean pushExpireAccount(String accountNo, Integer type) {
Map<String, Object> params = new HashMap<>();
params.put("account", accountNo);
params.put("type", type);
HttpConfig config = HttpConfig.custom()
.url(getBaseUrl() + PathConstant.EXPIRE_ACCOUNT)
.map(params);
String res = RequestUtil.put(config);// 发送put请求
System.out.println(res);
JSONObject resObj = JSONObject.parseObject(res);
Boolean result = resObj.getBoolean("success");
if (Boolean.FALSE.equals(result)) {
Integer status = Optional.ofNullable(resObj.getInteger("status")).orElse(-1);
String message = Optional.ofNullable(resObj.getString("message")).orElse("账号cookie失效上报失败");
throw new BusinessException(status, message);
}
accountService.updateAccountCookiesStatus(accountNo, type, false);
return true;
}
/**
* 根据账户类型, 从服务器同步账号
*
* @param type 账户类型(1:快手, 2:抖音, 3:懂车帝, 4:Bilibili)
* @return
*/
public List<AccountDto> pullAccountFromServer(AccountTypeEnum type) {
Map<String, Object> params = new HashMap<>();
params.put("type", type.getValue());
List<AccountDto> list = new ArrayList<>();
try {
HttpConfig config = HttpConfig.custom()
.url(getBaseUrl() + PathConstant.PULL_ACCOUNT)
.setGetParams(params);
String res = RequestUtil.get(config);// 发送get请求
JSONObject resObj = JSONObject.parseObject(res);
Boolean result = resObj.getBoolean("success");
if (Boolean.FALSE.equals(result)) {
log.info("同步账号请求结果:{}", res);
Integer status = Optional.ofNullable(resObj.getInteger("status")).orElse(-1);
String message = Optional.ofNullable(resObj.getString("message")).orElse("查询账号失败");
throw new BusinessException(status, message);
}
JSONArray jsonArray = Optional.ofNullable(resObj.getJSONArray("data")).orElse(new JSONArray());
jsonArray.forEach(item -> {
final JSONObject obj = (JSONObject) item;
final AccountDto dto = new AccountDto();
dto.setAccountNo(obj.getString("account"));
dto.setPlaybackSearchKey(obj.getString("playbackSearchKey"));
dto.setGroupId(obj.getLong("groupId"));
list.add(dto);
});
} catch (Exception e) {
return null;
}
return list;
}
}