package org.adv.fw.modular.adv.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.adv.fw.core.util.DateUtil;
import org.adv.fw.core.util.MD5Util;
import org.adv.fw.modular.adv.model.ChatGroup;
import org.adv.fw.modular.adv.model.MsgData;
import org.adv.fw.modular.adv.model.Param;
import org.adv.fw.modular.adv.model.Wx;
import org.adv.fw.modular.adv.service.IChatGroupService;
import org.adv.fw.modular.adv.service.IParamService;
import org.adv.fw.modular.adv.service.IWxService;
import org.adv.fw.modular.system.model.User;
import org.adv.fw.modular.system.service.IUserService;
import org.adv.fw.modular.util.AmrToMp3;
import org.adv.fw.modular.util.ali.AliOSS;
import org.adv.fw.modular.util.wx.Message;
import org.adv.fw.modular.util.wx.WXPublicUtils;
import org.apache.commons.lang3.StringUtils;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.chat2.Chat;
import org.jivesoftware.smack.util.MD5;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@Controller
@RequestMapping("/wxIm")
public class WxIm {
@Resource
private IWxService wxService;
@Resource
private WXPublicUtils wxPublicUtils;
@Resource
private IChatGroupService chatgroupService;
@Resource
private MsgController msg;
@Resource
private IParamService paramService;
@Resource
private IUserService userservice;
int sort = 0;
String kf = "";
@RequestMapping("/receiveWxMsg")
@ResponseBody
public void receiveWxMsg(HttpServletRequest request, HttpServletResponse response)
throws IOException, WxErrorException {
// request.setCharacterEncoding("UTF-8");
// response.setCharacterEncoding("UTF-8");
// String echostr=request.getParameter("echostr");
// PrintWriter out = response.getWriter();
// out.print(echostr);
// out.close();
String token = "";
Wx wx = wxService.selectById(1);
if (wx.getExpires().getTime() < new Date().getTime()) {
token = wxPublicUtils.getAccessToken();
wx.setToken(token);
wx.setExpires(new Date((new Date().getTime()) + 5400000));
wxService.updateById(wx);
} else {
token = wx.getToken();
}
Message message = wxPublicUtils.receiveWxMsg(request, response, token);
// 此处写逻辑,接受的各种数据消息
HashMap<String, Object> map = new HashMap<>();
map.put("openid", message.getFromUserName());
List<ChatGroup> list = chatgroupService.selectByMap(map);
String roomid = "";
String nickname = "";
String customerId = "";
String openid =message.getFromUserName();
String avatar="";
// msgType 消息主题(MsgType:文本-text;图片-image;语音-voice;视频-video;)
try {
Param param = paramService.selectById(1);
if (list.size() == 0) {
// 获取用户信息
JSONObject json = wxPublicUtils.findUserInfo(token, message.getFromUserName());
if(json.getString("errcode")!=null)
{
token = wxPublicUtils.getAccessToken();
wx.setToken(token);
wx.setExpires(new Date((new Date().getTime()) + 5400000));
wxService.updateById(wx);
json = wxPublicUtils.findUserInfo(token, message.getFromUserName());
}
System.out.println(json.toJSONString());
customerId = "c" + System.currentTimeMillis();
if(StringUtils.isBlank(openid))
{
return ;
}
msg.register(customerId, openid);
// 创建房间,要根据配置,选
java微信公众号消息管理开发(接受与发送聊天信息)
最新推荐文章于 2023-05-30 12:10:37 发布