php代码(Laravel框架):
use GuzzleHttp\Client;
public function is_follow(Request $request)
{
$user_info=DB::table('sdb_user')->where('id',$this->user['id'])->value('openid');
$data=DB::table('sdb_config')->where('name','token')->value('value');
$data=json_decode($data);
//判断是否需要重新获取token
if ($data->expire_time < time()){//到期
$appId=env('WECHAT_APP_ID');
$appSecret=env('WECHAT_APP_SECRET');
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
$res1=$this->httpGet($url);
$res = json_decode($res1);
$res_time=[
'token'=>$res->access_token,
'expire_time'=>time()+7000,
];
$res_time=json_encode($res_time);
DB::table('sdb_config')->where('name','token')->update(['value'=>$res_time]);
$access_token = $res->access_token;
}else{
$access_token=$data->token;
}
//获取用户是否关注公众号
$client1 = new Client();
$client1 = $client1->request('get', "https://api.weixin.qq.com/cgi-bin/user/info", [
'query' => [//get方式
//'access_token' => $pageContents['access_token'],
'access_token' => $access_token,
'openid' => $user_info,
'lang' => 'zh_CN',
],
]);
$res = $client1->getBody();
$res = json_decode($res, true);
return format($res['subscribe'],'是否关注');
//"subscribe"
}
function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
前端代码:
is_follow() {
uni.request({
url: this.$site_url + 'index/is_follow',
method: 'GET',
header:{
"Authorization":"Bearer "+ uni.getStorageSync("token")
},
success: res => {
if(res.data.code == 0){
window.location.href = "https://mp.weixin.qq.com/mp/profile_ext?action=home&__biz=Mzg5NDMzODg2NQ==&scene=110#wechat_redirect";
}
},
})
},
如果没有关注直接跳关注页。
注:如何快速获取biz
第一步:点击公众号右上角然后弹出公众号标识,然后点击查看历史消息
第二步:在弹出的窗口中选择复制链接地址
然后我们复制一下就可以看到对应的BIZ了