首先写一个WeixinUtils.java类:
1 package com.freekeer.c.util;
2
3 import java.security.MessageDigest;
4 import java.util.Formatter;
5 import java.util.HashMap;
6 import java.util.Map;
7 import java.util.Properties;
8
9 import org.apache.commons.lang3.StringUtils;
10
11 import com.alibaba.fastjson.JSONObject;
12
13 public class WeiXinUtils {
14
15 // 微信appId
16 private static String APPID;
17 // 微信公众号唯一密钥
18 private static String APPSECRET;
19 // 获取acc_token的接口
20 private static String ACC_TOKEN_URL;
21 // 获取jsapi_ticket url
22 private static String JSAPI_TICKET_URL;
23 // 生成签名的随机串
24 private static String NONCE_STR;
25
26 static {
27 Properties prop = AppPropTools.getProperties("/weixin.properties");
28 APPID = prop.getProperty("APPID");
29 APPSECRET = prop.getProperty("APPSECRET");
30 ACC_TOKEN_URL = prop.getProperty("ACC_TOKEN_URL") + "&appid=" + APPID
31 + "&secret=" + APPSECRET;
32 JSAPI_TICKET_URL = prop.getProperty("JSAPI_TICKET_URL");
33 NONCE_STR = prop.getProperty("NONCE_STR");
34 }
35
36 // 私有构造方法
37 private WeiXinUtils() {
38
39 }
40
41 /**
42 * 获取微信acc_token
43 *
44 * @return
45 * @throws Exception
46 */
47 public static String getAccToken() throws Exception {
48 // 先从redis取,取不到再从微信里面取
49 String weixin_acc_token = RedisHelper.getStringValue(
50 "weixin_acc_token", 2);
51 if (StringUtils.isEmpty(weixin_acc_token)) {
52 String resultStr = HttpURLConnectionUtil
53 .getWebHTMLCode(ACC_TOKEN_URL);
54 JSONObject resultObj = JSONObject.parseObject(resultStr);
55 String accToken = resultObj.getString("access_token");
56 int expiresIn = resultObj.getIntValue("expires_in");
57 // 写进redis
58 RedisHelper.setStringValue("weixin_acc_token", accToken, expiresIn,
59 2);
60 return accToken;
61 }
62 return weixin_acc_token;
63
64 }
65
66 /**
67 * 获取微信票据
68 *
69 * @return
70 * @throws Exception
71 */
72 public static String getTicket() throws Exception {
73 // 先从redis中取ticket,没有再从这里取
74 String weixin_js_api_tiket = RedisHelper.getStringValue(
75 "weixin_js_api_tiket", 2);
76 if (StringUtils.isEmpty(weixin_js_api_tiket)) {
77
78 String accToken = getAccToken();
79 String resultStr = HttpURLConnectionUtil
80 .getWebHTMLCode(JSAPI_TICKET_URL + accToken);
81 JSONObject resultObj = JSONObject.parseObject(resultStr);
82 if (resultObj.getIntValue("errcode") == 0) {
83 String ticket = resultObj.getString("ticket");
84 int expires_in = resultObj.getIntValue("expires_in");
85 // 写入redis
86 RedisHelper.setStringValue("weixin_js_api_tiket", ticket,
87 expires_in, 2);
88 return ticket;
89 }
90 return null;
91 }
92 return weixin_js_api_tiket;
93 }
94
95 /**
96 * 获取签名
97 *
98 * @param timeStamp
99 * @param requestUrl
100 * @return
101 * @throws Exception
102 */
103 public static String getSignature(Long timeStamp, String requestUrl)
104 throws Exception {
105 String ticket = getTicket();
106 StringBuffer allStr = new StringBuffer("jsapi_ticket=");
107 allStr.append(ticket).append("&noncestr=").append(NONCE_STR)
108 .append("×tamp=").append(timeStamp).append("&url=")
109 .append(requestUrl);
110 MessageDigest crypt = MessageDigest.getInstance("SHA-1");
111 crypt.reset();
112 crypt.update(allStr.toString().getBytes("UTF-8"));
113 String signature = byteToHex(crypt.digest());
114 return signature;
115 }
116
117 /**
118 * SHA1加密
119 *
120 * @param hash
121 * @return
122 */
123 private static String byteToHex(final byte[] hash) {
124 Formatter formatter = new Formatter();
125 for (byte b : hash) {
126 formatter.format("%02x", b);
127 }
128 String result = formatter.toString();
129 formatter.close();
130 return result;
131 }
132
133 /**
134 * 获取微信JS页面Config所需参数
135 *
136 * @param timeStamp
137 * @param requestUrl
138 * @return
139 * @throws Exception
140 */
141 public static Map<String, Object> getConfig(long timeStamp,
142 String requestUrl) throws Exception {
143 Map<String, Object> map = new HashMap<String, Object>();
144 String signature = getSignature(timeStamp, requestUrl);
145 map.put("signature", signature);
146 map.put("nonceStr", NONCE_STR);
147 map.put("timestamp", timeStamp);
148 map.put("appId", APPID);
149 return map;
150 }
151
152 }
153
1 package com.freekeer.c.controller;
2
3 import java.util.Date;
4 import java.util.Map;
5
6 import javax.servlet.http.HttpServletRequest;
7 import javax.servlet.http.HttpServletResponse;
8
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.bind.annotation.RequestMethod;
13 import org.springframework.web.bind.annotation.RequestParam;
14 import org.springframework.web.bind.annotation.RestController;
15
16 import com.freekeer.c.util.WeiXinUtils;
17
18 /**
19 * 热搜词控制层
20 *
21 * @author WuWei
22 */
23 @RestController
24 public class WeixinController extends BaseController {
25
26 @SuppressWarnings("unused")
27 private static Logger log = LoggerFactory.getLogger(WeixinController.class);
28
29 @RequestMapping(value = "/wx-config", method = { RequestMethod.GET })
30 public Map<String, Object> getConfig(HttpServletRequest request,
31 HttpServletResponse response, @RequestParam(value="url", required=true) String url) throws Exception {
32 long timeStamp = new Date().getTime()/1000;
33 Map<String, Object> result = WeiXinUtils.getConfig(timeStamp, url);
34 return result;
35 }
36
37 }
38
1 function weixinShareZp(demandData) {
2 var title = demandData.profession;
3 var desc = demandData.projectName+",在"+demandData.workCityName+"招聘"+title+demandData.requireStaffCount+"人";
4 var url = "../wx-config?url="+encodeURIComponent(window.location.href);
5 var logoUrl = "http://xxx-resource-public.oss-cn-beijing.aliyuncs.com/share_img/logo.png";
6 $.get(url,[],function(data) {
7 wx.config({
8 debug:true,
9 appId:data.appId,
10 timestamp:data.timestamp,
11 nonceStr:data.nonceStr,
12 signature:data.signature,
13 jsApiList: [
14 'onMenuShareTimeline',
15 'onMenuShareAppMessage',
16 'onMenuShareQQ',
17 'onMenuShareWeibo',
18 'onMenuShareQZone'
19 ]
20 });
21 wx.ready(function(){
22 // 获取“分享到朋友圈”按钮点击状态及自定义分享内容接口
23 wx.onMenuShareTimeline({
24 imgUrl:logoUrl,
25 link:window.location.href,
26 desc:desc,
27 title:title,
28 type:"link"
29 });
30 // 获取“分享给朋友”按钮点击状态及自定义分享内容接口
31 wx.onMenuShareAppMessage({
32 imgUrl: logoUrl,
33 link: window.location.href,
34 desc: desc,
35 title: title,
36 type: "link"
37 });
38 wx.onMenuShareQQ({
39 title: title,
40 desc: desc,
41 link: window.location.href,
42 imgUrl: logoUrl
43 });
44 wx.onMenuShareWeibo({
45 title: title,
46 desc: desc,
47 link: window.location.href,
48 imgUrl: logoUrl,
49 });
50 wx.onMenuShareQZone({
51 title: title,
52 desc: desc,
53 link: window.location.href,
54 imgUrl: logoUrl,
55 });
56 });
57
58 });
59 }