package cn.com.ctop.callback.controller;

import cn.com.ctop.callback.util.HttpUtils;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Controller
public class CallbackController {
    private Logger logger = LoggerFactory.getLogger(CallbackController.class);

    @ExceptionHandler({Exception.class})
    public String exception(Exception e) {
        e.printStackTrace();
        return e.getMessage();
    }

    @GetMapping("/kuaishouCallback")
    @ResponseBody
    public String kuaishouCallback(HttpServletRequest request, HttpServletResponse response, @RequestParam("auth_code") String authCode, @RequestParam("state") String state) {
        String result = null;
        try {
            JSONObject json = JSONObject.parseObject(state);
            if (json == null) {
                throw new Exception("回调信息为空");
            }
            Map<String, Object> map = new HashMap<>();
            map.put("auth_code", authCode);
            map.put("state", state);
            String channelType = json.getString("channelType");
            if (channelType.equals("jiaoyang")) {
                logger.info("骄阳回调开始");
                result = HttpUtils.httpGet("http://192.168.1.8:8805/jeecg-boot/kuaishou", map, null);

            } else {
                logger.info("有腾回调开始");
                result = HttpUtils.httpGet("http://129.28.197.250:8804/jeecg-boot/kuaishou", map, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = e.getMessage();
        }
        return result;
    }
}
