微信小程序发送信息到公众号 - 古月容博客-php个人技术博客-技术交流分享

2018
IT资讯信息分享

微信小程序发送信息到公众号


js:


var app = getApp();
Page({
data: {
Loadingtime: '',
web_src: 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx5d6dd710f5e8c4a3&redirect_uri=Redirect_uri&response_type=code&scope=snsapi_base&state=123#wechat_redirect',
},
onLoad: function() {
wx.showToast({
title: "授权中",
icon: "loading",
mask: !0
});
setTimeout(function () {
wx.navigateTo({
url: "../index/index",
})
}, 2000) //延迟时间 这里是1秒
},
onReady: function() {},
onShow: function() {},
onHide: function() {},
onUnload: function() {},
onPullDownRefresh: function() {},
onReachBottom: function() {},
});
Redirect_uri 为后台接口回调地址 需urlEncode 处理。



view :



<view class="container">
<web-view src="{{web_src}}"/>

</view>



后台接口处理:

 function doPageGetWxcode(){//接口地址
    $code = $_GPC['code'];
$res=getunionId($code,$_W);

}

   function getopenida($code){   //授权获取code 取用户openid
    $appid = '*****************';//公众号wx5d6dd710f5e8c4a3
    $appsecret = '*****************';//公众号 appsecret 

    $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";//基础接口
          $curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//执行
$data = curl_exec($curl);
curl_close($curl);
           $data = json_decode($data,true);
      return $data['openid'];
    }



  function getunionId($code,$_W){
        $access_token=getaccess_tokenc($_W);//获取access_token;
        $openid=getopenida($code);//获取openid
        $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
        $curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$datao = curl_exec($curl);
curl_close($curl);
           $datao = json_decode($datao,true);

/**通过access_token和openid 获取unionid**/
        $unionid=$datao['unionid'];
        $res="**************"//查询是否存在unionid的数据
        if($res){    
       存在 修改操作数据
        }else{
      不存在 新增数据
        }

}



  function getaccess_tokenc($_W){       
      global $_GPC, $_W;
        $tokenName='access_token'.$_W['uniacid'];
        $timeName='access_token_time'.$_W['uniacid'];
        load()->classs('wesession');
        WeSession::start($_W['uniacid'], CLIENT_IP);
      if($_SESSION[$timeName]<time() || !$_SESSION[$tokenName]){  
    $appid = '*****************';//公众号wx5d6dd710f5e8c4a3
    $appsecret = '*****************';//公众号 appsecret 
     // $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
      $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret."";
     $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,$url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
            $data = curl_exec($ch);
            curl_close($ch);
            $data = json_decode($data,true);

//access_token需缓存
            $_SESSION[$tokenName]=$data['access_token'];
            $_SESSION[$timeName]=time()+300;
            return $data['access_token'];
      }else{      
       return $_SESSION[$tokenName];
      }
    }


基于微擎开发






×

感谢您的支持,我们会一直保持!

扫码支持
请土豪扫码随意打赏

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

打赏作者
版权所有,转载注意明处:古月容个人博客 » 微信小程序发送信息到公众号
标签: 小程序 公众号 推送信息

发表评论

表情

网友评论(0)