package com.icloud.server.wxserver;

import com.alibaba.fastjson.JSONObject;
import com.icloud.server.scala.engine.service.GoodsPayService;
import com.icloud.server.scala.engine.service.Goodspaye;
import com.icloud.server.web.JsonResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
 * @Author : yang
 * @Date : 2022-07-27 15:48
 * @Version : 2.1.2
 */

@Api( tags =  "获取openid" , value = "API",hidden = true )
@RestController
@RequestMapping("wx/")
@CrossOrigin
public class WxOpenid {

    @Autowired
    private GoodsPayService goodspayservice;
    /**
     * @Description 获取到 openID
     *  code 值是根据回调地址从微信获取的参数信息
     *  https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
     **/
    @RequestMapping("getopenid")
    public String getUsercode(String code,String goodsid, HttpServletRequest request){
//        String realmname = request.getHeader("Referer");
//        String r2=request.getHeader("realmname");
//        String url="";
//        if(realmname!=null){
//            url=realmname;
//        }else if(r2!=null){
//            url=r2;
//        }else {
//            return null;
//        }

        Goodspaye gp = goodspayservice.getGoodsPayById(goodsid);
        String appId=gp.payrealmname().appid();
        String appSecret=gp.payrealmname().secret();

        System.out.println("code=====:"+code);
        String url ="https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";
        // 替换 appid 和 secret
        // 转换连接请求为 GET 方式
        url = url.replace("APPID",appId).replace("SECRET",appSecret).replace("CODE",code);
        String json = String.valueOf(HttpRequestUtil.httpRequest(url,""));
        // 根据 Code 拿到 OpenId
        JSONObject obj = JSONObject.parseObject(json);
        // 在日志打印出报文
//        log.info(obj.toJSONString());
        // 拿到 openid
        String openId = obj.getString("openid");
        return openId;
    }

//    @GetMapping("/getopenid2")
//    @ResponseBody
//    public String getopenid(@RequestParam String code,@RequestParam String goodsid,HttpServletRequest request, HttpServletResponse response) {
//        System.out.println("====code:"+code);
//        System.out.println("---goodsid:"+goodsid);
//        Goodspaye gp = goodspayservice.getGoodsPayById(goodsid);
//        String appid="";
//        String secret="";
//        if(gp!=null){
//             appid=gp.payrealmname().appid();
//             secret=gp.payrealmname().secret();
//        }else {
//            appid="wx3c42b420d22aacbf";
//            secret="2f755b9edde90d8405285410475c82f1";
//            appid="wxb5a8b719332b6d5c";
//            secret="79f2011431d83d21c4c19273ce21388b";
//        }
//        response.setHeader("Access-Control-Allow-Origin", "*");
//        /*星号表示所有的域都可以接受，*/
//        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
//        String wxLoginUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
//        String param = "appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
//        String jsonString = GetPostUntil.sendGet(wxLoginUrl, param);
//        JSONObject json = JSONObject.parseObject(jsonString);
//        String openid = json.getString("openid");
//        System.out.println("###############" + openid);
//        return openid;
//
//    }

    @GetMapping("/getopenid2")
    @ResponseBody
    public JSONObject getopenid2(@RequestParam String code, @RequestParam String goodsid, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("====code:"+code);
        System.out.println("---goodsid:"+goodsid);
        Goodspaye gp = goodspayservice.getGoodsPayById(goodsid);
        String appid="";
        String secret="";
        if(gp!=null){
            appid=gp.payrealmname().appid();
            secret=gp.payrealmname().secret();
        }else {
            appid="wx3c42b420d22aacbf";
            secret="2f755b9edde90d8405285410475c82f1";
            appid="wxb5a8b719332b6d5c";
            secret="79f2011431d83d21c4c19273ce21388b";
        }
        response.setHeader("Access-Control-Allow-Origin", "*");
        /*星号表示所有的域都可以接受，*/
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        String wxLoginUrl = "https://api.weixin.qq.com/sns/oauth2/access_token";
        String param = "appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
        String jsonString = GetPostUntil.sendGet(wxLoginUrl, param);
        JSONObject json = JSONObject.parseObject(jsonString);
        String openid = json.getString("openid");
        System.out.println("###############" + openid);
        String refresh_token = json.getString("refresh_token");
        System.out.println("###############" + refresh_token);
        String access_token = json.getString("access_token");
        System.out.println("###############" + access_token);
        String scope = json.getString("scope");
        System.out.println("###############" + scope);
        return json;

//        return openid;

    }
//
//    @GetMapping("/getopenid3")
//    @ResponseBody
//    public JSONObject getopenid3( @RequestParam String access_token,@RequestParam String openid) {
//        String wxLoginUrl = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN";
//        String param = "access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
//        String jsonString = GetPostUntil.sendGet(wxLoginUrl, param);
//        JSONObject json = JSONObject.parseObject(jsonString);
//        return json;
//    }

    @GetMapping("/getopenid4")
    @ResponseBody
    public JSONObject getopenid4( @RequestParam String access_token,@RequestParam String openid) {
        //https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
        String wxLoginUrl = "https://api.weixin.qq.com/sns/userinfo";
        String param = "access_token=" + access_token + "&openid=" + openid + "&lang=zh_CN";
        String jsonString = GetPostUntil.sendGet(wxLoginUrl, param);
        JSONObject json = JSONObject.parseObject(jsonString);
        return json;
    }
}
