url scheme 實現跳轉到小程式頁面
技術標籤:小程式
獲取小程式scheme碼,適用於簡訊、郵件、外部網頁等拉起小程式的業務場景。通過該介面,可以選擇生成到期失效和永久有效的小程式碼
/**
* 獲取url scheme 介面
*/
public function getUrlScheme()
{
$access_token = $this->get_accept_access_token();
$path = 'pages/myindex/myjoinguide';
//query 是指 傳的引數
$scene = 'id=19';
$post_data = [
'jump_wxa' => [
'path' => $path,
'query' => $scene
],
'is_expire' => true,
];
$post_data = json_encode($post_data);
// dd($post_data);
$result = $this->api_notice_increment($url, $post_data);
dd(json_decode($result));
}
//獲取接單小程式access token
public function get_accept_access_token()
{
$appid = env('ACCEPT_MINI_PROGRAM_APPID');
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
$data = $this->curl_get($url);
$dats = json_decode($data, true);
return $dats['access_token'];
}
public function api_notice_increment($url, $data)
{
$ch = curl_init();
$header = [
"Accept-Charset" => "utf-8"
];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
// var_dump($tmpInfo);
// exit;
if (curl_errno($ch)) {
return false;
} else {
// var_dump($tmpInfo);
return $tmpInfo;
}
}
呼叫介面getUrlScheme得到
h5頁面中加上
location.href = '那個openlink連結' 即可
關於微信小程式獲取url scheme的一些注意事項:
1、請求方法為post,注意jump_wxa引數的值為object;
2、Android系統不支援直接識別URL Scheme,使用者無法通過Scheme正常開啟小程式,開發需要使用H5頁面中轉,再跳轉到Scheme實現開啟小程式,但是並不是所有的瀏覽器都支援;
3.注意是在非微信瀏覽器開啟的