package com.pj.api.wx;

import cn.hutool.json.JSONUtil;
import com.pj.api.service.ApiService;
import com.pj.utils.sg.AjaxJson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("wx")
@RestController
@Slf4j
public class WxController {
    @Resource
    private WxService wxService;

    @RequestMapping(value = "notify")
    public void notify(HttpServletRequest request, HttpServletResponse response) throws Exception{
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");
        Writer writer = response.getWriter();
        Map<String,String>result=new HashMap<>();
        result.put("return_code", "SUCCESS");
        result.put("return_msg", "OK");
        wxService.WxNotify(request);
        writer.write(JSONUtil.toJsonStr(result));
        writer.flush();
        writer.close();
    }

    /**
     * 统一下单接口
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "pre-pay")
    public AjaxJson prePay(HttpServletRequest request) throws Exception {
        return AjaxJson.getSuccessData(wxService.prePay(request));
    }

    @GetMapping(value = "getRedirectUrl")
    public AjaxJson getRedirectUrl(String path,@RequestParam(required = false) String state){
        return AjaxJson.getSuccessData(wxService.getRedirectUrl(path,state));
    }
    @GetMapping(value = "getOpenidByCode")
    public AjaxJson getOpenidByCode(String code,@RequestParam(required = false) String openid){
        return AjaxJson.getSuccessData(wxService.getOpenidByCode(code,openid));
    }
    @GetMapping(value = "getWxConfig")
    public AjaxJson getWxConfig(String url){
        return AjaxJson.getSuccessData(wxService.getWxConfig(url));
    }
}
