package com.hc.web.config.util;


import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @version 1.0
 * @author: ZhengLiKun
 * @createTime: 2023-07-25  15:37
 * @description: 工具类
 */
public class WxUtil {

    /***
     * @author: zhaoyun
     * @createTime:
     * @description: 获取系统时间北京标准格式
     * @paeams: [当前时间加num, type：1为当前时间 type：2为当前时间+num]
     */
    public static String getExpireTime(Integer num, Integer type){
        ZonedDateTime now = ZonedDateTime.now();
        // 加十分钟
        ZonedDateTime tenMinutesLater = now.plusMinutes(num);

        // 创建自定义的日期时间格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX");

        // 格式化时间为指定格式
        if(type.equals(1)){
            return now.format(formatter);
        }else if(type.equals(2)){
            return tenMinutesLater.format(formatter);
        }
        return now.format(formatter);
    }

    /***
     * @author: zhaoyun
     * @createTime:
     * @description: 读取微信回调body
     */
    public static String getRequestBody(HttpServletRequest request) throws IOException {
        ServletInputStream stream;
        BufferedReader reader = null;
        StringBuffer sb = new StringBuffer();
        try {
            stream = request.getInputStream();
            // 获取响应
            reader = new BufferedReader(new InputStreamReader(stream));
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            throw new IOException("读取返回支付接口数据流出现异常！");
        } finally {
            reader.close();
        }
        return sb.toString();
    }


}