/**
* 微信H5登陆
*/
public function weixin_login()
{
$ua = $_SERVER['HTTP_USER_AGENT'];
//MicroMessenger 是android/iphone版微信所带的
//Windows Phone 是winphone版微信带的 (这个标识会误伤winphone普通浏览器的访问)
if(strpos($ua, 'MicroMessenger') == false && strpos($ua, 'Windows Phone') == false){
//普通浏览器
echo "<script>alert('请用微信客户端访问');</script>";
$this->redirect('login');
exit;
}
if(empty(session('h5_uid'))){
if(empty($_GET['code'])){
$this->getCode();
exit;
}
$code=$_GET['code'];
$this->getAccessToken($code);
}
//如果该微信用户未绑定基础信息,就去绑定基础信息
$info=Db::name('user')->where('id',session('h5_uid'))->find();
if(empty($info['name'])){
$this->redirect('register');
}
if(!empty($info['mobile'])){
session('mobile',$info['mobile']);
}
//如果是购买会员VIP页面过来的,则微信登陆后返回企业自画像
if(cookie('is_vip')==1){
$this->redirect('Vip/index');
}
//如果是企业自画像过来,则微信登陆后返回企业自画像
if(cookie('is_business')==1){
$this->redirect('Business/index');
}
$this->redirect('Mp/my');
exit;
}
/**
*获取code值
*TODO::从哪里来就跳转回哪里
*/
public function getCode()
{
$redirect_uri=urlencode("http://".$_SERVER['HTTP_HOST']."/home/index/weixin_login");
$response_type='code';
$scope='snsapi_userinfo';
$url= "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".config('sys.appid')."&redirect_uri=".$redirect_uri."&response_type=code&scope=".$scope."&state=login#wechat_redirect";
header("location:$url");
}
/**
* 根据code值获取access_token,openid, 将refresh_token写入session里,或者不写
*/
public function getAccessToken($code)
{
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".config('sys.appid')."&secret=".config('sys.appsecret')."&code=".$code."&grant_type=authorization_code";
$result = curl_get($url);
$wxResult = json_decode($result, true); //转换为数组
if(empty($wxResult)) {
echo json_encode(['code'=>0,'status'=>false,'msg'=>'获取access_token及openID时异常,微信内部错误']);
exit();
} else {
$loginFail = array_key_exists('errcode', $wxResult);
if ($loginFail) {
echo json_encode($wxResult);
// echo $this->processLoginError($wxResult);
exit();
} else {
return $this->grantToken($wxResult);
}
}
}
private function grantToken($wxResult){
$openid=$wxResult['openid'];
$user=Db::name("user")->where('openid',$openid)->find();
if(!empty($user)){
$uid = $user['id'];
}else{
$uid = $this->newUser($openid);
}
$userInfo= $this->saveUserInfo($wxResult,$uid);
// $cachedValue=$this->prepareCachedValue($wxResult,$uid);
// $token=$this->saveToCache($cachedValue);
// cookie('mobile',$user['phone']);
session('h5_uid',$uid);
session('openid',$openid);
return true;
// return $this->redirect('Mp/my');
}
/**
* 保存用户信息
* 背景:拿到access_token和openid之后,进一步保存用户信息
* @param array() $wxResult 示例:
* @return bool or false
*/
private function saveUserInfo($wxResult,$uid)
{
$url="https://api.weixin.qq.com/sns/userinfo?access_token=".$wxResult['access_token']."&openid=".$wxResult['openid']."&lang=zh_CN";
$res=curl_get($url);
$wxRes=json_decode($res,true);
unset($wxRes['privilege']);
if(empty($wxRes)){
Log::error('保存用户信息失败,微信服务器返回的数据为空');
}else{
$loginFail = array_key_exists('errcode', $wxRes);
if ($loginFail) {
Log::error('请求信息出错,无法保存用户信息:'.$wxRes);
}else{
unset($wxRes['language']);
$user = Db::name('user')->where('id',$uid)->update($wxRes);
}
}
return $uid;
}
/*
* 如果不存在openid 就插入数据库中
* */
private function newUser($openid)
{
$id=Db::name("user")->insertGetId(['openid'=>$openid,'add_time'=>time()]);
return $id;
}