- 浏览: 120041 次
- 性别:
- 来自: 南京
文章分类
- 全部博客 (97)
- Jquery (3)
- Oracle (11)
- JS (12)
- Struts2 (4)
- java (17)
- 课程 (1)
- Jboss (0)
- EJB (0)
- MAVEN (1)
- Design Pattern (0)
- css (2)
- axis1.4 (2)
- hessian (1)
- big data (1)
- Tomcat (3)
- redis (1)
- OVAL validate (1)
- express (1)
- eclipse (1)
- name node 无法启动 (1)
- hadoop (2)
- mysql (1)
- spring (1)
- sbt (1)
- angular (1)
- cas (1)
- JSSDK (1)
- 微信支付 (1)
- 小程序 (1)
- mybatis (1)
- jxta (1)
- idea (2)
- lay ui (1)
- sofa (1)
最新评论
-
duanwenping520:
这也太省事了把!
sj22
微信 h5分享
1.前端jsp 需要去除追加字符,传到后端进行签名
2.后端返回配置。
后端代码:
@RequestMapping(value="getJingliList")
public void getJingliList(ModelMap modelMap,HttpServletResponse response,HttpServletRequest request,String url){
//String url ="http://jingli.shiyetv.com";
Map<String,Object> map = new HashMap<>();
String timeStamp = WxConfig.create_timestamp();//时间戳
String nonceStr = WxConfig.getUUID();//随机字符串,不长于32位
String signature = WxConfig.getSignature( url, timeStamp, nonceStr);
map.put("timeStamp", timeStamp);
map.put("nonceStr", nonceStr);
map.put("url", url);
map.put("signature", signature);
map.put("appid", WxConfig.APPID);
Tools.printJsonToPage(map, response);
}
配置类
package com.jingli.util;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.util.*;
/**
* Created by Administrator on 2017/6/9.
*/
public class WxConfig {
private final static Log logger = LogFactory.getLog(WxConfig.class);
public final static String WEIXIN_JSAPI_TICKET_URL ="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
public static String APPID="xxx";
private static String APPSECRET="xx";
private static Map<String, String> map = new HashMap<String,String>();
//access_token
public static Map<String,String> getAccessToken(){
String access_token =null;
JsonObject object = null;
Long nowDate = new Date().getTime();
String accessToken = map.get("access_token");
String time = map.get("time");
if (accessToken != null && time != null && nowDate - Long.parseLong(time) < 3000 * 1000) {
logger.info("get getAccessToken from cache " +accessToken);
return map;
}
else{
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APPSECRET;
try{
logger.info("get getAccessToken from remote " +nowDate );
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
String tokens = EntityUtils.toString(httpEntity, "utf-8");
Gson token_gson = new Gson();
object = token_gson.fromJson(tokens, JsonObject.class);
access_token = object.get("access_token").toString().replaceAll("\"", "");
String expires_in = object.get("expires_in").toString().replaceAll("\"", "");
map.put("access_token",access_token);
map.put("time",nowDate+"");
//mapToken.put("accessToken", access_token);
logger.info("access_token:"+access_token+";expires_in:"+expires_in);
String jsTickect= getJsapiTicket(access_token);
map.put("jsapi_ticket",jsTickect);
}catch (Exception e) {
access_token = null;
e.printStackTrace();
logger.error("errcode:{}:"+object.get("errcode")+"errmsg:{}:"+object.get("errmsg"));
}
}
return map;
}
//jsapi_ticket
public static String getJsapiTicket(String accessToken){
String ticket =null;
JsonObject object = null;
Long nowDate = new Date().getTime();
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+accessToken+"&type=jsapi";
try{
logger.info("get getJsapiTicket from remote " +nowDate );
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
String tokens = EntityUtils.toString(httpEntity, "utf-8");
Gson token_gson = new Gson();
object = token_gson.fromJson(tokens, JsonObject.class);
ticket = object.get("ticket").toString().replaceAll("\"", "");
String expires_in = object.get("expires_in").toString().replaceAll("\"", "");
logger.info("jsapi_ticket:"+ticket+";expires_in:"+expires_in);
}catch (Exception e) {
ticket = null;
e.printStackTrace();
logger.error("ticket errcode:{}:"+object.get("errcode")+"errmsg:{}:"+object.get("errmsg"));
}
return ticket;
}
//生成随机字符串UUID
public static String getUUID(){
String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
return uuid;
}
//JS-SDK Signature
public static String getSignature(String url,String timeStamp,String nonceStr){
Map<String,String> accessToken = getAccessToken();
String jsapi_ticket =accessToken.get("jsapi_ticket");
logger.info("accessToken==="+accessToken);
String[] paramArr = new String[] { "jsapi_ticket=" + jsapi_ticket,
"timestamp=" + timeStamp, "noncestr=" + nonceStr, "url=" + url };
Arrays.sort(paramArr);
// 将排序后的结果拼接成一个字符串
String content = paramArr[0].concat("&"+paramArr[1]).concat("&"+paramArr[2])
.concat("&"+paramArr[3]);
logger.info("微信JS-SDK权限验证的签名串:"+content);
//这个签名.主要是给加载微信js使用.别和上面的搞混了.
String signature = DigestUtils.shaHex(content);
logger.info("微信JS-SDK权限验证的签名:"+signature);
return signature;
}
public static String create_timestamp() {
return Long.toString(System.currentTimeMillis() / 1000);
}
}
前端代码
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js" ></script>
function detail(){
var urlAction="${web_root}/showPage/getJingliList.action"
var url=window.location.href.split('#')[0];
var data={
url:url
};
getRemotePostData(urlAction,data,showData)
function showData(res){
var count1 ="我是第"+res.count+"位";
$("#showCount").html(count1);
var list = res.jingliList;
var area="";
var vote ="";
for(var i=0;i<list.length;i++){
//console.info(list[i]);
var item =list[i];
var name =item.name;
var count= item.count;
var id = item.id;
area +='<span class="bigbox5-left-text">'+name+'</span>';
vote +='<div class="bigbox5-rightbox">' +
'<span class="bigbox5-right-btn" onclick="vote('+id+')" >敬礼</span>'+
'<span class="bigbox5-right-text1" id="count_'+id+'">'+count+'</span>'+
'</div>';
}
$("#showArea").html(area);
$("#showResult").html(vote);
initConfig(res);
}
}
function initConfig(res){
wx.config({
debug: false,
appId: res.appid,
timestamp:res.timeStamp,
nonceStr:res.nonceStr,
signature:res.signature,
jsApiList: [
'checkJsApi',
'onMenuShareTimeline',
'hideOptionMenu',
'onMenuShareAppMessage'
]
});
wx.ready(function(){
//wx.hideOptionMenu();/***隐藏分享菜单****/
wx.checkJsApi({
jsApiList: [
'getLocation',
'onMenuShareTimeline',
'onMenuShareAppMessage'
],
success: function (res) {
//alert(res.errMsg);
}
});
var title="向国旗敬礼";
var desc ="上海文明网";
var url=window.location.href.split('#')[0];
var link=url;
var imgUrl="http://xxx.shiyetv.com/images/guoqi.gif"
wx.onMenuShareAppMessage({
title: title,
desc: desc,
link: link,
imgUrl: imgUrl,
type: 'link',
success: function (res) {
alert('分享成功');
},
cancel: function (res) {
//alert('已取消');
},
fail: function (res) {
//alert(res.errMsg);
}
});
// 2.2 监听“分享到朋友圈”按钮点击、自定义分享内容及分享结果接口
wx.onMenuShareTimeline({
title:title,
desc: desc,
link: link,
imgUrl: imgUrl,
type: 'link',
success: function (res) {
alert('分享成功');
//分享之后增加游戏次数
},
cancel: function (res) {
//alert('已取消');
},
fail: function (res) {
// alert(res.errMsg);
}
});
wx.error(function (res) {
// alert(res.errMsg);
});
});
}
1.前端jsp 需要去除追加字符,传到后端进行签名
2.后端返回配置。
后端代码:
@RequestMapping(value="getJingliList")
public void getJingliList(ModelMap modelMap,HttpServletResponse response,HttpServletRequest request,String url){
//String url ="http://jingli.shiyetv.com";
Map<String,Object> map = new HashMap<>();
String timeStamp = WxConfig.create_timestamp();//时间戳
String nonceStr = WxConfig.getUUID();//随机字符串,不长于32位
String signature = WxConfig.getSignature( url, timeStamp, nonceStr);
map.put("timeStamp", timeStamp);
map.put("nonceStr", nonceStr);
map.put("url", url);
map.put("signature", signature);
map.put("appid", WxConfig.APPID);
Tools.printJsonToPage(map, response);
}
配置类
package com.jingli.util;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.util.*;
/**
* Created by Administrator on 2017/6/9.
*/
public class WxConfig {
private final static Log logger = LogFactory.getLog(WxConfig.class);
public final static String WEIXIN_JSAPI_TICKET_URL ="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
public static String APPID="xxx";
private static String APPSECRET="xx";
private static Map<String, String> map = new HashMap<String,String>();
//access_token
public static Map<String,String> getAccessToken(){
String access_token =null;
JsonObject object = null;
Long nowDate = new Date().getTime();
String accessToken = map.get("access_token");
String time = map.get("time");
if (accessToken != null && time != null && nowDate - Long.parseLong(time) < 3000 * 1000) {
logger.info("get getAccessToken from cache " +accessToken);
return map;
}
else{
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APPSECRET;
try{
logger.info("get getAccessToken from remote " +nowDate );
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
String tokens = EntityUtils.toString(httpEntity, "utf-8");
Gson token_gson = new Gson();
object = token_gson.fromJson(tokens, JsonObject.class);
access_token = object.get("access_token").toString().replaceAll("\"", "");
String expires_in = object.get("expires_in").toString().replaceAll("\"", "");
map.put("access_token",access_token);
map.put("time",nowDate+"");
//mapToken.put("accessToken", access_token);
logger.info("access_token:"+access_token+";expires_in:"+expires_in);
String jsTickect= getJsapiTicket(access_token);
map.put("jsapi_ticket",jsTickect);
}catch (Exception e) {
access_token = null;
e.printStackTrace();
logger.error("errcode:{}:"+object.get("errcode")+"errmsg:{}:"+object.get("errmsg"));
}
}
return map;
}
//jsapi_ticket
public static String getJsapiTicket(String accessToken){
String ticket =null;
JsonObject object = null;
Long nowDate = new Date().getTime();
String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+accessToken+"&type=jsapi";
try{
logger.info("get getJsapiTicket from remote " +nowDate );
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
String tokens = EntityUtils.toString(httpEntity, "utf-8");
Gson token_gson = new Gson();
object = token_gson.fromJson(tokens, JsonObject.class);
ticket = object.get("ticket").toString().replaceAll("\"", "");
String expires_in = object.get("expires_in").toString().replaceAll("\"", "");
logger.info("jsapi_ticket:"+ticket+";expires_in:"+expires_in);
}catch (Exception e) {
ticket = null;
e.printStackTrace();
logger.error("ticket errcode:{}:"+object.get("errcode")+"errmsg:{}:"+object.get("errmsg"));
}
return ticket;
}
//生成随机字符串UUID
public static String getUUID(){
String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
return uuid;
}
//JS-SDK Signature
public static String getSignature(String url,String timeStamp,String nonceStr){
Map<String,String> accessToken = getAccessToken();
String jsapi_ticket =accessToken.get("jsapi_ticket");
logger.info("accessToken==="+accessToken);
String[] paramArr = new String[] { "jsapi_ticket=" + jsapi_ticket,
"timestamp=" + timeStamp, "noncestr=" + nonceStr, "url=" + url };
Arrays.sort(paramArr);
// 将排序后的结果拼接成一个字符串
String content = paramArr[0].concat("&"+paramArr[1]).concat("&"+paramArr[2])
.concat("&"+paramArr[3]);
logger.info("微信JS-SDK权限验证的签名串:"+content);
//这个签名.主要是给加载微信js使用.别和上面的搞混了.
String signature = DigestUtils.shaHex(content);
logger.info("微信JS-SDK权限验证的签名:"+signature);
return signature;
}
public static String create_timestamp() {
return Long.toString(System.currentTimeMillis() / 1000);
}
}
前端代码
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js" ></script>
function detail(){
var urlAction="${web_root}/showPage/getJingliList.action"
var url=window.location.href.split('#')[0];
var data={
url:url
};
getRemotePostData(urlAction,data,showData)
function showData(res){
var count1 ="我是第"+res.count+"位";
$("#showCount").html(count1);
var list = res.jingliList;
var area="";
var vote ="";
for(var i=0;i<list.length;i++){
//console.info(list[i]);
var item =list[i];
var name =item.name;
var count= item.count;
var id = item.id;
area +='<span class="bigbox5-left-text">'+name+'</span>';
vote +='<div class="bigbox5-rightbox">' +
'<span class="bigbox5-right-btn" onclick="vote('+id+')" >敬礼</span>'+
'<span class="bigbox5-right-text1" id="count_'+id+'">'+count+'</span>'+
'</div>';
}
$("#showArea").html(area);
$("#showResult").html(vote);
initConfig(res);
}
}
function initConfig(res){
wx.config({
debug: false,
appId: res.appid,
timestamp:res.timeStamp,
nonceStr:res.nonceStr,
signature:res.signature,
jsApiList: [
'checkJsApi',
'onMenuShareTimeline',
'hideOptionMenu',
'onMenuShareAppMessage'
]
});
wx.ready(function(){
//wx.hideOptionMenu();/***隐藏分享菜单****/
wx.checkJsApi({
jsApiList: [
'getLocation',
'onMenuShareTimeline',
'onMenuShareAppMessage'
],
success: function (res) {
//alert(res.errMsg);
}
});
var title="向国旗敬礼";
var desc ="上海文明网";
var url=window.location.href.split('#')[0];
var link=url;
var imgUrl="http://xxx.shiyetv.com/images/guoqi.gif"
wx.onMenuShareAppMessage({
title: title,
desc: desc,
link: link,
imgUrl: imgUrl,
type: 'link',
success: function (res) {
alert('分享成功');
},
cancel: function (res) {
//alert('已取消');
},
fail: function (res) {
//alert(res.errMsg);
}
});
// 2.2 监听“分享到朋友圈”按钮点击、自定义分享内容及分享结果接口
wx.onMenuShareTimeline({
title:title,
desc: desc,
link: link,
imgUrl: imgUrl,
type: 'link',
success: function (res) {
alert('分享成功');
//分享之后增加游戏次数
},
cancel: function (res) {
//alert('已取消');
},
fail: function (res) {
// alert(res.errMsg);
}
});
wx.error(function (res) {
// alert(res.errMsg);
});
});
}
发表评论
-
url 二级域名匹配
2016-10-14 16:32 550private static void urlMatche(S ... -
Https Basic Auth
2016-03-21 15:16 1117最近测试 Https 连接,遇到了些问题。 1. Cause ... -
unicdoe 转码
2015-12-04 16:15 444开始以为是乱码,我日。 http://www.faqs.org ... -
eclipse中取消show in Breadcrumb方法
2015-11-23 18:01 1640eclipse中取消show in Breadcrumb方法 ... -
sqserver order by 与java compareTo ,xml sting getBytes乱码
2015-08-04 23:10 5731.sqlsever orderby 顺序,取出list, 然 ... -
手动构建xml xls 日期 datetime number type
2014-10-28 19:07 1359http://ewbi.blogs.com/develops/ ... -
压缩 inputstream to zipoutputstream to bytearrayoutputstream
2014-10-21 20:06 1513http://stackoverflow.com/questi ... -
JMX demo
2014-02-07 11:05 553http://docs.oracle.com/javase/6 ... -
xsl dom 一个xml 引用另一个xml的某些节点
2014-02-07 11:07 626http://www.coderanch.com/t/1263 ... -
excel 导入导出
2013-03-20 21:35 780else if (type.equalsIgnoreCase( ... -
servlet 线程安全 application.setattribute,加上synchronized
2013-03-20 20:35 1631http://www.alixixi.com/Dev/Web/ ... -
sj22
2013-01-09 21:37 946Organizations 字段名 含义 类型 缺省值 备 ... -
xerces jar和dom,jaxb解析冲突的解决方法
2012-10-12 22:41 31221.采用dom解析的时候,遇到Xerces.jar,会出现 E ... -
jaxb style
2012-10-12 11:55 633http://www.coderanch.com/t/4952 ... -
jsp out.print 乱码
2014-02-07 11:07 1266response.setContentType( " ... -
定时器使用Timer,timerTask
2012-02-01 17:14 8041.每隔10分钟触发 web.xml <listener ...
相关推荐
针对uniapp 微信H5分享,我已经封装好,非常方便好用,有需要的朋友可以去下载,更多详细使用内容可以看我的博客https://blog.csdn.net/nihao123456789222/article/details/121954495
微信h5分享好友和朋友圈-内含html引入、wxshare.js、php服务端接口
购买的php版微信h5支付分享给大家,帮助大家解决h5支付难题
这个是我自己写的分享微信内的分享js写法,总是报错的参考一下,也可直接复制,另外还需要引用微信接口的js
微信 H5分享 下载jssdk文件-附件资源
H5微信分享好友朋友圈带图片和描述。本人亲测完全没问题,如果有问题包退。哈哈,过来支持一下吧!~~~~
关于Vue中路由使用history模式,开发微信H5页面分享时在安卓上签名有效成功,但是在IOS设备上一直报错签名失效问题 问题描述:在Vue开发过程中,路由使用History模式下,在使用微信分享时,在微信开发者工具上一切...
项目支付接口已对接完成 花了1000块钱买的 绝对值 分享给大家
微信H5答题小游戏,问题可编辑,带分值测评,带分享功能,可二次开发!
微信JS-SDK分享接口,可通过javascript文件引入方式来实现自定义分享。 具体说明:https://blog.csdn.net/gaofei880219/article/details/80147575
微信公众-提示分享到朋友圈样式demo-微信公众平台-微网站-微APP
只进行微信分享,不获取用户信息,直接复制粘贴,然后修改请求域名就可以使用,未经同意,切勿转载,经本人实测可用,
2018年最新的H5微信分享,从我的项目中提取出来的,只需要引入一个PHP类文件即可,非常方便,拿来即用
经过两周的研究,终于研究出来完美的微信H5支付,移动端网页微信支付唤起微信客户端进行支付,提高用户体验,虽然投资很大,愿意分享给大家,希望我的心血给大家带来更大的帮助,拒绝恶意评价,配置很简单,不会配置...
800元购买的微信 h5支付demo,感觉很不多,大家可以借鉴一下,分享给大家
h5微信分享代码详细代码,请参考腾讯微信官方h5开发文档!!!
微信小程序开发附源码:详解Vue开发微信H5微信分享签名失败问题解决方案