微信公众号 唤醒手机导航APP 一看就懂 复制即用

公司自研发框架,基本上没啥看不懂的 基本都是直接复制用就好了!希望能帮助到需要的朋友!

新建俩个同级文件用来保存 jsapi_ticket 和 access_token的文件 命名:jsapi_ticket.json 和 access_token.json(因为不能频繁去获取所以在规定时间内(7200秒)不要去重新获取 用第一次获取到的)

JSSDK文件:

<?php

class JSSDK {

    private $appId;
    private $appSecret;

    public function __construct($appId, $appSecret) {
        $this->appId = $appId;
        $this->appSecret = $appSecret;
    }

    public function getSignPackage() {
        $jsapiTicket = $this->getJsApiTicket();
        $url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";  # 地址要是当前调用地图的页面地址  
        $timestamp = time();
        $nonceStr = $this->createNonceStr();

        // 这里参数的顺序要按照 key 值 ASCII 码升序排序
        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$timestamp&url=$url";

        $signature = sha1($string);

        $signPackage = array(
            "appId" => $this->appId,
            "nonceStr" => $nonceStr,
            "timestamp" => $timestamp,
            "url" => $url,
            "signature" => $signature,
            "rawString" => $string
        );
        return $signPackage;
    }

    private 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;
    }

    private function getJsApiTicket() {
        $data = json_decode(file_get_contents(dirname(__FILE__)."/jsapi_ticket.json"));
        if ($data->expire_time < time()) {
            $accessToken = $this->getAccessToken();
            $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
            $res = json_decode($this->httpGet($url));
            $ticket = $res->ticket;
            if ($ticket) {
                $data->expire_time = time() + 7000;
                $data->jsapi_ticket = $ticket;
                $fp = fopen(dirname(__FILE__)."/jsapi_ticket.json", "w");
                fwrite($fp, json_encode($data));
                fclose($fp);
            }
        } else {
            $ticket = $data->jsapi_ticket;
        }

        return $ticket;
    }

    private function getAccessToken() {
        $data = json_decode(file_get_contents(dirname(__FILE__)."/access_token.json"));
        if ($data->expire_time < time()) {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
            $res = json_decode($this->httpGet($url));
            $access_token = $res->access_token;
            if ($access_token) {
                $data->expire_time = time() + 7000;
                $data->access_token = $access_token;
                $fp = fopen(dirname(__FILE__)."/access_token.json", "w");
                fwrite($fp, json_encode($data));
                fclose($fp);
            }
        } else {
            $access_token = $data->access_token;
        }
        return $access_token;
    }

    private function httpGet($url) {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_TIMEOUT, 500);
        curl_setopt($curl, CURLOPT_URL, $url);

        $res = curl_exec($curl);
        curl_close($curl);

        return $res;
    }

}

 

<?php
class indexController extends commonController{
    /**
     * 测试用  只需要传你公众号的 appid 和 appsecret
     */
    public function AGetJsApiConfAction(){
        $Config = Config::getInstance();
        $getWechat = $Config->getWechat();
        $appId = $getWechat['appid'];
        $appSecret = $getWechat['appsecret'];

        $JSSDK = new JSSDK($appId,$appSecret);
        $jsapiTicket = $JSSDK->getSignPackage();
        $this->view->setVariable("jsapiTicket", $jsapiTicket);
        $this->view->setViewfilepath(MvcReg::$_moduleName . '/views/' . MvcReg::$_actionName . 'View.php');
        $this->view->render();
    }
}

 

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <?php include_once MvcReg::$_moduleName . '/views/layout/head.php'; ?>
    </head>
    <body>
        <h1 id="openLocation">点击唤醒手机安装导航APP</h1>

        <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
        <script>
            wx.config({
                debug: false,
                appId: '<?php echo $jsapiTicket['appId']?>',
                timestamp: '<?php echo $jsapiTicket['timestamp']?>',
                nonceStr: '<?php echo $jsapiTicket['nonceStr']?>',
                signature: '<?php echo $jsapiTicket['signature']?>',
                jsApiList: [
                    'openLocation',
                ]
            });
            wx.ready(function () {
                document.querySelector('#openLocation').onclick = function () {
                    wx.openLocation({
                        latitude: 31.2192440000,
                        longitude: 121.3607530000,
                        name: '上海市长宁区统一梦时代大厦',
                        address: '上海市长宁区天山西路568号',
                        scale: 14,
                        infoUrl: 'http://weixin.qq.com'
                    });
                };
            });
        </script>
    </body>
</html>

 

转载于:https://www.cnblogs.com/G921123/p/11394949.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值