Giter Site home page Giter Site logo

wechatshare-js's Introduction

wechatShare-JS

微信JSSDK分享到朋友圈、微博、QQ好友。 ##升级 为了更灵活的使用,请使用V0.1.1版本,原V0.1.0请点击这里查看。

JS-SDK使用权限签名算法步骤

  1. 使用appId、appSecret获取AccessToken:

http请求方式: GET https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET 返回参数: {"access_token":"ACCESS_TOKEN","expires_in":7200} 文档地址: https://mp.weixin.qq.com/wiki/15/54ce45d8d30b6bf6758f68d2e95bc627.html

  1. 使用1)中AccessToken获取jsapi_ticket:

http请求方式: GET https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi 返回参数: {"errcode":0,"errmsg":"ok","ticket":"bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA","expires_in":7200}

  1. 使用2)中jsapi_ticket生成signature:

签名生成规则如下:参与签名的字段包括noncestr(随机字符串), 有效的jsapi_ticket, timestamp(时间戳), url(当前网页的URL,不包含#及其后面部分) 。对所有待签名参数按照字段名的ASCII 码从小到大排序(字典序)后,使用URL键值对的格式(即key1=value1&key2=value2…)拼接成字符串string1。这里需要注意的是所有参数名均为小写字符。对string1作sha1加密,字段名和字段值都采用原始值,不进行URL 转义。

准备工作

1.引入jquery、sha1加密、weixin、share插件这4个JS文件

<script src="js/jquery-1.11.1.js"></script>
<script src="js/jquery.sha1.js"></script>
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script src="js/jquery.wechat.share.js"></script>

使用

在页面中添加script脚本,建议通过后台产生签名的方式写入到前端页面,这样更安全(推荐使用优先级如下)

<script>
  $(function(){

    //第一种方式:使用appId,signature
    var shareData = {
        appId: 'wx2c82806de7e9cc08',
		signature: '0f9de62fce790f9a083d5c99e95740ceb90c27ed',
        title: '标题',
        desc: '简介',
        link: '分享的链接。一般可以用location.href',
        imgUrl: '分享的图片链接地址(图片一定要是网络图片地址哦,这样才能保证别人看到的图片是正常显示的)'
    };
    $.wechatShare(shareData);

    //第二种方式:使用appId,accessToken
    shareData = {
        appId: 'wx2c82806de7e9cc08',
        accessToken: '37vCDdOYC1nbgDZD__-Qs_poFtAmYkumU7xxxxx6Lj66_xJ8bj8LDFY3SwvBe1QHQ4HT_fzMjSAZeeQ-BhvVvUh8K00j4CWFOVa-N4QQTcAFAVVY',
        title: '标题',
        desc: '简介',
        link: '分享的链接。一般可以用location.href',
        imgUrl: '分享的图片链接地址(图片一定要是网络图片地址哦,这样才能保证别人看到的图片是正常显示的)'
    };
    $.wechatShare(shareData);

    //第三种方式:使用appId,appSecret
    shareData = {
        appId: 'wx2c82806de7e9cc08',
        appSecret: 'cc4c224b50fkjlksdafkldfsakljsdfk',
        title: '标题',
        desc: '简介',
        link: '分享的链接。一般可以用location.href',
        imgUrl: '分享的图片链接地址(图片一定要是网络图片地址哦,这样才能保证别人看到的图片是正常显示的)'
    };
    $.wechatShare(shareData);
  })
</script>

完毕

是不是感觉非常简单~~

wechatshare-js's People

Contributors

jiangxianli avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.