基于JavaEE—微信网页(四)微信的程序接入(代码-模拟示例)

由于微信需要外网访问部署,为了方便大家测试本篇文章主要用jsp,servlet模拟演示了三个基本角色

模拟用户点击公众号点击的菜单的效果

[java]view plaincopy

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"

  2. pageEncoding="UTF-8"%>

  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">

  4. <html>

  5. <head>

  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

  7. <title>用户手机</title>

  8. <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.8.3.js">

  9. </script>

  10. </head>

  11. <body>

  12. <form action="${pageContext.request.contextPath }/WechatController/getCode.do" method="get">

  13. <input type="submit" value="哈尔滨爱尚实训"/>

  14. <input type="hidden" value="/user/Calculator.do" name="redURL"> <!-- 回调地址,即为客户端程序(第三方程序)入口 -->

  15. </form>

  16. </body>

  17. </html>

模拟微信的服务器

[java]view plaincopy

  1. import java.io.IOException;

  2. import java.util.UUID;

  3. import javax.servlet.http.HttpServletResponse;

  4. import org.springframework.stereotype.Controller;

  5. import org.springframework.web.bind.annotation.RequestMapping;

  6. import org.springframework.web.bind.annotation.ResponseBody;

  7. /**

  8. *

  9. * 模拟微信服务器

  10. * @author: 开唯

  11. * @date: 2018年1月8日下午5:00:19

  12. * @location 哈尔滨爱尚实训

  13. */

  14. @Controller

  15. @RequestMapping("/WechatController")

  16. public class WechatController {

  17. //用户点击后 ------- 模拟用,模拟微信服务器生成CODE

  18. @RequestMapping("getCode")

  19. public String getCode(String redURL){

  20. UUID code = UUID.randomUUID();

  21. return"redirect:"+redURL+"?code="+code.toString(); //重定向到第三方网址

  22. }

  23. /**

  24. <span class="space" style="white-space:pre;display:inline-block;text-indent:2em;line-height:inherit;"> * 模拟通过code获取openid

  25. <span class="space" style="white-space:pre;display:inline-block;text-indent:2em;line-height:inherit;"> */</span>

  26. </span><span class="space" style="white-space:pre;display:inline-block;text-indent:2em;line-height:inherit;"> </span>

  27. @RequestMapping("getOpenid")

  28. @ResponseBody

  29. public void getOpenid(String code,HttpServletResponse response) throws IOException{

  30. //根据code查找用户openid 假设查出用户openid为999

  31. response.getWriter().write("999");

  32. }

  33. }

第三方(客户端程序)

[java]view plaincopy

  1. import java.io.IOException;

  2. import org.json.JSONException;

  3. import org.springframework.stereotype.Controller;

  4. import org.springframework.web.bind.annotation.RequestMapping;

  5. import com.myhome.utill.URLParam;

  6. import com.myhome.utill.userDate.UserLocationData;

  7. /**

  8. *

  9. *

  10. * 通过微信回调函数访问计算器

  11. * @author: 开唯

  12. * @date: 2017年12月12日下午5:10:34

  13. * @location 哈尔滨爱尚实训<span class="space" style="white-space:pre;display:inline-block;text-indent:2em;line-height:inherit;"> </span>

  14. */

  15. @Controller

  16. @RequestMapping("/user")

  17. public class CalculatorController {

  18. /**

  19. *

  20. *

  21. * 访问程序

  22. * @param:微信传输过来的code

  23. * @return:String

  24. *

  25. * @author: 苟开唯

  26. * @throws JSONException IOException

  27. * @date: 2017年12月13日上午9:02:27

  28. *

  29. */

  30. @RequestMapping("Calculator")

  31. public String calculator(String code) throws JSONException, IOException{

  32. System.out.println("访问程序");

  33. String openid = null;

  34. //模拟访问微信服务器 code --> openid

  35. String url = "http://localhost:8080/myhome/WechatController/getOpenid.do?code="+code;

  36. StringBuffer readStringFromUrl = URLParam.readStringFromUrl(url);

  37. openid = readStringFromUrl.toString();

  38. String[] location = UserLocationData.getLocation(openid);

  39. //在这里根据openid关联用户群体,进行业务处理

  40. return"XXXXX" ; //view

  41. }}

个人总结:

开发者所做的,就是在按钮上部署程序地址,在进入controller时获取到Code,去再次访问微信服务器,获取openid。使用openid与我们程序进行关联操作。

下一篇预告:微信的消息接收