package com.icloud.server.wxserver;

import com.alibaba.fastjson.JSONObject;
//import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;

/** 网页授权获取用户 openID ----有了公众号后开发
 * @Author : yang
 * @Date : 2022-04-30 12:56 AM
 * @Version : 2.1.2
 */

@RestController
@RequestMapping("wxLogin/")
@CrossOrigin
public class WxLogin {

    // 引入日志文件
//    static Logger log = Logger.getLogger(WxLogin.class.getClass());

    // 获取定义的 appId
    private String appId="wx3c42b420d22aacbf";

    // 获取定义的 appSecret
    private String appSecret="2f755b9edde90d8405285410475c82f1";

    /**
     * 微信登录地址
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "login", method = {RequestMethod.POST,RequestMethod.GET})
    public String login(HttpServletResponse response, HttpServletRequest request) throws IOException {
        String wxurl = getRedirectUrl(request);
        return "redirect:"+wxurl;
    }

   /**
    * @Description:获取 code 信息,网页授权不需要 token
    * @Author: yang
    * @Date: 2022-04-30 12:58 AM
    *
    * @param request
    *
    * @return java.lang.String
    **/
    private String getRedirectUrl(HttpServletRequest request){
        // 获取 Code 信息
        String wxurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+appId+"&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
//        log.info(wxurl);
        // 拼接回调地址，必须是微信白名单才可以！！！
        String ecodeurl = URLEncoder.encode("https://shanghailog.forcetv.com.cn:8092/wxLogin/getUsercode");
//        String ecodeurl = URLEncoder.encode("https://填写你配置的回调域名/rescue/wechatlogin/getUsercode");
        wxurl=wxurl.replaceAll("REDIRECT_URI",ecodeurl);
//        log.info("Code："+wxurl);
        return wxurl;
    }

    /**
     * @Description 获取到 openID
     *  code 值是根据回调地址从微信获取的参数信息
     *  https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html
     **/
    @RequestMapping("getUsercode")
    public String getUsercode(String code, HttpSession session){
        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;
    }


}
