WxloginController.php
5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
// +----------------------------------------------------------------------
// | bronet [ 以客户为中心 以奋斗者为本 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;
use cmf\controller\HomeBaseController;
use think\Session;
use think\Db;
class WxloginController extends HomeBaseController
{
/**
* 初始化
*/
public function _initialize()
{
parent::_initialize(); // TODO: Change the autogenerated stub
if(!Session::get('user.accessToken')){
session('uid',null);
session('user.accessToken',null);
session('user.accessTime',null);
$this->OAuth('http://' .$_SERVER['HTTP_HOST']);
}else{
// 判断accesstoken时间
$overTime = Session::get('user.accessTime');
if(($overTime + 7100) < time() ){
// 超过时间,重新获取accesstoken
session('uid',null);
session('user.accessToken',null);
session('user.accessTime',null);
$this->OAuth('http://' .$_SERVER['HTTP_HOST']);
}else{
// 未超过
// 查询用户是否存在
$uid = Session::get('uid');
$id = Db::name('Member')->where(array('id'=>$uid,'status'=>1))->value('id');
if(!$id){
$this->OAuth('http://' .$_SERVER['HTTP_HOST']);
}else{
$url = 'http://' .$_SERVER['HTTP_HOST'];
}
}
}
}
/**
* Main:判断是否是微信打开
*/
function is_weixin(){
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
return true;
}
return false;
}
/**
* Main:授权登录 获取openid
*/
public function OAuth($redirect_uri){
echo "<pre/>";
print_r($redirect_uri);
die;
if (!Session::get('uid')) {
$url = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$temp_one = explode('code=',$url);
if(!empty($temp_one[1])){
$temp_two = explode('&state',$temp_one[1]);
if ($temp_two[0]) {
//第二步 获取access_token
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx100c2e5f27e34b92&secret=4de2264efb220ea89c1095580f4f4350&code=" . $temp_two[0] . "&grant_type=authorization_code";
$resp = file_get_contents($url);
$resp_arr = json_decode($resp, true);
// 将accesstoken 存session
Session::set('user.accessToken',$resp_arr['access_token']);
Session::set('user.accessTime',time());
//第三步
$refresh_token = $resp_arr['refresh_token'];
$url1 = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=wx100c2e5f27e34b92&grant_type=refresh_token&refresh_token=".$refresh_token;
$resp1 = file_get_contents($url1);
$resp_arr1 = json_decode($resp1, true);
//第四步
$access_token = $resp_arr1['access_token'];
$openid = $resp_arr1['openid'];
$url2 = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
$resp2 = file_get_contents($url2);
$resp_arr2 = json_decode($resp2, true);
$user_headFile['headimgurl'] = $resp_arr2['headimgurl'];
// 图片路径
$save_path['wx_name'] = $resp_arr2['nickname'];
$save_path['wx_pic'] = $resp_arr2['headimgurl'];
$save_path['openid'] = $resp_arr2['openid'];
$save_path['create_time'] = time();
$save_path['update_time'] = time();
$is_isset = Db::name('Member')->where(array('openid' => $resp_arr2['openid']))->find();
if(!$is_isset){
$user_id = Db::name('Member')->insertGetId($save_path);
Session::set('uid',$user_id);
}else{
Session::set('uid',$is_isset['id']);
}
} else {
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx100c2e5f27e34b92&redirect_uri=" . $redirect_uri . "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
Header("Location: $url");
exit;
}
}else{
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx100c2e5f27e34b92&redirect_uri=" . $redirect_uri . "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
Header("Location: $url");
exit;
}
}
}
}