package com.ematou.wxservice.controller;

import com.alibaba.fastjson.JSONObject;
import com.ematou.wxservice.common.web.R;
import com.ematou.wxservice.entity.vo.BindingInfo;
import com.ematou.wxservice.entity.vo.TemplateMessage;
import com.ematou.wxservice.service.WeChatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author lhm
 * @version 1.0
 * 2021-05-13 11:56
 */
@RestController
public class WeChatController {

    @Autowired
    WeChatService weChatService;

    @PostMapping("/template/message")
    public R<?> sendTemplateMsg(@RequestBody TemplateMessage templateMessage){
        Integer i = weChatService.sendTemplateMsg(templateMessage);
        if (i==0) {
            return new R<String>().error("推送消息失败！");
        } else {
            return new R<String>().success();
        }
    }

    @GetMapping("/oauth2")
    public R<?> oauth2(@RequestParam("code") String code){
        Map<String, Object> map = weChatService.oauth2(code);
        return new R<>().success(map);
    }

    @PostMapping("jsSign")
    public R<?> jsSign(@RequestBody JSONObject obj){
        String url = obj.getString("url");
        Map<String, Object> map = weChatService.jsSign(url);
        return new R<>().success(map);
    }
}
