实现这功能首先,小程序端添加业务域名
php代码
<?php
declare (strict_types=1);
namespace app\controller\Admin;
use app\model\Set;
use app\Request;
class Admin_Url_Scheme
{
public function getScheme(Request $request) {
$appid = '小程序appid';
$secret = '小程序appsecret';
//path是要跳转的小城页面地址,query为要携带的参数
$body = ['jump_wxa'=>['path'=>'/pages/index/index','query'=>$scheme['name']]];
$tokenurl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $secret;
$data = file_get_contents($tokenurl);
$data = json_decode($data, true);
$token = $data['access_token'];
$url = "https://api.weixin.qq.com/wxa/generatescheme?access_token=" . $token;
$data = $this->curl_post($url, null, $body);
if ($data['errmsg'] == 'ok'){
return json(['code' => 200,'data'=>$data['openlink'], 'msg' =>'操作成功']);
}else{
return json(['code' => 201,'data'=>$data, 'msg' =>'异常']);
}
}
function curl_post($url,$herder,$body){
//一般框架都会自带GuzzleHttp,没有的请手动安装,或者利用curl post请求
$client = new \GuzzleHttp\Client();
try {
$pram = $client->post($url,[
'headers'=>$herder,
'json'=>$body,
]);
$content = json_decode($pram->getBody()->getContents(),true);
return $content;
}catch (ErrorException $exception){
return $exception->getCode();
}
}
}
下面使用js的location.href
location.href = 'https://***.***.com'
我前端用的是uniapp,vue的话要安装axios或者jq
<template>
<view class="content">
<image class="logo" src="/static/logo.png"></image>
</view>
</template>
<script>
export default {
data() {
return {}
},
onLoad() {
const that = this
that.getScheme()
},
methods: {
getScheme() {
uni.request({
url: 'https://***.****.com/admin/scheme', //仅为示例,并非真实接口地址。
data: {
name: 'qiqvxinxikeji'
},
success: (res) => {
if (res.data.code === 200) {
location.href = res.data.data
return;
}
}
});
}
}
}
</script>
<style lang="scss">
.content{
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
短信内容放前端链接地址,这样就可以通过打开游览器跳转到小程序了
注意
1.必须是发布的小程序才会被唤醒
2.接口有上限,如果需求不高不用理会
解决方法就是在请求的时候用redis存储url scheme返回的地址和请求时间, 设置一段时间再次发送新的请求再次覆盖存储数据