当前位置: 首页 > 工具软件 > JSDK3 > 使用案例 >

laravel jsdk引入

常坚
2023-12-01

 后端代码

public function index(){
    $appid = 'wxxxxxxxxxxxxx1';//公众号后台获取
            $appsecret = 'd4239xxxxxxxxxxx7d77b';//公众号后台获取
            $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $appsecret;
            $res=$this->httpGet($url);
            $arr = json_decode($res,true);
            if(isset($arr['access_token'])){//判断是否拿到token,若无则显示而不显距离
                $sign = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$arr['access_token'].'&type=jsapi';
                $res=$this->httpGet($sign);
                $arr = json_decode($res,true);
                $params = [
                  'noncestr' => (string)mt_rand(10000, 99999),
                  'jsapi_ticket'=>$arr['ticket'],
                  'timestamp' => time(),
                  'url' => url('room/index'),
                  ];
                  ksort($params);
                  $sequence = '';
                  foreach ($params as $key => $value) {
                      $sequence .= "$key=$value&";
                  }
                  $sequence = mb_substr($sequence, 0, -1);
                  $signature=sha1($sequence);
                  $params['appid'] = $appid;
                  $params['sign'] = $signature;
            }else{
                  $params = [
                  'noncestr' => (string)mt_rand(10000, 99999),
                  'jsapi_ticket'=>'',
                  'timestamp' => time(),
                  'url' => url('room/index'),
                  ];
                  $params['appid'] = $appid;
                  $params['sign'] ='';
            }
     $params = json_encode($params);
     return view('room.index',['arr'=>json_decode($params)]);
}
private function httpGet($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;
    }

前端jq代码

 $(function(){
        wx.config({
            debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
            appId: '{{$arr->appid}}', // 必填,公众号的唯一标识
            timestamp: '{{$arr->timestamp}}', // 必填,生成签名的时间戳
            nonceStr: '{{$arr->noncestr}}', // 必填,生成签名的随机串
            signature: '{{$arr->sign}}',// 必填,签名
            jsApiList: [
                'getLocation',
                'openLocation',
            ]
        })
        wx.ready(function(){
            //$('.loading').show();//加载层
            wx.getLocation({//jsdk功能
           

            });
        })
        wx.error(function(res){
            //$('.loading').hide();
            //$('.loading').show();
            $('#hotels').html( $('#hotel_msg').html());
        });
    });

 

 类似资料: