微信浏览器禁止用户分享功能实现—java

微信浏览器禁止用户分享功能实现:

前端js如下:vue实现

//引入JS
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.4.0.js"></script>



api.getSign({url:encodeURIComponent(window.location.href.spilt('#')[0])}).then(res=>{
    wx.config({
        debug: true,// 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参
                    //数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
        appId: res.data.body.appId, // 必填,公众号的唯一标识
        timetamp: res.data.body.timetamp,// 必填,生成签名的时间戳
        nonceStr;res.data.body.nonceStr,// 必填,生成签名的随机串
        signature: res.data.body.signature,// 必填,签名,生成规则见微信公众号开发文档附录1
        jsApiList: ['hideOptionMenu']
    });
    wx.ready(function() {
        //ready会在config信息验证后执行,所有接口调用都必须在config接口获得结果后。
         wx.hideOptionMenu({
            success: function(res){
            
                alert('hide suc');
            },
             fail: function(res){
            
                alert('fail :' + res.errMsg);
            },

java后端代码:

public final static String ticketUrl = 
"https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi"
;
@ResponseBody
    @RequestMapping("/mobile/sign")
    public Result getSign(String url){//前端调用接口传入
        logger.info("wx_sign_url:{}", url);
        if (StringUtils.isEmpty(url)) {
            return ResultHelper.setResultData(ResultCodeEnum.BAD_REQUEST_ILLEGAL_PARAM);
        }
        WxSignVO wxSignVO = new WxSignVO();
        wxSignVO.setAppId("appid");//对应微信公众号的appid
        wxSignVO.setTimestamp(new Date().getTime()/1000 + "");//时间戳
        wxSignVO.setNonceStr("wxsigndemo");//随机字符串,可以随机或固定

        String ticket = getTicket2WX();
        logger.info("wx_sign_ticket:{}",ticket);
        if (StringUtils.isEmpty(ticket)) {
            return ResultHelper.setResultData(ResultCodeEnum.BAD_REQUEST);
        }
        try {
            //按照微信公众号开发文档附录1 顺序拼接
            String sign = "jsapi_ticket=" + ticket + "&noncestr=" + wxSignVO.getNonceStr() + "&timestamp=" + wxSignVO.getTimestamp() + "&url=" + URLDecoder.decode(url, "UTF-8");
            logger.info("wx_sign_before:{}",sign);
            sign = MD5Util.sha1(sign);//进行sha1加密
            logger.info("wx_sign_after:{}",sign);
            wxSignVO.setSignature(sign);
            return ResultHelper.setResultData(wxSignVO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultHelper.setResultData(ResultCodeEnum.BAD_REQUEST_ILLEGAL_PARAM);
    }

    private String getTicket2WX() {
        String ticket = redisClient.get("demo_wx_sign");//ticket放入缓存中,2小时失效
        if(StringUtils.isNotBlank(ticket)){
            return ticket;
        }
        HttpsClient httpClient = HttpsClient.getAsyncHttpClient();
        String signUrl = ticketUrl;
        signUrl = signUrl.replace("ACCESS_TOKEN",  redisClient.get("demo_wx_access-token"));//获取access_token,需要定时刷新,2h过期
        logger.info("wx_sign_signUrl:{}", signUrl);
        String response = httpClient.get(signUrl);
       //发送https get请求  见https://blog.csdn.net/fz13768884254/article/details/83341873
        JSONObject jsonTexts = (JSONObject) JSON.parse(response);
        logger.info("wx-:demo/sign=response:{}", response);
        if (jsonTexts.get("ticket") != null) {
            ticket = jsonTexts.get("ticket").toString();
            //设置缓存1.5h
            redisClient.set("demo_wx_sign",6000,ticket);
            return ticket;
        }
        return null;
    }

WxSignVO对象:

package com.demo.test.wx;

import lombok.Data;

/**
 * @description: 微信签名
 * @author: feng
 * @create: 2018
 **/
@Data
public class WxSignVO {

    private String appId;

    private String timestamp;

    private String nonceStr;

    private String signature;
}

 

https get请求链接: 

SHA1 sha1加密链接:

ResultHelper自定义返回json格式Result对象

展开阅读全文

没有更多推荐了,返回首页