AccountRpcService.java 3.8 KB
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;
    }
}