• 微信分享接口


    后台:
    public function share()
    {
        $appId = C("WX_APPID"); //appid
        $appSecret = C("WX_APPSECRET");// 秘钥
        $curl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_URL, $curl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        $res = curl_exec($ch);// 获得token
        curl_close($ch);
        $ress = json_decode($res,True);
        $token = $ress['access_token'];// 取出  至于存储代码就不列举了
        $jsurl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=".$token."&type=jsapi";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, 5);
        curl_setopt($ch, CURLOPT_URL, $jsurl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        //$res = file_get_contents($curl);// 获得token
        $js = curl_exec($ch);// 获得token
        curl_close($ch);
        $jss = json_decode($js,True);
        $jsapi_ticket = $jss['ticket'];//   取出JS凭证, 至于存储代码就不列举了
        $dataa['noncestr'] =  'sjijfdif'; //随意字符串 一会要传到JS里去.要求一致
        $dataa['jsapi_ticket'] = $jsapi_ticket;
        $dataa['timestamp'] = time();
        $dataa['url'] = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];//动态获取URL
        ksort($dataa);
        $signature = '';
        foreach($dataa as $k => $v){
            $signature .= $k.'='.$v.'&';
        }
        $signature = substr($signature, 0, strlen($signature)-1);
        $signature = sha1($signature);// 必填,签名,见附录1
        $this->assign(array(
            "appId"=>$appId,
            "timestamp"=>$dataa['timestamp'],
            "nonceStr"=>$dataa['noncestr'],
            "signature"=>$signature,
        ));
        $this->display();
    }

    前台:

    	<script type="text/javascript">
            // 通过config接口注入权限验证配置
            wx.config({
                debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
                appId: '{$appId}',   // 必填,公众号的唯一标识
                timestamp:'{$timestamp}' , // 必填,生成签名的时间戳
                nonceStr: '{$nonceStr}', // 必填,生成签名的随机串
                signature: '{$signature}',// 必填,签名
                jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'] // 必填,需要使用的JS接口列表
            });
            // 通过ready接口处理成功验证
            wx.ready(function(){
                // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。
                // 对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
    
                var options = {
                    title: '欢乐印推广活动',// 分享标题
                    link: window.location.href.split('#')[0],// 分享链接,记得使用绝对路径
                    imgUrl: "http://{$_SERVER['SERVER_NAME']}__CONTROLLER__/qrcode?t_id={$Think.get.t_id}&u_id={$Think.get.u_id}",// 分享图标,记得使用绝对路径
                    desc: '这是一个高大帅气的活动哦!',// 分享描述
                    success: function () {
                        console.info('分享成功!');
                        // 用户确认分享后执行的回调函数
                    },
                    cancel: function () {
                        console.info('取消分享!');
                        // 用户取消分享后执行的回调函数
                    }
                }
                wx.onMenuShareTimeline(options);// 分享到朋友圈
                wx.onMenuShareAppMessage(options);// 分享到朋友
            });
    	</script>


  • 相关阅读:
    本月时间按天显示
    微信小程序----当前时间的时段选择器插件(今天、本周、本月、本季度、本年、自定义时段)
    vuex进行传值
    echart 自定义 formatter
    git 登录流程
    Java学习-反射
    mysql数据类型char、varchar、text的一些区别
    微信小程序踩坑记录
    Rancher、Helm、HelmFile
    句子迷 2015_01_10
  • 原文地址:https://www.cnblogs.com/ningjiabing/p/10066098.html
Copyright © 2020-2023  润新知