微信jssdk分享

php

浏览数:148

2019-1-7

片段 1片段 2


js部分

var  weixin = {
    config:function(){

        //获取当前的网页地址
        var url=window.location.href.split("#")[0];
        //请求后端获得必要的签名
        $.get("jssdk.php",{"url":url},function(s){

            wx.congig({
                debug:false,//开启调试模式
                appId:s.appId,//必填,公众号的唯一标识
                timestamp:s.timestamp, //必填,生成签名的时间戳
                noceStr:s.noceStr, //必填,生成签名的的随机字符串
                signatrue:s.signatrue, //必填,签名,见附录一
                jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'chooseImage', 'uploadImage', 'downloadImage'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2

            })
        });
    },

    share:function (title,desc,url,img){
        //注意分享链接与图片地址的域名必须与公众号的安全域名一致
        var data = {
            title:title, //分享标题
            link: url, // 分享链接
            imgUrl: img, //分享图标
            desc: desc, //分享描述
            success: function (){
                //分享成功
            },
            error: function () {
                //分享失败
            }
        };

        //分享到朋友圈
        wx.onMenuShareTimeline({
            title:data.title,//分享标题
            link:data.url,//分享链接
            imgUrl:data.img,//分享图标
            success:data.success,//确认按钮
            cansl:data.ansel//取消按钮
        });
        //分享给好友
        wx.onMenuShareAppMessage({
            title:data.title,//分享标题
            link:data.link,//分享链接
            imgUrl:data.img,//分享图标
            type:"",//分享类型,music,video或link,不填默认为link
            dataUrl:"",//如果type是music或video,则要提供数据链接,默认为空
            success:data.success,//确认按钮
            cansl:data.ansel//取消按钮
        })
    }

}

$(window).load({
    wx.config();
    wx.ready(function(){
        wx.share("微信分享","微信分享","www.xxx.com","www.xxx.com/img.jpg");
    })
})


获取签名

<?php
/*************************************************
==================================================

    data/jssdk.php?url=提交页面的路径
    注意提交的路径,路径是调用JS-SDK的页面路径,包括?后面的参数,但不包括hash的内容
    return {appid,timestamp,nonecStr,signature}
==================================================
**************************************************/

//在这里设置你的APPID,AppSecret
$appid = "wx260971a15ae00050";
$secret ="bea7d4b21ddacbe7e7c036be3d5ac85d";

//开始
define("APPID",$appid);
define('SECRET', $secret);

$url = $_GET["url"];
echo sendJson($url);

function sendJson($url){
    $ticketTokenApi = getTicket();
    $timestamp = time();
    $nonceStr = createNonceStr();
    $string = "jsapi_ticket=$ticketTokenApi&noncestr=$nonceStr&timestamp=$timestamp&url=$url";
    $signature = sha1($string);
    $data = array(
        "appId"=> APPID,
        "timestamp"=>$timestamp,
        "nonceStr"=>$nonceStr,
        "signature"=>$signature
    );
    return json_encode($data);
}

//获取access_token  要设置IP白名单才能获取token
function getAccess(){
    $data = json_decode(file_get_contents("access.json"));
    if($data->time < time()){
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".APPID."&secret=".SECRET;
        $res = json_decode(getHttp($url));
        $access = $res->access_token;
        if($access){
            $data->access = $access;
            $data->time = time()+7000;
//          chmod("access.json",0755);
            $fp = fopen("access.json","w");
            fwrite($fp, json_encode($data));
            fclose($fp);
        }
    }else{
            $access = $data->access;
    }
    return $access;
}

//获取jsApi_ticket票据
function getTicket(){
    $data = json_decode(file_get_contents("ticket.json"));
    if($data->time < time()){
        $accessToken = getAccess();
        $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
        $res = json_decode(getHttp($url));
        $ticket = $res->ticket;
        if($ticket){
            $data->ticket = $ticket;
            $data->time = time()+7000;
            $fp = fopen("ticket.json","w");
            fwrite($fp, json_encode($data));
            fclose($fp);
        }
    }else{
        $ticket = $data->ticket;
    }
    return $ticket;
}

//随机字符串
function createNonceStr($length = 16) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    $str = "";
    for ($i = 0; $i < $length; $i++) {
      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
  }

//curl封装
function getHttp($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 500);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_URL, $url);
    $res = curl_exec($curl);
    curl_close($curl);
    return $res;
}

?>