package com.ruoyi.web.controller.coffee;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.QRCodeUtil;
import com.ruoyi.framework.web.service.WeixinServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/weixin")
public class WeixinController {

    @Autowired
    WeixinServiceImpl weixinService;

    //这个就是那个使用传code进来的接口
    @GetMapping("/login")
    public AjaxResult login(@RequestParam("code") String code, @RequestParam("encryptedData") String encryptedData, @RequestParam("iv") String iv,@RequestParam(required = false) String source){
        AjaxResult ajax = AjaxResult.success();
        String token = weixinService.login(code,encryptedData,iv,source);
        ajax.put(Constants.TOKEN, token);
        return null;
    }

    @RequestMapping("/wxNotify")
    public void wxNotify(HttpServletRequest request,HttpServletResponse response){
        try{
            weixinService.wxNotify(request,response);
        }catch (Exception e){
            throw new ServiceException("微信回调发生异常");
        }
    }


}
