html屏蔽分享按钮
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>test</title>
<script src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
<script>
var link = location.href;
$(function() {
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来
appId: 'wx6fede0c9989aa98c', // 必填,公众号的唯一标识
timestamp: "1513666450", // 必填,生成签名的时间戳
nonceStr: "b5f88ccf-227a-41c9-9db3-d585ef6a933d", // 必填,生成签名的随机串
signature: "843f834527fc605de31303af5a5bfba97f406e1e",// 必填,签名,见附录1
jsApiList: [
"hideOptionMenu"
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2"closeWindow","hideOptionMenu","hideMenuItems","hideAllNonBaseMenuItem"
});
wx.ready(function () {
//wx.closeWindow({success:function(){alert("success")}});
/*wx.hideMenuItems({
menuList: ["menuItem:share:appMessage","menuItem:share:qq","/menuItem:share:QZone","menuItem:share:timeline"] // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3
});*/
//wx.hideAllNonBaseMenuItem();
wx.hideOptionMenu();
/*wx.hideMenuItems({
menuList: ['menuItem:share:qq',
'menuItem:share:weiboApp',
'menuItem:favorite',
'menuItem:share:facebook',
'/menuItem:share:QZone']// 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3
});*/
});
});
</script>
</head>
<body>
<h1>jssdk屏蔽右上角分享按钮。。</h1>
</body>
</html>
springmvc控制器获取签名信息
/**
*获取签名(签名、随机字符串、时间戳)
*/
@Controller
public class Sign {
private static final String APPID = "wx6fede0c9989aa98c";//唯一标识
private static final String APPSECRET = "b38cbaf67f9ee83e6441cde16a9bd32b";//密钥
@ResponseBody
@RequestMapping(value="/sign",produces={"application/json;charset=UTF-8"})
public String sign(HttpServletRequest request) throws ClientProtocolException, IOException {
Map<String, String> ret = new HashMap<String, String>();
String noncestr = UUID.randomUUID().toString();//生成签名的随机串
String timestamp = Long.toString(System.currentTimeMillis() / 1000);//时间戳
String jsapi_ticket = WeiXinUtil.getJsAPI_ticket(APPID, APPSECRET);//需要动态生成,根据access_token发送https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=abc&type=jsapi
String url = "http://127.0.0.1:8020/jssdk/index.html";//request.getParameter("url");//"http://bzzx.whonemap.cn:8799/WXYZT/index.jsp";//当前网页的URL
String string1;
String signature = "";//签名
//注意这里参数名必须全部小写,且必须有序
string1 = "jsapi_ticket=" + jsapi_ticket +
"&noncestr=" + noncestr +
"×tamp=" + timestamp +
"&url=" + url;
System.out.println(string1);
try
{
MessageDigest crypt = MessageDigest.getInstance("SHA-1");//提供SHA-1 or SHA-256算法
crypt.reset();//重设
crypt.update(string1.getBytes("UTF-8"));//对参数进行sha-1加密,使用指定的字节数组更新摘要
signature = byteToHex(crypt.digest());
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
ret.put("jsapi_ticket", jsapi_ticket);
ret.put("nonceStr", noncestr);
ret.put("timestamp", timestamp);
ret.put("url", url);
ret.put("signature", signature);
String signStr = JSONUtils.toJSONString(ret);
return signStr;
}
//字节数组转十六进制字符串
private static String byteToHex(final byte[] hash) {
Formatter formatter = new Formatter();
for (byte b : hash)
{
formatter.format("%02x", b);
}
String result = formatter.toString();
formatter.close();
return result;
}
}
获取api_ticket
api_ticket 是用于调用微信卡券JS API的临时票据,有效期为7200 秒,通过access_token 来获取。
/**
* 获取access_token和jsapi_ticket
*/
public class WeiXinUtil {
//从微信后台拿到APPID和APPSECRET 并封装为常量
private static final String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
private static final String JSAPI_TICKET_UTL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi";
/**
* 编写Get请求的方法。但没有参数传递的时候,可以使用Get请求
*
* @param url 需要请求的URL
* @return 将请求URL后返回的数据,转为JSON格式,并return
*/
public static JSONObject doGetStr(String url) throws ClientProtocolException, IOException {
@SuppressWarnings({ "deprecation", "resource" })
DefaultHttpClient client = new DefaultHttpClient();//获取DefaultHttpClient请求
HttpGet httpGet = new HttpGet(url);//HttpGet将使用Get方式发送请求URL
JSONObject jsonObject = null;
HttpResponse response = client.execute(httpGet);//使用HttpResponse接收client执行httpGet的结果
HttpEntity entity = response.getEntity();//从response中获取结果,类型为HttpEntity
if(entity != null){
String result = EntityUtils.toString(entity,"UTF-8");//HttpEntity转为字符串类型
jsonObject = JSONObject.fromObject(result);//字符串类型转为JSON类型
}
return jsonObject;
}
/**
* 编写Post请求的方法。当我们需要参数传递的时候,可以使用Post请求
*
* @param url 需要请求的URL
* @param outStr 需要传递的参数
* @return 将请求URL后返回的数据,转为JSON格式,并return
*/
public static JSONObject doPostStr(String url,String outStr) throws ClientProtocolException, IOException {
DefaultHttpClient client = new DefaultHttpClient();//获取DefaultHttpClient请求
HttpPost httpost = new HttpPost(url);//HttpPost将使用Get方式发送请求URL
JSONObject jsonObject = null;
httpost.setEntity(new StringEntity(outStr,"UTF-8"));//使用setEntity方法,将我们传进来的参数放入请求中
HttpResponse response = client.execute(httpost);//使用HttpResponse接收client执行httpost的结果
String result = EntityUtils.toString(response.getEntity(),"UTF-8");//HttpEntity转为字符串类型
jsonObject = JSONObject.fromObject(result);//字符串类型转为JSON类型
return jsonObject;
}
/**
* 获取AccessToken
* @return 返回拿到的access_token及有效期
*/
public static AccessToken getAccessToken(String APPID,String APPSECRET) throws ClientProtocolException, IOException{
AccessToken token = new AccessToken();
String url = ACCESS_TOKEN_URL.replace("APPID", APPID).replace("APPSECRET", APPSECRET);//将URL中的两个参数替换掉
JSONObject jsonObject = doGetStr(url);//使用刚刚写的doGet方法接收结果
if(jsonObject!=null){ //如果返回不为空,将返回结果封装进AccessToken实体类
token.setToken(jsonObject.getString("access_token"));//取出access_token
int expires_in = jsonObject.getInt("expires_in");
int currentTime = currentTime();
int expires_out = expires_in + currentTime;
token.setExpiresIn(expires_out);//取出access_token的有效期
}
return token;
}
/**
* 获取jsapi_ticket
* @return
*/
public static String getJsAPI_ticket(String APPID,String APPSECRET) throws ClientProtocolException, IOException{
String access_token = "";
String jsapi_ticket = "";
AccessToken config = readXml();
if(currentTime()>=config.getExpiresIn()) {//如果token过期
AccessToken token = getAccessToken(APPID, APPSECRET);
access_token = token.getToken();
String url = JSAPI_TICKET_UTL.replace("ACCESS_TOKEN", access_token);
JSONObject jsonObject = doGetStr(url);//从微信获取ticket
if(jsonObject!=null) {
jsapi_ticket = jsonObject.getString("ticket");
token.setJsApiTicket(jsapi_ticket);
writeXml(token);
}
}else {
jsapi_ticket = config.getJsApiTicket();//如果token没有过期,从配置文件获取
}
return jsapi_ticket;
}
//获取当前时间
public static int currentTime() {
return (int) (System.currentTimeMillis()/1000);
}
//读配置文件
public static AccessToken readXml() {
AccessToken config = null;
InputStream is = WeiXinUtil.class.getResourceAsStream("/token.xml");
try {
JAXBContext jaxbContext = JAXBContext.newInstance(AccessToken.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); // 从指定的文件解组 XML 数据并返回得到的内容树。
config = (AccessToken) jaxbUnmarshaller.unmarshal(is);
System.out.println(config);
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // 创建一个可以用来将 XML 数据转换为 java 内容树的 Unmarshaller 对象。
finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return config;
}
//写配置文件,token重新获取时
public static void writeXml(AccessToken config) throws FileNotFoundException {
URL url = WeiXinUtil.class.getResource("/token.xml");
File file = new File(url.getFile());
OutputStream os = new FileOutputStream(file);
try {
JAXBContext jaxbContext = JAXBContext.newInstance(AccessToken.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // 从指定的文件解组 XML 数据并返回得到的内容树。
jaxbMarshaller.marshal(config, os);
System.out.println(config);
} catch (JAXBException e) {
e.printStackTrace();
} // 创建一个可以用来将 XML 数据转换为 java 内容树的 Unmarshaller 对象。
finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
步骤一:绑定域名 先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。
步骤二:引入JS文件 在需要调用JS接口的页面引入如下JS文件,(支持https):http://res.wx.qq.com/open/js/jweixin-1.2.0.js
步骤三:通过config接口注入权限验证配置 所有需要使用JS-SDK的页面必须先注入配置信息,否则将无法调用(同一个url仅需调用一次,对于变化url的SPA的web
app可在每次url变化时进行调用,目前Android微信客户端不支持pushState的H5新特性,所以使用pushState来实现web
app的页面会导致签名失败,此问题会在Android6.2中修复)。 wx.config({
debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: ”, // 必填,公众号的唯一标识
timestamp: , // 必填,生成签名的时间戳
nonceStr: ”, // 必填,生成签名的随机串
signature: ”,// 必填,签名,见附录1
jsApiList: [] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2 });步骤四:通过ready接口处理成功验证 wx.ready(function(){
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
});
步骤五:通过error接口处理失败验证 wx.error(function(res){
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
});