java spring boot 微信公众号 分享功能

/**
     * 方法名:getWxConfig</br>
     * 详述:获取微信的配置信息 </br>
     * @param
     * @return 说明返回值含义
     * @throws
     */
        public Map<String, String> getWxConfig(HttpServletRequest request, HttpServletResponse response) {
            response.setHeader("Access-Control-Allow-Origin", "*");

            Map<String, String> ret = new HashMap<>();
            //获取前台传来的三个参数
            String timestamp =  WXPayUtil.getCurrentTimestamp()+"";
            String nonce_str = WXPayUtil.generateNonceStr();
            String url = request.getParameter("url");
            logger.info("url"+url+"==============="+nonce_str+"============"+timestamp);

            //从缓存中读取token信息,如果没有则获取一个新的token,通过token获取ticket信息
            String access_token = (String)request.getSession().getAttribute("access_token");
            if(access_token == null) {
                /** 获取AccessToKen*/
                String getAccessToKen = "https://api.weixin.qq.com/cgi-bin/token?"
                        + "grant_type=client_credential&appid="+env.getProperty("wxlog.appid")+"&secret="+ env.getProperty("wxlog.appsecret");
                String  jsonObject = MyHttpUtils.doPost(getAccessToKen,"",Charset.forName("UTF-8").toString());

                JSONObject rq = JSON.parseObject(jsonObject);
                /**获取jsapi_ticket*/
                String getTicket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+rq.get("access_token").toString()+"&type=jsapi";
                String rest = MyHttpUtils.doPost(getTicket,"",Charset.forName("UTF-8").toString());
                JSONObject re = JSON.parseObject(rest);
                /**将信息保存入缓存中*/
                request.getSession().setAttribute("token", rq.get("access_token").toString());
                request.getSession().setAttribute("ticket", re.get("ticket").toString());
            }
            /**生成签名*/
            String ticket = (String)request.getSession().getAttribute("ticket"); //微信返回的ticket
            String signature = shareUtil.getSignature(ticket,url,nonce_str,timestamp); //获取签名
            ret.put("appId", env.getProperty("wxlog.appid"));
            ret.put("nonceStr", nonce_str);
            ret.put("timestamp", timestamp);
            ret.put("signature", signature);
            return ret;
        }
---------------------------------------------------------------------------------
    var  url2=location.href.split('#')[0];
  function getwxconfing(title,pretext,preimage){
            mui.post('/wxpya/getWxConfig',
                {
                    url:url2
                },
                function (data) {
                console.log(url);
                    console.log(data);
                    if (data!=undefined) {
                        wx.config({
                            debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                            appId: data.appId, // 必填,公众号的唯一标识
                            timestamp: data.timestamp, // 必填,生成签名的时间戳
                            nonceStr: data.nonceStr, // 必填,生成签名的随机串
                            signature: data.signature,// 必填,签名
                            jsApiList: [//需要调用的JS接口列表
                                'checkJsApi',//判断当前客户端版本是否支持指定JS接口
                                'onMenuShareAppMessage',//分享给好友
                                'onMenuShareTimeline'//分享到朋友圈
                            ]
                        });
                        var config = {
                            title: title, // 分享标题
                            desc: pretext, // 分享描述
                            link: url, // 分享链接
                            imgUrl: imgsrcpath()+preimage,
                            success: function () { /*$.alert("成功分享");*/ },
                            cancel: function () { /*$.alert("分享失败,您取消了分享!");*/ }
                        };
                        wx.ready(function () {
                            wx.onMenuShareAppMessage(config);
                            wx.onMenuShareTimeline(config);
                        });
                    }
                }, 'json'
            )
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章