java微信公众号消息管理开发(接受与发送聊天信息)

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);

				// 创建房间,要根据配置,选
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值