var protocol = window.location.protocol; //获取协议
var host = window.location.host; //获取域名
var posuDomainUrl = protocol + "//" + host;
function WeChat(url, title, img, desc) {
var configData = {
title: title, // 分享标题
desc: desc, // 分享描述
link: url, // 分享链接
imgUrl: img, // 分享图标
success: function () {
// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
};
$.ajax({
url: posuDomainUrl + '/wechatShare/wechatShare.do',
type: 'POST',
data: {url:url},
success: function (resp) {
if (resp.data) {
resp.data.debug=false;
resp.data.jsApiList=['onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ','onMenuShareWeibo','onMenuShareQZone'];
wx.config(resp.data);
wx.ready(function () {
//分享到朋友圈
wx.onMenuShareTimeline({ //例如分享到朋友圈的API
title: title, // 分享标题
link: url, // 分享链接
imgUrl: img, // 分享图标
success: function () {
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
//分享给朋友
wx.onMenuShareAppMessage({
title: title, // 分享标题
desc: desc, // 分享描述
link: url, // 分享链接
imgUrl: img, // 分享图标
type: '', // 分享类型,music、video或link,不填默认为link
dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
success: function () {
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
//分享到QQ
wx.onMenuShareQQ(configData);
//分享到腾讯微博
wx.onMenuShareWeibo(configData);
//分享到QQ空间
wx.onMenuShareQZone(configData);
wx.error(function (res) {
//alert(res.errMsg); //打印错误消息。及把 debug:false,设置为debug:ture就可以直接在网页上看到弹出的错误提示
});
});
} else {
return;
}
},error:function(e){
console.log(e);
}
});
}
package com.hlziben.web.controller.webController.wechatShare;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hlziben.web.common.request.HonlineRestTemplate;
import com.hlziben.web.common.utils.EncryptUtils;
import com.hlziben.web.common.utils.HttpClientUtil;
import com.hlziben.web.common.utils.Setting;
import com.tzbao.api.common.utils.JsonResponse;
/**
*
*<dl>
*<dt>类名:WechatShareController</dt>
*<dd>描述: </dd>
*</dl>
*/
@Controller
@RequestMapping("wechatShare")
public class WechatShareController {
public static final Log log = LogFactory.getLog(WechatShareController.class);
@Resource
private HonlineRestTemplate honlineRestTemplate;
@Resource
private StringRedisTemplate numberRedisTemplate;
/**
* 向微信请求accessToken和ticket
* @param role
* @param phone
* @param code
* @param password
* @param repassword
* @param request
* @param response
* @param model
* @return
*/
@RequestMapping(method = RequestMethod.POST, value = "/wechatShare.do")
@ResponseBody
public Object register(
@RequestParam("url") String url, //前台传入URL
HttpServletRequest request, HttpServletResponse response, ModelMap model) {
try{
//检查Redis中现有的accessToken和ticket
Long timeStamp=System.currentTimeMillis();
String accessToken=numberRedisTemplate.opsForValue().get("member_wechat_access_token");
String appId=Setting.getSetting("weixin.app.id");
String appSecret=Setting.getSetting("weixin.app.secret");
String ticket=numberRedisTemplate.opsForValue().get("wechatTicket");
//如果redis中的accessToken为空,则重新获取
if(StringUtils.isBlank(accessToken)||StringUtils.isBlank(ticket)){
String accessTokenUrl="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+appSecret;
JSONObject objectAuthToken=HttpClientUtil.httpGetRequest(accessTokenUrl);
accessToken=objectAuthToken.get("access_token").toString();
numberRedisTemplate.opsForValue().set("member_wechat_access_token",accessToken,Long.valueOf(6600), TimeUnit.SECONDS);
String ticketUrl="https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token="+accessToken;
JSONObject objectTicket=HttpClientUtil.httpGetRequest(ticketUrl);
ticket=objectTicket.get("ticket").toString();
numberRedisTemplate.opsForValue().set("wechatTicket", ticket,Long.valueOf(6600), TimeUnit.SECONDS);
}
//获取微信accessToken及ticket
Map<String, Object> resultMap = new HashMap<>();
String nonceString=UUID.randomUUID().toString();
String encodeStr="jsapi_ticket="+ticket+"&noncestr="+nonceString+"×tamp="+timeStamp.toString()+"&url="+url;
String signature=EncryptUtils.codesBySHA1(encodeStr);
log.info(signature);
resultMap.put("appId",appId);
resultMap.put("nonceStr",nonceString);
resultMap.put("timestamp", timeStamp.toString());
resultMap.put("url", url);
resultMap.put("signature",signature);
resultMap.put("rawString",encodeStr);
return JsonResponse.getSuccessResponse(resultMap);
}catch(Exception e){
log.error(e);
return JsonResponse.getServerErrorResponse("微信共享失败");
}
}
}