• 使用微信扫一扫时获取sign签名


    private $appId = '你的APPID';
    private $appSecret = '你的APPsecret';
    /**
         * 获取签名信息
         * @return array
         */
        public function getSignPackage()
        {
            $jsapiTicket = $this->getJsApiTicket();
            $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
            $url = "$protocol$_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
            );
            return $signPackage;
        }
    
        /**
         * 获取access_token
         * @return mixed
         * 因为每天最多只能向微信请求200次access_token,所以要存到缓存里或者本地文件中
         */
        private function getAccessToken()
        {
            if(Cache::get('access_token'))
            {
                $access_token = Cache::get('access_token');
                return $access_token;
            }
            else
            {
                $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
                $res = json_decode(file_get_contents($url));
                $access_token = $res->access_token;
                Cache::put('access_token',$access_token,7000);
                return $access_token;
            }
        }
    
        /**
         * 获取ticket
         * @return mixed
         */
        private function getJsApiTicket()
        {
            if(Cache::get("jsapi_ticket"))
            {
                $jsapi_ticket = Cache::get('jsapi_ticket');
                return $jsapi_ticket;
            }
            else
            {
                if(Cache::get('access_token'))
                {
                    $accessToken = Cache::get('access_token');
                    $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
                    $res = json_decode(file_get_contents($url));
                    $ticket = $res->ticket;
                    Cache::put('jsapi_ticket',$ticket,7000);
                    return $ticket;
                }
                else
                {
                    $accessToken = $this->getAccessToken();
                    $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
                    $res = json_decode(file_get_contents($url));
                    $ticket = $res->ticket;
                    Cache::put('jsapi_ticket',$ticket,7000);
                    return $ticket;
                }
            }
        }
    
        /**
         * 随机数生成算法
         * @param int $length
         * @return string
         */
        private function createNoncestr($length = 32)
        {
            $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
            $str = "";
            for ($i = 0; $i < $length; $i++) {
                $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
            }
            return $str;
        }
  • 相关阅读:
    浏览器内核
    link和@import的区别
    跨域iframe如何通信
    数组、对象基本操作
    浏览器兼容性问题
    微信H5开发
    前端面试整理
    js多维数组转一维数组
    js实现继承
    javascript函数闭包(closure)
  • 原文地址:https://www.cnblogs.com/wxy0126/p/12049988.html
Copyright © 2020-2023  润新知