赞
踩
微信公众号主要面向名人、政府、媒体、企业等机构推出的合作推广业务。在这里可以通过微信渠道将品牌推广给上亿的微信用户,减少宣传成本,提高品牌知名度,打造更具影响力的品牌形象。
PS:互联网产品的来源:1.PC端、移动端(Android、IOS、Windows Phone(倒闭)等)混合开发:原生+H5、微信开发、支付宝开发等。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>chauncy</groupId> <artifactId>WeChat-Develop</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>WeChat-Service</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.3.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencies> <!-- 集成commons工具类 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <!-- 集成lombok 框架 --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!-- 集成redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- 集成aop --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!-- 集成web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 集成发送邮件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!-- 集成mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper</artifactId> <version>3.3.7</version> </dependency> <!-- 阿里巴巴数据源 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.14</version> </dependency> <!-- httpclient --> <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.30</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.8</version> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo</artifactId> <version>2.5.3</version> <exclusions> <exclusion> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency> <dependency> <groupId>xstream</groupId> <artifactId>xstream</artifactId> <version>1.2.2</version> </dependency> </dependencies> <build> <finalName>springboot-demo</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> <mainClass>${start-class}</mainClass> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.2-beta-5</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.springboot.server.demo.SampleController</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>assemble-all</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
package chauncy.controller; import java.io.PrintWriter; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSONObject; import chauncy.entity.TextMessage; import chauncy.util.CheckUtil; import chauncy.util.HttpClientUtil; import chauncy.util.XmlUtils; /** * 微信对接步骤: * 1.填写服务器地址 * 2.验证服务器地址是否正确(验签) * 3.验证通过后,任何微信操作都会以post请求通知到该服务地址 * @classDesc: 功能描述(微信消息处理类) * @author: ChauncyWang * @version: 1.0 */ @Controller public class DispatCherServlet { private static final Logger logger=LoggerFactory.getLogger(DispatCherServlet.class); /** * @methodDesc: 功能描述(微信验签接口) * @author: ChauncyWang * @param signature * @param timestamp * @param nonce * @param echostr * @return * @returnType: String */ @RequestMapping(value = "/dispatCherServlet", method = RequestMethod.GET) @ResponseBody public String getDispatCherServlet(String signature, String timestamp, String nonce, String echostr) { boolean checkSignature = CheckUtil.checkSignature(signature, timestamp, nonce); if (!checkSignature) { return null; } return echostr; } /** * @methodDesc: 功能描述(微信接收消息后处理返回消息接口) * @author: ChauncyWang * @param request * @param response * @returnType: void */ @RequestMapping(value = "/dispatCherServlet", method = RequestMethod.POST) @ResponseBody public void getDispatCherServlet(HttpServletRequest request,HttpServletResponse response){ response.setContentType("text/html;charset=utf-8");//防止乱码 try(PrintWriter out=response.getWriter()){ Map<String, String> result = XmlUtils.parseXml(request); String toUserName = result.get("ToUserName"); String fromUserName = result.get("FromUserName"); String msgType = result.get("MsgType"); String content = result.get("Content"); String resultXml = null; switch (msgType == null ? "":msgType){ case "text": if(content.equals("chauncy")){ resultXml = SendTextMessage(toUserName, fromUserName,"chauncy is handsome boy","text"); }else{ resultXml = CallQingYunKeRobotApi(toUserName, fromUserName, content); } break; default: resultXml = CallQingYunKeRobotApi(toUserName, fromUserName, content); break; } out.print(resultXml); }catch (Exception e) { logger.error("给微信回消息出错!",e.getMessage()); } } /** * @methodDesc: 功能描述(调用青云客智能聊天机器人API) * @author: ChauncyWang * @param toUserName * @param fromUserName * @param content * @return * @returnType: String */ private String CallQingYunKeRobotApi(String toUserName, String fromUserName, String content) { String resultXml; String apiResultStr = HttpClientUtil.doGet("http://api.qingyunke.com/api.php?key=free&appid=0&msg="+content); JSONObject jsonObj = new JSONObject().parseObject(apiResultStr); Integer apiResult = jsonObj.getInteger("result"); if(apiResult != null && apiResult==0){ String apiContent = jsonObj.getString("content"); resultXml = SendTextMessage(toUserName, fromUserName,apiContent,"text"); }else{ resultXml = SendTextMessage(toUserName, fromUserName,"I dont know you message","text"); } return resultXml; } /** * @methodDesc: 功能描述(组装返回微信的实体生成对应的XML报文) * @author: ChauncyWang * @param toUserName * @param fromUserName * @param content * @param text * @return * @returnType: String */ private String SendTextMessage(String toUserName, String fromUserName,String content,String text) { String resultXml=null; TextMessage textMessage = new TextMessage(); textMessage.setToUserName(fromUserName); textMessage.setFromUserName(toUserName); textMessage.setCreateTime(System.currentTimeMillis()); textMessage.setContent(content); textMessage.setMsgType(text); resultXml=XmlUtils.messageToXml(textMessage); return resultXml; } }
package chauncy.util; import java.security.MessageDigest; import java.util.Arrays; public class CheckUtil { public static final String tooken = "chauncy"; // 开发者自行定义Tooken public static boolean checkSignature(String signature, String timestamp, String nonce) { // 1.定义数组存放tooken,timestamp,nonce String[] arr = { tooken, timestamp, nonce }; // 2.对数组进行排序 Arrays.sort(arr); // 3.生成字符串 StringBuffer sb = new StringBuffer(); for (String s : arr) { sb.append(s); } // 4.sha1加密,网上均有现成代码 String temp = getSha1(sb.toString()); // 5.将加密后的字符串,与微信传来的加密签名比较,返回结果 return temp.equals(signature); } public static String getSha1(String str) { if (str == null || str.length() == 0) { return null; } char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; try { MessageDigest mdTemp = MessageDigest.getInstance("SHA1"); mdTemp.update(str.getBytes("UTF-8")); byte[] md = mdTemp.digest(); int j = md.length; char buf[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; buf[k++] = hexDigits[byte0 >>> 4 & 0xf]; buf[k++] = hexDigits[byte0 & 0xf]; } return new String(buf); } catch (Exception e) { return null; } } }
package chauncy.util; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import com.thoughtworks.xstream.XStream; import chauncy.entity.TextMessage; public class XmlUtils { /** * 解析微信发来的请求(XML) * * @param request * @return Map<String, String> * @throws Exception */ @SuppressWarnings("unchecked") public static Map<String, String> parseXml(HttpServletRequest request) throws Exception { // 将解析结果存储在HashMap中 Map<String, String> map = new HashMap<String, String>(); // 从request中取得输入流 InputStream inputStream = request.getInputStream(); // 读取输入流 SAXReader reader = new SAXReader(); Document document = reader.read(inputStream); // 得到xml根元素 Element root = document.getRootElement(); // 得到根元素的所有子节点 List<Element> elementList = root.elements(); // 遍历所有子节点 for (Element e : elementList) map.put(e.getName(), e.getText()); // 释放资源 inputStream.close(); inputStream = null; return map; } /** * 文本消息对象转换成xml * * @param textMessage * 文本消息对象 * @return xml */ public static String messageToXml(TextMessage textMessage) { xstream.alias("xml", textMessage.getClass()); return xstream.toXML(textMessage); } /** * 扩展xstream使其支持CDATA */ private static XStream xstream = new XStream(); }
package chauncy.entity; /** * @classDesc: 功能描述(给微信回消息实体类) * @author: ChauncyWang * @version: 1.0 */ public class TextMessage { private String ToUserName; private String FromUserName; private Long CreateTime; private String MsgType; private String Content; public String getToUserName() { return ToUserName; } public void setToUserName(String toUserName) { ToUserName = toUserName; } public String getFromUserName() { return FromUserName; } public void setFromUserName(String fromUserName) { FromUserName = fromUserName; } public Long getCreateTime() { return CreateTime; } public void setCreateTime(Long createTime) { CreateTime = createTime; } public String getMsgType() { return MsgType; } public void setMsgType(String msgType) { MsgType = msgType; } public String getContent() { return Content; } public void setContent(String content) { Content = content; } }
package chauncy;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AppServer {
public static void main(String[] args) {
SpringApplication.run(AppServer.class, args);
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。