微信点击上传功能

package cn.com.shdmt.service;

import cn.com.shdmt.BizConstants;
import cn.com.shdmt.entity.*;
import cn.com.shdmt.framework.util.MessageUtil;
import cn.com.shdmt.framework.util.weXinUtil;
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.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.io.File;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
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;

@Autowired
private OnlineOrderFilesService onlineOrderFilesService;
@Autowired
private KeyReplyService keyReplyService;

@Value("#{appProperties[‘wexin.config.service.app_id‘]}")
private String serviceAppId;

@Value("#{appProperties[‘wexin.config.service.secret‘]}")
private String serviceSecret;

@Value("#{appProperties[‘upload.config.uploadPath‘]}")
private String uploadPath;

@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");
respContent = "欢迎关注地面通";
//得到他发送的消息
String c = content.toLowerCase();
KeyReplyEntity keyReplyEntity =keyReplyService.findByKeyAndIsDeleted(c,Boolean.FALSE);
if(keyReplyEntity!=null){
respContent = keyReplyEntity.getReply();
}
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) {
//保存这张图片
String fileDate = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTimeInMillis());
File dir = new File(uploadPath + "/wexin/" + fileDate + "/");
if (!dir.exists()) {
try {
dir.mkdirs();
} catch (Exception e) {
System.out.println("创建路径失败");
}
}
String mediaId = requestMap.get("MediaId");
weXinUtil.downloadMedia(getServiceToken(), mediaId, uploadPath + "/wexin/" + fileDate + "/");
String imageUrl = "wexin/"+fileDate+"/"+mediaId+".jpg";
OnlineOrderFilesEntity onlineOrderFilesEntity =new OnlineOrderFilesEntity();
onlineOrderFilesEntity.setProductId(imageUrl);
onlineOrderFilesEntity.setCustomerId(customerEntities.get(0).getId());
onlineOrderFilesService.save(onlineOrderFilesEntity);
respContent = "上传工单成功";
textMessage.setContent(respContent);
respMessage = MessageUtil.textMessageToXml(textMessage);

}else{
respContent = "<a href=\"http://yaoyao66123.eicp.net/customCenter/tologin?openid="+open_id+"/\">请点击绑定</a>";
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);
}
}

public String getServiceToken() {
AccessTokenEntity accessTokenEntity = accessTokenService.findByType(BizConstants.WEXIN_SERVICE_NUM);
//如果没有查到 则 创建 获取token
if (accessTokenEntity == null) {
accessTokenEntity = new AccessTokenEntity();
Map result = weXinUtil.getAccessToken(serviceAppId, serviceSecret, null);
if (!result.containsKey("no-change")) {
accessTokenEntity.setType(BizConstants.WEXIN_SERVICE_NUM);
accessTokenEntity.setAccessToken(result.get("token").toString());
accessTokenEntity.setExceedTime(Timestamp.valueOf(result.get("exceedTime").toString()));
accessTokenService.save(accessTokenEntity);
}
} else {
Map result = weXinUtil.getAccessToken(serviceAppId, serviceSecret, accessTokenEntity.getExceedTime());
if (!result.containsKey("no-change")) {
//去更新
accessTokenEntity.setAccessToken(result.get("token").toString());
accessTokenEntity.setExceedTime(Timestamp.valueOf(result.get("exceedTime").toString()));
accessTokenService.save(accessTokenEntity);
}
}
return accessTokenEntity.getAccessToken();
}

}

时间: 2024-10-04 10:09:29

微信点击上传功能的相关文章

微信js sdk上传多张图片

微信js sdk上传多张图片,微信上传多张图片 该案例已tp3.2商城为例 直接上代码: php代码: public function ind(){ $appid="11111111111111111111"; $secret="11111111111111111111"; $token = S('access_token'); if (!$token) { $res = file_get_contents("https://api.weixin.qq.c

java微信接口之四—上传素材

一.微信上传素材接口简介 1.请求:该请求是使用post提交地址为: https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=ACCESS_TOKEN 其中ACCESS_TOKEN是我们动态获取的. 发送的数据: {"articles":[ { "thumb_media_id":"qI6_Ze_6PtV7svjolgs-rN6stStuHIjs9_DidOHaj0Q-mwvBelOXC

nodejs 实现简单的文件上传功能

首先需要大家看一下目录结构,然后开始一点开始我们的小demo. 文件上传总计分为三种方式: 1.通过flash,activeX等第三方插件实现文件上传功能. 2.通过html的form标签实现文件上传功能,优点:浏览器兼容好. 3.通过xhr level2的异步请求,可以百度formData对象. 这里使用2做个练习. node插件请看下package.json文件 { "name": "upload", "version": "0.1

MVC微信浏览器图片上传(img转Base64)

因公司业务需要,需要做一个微信公众号里的图片上传功能,主要用到的技术就是 img转base64 上到服务器 话不多说, 贴代码 先看前端显示出来的东西 OK 图片不重要,看代码 <!--微信图片上传--> <section class="logo-license"> <div class="half"> <div class="uploader"> <a class="license

thinkphp实现UploadFile.class.php图片上传功能

图片上传在网站里是很常用的功能.ThinkPHP里也有自带的图片上传类(UploadFile.class.php) 和图片模型类(Image.class.php).方便于我们去实现图片上传功能,下面是实现方法 1.我们首先需要创建一个表 复制代码代码如下: CREATE TABLE IF NOT EXISTS `tp_image` ( `id` int(11) NOT NULL AUTO_INCREMENT, `image` varchar(200) NOT NULL, `create_time

(转)淘淘商城系列——实现图片上传功能

http://blog.csdn.net/yerenyuan_pku/article/details/72808000 上文我们使用FastDFS-Client进行了简单的文件上传操作测试,淘淘商城项目中添加商品时上传图片的功能还没实现,如下图所示.本文将花大量笔墨来教大家如何实现图片上传这个功能. 我们来看下item-add.jsp页面,可以看到上传图片触发的方法是通过叫做picFileUpload的class来处理的,在<a>标签的下方是一个隐藏域,是用来接收图片上传到图片服务器的回显地址

qt实现头像上传功能

想必大家都使用过qt的自定义头像功能吧,那么图1应该不会陌生,本片文章我就是要模拟一个这样的功能,虽然没有这么强大的效果,但是能够满足一定的需求. 图1 qq上传图片 首先在讲解功能之前,我先给出一片文章,QT实现的类似QQ的头像选择框,这篇文章也是讲解头像上传功能的,而我自己的代码是从这个demo中优化而来,不仅对代码进行了重构,而且处理了快速拖动时,边框消失的问题.使用事件和双缓冲来尽量减少重新绘制的几率.接下里我会一步一步进行讲解,怎么实现图片自定义截取功能.一.概要首选,我给出4个类,并

C#.net实现图片上传功能

C#.NET前台:<asp:Image ID="imgFood" runat="server" /> <asp:FileUpload ID="fileUpLoadPic" runat="server" /> <asp:Button ID="Button1" runat="server" Text="上传" onclick="bt

struts2中实现文件上传功能

在web项目中,文件上传.头像上传这样的功能经常是要用到的,下面就以在struts2中实现文件上传功能为例子,简单地理一下文件上传功能的编码思路. 项目目录结构 项目源代码 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:x