WeAccessTokenInterceptor.java 4.0 KB
Newer Older
1 2
package com.linkwechat.wecom.interceptor;

3
import cn.hutool.core.util.StrUtil;
水库浪子 已提交
4
import cn.hutool.json.JSONUtil;
5 6 7 8
import com.dtflys.forest.exceptions.ForestRuntimeException;
import com.dtflys.forest.http.ForestRequest;
import com.dtflys.forest.http.ForestResponse;
import com.dtflys.forest.interceptor.Interceptor;
9
import com.dtflys.forest.utils.ForestDataType;
10
import com.linkwechat.common.config.WeComeConfig;
水库浪子 已提交
11
import com.linkwechat.common.constant.WeConstans;
水库浪子 已提交
12
import com.linkwechat.wecom.domain.dto.WeResultDto;
13
import com.linkwechat.wecom.service.IWeAccessTokenService;
fix  
孙喜旺 已提交
14
import lombok.extern.slf4j.Slf4j;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.stereotype.Component;
17

18 19
import java.util.Arrays;

20 21 22 23 24
/**
 * @description: 微信token拦截器
 * @author: HaoN
 * @create: 2020-08-27 22:36
 **/
fix  
孙喜旺 已提交
25
@Slf4j
26
@Component
27
public class WeAccessTokenInterceptor implements Interceptor{
28 29


30 31 32 33 34 35 36 37 38
    @Autowired
    private  IWeAccessTokenService iWeAccessTokenService;


    @Autowired
    private WeComeConfig weComeConfig;



39 40 41 42 43
    /**
     * 该方法在请求发送之前被调用, 若返回false则不会继续发送请求
     */
    @Override
    public boolean beforeExecute(ForestRequest request) {
44
        String uri=request.getUrl().replace("http://","");
fix  
孙喜旺 已提交
45
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>uri:{}",uri);
S
sunxiwang 已提交
46 47 48 49 50
        //request.setContentType("application/json");
        if(!Arrays.asList(weComeConfig.getFileUplodUrl()).contains(uri)){
            request.setDataType(ForestDataType.JSON);
            request.setContentType("application/json");
        }
51
        // 添加请求参数access_token
52 53 54 55 56 57 58
        if(!Arrays.asList(weComeConfig.getNoAccessTokenUrl()).contains(uri)){
            String token="";

            if(Arrays.asList(weComeConfig.getNeedContactTokenUrl()).contains(uri)){ //需要联系人token
                token=iWeAccessTokenService.findContactAccessToken();
            }else if(Arrays.asList(weComeConfig.getNeedProviderTokenUrl()).contains(uri)){ //需要供应商token
                token=iWeAccessTokenService.findProviderAccessToken();
59 60
            }else if(Arrays.asList(weComeConfig.getNeedChatTokenUrl()).contains(uri)){ //需要会话存档token
                token=iWeAccessTokenService.findChatAccessToken();
61
            }else  if(Arrays.asList(weComeConfig.getThirdAppUrl()).contains(uri)){ //第三方自建应用token
62 63 64 65 66 67 68 69


                token=iWeAccessTokenService.findThirdAppAccessToken(

                        StrUtil.isEmpty(request.getHeaderValue(WeConstans.THIRD_APP_PARAM_TIP))?(String) request.getQuery(WeConstans.THIRD_APP_PARAM_TIP):request.getHeaderValue(WeConstans.THIRD_APP_PARAM_TIP)

                );

70
            } else{
71 72
                token=iWeAccessTokenService.findCommonAccessToken();
            }
fix  
孙喜旺 已提交
73 74 75
            if (uri.contains("ticket/get")){
                request.addQuery("type","agent_config");
            }
76 77

            request.addQuery("access_token",token);
78 79 80 81
        }
        //添加服务器统一请求地址
        request.setUrl(weComeConfig.getServerUrl()+weComeConfig.getWeComePrefix()+uri);

82
        return true;
83 84 85 86 87 88 89 90 91 92 93 94 95
    }



    /**
     *  请求发送失败时被调用
     * @param e
     * @param forestRequest
     * @param forestResponse
     */
    @Override
    public void onError(ForestRuntimeException e, ForestRequest forestRequest, ForestResponse forestResponse) {

水库浪子 已提交
96

97 98
    }

99

100
    /**
水库浪子 已提交
101
     *  请求成功调用(微信端错误异常统一处理)
102 103 104 105 106 107
     * @param o
     * @param forestRequest
     * @param forestResponse
     */
    @Override
    public void onSuccess(Object o, ForestRequest forestRequest, ForestResponse forestResponse) {
fix  
孙喜旺 已提交
108
        log.info("url:【{}】,result:【{}】",forestRequest.getUrl(),forestResponse.getContent());
水库浪子 已提交
109
        WeResultDto weResultDto = JSONUtil.toBean(forestResponse.getContent(), WeResultDto.class);
水库浪子 已提交
110
        if(null != weResultDto.getErrcode() && !weResultDto.getErrcode().equals(WeConstans.WE_SUCCESS_CODE)&& !weResultDto.getErrcode().equals(WeConstans.NOT_EXIST_CONTACT) ){
水库浪子 已提交
111 112 113
            throw new ForestRuntimeException(forestResponse.getContent());
        }

114
    }
水库浪子 已提交
115 116


117
}