请求函数
public function get_code(){
//获取域名
$yuming=cmf_get_option('site_info')['site_yuming'];
//获取appid
$appid=cmf_get_option('site_info')['site_appid'];
//回调地址
$redirect_uri='http%3a%2f%2f'.$yuming.'%2fportal%2findex%2fget_openid';
$url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_base&state=123#wechat_redirect?".time();
//跳转微信接口
Header("Location:$url");
}
回调函数
public function get_openid(){
$data1=input();//接收回调信息
$appid=cmf_get_option('site_info')['site_appid'];
$secret=cmf_get_option('site_info')['site_secret'];
//请求openid接口
$data=file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$secret."&code=".$data1['code']."&grant_type=authorization_code");
//处理信息
$data=json_decode($data,true);
$openid='';
$count=1;
if(empty($data['openid'])){
$openid='j'.time();
}else{
$openid= $data['openid'];
if(!Db::name('member')->where('openid',$openid)->find()){
Db::name('member')->insert(['openid'=>$openid,'time'=>time()]);
}else{
$count=2;
}
}
$url='http://'.cmf_get_option('site_info')['site_yuming'].'/dist/index.html?openid='.$openid.'&count='.$count;
//跳转项目 入口地址
$this->redirect($url);
}