package com.gree.mall.miniapp.logic.common.outside;

import com.gree.mall.miniapp.exception.RemoteServiceException;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.utils.ApplicationContextUtils;
import com.gree.mall.miniapp.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.HashMap;
import java.util.Map;

public class OutsideBaseApi {

    @Value("${pc.server.url}")
    protected String pcUrl;
    @Autowired
    protected CommonLogic commonLogic;


    public <T> T checkData(ResponseHelper<T> result){
        if(result.getCode() != ResponseHelper.ResponseCode_Success){
            String errMsg = result.getMessage().replace("温馨提示：", "");
            throw new RemoteServiceException(result.getCode(),errMsg);
        }
        return result.getData();
    }


    public Map<String,String> getHeader(){
        String token = CommonUtils.getToken(ApplicationContextUtils.getHttpServletRequest());
        String source = CommonUtils.getSource(ApplicationContextUtils.getHttpServletRequest());
        Map<String,String> header = new HashMap<>();
        header.put("x-token",token);
        header.put("source",source);
        return header;
    }

}
