来源:自学PHP网 时间:2015-04-14 12:58 作者: 阅读:次
[导读] package cn com shdmt service;import cn com shdmt entity ConcernedCustomerEntity;import cn com shdmt entity CustomerEntity;import cn com shdmt entity WeXinEventEntity;import cn com shdmt...
package cn.com.shdmt.service; import cn.com.shdmt.entity.ConcernedCustomerEntity; import cn.com.shdmt.entity.CustomerEntity; import cn.com.shdmt.entity.WeXinEventEntity; import cn.com.shdmt.entity.WelMsgEntity; import cn.com.shdmt.framework.util.MessageUtil; import cn.com.shdmt.framework.wexinmessage.Article; import cn.com.shdmt.framework.wexinmessage.NewsMessage; import cn.com.shdmt.framework.wexinmessage.TextMessage; import cn.com.shdmt.repository.ConcernedCustomerDao; import cn.com.shdmt.repository.CustomDao; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; import java.sql.Timestamp; import java.util.*; /** * Created by yaoyao on 2015/3/13. */ @Component @Transactional public class WeXinCoreService { @Autowired private ConcernedCustomerDao concernedCustomerDao; @Autowired private AccessTokenService accessTokenService; @Autowired private CustomDao customDao; @Autowired private WeXinEventService weXinEventService; @Autowired private CodeService codeService; @Autowired private SmsService smsService; @Autowired private WelMsgService welMsgService; @Value("#{appProperties['upload.config.fileURLPrefix']}") private String fileURLPrefix; public String processRequest(String msg) { String respMessage = null; try { // 默认返回的文本消息内容 String respContent = "请求处理异常,请稍候尝试!"; // xml请求解析 Map<String, String> requestMap = MessageUtil.parseXml(msg); System.out.println("Event==" + requestMap.get("Event")); // 发送方帐号(open_id) String open_id = requestMap.get("FromUserName"); // 公众帐号 String toUserName = requestMap.get("ToUserName"); // 消息类型 String msgType = requestMap.get("MsgType"); // 回复文本消息 TextMessage textMessage = new TextMessage(); textMessage.setToUserName(open_id); textMessage.setFromUserName(toUserName); textMessage.setCreateTime(new Date().getTime()); textMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_TEXT); textMessage.setFuncFlag(0); // 文本消息 if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) { String content = requestMap.get("Content"); //是否为绑定号码 WeXinEventEntity weXinEventEntity = weXinEventService.getAllWeXinEvent(open_id); if (weXinEventEntity != null) { if (!StringUtils.isBlank(content)) { //获取 微信用户 List<ConcernedCustomerEntity> concernedCustomerEntities = concernedCustomerDao.findByOpenidAndType(open_id, 1); //根据用户名密码查询 if (StringUtils.isEmpty(weXinEventEntity.getValue())) { if (concernedCustomerEntities.size() > 0) { //保存他要绑定的登录名称 weXinEventEntity.setValue(content); weXinEventService.save(weXinEventEntity); respContent = "请输入你要绑定的用户名的密码"; } } else { //他是在绑定数据 已完成第一步 根据这个openid 查询出用户名 并得到密码 List<CustomerEntity> customerEntities = customDao.findByCustomerCodeAndPassword(weXinEventEntity.getValue(), content); if (customerEntities.size() > 0 && concernedCustomerEntities.size() > 0) { CustomerEntity customer = customerEntities.get(0); customer.setConcernedCustomer(concernedCustomerEntities.get(0)); respContent = "绑定成功"; } else { respContent = "绑定失败,用户名和密码不匹配"; } } } } else { respContent = "你发送的消息是" + content; } textMessage.setContent(respContent); respMessage = MessageUtil.textMessageToXml(textMessage); } // 图片消息 else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_IMAGE)) { //如果绑定了 则上传该工单 List<ConcernedCustomerEntity> concernedCustomerEntities = concernedCustomerDao.findByOpenidAndType(open_id, 1); if (concernedCustomerEntities.size() > 0) { ConcernedCustomerEntity concernedCustomerEntity = concernedCustomerEntities.get(0); List<CustomerEntity> customerEntities = customDao.findByConcernedCustomer(concernedCustomerEntity); if (customerEntities.size() > 0) { respContent = "您已经绑定微信"; } else { //把状态放入一个表中 1 并且过期时间设定 哪个用户 WeXinEventEntity weXinEventEntity = weXinEventService.getAllWeXinEvent(open_id); WeXinEventEntity weXinEventEntity1 = weXinEventService.findByOpenid(open_id); if (weXinEventEntity == null) { if (weXinEventEntity1 != null) { weXinEventService.dl(weXinEventEntity1); } weXinEventEntity = new WeXinEventEntity(); } weXinEventEntity.setEvent("binding");//事件类型为moble weXinEventEntity.setIswait(Boolean.TRUE);//在等待 weXinEventEntity.setOpenid(open_id); Calendar calendar1 = Calendar.getInstance(); calendar1.add(Calendar.MINUTE, +10);//当前时间+10分钟 是过期时间 weXinEventEntity.setOut_time(new Timestamp(calendar1.getTimeInMillis())); weXinEventService.save(weXinEventEntity); respContent = "请输入你要绑定的登录名称"; } } textMessage.setContent(respContent); respMessage = MessageUtil.textMessageToXml(textMessage); } // 地理位置消息 else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LOCATION)) { respContent = "您发送的是地理位置消息!"; } // 链接消息 else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_LINK)) { respContent = "您发送的是链接消息!"; } // 音频消息 else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_VOICE)) { respContent = "您发送的是音频消息!"; } // 事件推送 else if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_EVENT)) { // 事件类型 String eventType = requestMap.get("Event"); // 自定义菜单点击事件 if (eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)) { attention(open_id); NewsMessage newsMessage = buildmessage(open_id, toUserName,0); // 将图文消息对象转换成xml字符串 respMessage = MessageUtil.newsMessageToXml(newsMessage); } else if (eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)) { // 取消关注,用户接受不到我们发送的消息了,可以在这里记录用户取消关注的日志信息 canelAttention(open_id); } else if (eventType.equalsIgnoreCase(MessageUtil.EVENT_TYPE_CLICK)) { // 事件KEY值,与创建自定义菜单时指定的KEY值对应 String eventKey = requestMap.get("EventKey"); if (eventKey.equals("CONTACT_US")) { NewsMessage newsMessage = buildmessage(open_id, toUserName,1); // 将图文消息对象转换成xml字符串 respMessage = MessageUtil.newsMessageToXml(newsMessage); } } /*else if (eventType.equalsIgnoreCase("VIEW")) { // 事件KEY值,与创建自定义菜单时指定的KEY值对应 String eventKey = requestMap.get("EventKey"); String e = "EventKey=https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx186f9fb0382ee5c5&redirect_uri=http://yaoyao66123.eicp.net/customCenter/ordersearch&response_type=code&scope=snsapi_base&state=1#wechat_redirect"; if (eventKey.equals(e)) { textMessage.setContent(respContent); respMessage = MessageUtil.textMessageToXml(textMessage); } }*/ } } catch (Exception e) { e.printStackTrace(); respMessage = "有异常了。。。"; } return respMessage; } private void attention(String open_id) { List<ConcernedCustomerEntity> list = concernedCustomerDao.findByOpenid(open_id); if (list.size() > 0) { //之前关注过 ConcernedCustomerEntity concernedCustomerEntity = list.get(0); concernedCustomerEntity.setCancelledtime(null); concernedCustomerEntity.setIsconcerned(Boolean.TRUE); } } private NewsMessage buildmessage(String open_id, String toUserName, Integer type) { // 创建图文消息 NewsMessage newsMessage = new NewsMessage(); newsMessage.setToUserName(open_id); newsMessage.setFromUserName(toUserName); newsMessage.setCreateTime(new Date().getTime()); newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS); newsMessage.setFuncFlag(0); List<WelMsgEntity> welMsgEntities = welMsgService.findAll(type); List<WelMsgEntity> needlist = new ArrayList<WelMsgEntity>(); if (welMsgEntities.size() > 0) { for (WelMsgEntity welMsgEntity : welMsgEntities) { if (needlist.size() < 8) { needlist.add(welMsgEntity); } } } List<Article> articleList = new ArrayList<Article>(); for (WelMsgEntity welMsgEntity : needlist) { Article article = new Article(); article.setTitle(welMsgEntity.getTitle()); article.setDescription(welMsgEntity.getDescription()); article.setPicUrl(fileURLPrefix + welMsgEntity.getPicurl()); article.setUrl(welMsgEntity.getUrl()); articleList.add(article); } // 设置图文消息个数 newsMessage.setArticleCount(articleList.size()); // 设置图文消息包含的图文集合 newsMessage.setArticles(articleList); return newsMessage; } private void canelAttention(String open_id) { List<ConcernedCustomerEntity> list = concernedCustomerDao.findByOpenid(open_id); if (list.size() > 0) { ConcernedCustomerEntity concernedCustomerEntity = list.get(0); concernedCustomerEntity.setCancelledtime(new Timestamp(System.currentTimeMillis())); concernedCustomerEntity.setIsconcerned(Boolean.FALSE); concernedCustomerDao.save(concernedCustomerEntity); } } } |
自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习
京ICP备14009008号-1@版权所有www.zixuephp.com
网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com