zoukankan      html  css  js  c++  java
  • tp5分享微信朋友圈

    php文件

    public function weixin(){
            $url = input('urll');//获取当前页面的url,接收请求参数
            $root['url'] = $url;
            //获取access_token,并缓存
            $file = RUNTIME_PATH.'/access_token';//缓存文件名access_token
            $appid='wx----------da7'; // 填写自己的appid
            $secret='53bd6-------------bde'; // 填写自己的appsecret
            $expires = 3600;//缓存时间1个小时
            if(file_exists($file)) {
            $time = filemtime($file);
            if(time() - $time > $expires) {
            $token = null;
            }else {
            $token = file_get_contents($file);
            }
            }else{
            fopen("$file", "w+");
            $token = null;
            }
            if (!$token || strlen($token) < 6) {
            $res = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret."");
            $res = json_decode($res, true);
            $token = $res['access_token'];
            // write('access_token', $token, 3600);
            @file_put_contents($file, $token);
            }
    
            //获取jsapi_ticket,并缓存
            $file1 = RUNTIME_PATH.'/jsapi_ticket';
            if(file_exists($file1)) {
            $time = filemtime($file1);
            if(time() - $time > $expires) {
            $jsapi_ticket = null;
            }else {
            $jsapi_ticket = file_get_contents($file1);
            }
            }else{
            fopen("$file1", "w+");
            $jsapi_ticket = null;
            }
            if (!$jsapi_ticket || strlen($jsapi_ticket) < 6) {
            $ur = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=$token&type=jsapi";
            $res = file_get_contents($ur);
            $res = json_decode($res, true);
            $jsapi_ticket = $res['ticket'];
            @file_put_contents($file1, $jsapi_ticket);
            }
    
            $timestamp = time();//生成签名的时间戳
            $metas = range(0, 9);
            $metas = array_merge($metas, range('A', 'Z'));
            $metas = array_merge($metas, range('a', 'z'));
            $nonceStr = '';
            for ($i=0; $i < 16; $i++) {
            $nonceStr .= $metas[rand(0, count($metas)-1)];//生成签名的随机串
            }
    
            $string1="jsapi_ticket=".$jsapi_ticket."&noncestr=".$nonceStr."&timestamp=".$timestamp."&url=".$url."";
            $signature=sha1($string1);
            $root['appid'] = $appid;
            $root['nonceStr'] = $nonceStr;
            $root['timestamp'] = $timestamp;
            $root['signature'] = $signature;
    
            echo json_encode($root);
        }  

    html模板文件

    <script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
    <script>
    window.onload=function(){
    var ajaxurl =  '{:url("index/weixin")}';
    var urll=location.href.split('#')[0];
    $.ajax({
        url:ajaxurl,
        type:"post",
        data:{urll:urll},
        dataType:"json",
        success:function(s){
            wx.config({
                debug: false, //分享成功后可以关闭 false
                appId: s.appid, 
                timestamp: s.timestamp, 
                nonceStr: s.nonceStr, 
                signature: s.signature,
                jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'] 
            });
            wx.ready(function(){
                wx.onMenuShareTimeline({
                    title: '{$info.goods_name}', // 分享标题
                    link: s.url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                    imgUrl: "http://www.xiwang0470.com/{$info.title_pic}", // 分享图标 使用绝对路径
                    success: function () {
    
                    }
                });
                wx.onMenuShareAppMessage({
                    title: '{$info.goods_name}', 
                    desc: '{$info.goods_name}', // 分享描述
                    link: s.url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
                    imgUrl: "http://www.xiwang0470.com/{$info.title_pic}", // 分享图标 使用绝对路径
                    type: '', // 分享类型,music、video或link,不填默认为link
                    dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
                    success: function () {
                    
                    }
                });
            });
            
        },
        error:function(){
            console.log("通信失败");
        }
    });
    }
    /* alert(location.href.split('#')[0]); */ //弹出的url必须与访问地址一致
    
    </script>
  • 相关阅读:
    Python 五个知识点搞定作用域
    python中的三元表达式(三目运算符)
    Python中 sys.argv的用法简明解释
    pycharm多行批量缩进和反向缩进快捷键
    Python终端如何输出彩色字体
    第8周LeetCode记录
    第7周Leetcode记录
    《Java核心技术》第九章读书笔记(集合)
    《Java核心技术》第八章读书笔记(泛型)
    《Java核心技术》第七章读书笔记(异常处理)
  • 原文地址:https://www.cnblogs.com/loveufofbi/p/13737675.html
Copyright © 2011-2022 走看看