获取微信共享收货地址

前言:代码比较简单,微信的配置太多了,趟了一堆坑

注意事项:

1.支付授权目录,必须一致,一个符号和大小都不能错

image.png

例如前端url是:www.test.net/Address/,那么授权目录必须也是www.test.net/Address/,"/"都不能少,
前端框架会自动把"/"去掉,我们折腾半天都是泪啊
2.公众号获取用户信息
微信公众号->开发->接口权限->网页授权获取用户基本信息
3.公众号设置
微信公众号->设置->功能设置(都设置上)
4.白名单设置
微信公众号->安全中心->IP白名单

1.阅读文档

https://pay.weixin.qq.com/wiki/doc/api/external/jsapi.php?chapter=7_8&index=7

2.前端代码

IOS好像没反应一样,哈哈,其实只是IOS屏蔽了alert弹框,效果还是有的





    获取共享地址
    
    









3.后端代码

调用方法,

        $JssdkService =new JssdkService($appid,$appsecret,$url);
        $data=$JssdkService->getSignPackage();

服务类JssdkService代码

appId = $appId;
        $this->appSecret = $appSecret;
        $this->url = $url;
    }

    public function getSignPackage()
    {
        $jsapiTicket = $this->getJsApiTicket();
        $url = $this->url; //$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $timestamp = time();
        $nonceStr = $this->createNonceStr();

        // 这里参数的顺序要按照 key 值 ASCII 码升序排序
        $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$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()
    {
        // jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
        $ticket = Cache::get('weixini_ticket');

        if (empty($ticket)) {
            $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;
            Cache::set('weixini_ticket', $ticket, 7000);

        }
        return $ticket;
    }

    private function getAccessToken()
    {
        // access_token 应该全局存储与更新,以下代码以写入到文件中做示例
        $access_token = Cache::get('weixini_AccessToken');
        if (empty($access_token)) {
            $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;

            $res = Cache::set('weixini_AccessToken', $access_token, 7000);

        }

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

你可能感兴趣的:(获取微信共享收货地址)