package com.ruoyi.web.controller.wx;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.wx.AppletLoginForm;
import com.ruoyi.system.domain.wx.AppletSessionDTO;
import com.ruoyi.system.domain.wx.WxPayOrderReqVo;
import com.ruoyi.system.domain.wx.WxPayRespVo;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.IWxPayService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import utils.WxCodeSessionUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author: tjf
 * @Date: 2024/1/17 14:10
 * @Describe:
 */
@RestController
@RequestMapping("/wx/pay")
public class WxPayController extends BaseController {

    @Resource
    private IWxPayService wxPayService;

    @ApiOperation(value = "微信预支付", notes = "微信预支付")
    @PostMapping("/createOrder")
    public AjaxResult createOrder(@RequestBody @Validated WxPayOrderReqVo req) throws Exception {
        return wxPayService.createOrder(req);
    }

    @ApiOperation(value = "微信支付回调", notes = "微信支付回调")
    @PostMapping("/payNotify")
    public AjaxResult payNotify(HttpServletRequest request){
        //注意：回调接口需要暴露到公网上，且要放开token验证
        wxPayService.payNotify(request);
        return AjaxResult.success();
    }

    /**
     * 小程序登录
     * @param  form
     * @return
     */
    @PostMapping("/jsCode")
    public AjaxResult code2Session(@RequestBody AppletLoginForm form){
        AppletSessionDTO appletSessionDTO = WxCodeSessionUtil.jscode2Session(form);
        return AjaxResult.success(appletSessionDTO);
    }
}
