package com.doumee.core.wx; 
 | 
  
 | 
import com.alibaba.fastjson.JSONObject; 
 | 
import com.doumee.biz.system.SystemDictDataBiz; 
 | 
import com.doumee.core.utils.DateUtil; 
 | 
import com.doumee.core.utils.HttpsUtil; 
 | 
import com.doumee.dao.business.model.Bookings; 
 | 
import com.doumee.dao.business.model.Rooms; 
 | 
import com.doumee.dao.system.model.SystemUser; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.http.ResponseEntity; 
 | 
import org.springframework.stereotype.Service; 
 | 
import org.springframework.web.client.RestTemplate; 
 | 
  
 | 
import java.math.BigDecimal; 
 | 
import java.util.Date; 
 | 
import java.util.HashMap; 
 | 
import java.util.Map; 
 | 
  
 | 
/** 
 | 
 * Created by IntelliJ IDEA. 
 | 
 * 
 | 
 * @Author : Rk 
 | 
 * @create 2022/12/7 17:27 
 | 
 */ 
 | 
@Service 
 | 
@Slf4j 
 | 
public class SendWxMessage { 
 | 
  
 | 
    private static String programUrl = "packagesMine/meetingDetails/meetingDetails?id="; 
 | 
  
 | 
    @Autowired 
 | 
    private SystemDictDataBiz systemDictDataBiz; 
 | 
  
 | 
    /** 
 | 
     * 会议开始通知 
 | 
     * @param systemUser 用户 
 | 
     * @param bookings 会议信息 
 | 
     * @param rooms 会议室信息 
 | 
     */ 
 | 
    public  void  bookingsStart(SystemUser systemUser, Bookings bookings, Rooms rooms){ 
 | 
        RestTemplate restTemplate = new RestTemplate(); 
 | 
        String accessToken = systemDictDataBiz.getWxAccessToken(); 
 | 
  
 | 
        log.info("微信小程序 会议开始订阅消息发送日志 -> accessToken:{}",accessToken); 
 | 
        //这里简单起见我们每次都获取最新的access_token(时间开发中,应该在access_token快过期时再重新获取) 
 | 
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+accessToken; 
 | 
        //拼接推送的模版 
 | 
        WxMsgVO wxMsgVo = new WxMsgVO(); 
 | 
        //用户的openid(要发送给那个用户) 
 | 
        wxMsgVo.setTouser(systemUser.getOpenid()); 
 | 
        //订阅消息模板id 
 | 
        wxMsgVo.setTemplate_id("di_lWUtlqvWAo7aWKZCH8Kzs1Cv2z3H6fOkKI4RwJzg"); 
 | 
        Map<String, TemplateData> m = new HashMap<>(4); 
 | 
        //会议主题 
 | 
        m.put("thing1", new TemplateData(bookings.getName())); 
 | 
        //会议地点 
 | 
        m.put("thing2", new TemplateData(rooms.getName())); 
 | 
        //时间 
 | 
        m.put("thing3", new TemplateData(DateUtil.getDate(bookings.getStartTime(),"yyyy-MM-dd"))); 
 | 
        //主持人 
 | 
        m.put("name4", new TemplateData(systemUser.getRealname())); 
 | 
        wxMsgVo.setPage(programUrl +bookings.getId()); 
 | 
        wxMsgVo.setData(m); 
 | 
        ResponseEntity<String> responseEntity = 
 | 
                restTemplate.postForEntity(url, wxMsgVo, String.class); 
 | 
        log.info("微信小程序-> 取消通知订阅消息发送日志:{}",JSONObject.toJSONString(responseEntity)); 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 会议取消通知 
 | 
     * @param systemUser 用户 
 | 
     * @param bookings 会议信息 
 | 
     * @param rooms 会议室信息 
 | 
     */ 
 | 
    public  void  bookingsCancel(SystemUser systemUser, Bookings bookings, Rooms rooms){ 
 | 
        RestTemplate restTemplate = new RestTemplate(); 
 | 
        String accessToken =systemDictDataBiz.getWxAccessToken() ; 
 | 
        log.info("微信小程序 会议开始订阅消息发送日志 -> accessToken:{}",accessToken); 
 | 
        //这里简单起见我们每次都获取最新的access_token(时间开发中,应该在access_token快过期时再重新获取) 
 | 
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+accessToken; 
 | 
        //拼接推送的模版 
 | 
        WxMsgVO wxMsgVo = new WxMsgVO(); 
 | 
        //用户的openid(要发送给那个用户) 
 | 
        wxMsgVo.setTouser(systemUser.getOpenid()); 
 | 
        //订阅消息模板id 
 | 
        wxMsgVo.setTemplate_id("6A79DG8Fd9CjUDHhxJVv8dWcqww0v9_wwQLr7_ZuNqU"); 
 | 
        Map<String, TemplateData> m = new HashMap<>(4); 
 | 
        //会议名称 
 | 
        m.put("thing4", new TemplateData(bookings.getName())); 
 | 
        //会议时间 
 | 
        m.put("time2", new TemplateData(DateUtil.getDate(bookings.getStartTime(),"yyyy-MM-dd"))); 
 | 
//        m.put("time4", new TemplateData( 
 | 
//                DateUtil.getDate(bookings.getStartTime(),"yyyy-MM-dd")+ 
 | 
//                        " " + DateUtil.getDate(bookings.getStartTime(),"HH:mm")+ 
 | 
//                        " 至 " + DateUtil.getDate(bookings.getEndTime(),"HH:mm") ) ); 
 | 
        //会议室 
 | 
        m.put("thing1", new TemplateData(rooms.getName())); 
 | 
        //备注 
 | 
        m.put("thing3", new TemplateData(bookings.getRemark())); 
 | 
        wxMsgVo.setPage(programUrl +bookings.getId()); 
 | 
        wxMsgVo.setData(m); 
 | 
        ResponseEntity<String> responseEntity = 
 | 
                restTemplate.postForEntity(url, wxMsgVo, String.class); 
 | 
        log.info("微信小程序->取消通知订阅消息发送日志:{}",JSONObject.toJSONString(responseEntity)); 
 | 
    } 
 | 
  
 | 
  
 | 
    /** 
 | 
     * 会议预约成功通知 
 | 
     * @param systemUser 用户 
 | 
     * @param bookings 会议信息 
 | 
     * @param rooms 会议室信息 
 | 
     */ 
 | 
    public void  bookingsReservation(SystemUser systemUser, Bookings bookings, Rooms rooms){ 
 | 
        RestTemplate restTemplate = new RestTemplate(); 
 | 
        String accessToken = systemDictDataBiz.getWxAccessToken() ; 
 | 
        log.info("微信小程序 会议开始订阅消息发送日志 -> accessToken:{}",accessToken); 
 | 
  
 | 
        //这里简单起见我们每次都获取最新的access_token(时间开发中,应该在access_token快过期时再重新获取) 
 | 
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+accessToken; 
 | 
        //拼接推送的模版 
 | 
        WxMsgVO wxMsgVo = new WxMsgVO(); 
 | 
        //用户的openid(要发送给那个用户) 
 | 
        wxMsgVo.setTouser(systemUser.getOpenid()); 
 | 
        //订阅消息模板id 
 | 
        wxMsgVo.setTemplate_id("usZxJqJ2AYPg24ViPMuY2lI-YRjdSD2JvFY3gY5kqh4"); 
 | 
        Map<String, TemplateData> m = new HashMap<>(4); 
 | 
        //会议主题 
 | 
        m.put("thing3", new TemplateData(bookings.getName())); 
 | 
        //会议时间 
 | 
        m.put("time1", new TemplateData( 
 | 
                DateUtil.getDate(bookings.getStartTime(),"yyyy-MM-dd") 
 | 
//                        + 
 | 
//                        " " + DateUtil.getDate(bookings.getStartTime(),"HH:mm")+ 
 | 
//                        " 至 " + DateUtil.getDate(bookings.getEndTime(),"HH:mm") 
 | 
        ) ); 
 | 
        //会议地点 
 | 
        m.put("thing2", new TemplateData(rooms.getName())); 
 | 
        //发起人 
 | 
        m.put("thing4", new TemplateData(systemUser.getRealname())); 
 | 
        wxMsgVo.setPage(programUrl +bookings.getId()); 
 | 
        wxMsgVo.setData(m); 
 | 
        ResponseEntity<String> responseEntity = 
 | 
                restTemplate.postForEntity(url, wxMsgVo, String.class); 
 | 
        log.info("微信小程序-> 会议预约成功订阅消息发送日志:{}",JSONObject.toJSONString(responseEntity)); 
 | 
    } 
 | 
  
 | 
  
 | 
  
 | 
//    /** 
 | 
//     * 获取token 
 | 
//     */ 
 | 
//    private static String getAccessToken() { 
 | 
//        //发送请求获取token 
 | 
//        JSONObject token = null; 
 | 
//        try { 
 | 
//            token = JSONObject.parseObject(HttpsUtil.get(APP_ACCESS_TOKEN_URL,false)); 
 | 
//        } catch (Exception e) { 
 | 
//            e.printStackTrace(); 
 | 
//        } 
 | 
//        JSONObject jsonObject = token; 
 | 
//        String accessToken = (String) jsonObject.get("access_token"); 
 | 
//        Integer expiresIn = (Integer) jsonObject.get("expires_in"); 
 | 
//        //创建token对象,并存储 
 | 
//        return new AccessToken(accessToken,String.valueOf(expiresIn)).getAccessToken(); 
 | 
//    } 
 | 
  
 | 
} 
 |