微信公众号JSSDK分享


后台获取wx.config签名验证数据

@RequestMapping(value = "pay_wx_config", method = RequestMethod.POST)
@ResponseBody
public SimpleResult pay_wx_config() {
    SimpleResult result = new SimpleResult();

    //java 获取请求 URL
    String current_url = config.getHost();
    current_url += this.getRequest().getRequestURI();     // 工程名
    if (this.getRequest().getQueryString() != null) //判断请求参数是否为空
    {
        current_url += "?" + this.getRequest().getQueryString();
    }   // 参数
    try {
        ParameterMap pm = this.getParameterMap();
        if(StringUtils.isNotBlank(pm.getString("current_url"))){
            current_url = pm.getString("current_url");
        }
        System.out.println("----" + current_url);
        String url = "";
        String resultstr = "";
        JSONObject object = null;
        Map<String, String> params = new HashMap<String, String>();
        params.put("1", "1");
        MyPayConfig myconfig = new MyPayConfig(config);
        WXPay wxpay = new WXPay(myconfig);
        String token = WechatAppAccessToken.getAppAccessToken();
        String ticket = WechatAppAccessToken.getJsapiTicket(token);
        String timestamp = WxSign.create_timestamp();
        //todo sha1 之后的签名
        Map signMap = WxSign.sign(ticket, current_url);
        signMap.put("appId", config.getAppId());
        System.out.println(JSONUtil.toJson(signMap));
        result.setData(signMap);
        result.setResult(HttpStatus.OK.getStatusCode());
        result.setMessage("获取WxConfig成功");

    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        result.setResult(HttpStatus.BAD_REQUEST.getStatusCode());
        result.setMessage("获取WxConfig失败");
        result.setErrorMsg(e.getMessage());
    }
    return result;
}



前台JSdemo

$.ajax({
    url: 'pay_wx_config',
    data: {current_url:window.location.href},
    type: "POST",
    dataType: "json",
    async: false,
    success: function (res) {
        if(res!=null && res.result==200){
            wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                appId: res.data.appId, // 必填,公众号的唯一标识
                timestamp: res.data.timestamp, // 必填,生成签名的时间戳
                nonceStr: res.data.nonceStr, // 必填,生成签名的随机串
                signature: res.data.signature,// 必填,签名
                jsApiList: ['chooseWXPay', 'onMenuShareTimeline', 'hideOptionMenu', 'onMenuShareAppMessage'] // 必填,需要使用的JS接口列表
            });

            wx.ready(function() {
                wx.onMenuShareTimeline({
                    title: '小菜,厨房里的健康菜', // 分享标题
                    link: LINK_URL+'/app_index?locationFlag=1&pid='+getCookie('APP_USER_ID'), // 分享链接
                    imgUrl: 'https://baonuotest.oss-cn-beijing.aliyuncs.com/bainuo/e8590187-75b0-4115-aaad-2155bb20be08.jpg', // 分享图标
                    success: function () {
                        // 用户确认分享后执行的回调函数
                        // alert("分享成功");
                    },
                    cancel: function () {
                        // 用户取消分享后执行的回调函数
                        // alert("取消分享");
                    }
                });

                wx.onMenuShareAppMessage({
                    title: '小菜,厨房里的健康菜', // 分享标题
                    desc: '首单立减20元!天天低价享好菜!!!', // 分享描述
                    link: LINK_URL+'/app_index?locationFlag=1&pid='+getCookie('APP_USER_ID'), // 分享链接
                    imgUrl: 'https://baonuotest.oss-cn-beijing.aliyuncs.com/bainuo/e8590187-75b0-4115-aaad-2155bb20be08.jpg', // 分享图标
                    success: function () {
                        // 用户确认分享后执行的回调函数
                        // alert("分享成功");
                    },
                    cancel: function () {
                        // 用户取消分享后执行的回调函数
                        // alert("取消分享");
                    }
                })

                wx.error(function (err_res) {
                    console.log("wx.error")
                    console.log(err_res)
                });
            });
        }else{
            alert(res.message);
            clickFlag = true;
        }
    }

})

猜你喜欢

转载自blog.csdn.net/su717854075/article/details/80355839