package com.qiaoba.common.web.util;

import cn.hutool.http.ContentType;
import cn.hutool.json.JSONUtil;
import com.qiaoba.common.base.constant.BaseConstant;
import com.qiaoba.common.base.result.AjaxResult;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * ResponseUtil
 *
 * @author ailanyin
 * @version 1.0
 * @since 2023-04-25 22:48:43
 */
public class ResponseUtil {

    public static void response(HttpServletResponse response, String msg) throws IOException {
        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType(ContentType.JSON.getValue());
        response.setCharacterEncoding(BaseConstant.UTF8);
        PrintWriter writer = response.getWriter();
        writer.write(msg);
        writer.close();
    }

    public static void errorAuth(HttpServletResponse response, Integer code, String msg) throws IOException {
        AjaxResult result = AjaxResult.error(code, msg);
        response(response, JSONUtil.toJsonStr(result));
    }
}
