logo头像

迪拜 ❤ 天空

Thinkphp微信Auth实战开发

title: Thinkphp微信Auth实战开发

categories: php

tags:[Thinkphp]


Thinkphp微信Auth实战开发

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* Created by PhpStorm.
* User: Mikkle
* Email:776329498@qq.com
* Date: 2016/12/28
* Time: 14:08
*/

namespace app\api\controller;
use com\Wechat;
use think\Cache;
use think\Model;
use think\Session;

class Auth extends Base
{
protected $isWechatBrowser = false;
protected $scope = 'snsapi_base';
protected $uuid;
protected $open_id;
protected $member_info;
protected $options;
protected $isReg = false;
protected $no_login = false;
public function _initialize()
{
parent::_initialize(); // TODO: Change the autogenerated stub
$user_agent = $this->request->server('HTTP_USER_AGENT');
if (! strpos($user_agent, 'MicroMessenger') === false ) $this->isWechatBrowser = true;
//判断提交方式和是否微信浏览器
if ($this->request->method() == 'GET' && $this->isWechatBrowser === true){
//未登录 重新登录
if (!$this->checkAuth()&& !$this->no_login ) $this->wxoauth();
$this->isLogin=true;
//设置全局登录
$this->loginGlobal();
if($this->isReg){
// if(!$this->checkUuidMobile()) $this->redirect('/index/WC_html_1/mainContainer.html#user/user_blind.html');
}
}

}

/**
* 设置全局登录
* #User: Mikkle
* #Email:776329498@qq.com
* #Date:
* @return bool
*/
public function loginGlobal(){
if (!$this->open_id) return false;
$member=model('base/user');
$member_info=$member->infoByOpenId($this->open_id);
if (!$member_info){
$data=[
'nickname'=>$this->member_info['nickname'],
'headimgurl'=>$this->member_info['headimgurl'],
'wechat_openid'=>$this->open_id,
];
$member->editData($data);
$member_info=$member->infoByOpenId($this->open_id);
$this->isReg = true;
}
$fans=model('base/WeFans');
$fans_info=$fans->infoByOpenId($this->open_id);
$this->member_info['appid']=$this->getOptions();
if (!$fans_info){
$fans->allowField(true)->save($this->member_info);
}else{
$fans->allowField(true)->save($this->member_info,['id' => $fans_info['id']]);
}
//设置全局登录 微信浏览器登录
if (!$this->checkLoginGlobal()) $this->setLoginGlobal($member_info,2);
$this->uuid = $member_info['uuid'];
}

/**
* 微信内页登录
* #User: Mikkle
* #Email:776329498@qq.com
* #Date:
* @return bool
*/
public function wxoauth()
{
if ($this->checkAuth()) return false;
$code = $this->request->param('code');
if (Session::has('code','html5') && $code == Session::get('code','html5') && $code) return false;
if (!$this->options) $this->getOptions();
$options = [
'token'=>$this->options["token"], //填写你设定的key
'appid'=>$this->options["appid"], //填写高级调用功能的app id
'appsecret'=>$this->options["appsecret"] //填写高级调用功能的密钥
];
Session::set('appid',$this->options["appid"],'html5');
$we_obj = new Wechat($options);
if ($code) {
$json = $we_obj->getOauthAccessToken();
if (!$json) {
$this->redirect($this->request->baseUrl());
// die('获取用户授权失败,请重新确认<a href="'.$this->request->baseUrl().'" >点击重试</a>');
}
$this->open_id = $json["openid"];
$access_token = $json['access_token'];
Session::set('open_id',$this->open_id,'html5');
Session::set('code',$code,'html5');
$user_info = $we_obj->getUserInfo($this->open_id);
$user_info['appid']=Session::get('appid','html5');
if ($user_info && !empty($user_info['nickname'])) {
$this->member_info = $user_info;
} elseif (strstr($json['scope'],'snsapi_userinfo')!==false) {
$user_info = $we_obj->getOauthUserinfo($access_token,$this->open_id);
$user_info['appid']=Session::get('appid','html5');
if ($user_info && !empty($user_info['nickname'])) {
$this->member_info = $user_info;
} else {
return $this->open_id;
}
}
if ($this->member_info) {
Session::set('member_info',$this->member_info,'html5');
Session::set('open_id',$this->open_id,'html5');
Session::delete('wx_redirect','html5');
return $this->open_id;
}
$this->scope = 'snsapi_userinfo';
}

if ($this->scope=='snsapi_base') {
$url = $this->request->url(true);
Session::set('wx_redirect',$url,'html5');
} else {
$url =Session::get('wx_redirect','html5');
}
if (!$url) {
Session::delete('wx_redirect','html5');
die('获取用户授权失败');
}


$oauth_url = $we_obj->getOauthRedirect($url,"wxbase",$this->scope);
header('Location:'.$oauth_url);
die;


}

/**
* 获取微信Options 使用缓存
* #User: Mikkle
* #Email:776329498@qq.com
* #Date:
* @param int $appid
* @return mixed
*/
public function getOptions( $appid = 1){
if(Cache::has('weOptions')){
$weOptions=Cache::get('weOptions');
}else{
$weOptions=model('we/We')->info($appid)->toarray();
Cache::set('weOptions',$weOptions,3600);
}
$this->options=$weOptions;
$this->appid=$weOptions["appid"];
return $weOptions["appid"];

}

/**
* 判断登录是否成功
* #User: Mikkle
* #Email:776329498@qq.com
* #Date:
* @return bool
*/
public function checkAuth()
{
if (!Session::has('member_info','html5')&&!Session::has('open_id','html5')){
return false;
}else{
$this->member_info = Session::get('member_info','html5');
$this->open_id = Session::get('open_id','html5');
$this->appid = Session::get('appid','html5');
if (!$this->options) $this->getOptions();
return true;
}
}

}

评论系统未开启,无法评论!