最新微信js-sdk使用 demo

服务器段逻辑实现

module WeixinShare
extend ActiveSupport::Concern


App_id = "********************"
App_secret = "**********************"



def WeixinShare.weixin_signature(page_url)
share_info = $redis.hgetall(page_url)
if share_info.present?
before_time = share_info["timestamp"].to_i
current_time = Time.now.to_i
if current_time - before_time > 7000
weixin_params=WeixinShare.generate_sha1_sign(page_url)
else
weixin_params={"appid"=>App_id,"timestamp"=>share_info["timestamp"],"nonceStr"=>share_info["noncestr"],"signature"=>share_info["signature"]}
end
else
weixin_params=WeixinShare.generate_sha1_sign(page_url)
end
end

def WeixinShare.generate_sha1_sign(page_url)
noncestr = SecureRandom.hex(8)
   jsapi_ticket = WeixinShare.jsapi_ticket
   timestamp = Time.now.to_i.to_s
   url = page_url
   sign_order = "jsapi_ticket="+jsapi_ticket+"&noncestr="+noncestr+"&timestamp="+timestamp+"&url="+url
   require 'digest/sha1'
    sha1_sign = Digest::SHA1.hexdigest(sign_order)
    $redis.hmset(page_url,"appid",App_id,"timestamp",timestamp,"noncestr",noncestr,"signature",sha1_sign)
   weixin_params={"timestamp"=>timestamp,"nonceStr"=>noncestr,"signature"=>sha1_sign,"appid"=>App_id}
end

def WeixinShare.jsapi_ticket
require 'net/http'
url = "https://api.weixin.qq.com/cgi-bin/token?"+
      "grant_type=client_credential&appid="+App_id+"&secret="+App_secret
json= JSON.parse(Net::HTTP.get(URI(url)))
url_ticket = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?"+
            "access_token="+json["access_token"]+"&type=jsapi"
ticket_json = JSON.parse(Net::HTTP.get(URI(url_ticket)))
ticket_json["ticket"]
end
end


直接调用 WeixinShare.weixin_signature(page_url)就可以生成签名

详细文档请看http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html


网页中代码对分享调用的代码

 weixin_info= {
      title:$("#share_title").val(),
      link:"",
      imgUrl:$("#share_image").val(),
      desc:$("#share_title").val()
    }
           
    $(document).ready(function(){
      $.ajax({
        type:"GET",
        url:"/articles/weixin_sign",
        data:{url:window.location.href},
        dataType:"json",
        success:function(data){
          console.log(data)
          wx.config({
            debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
            appId: data.appid, // 必填,公众号的唯一标识
            timestamp: parseInt(data.timestamp), // 必填,生成签名的时间戳
            nonceStr: data.nonceStr, // 必填,生成签名的随机串
            signature: data.signature,// 必填,签名,见附录1
            jsApiList: ["onMenuShareTimeline",
                        "onMenuShareAppMessage",
                        "onMenuShareQQ",
                        "onMenuShareWeibo"
            ] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
          });
        }
      });
      
      wx.ready(function(){
        
        wx.onMenuShareTimeline({
            title: weixin_info.title, // 分享标题
            link: weixin_info.link, // 分享链接
            imgUrl: weixin_info.imgUrl, // 分享图标
            success: function () { 
              $('#inference_answer').css("display","block")
              $('#answer_sharemcover').hide();
              $('#sharemcover').hide();
            },
            cancel: function () { 
              // alert("onMenuShareTimeline")
                // 用户取消分享后执行的回调函数
            }
        });


        wx.onMenuShareAppMessage({
          title: weixin_info.title, // 分享标题
          desc: weixin_info.desc, // 分享描述
          link:  weixin_info.link, // 分享链接
          imgUrl: weixin_info.imgUrl, // 分享图标
          success: function () { 
              $('#inference_answer').css("display","block")
              $('#answer_sharemcover').hide();
              $('#sharemcover').hide();
                        // 用户确认分享后执行的回调函数// 用户确认分享后执行的回调函数
          },
          cancel: function () { 
            // alert("onMenuShareAppMessage")
              // 用户取消分享后执行的回调函数
          }
        });




        wx.onMenuShareQQ({
          title: weixin_info.title, // 分享标题
          desc: weixin_info.desc, // 分享描述
          link:  weixin_info.link, // 分享链接
          imgUrl: weixin_info.imgUrl, // 分享图标
          success: function () { 
              $('#inference_answer').css("display","block")
              $('#answer_sharemcover').hide();
              $('#sharemcover').hide();
                        // 用户确认分享后执行的回调函数// 用户确认分享后执行的回调函数
          },
          cancel: function () { 
            // alert("onMenuShareQQ")
              // 用户取消分享后执行的回调函数
          }
        });


        wx.onMenuShareWeibo({
          title: weixin_info.title, // 分享标题
          desc: weixin_info.desc, // 分享描述
          link:  weixin_info.link, // 分享链接
          imgUrl: weixin_info.imgUrl, // 分享图标
          success: function () { 
            $('#inference_answer').css("display","block")
            $('#answer_sharemcover').hide();
            $('#sharemcover').hide();
              // 用户确认分享后执行的回调函数// 用户确认分享后执行的回调函数
          },
          cancel: function () { 
             
              // 用户取消分享后执行的回调函数
          }
        });
      });


    });

前端加密所需的js类库,包含各种加密方法,文件列表: crypto-js-develop, 0 , 2017-06-02 crypto-js-develop\.gitignore, 23 , 2017-06-02 crypto-js-develop\.jshintrc, 1036 , 2017-06-02 crypto-js-develop\CONTRIBUTING.md, 482 , 2017-06-02 crypto-js-develop\Gruntfile.js, 1133 , 2017-06-02 crypto-js-develop\LICENSE, 1169 , 2017-06-02 crypto-js-develop\README.md, 4010 , 2017-06-02 crypto-js-develop\docs, 0 , 2017-06-02 crypto-js-develop\docs\QuickStartGuide.wiki, 18085 , 2017-06-02 crypto-js-develop\grunt, 0 , 2017-06-02 crypto-js-develop\grunt\config, 0 , 2017-06-02 crypto-js-develop\grunt\config\clean.js, 107 , 2017-06-02 crypto-js-develop\grunt\config\copy.js, 362 , 2017-06-02 crypto-js-develop\grunt\config\jshint.js, 354 , 2017-06-02 crypto-js-develop\grunt\config\jsonlint.js, 259 , 2017-06-02 crypto-js-develop\grunt\config\modularize.js, 8622 , 2017-06-02 crypto-js-develop\grunt\config\update_json.js, 999 , 2017-06-02 crypto-js-develop\grunt\tasks, 0 , 2017-06-02 crypto-js-develop\grunt\tasks\modularize.js, 2325 , 2017-06-02 crypto-js-develop\package.json, 1143 , 2017-06-02 crypto-js-develop\src, 0 , 2017-06-02 crypto-js-develop\src\aes.js, 8157 , 2017-06-02 crypto-js-develop\src\cipher-core.js, 29262 , 2017-06-02 crypto-js-develop\src\core.js, 22209 , 2017-06-02 crypto-js-develop\src\enc-base64.js, 3570 , 2017-06-02 crypto-js-develop\src\enc-utf16.js, 3607 , 2017-06-02 crypto-js-develop\src\evpkdf.js, 3558 , 2017-06-02 crypto-js-develop\src\format-hex.js, 1410 , 2017-06-02 crypto-js-develop\src\hmac.js, 3666 , 2017-06-02 crypto-js-develop\src\lib-typedarrays.js, 1869 , 2017-06-02 crypto-js-develop\src\md5.js, 9092 , 2017-06-02 crypto-js-develop\src\mode-cfb.js, 1698 , 2017-06-02 crypto-js-develop\src\mode-ctr-gladman.js, 1899 , 2017-06-02 crypto-js-develop\src\mode-ctr.js, 1062 , 2017-06-02 crypto-js-develop\src\mode-ecb.js, 484 , 2017-06-02 crypto-js-develop\src\mode-ofb.js, 924 , 2017-06-02 crypto-js-develop\src\pad-ansix923.js, 854 , 2017-06-02 crypto-js-develop\src\pad-iso10126.js, 706 , 2017-06-02 crypto-js-develop\src\pad-iso97971.js, 505 , 2017-06-02 crypto-js-develop\src\pad-nopadding.js, 138 , 2017-06-02 crypto-js-develop\src\pad-zeropadding.js, 609 , 2017-06-02 crypto-js-develop\src\pbkdf2.js, 4125 , 2017-06-02 crypto-js-develop\src\rabbit-legacy.js, 6224 , 2017-06-02 crypto-js-develop\src\rabbit.js, 6195 , 2017-06-02 crypto-js-develop\src\rc4.js, 3081 , 2017-06-02 crypto-js-develop\src\ripemd160.js, 8812 , 2017-06-02 crypto-js-develop\src\sha1.js, 3722 , 2017-06-02 crypto-js-develop\src\sha224.js, 1521 , 2017-06-02 crypto-js-develop\src\sha256.js, 5206 , 2017-06-02 crypto-js-develop\src\sha3.js, 10236 , 2017-06-02 crypto-js-develop\src\sha384.js, 1833 , 2017-06-02 crypto-js-develop\src\sha512.js, 13077 , 2017-06-02 crypto-js-develop\src\tripledes.js, 23837 , 2017-06-02 crypto-js-develop\src\x64-core.js, 8562 , 2017-06-02 crypto-js-develop\test, 0 , 2017-06-02 crypto-js-develop\test\aes-profile.js, 1104 , 2017-06-02 crypto-js-develop\test\aes-test.js, 4997 , 2017-06-02 crypto-js-develop\test\cipher-test.js, 17242 , 2017-06-02 crypto-js-develop\test\des-profile.js, 1040 , 2017-06-02 crypto-js-develop\test\des-test.js, 6318 , 2017-06-02 crypto-js-develop\test\enc-base64-test.js, 3089 , 2017-06-02 crypto-js-develop\test\enc-hex-test.js, 475 , 2017-06-02 crypto-js-develop\test\enc-latin1-test.js, 503 , 2017-06-02 crypto-js-develop\test\enc-utf16-test.js, 2121 , 2017-06-02 crypto-js-develop\test\enc-utf8-test.js, 1445 , 2017-06-02 crypto-js-develop\test\evpkdf-profile.js, 326 , 2017-06-02 crypto-js-develop\test\evpkdf-test.js, 1375 , 2017-06-02 crypto-js-develop\test\format-openssl-test.js, 1795 , 2017-06-02 crypto-js-develop\test\hmac-md5-profile.js, 947 , 2017-06-02 crypto-js-develop\test\hmac-md5-test.js, 2662 , 2017-06-02 crypto-js-develop\test\hmac-sha224-test.js, 2812 , 2017-06-02 crypto-js-develop\test\hmac-sha256-test.js, 2852 , 2017-06-02 crypto-js-develop\test\hmac-sha384-test.js, 3012 , 2017-06-02 crypto-js-develop\test\hmac-sha512-test.js, 3172 , 2017-06-02 crypto-js-develop\test\kdf-openssl-test.js, 644 , 2017-06-02 crypto-js-develop\test\lib-base-test.js, 3132 , 2017-06-02 crypto-js-develop\test\lib-cipherparams-test.js, 2654 , 2017-06-02 crypto-js-develop\test\lib-passwordbasedcipher-test.js, 966 , 2017-06-02 crypto-js-develop\test\lib-serializablecipher-test.js, 2365 , 2017-06-02 crypto-js-develop\test\lib-typedarrays-test.js, 2249 , 2017-06-02 crypto-js-develop\test\lib-wordarray-test.js, 3303 , 2017-06-02 crypto-js-develop\test\md5-profile.js, 743 , 2017-06-02 crypto-js-develop\test\md5-test.js, 2772 , 2017-06-02 crypto-js-develop\test\mode-cbc-test.js, 2024 , 2017-06-02 crypto-js-develop\test\mode-cfb-test.js, 2103 , 2017-06-02 crypto-js-develop\test\mode-ctr-test.js, 2213 , 2017-06-02 crypto-js-develop\test\mode-ecb-test.js, 1484 , 2017-06-02 crypto-js-develop\test\mode-ofb-test.js, 2042 , 2017-06-02 crypto-js-develop\t
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值