當前位置:
首頁 >
Fastadmin/thinkphp 微信登录授权 Demo
發布時間:2024/8/26
44
生活家
生活随笔
收集整理的這篇文章主要介紹了
Fastadmin/thinkphp 微信登录授权 Demo
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
由于經常用這些就搞個示例方便調用吧!
這是index里面的文件
到時候就直接訪問 /index.php/index/index/wxindex
<?php
namespace appindexcontroller;
use appcommoncontrollerFrontend;
use fastWxsdk;
class Index extends Frontend
{
protected $noNeedLogin = '*';
protected $noNeedRight = '*';
// protected $layout = '';
public function _initialize(){
$this->appId = 'wx63c391768f362ca8'; //wxe6747dc052817254
$this->appSecret = '512ec5eb6117b86bcaccf3f10b45edc2'; //
}
public function wxindex()
{
$this->Wxsdk = new Wxsdk($this->appId,$this->appSecret);
if(!$this->Wxsdk->islogin()){
$user = $this->Wxsdk->login();
if(!empty($user['openid'])){
//存數據庫
$this->add_Wxuser($user);//獲取到微信用戶 存數據
// 然后繼續操作
}else{
$this->success('error', $user);
}
}
}
public function add_Wxuser($user){
//在你的數據庫 添加用戶信息
dump($user);
}
}
里面加載的類文件(QQ496631085)
文件名Wxsdk.php 放在extendfast目錄下面 看框架及版本定義
1 <?php
2
3 namespace fast;
4
5
6 class Wxsdk
7 {
8 private $appId;
9 private $appSecret;
10
11 public function __construct($appId, $appSecret) {
12 $this->appId = $appId;
13 $this->appSecret = $appSecret;
14 }
15
16
17 public function islogin(){
18 //驗證是否登錄過,如果沒有登錄就跳轉
19 if(empty(session('openid'))){
20 // $this->getUserOpenId();
21 return false;
22 // $this->redirect('/index/index/me');
23 // exit();
24 }else{
25 // echo " ";
26 return true;
27 // dump(session('openid'));
28 }
29 }
30
31 public function login(){
32 // $user ='1111';
33 $user = $this->getUserOpenId();
34
35 return $user;
36 }
37
38
39 public function getBaseInfo(){
40 //1.獲取到code
41
42
43 // $redirect_uri=urlencode($this->domain()."/index/index/index");//跳轉 可以自己改參數 QQ496631085
44 $redirect_uri=urlencode($this->domain()."/index/index/wxindex");
45
46 $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appId."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=xiaohe#wechat_redirect";
47
48 echo '<script>;location.href="'.$url.'";</script>;';
49 exit();
50 }
51
52
53
54
55 /**
56 * 判斷是否為https
57 * @return bool 是https返回true;否則返回false
58 */
59 public function domain() {
60 if ( !empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
61 return 'https://'.$_SERVER['HTTP_HOST'];
62 } elseif ( isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ) {
63 return 'https://'.$_SERVER['HTTP_HOST'];
64 } elseif ( !empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
65 return 'https://'.$_SERVER['HTTP_HOST'];
66 }else{
67 return 'http://'.$_SERVER['HTTP_HOST'];
68 }
69 }
70
71 //查詢這個用戶的信息
72 public function openid_nickname($openid){
73 $nickname = Db::name('wxuser')->where('openid',$openid)->find();
74 return $nickname['nickname'];
75 }
76
77 //獲取用戶的openid等信息
78 public function getUserOpenId(){
79 if(!empty($_SERVER['HTTP_REFERER']))
80 file_put_contents('tiaozhuan.txt', $_SERVER['HTTP_REFERER'].'
',FILE_APPEND);
81 // exit($_GET);
82 //2.獲取到網頁授權的access_token
83
84
85 // if(empty($this->request->get('code'))){
86 if(empty($_GET['code'])){
87 $this->getBaseInfo();
88 exit();
89 }
90 // dump($this->request->get());
91 $code = $_GET['code'];//?????????????????
92 $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appId."&secret=".$this->appSecret."&code=".$code."&grant_type=authorization_code ";
93 //3.拉取用戶的openid
94 $res = file_get_contents($url);
95
96 $data = json_decode($res,true);
97 if(!empty($data['access_token']) && !empty($data['openid'])){
98 $this->access_token=$data['access_token'];
99 $this->refresh_token=$data['refresh_token'];
100
101 $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$data['access_token']."&openid=".$data['openid']."&lang=zh_CN";
102 $userInfo = file_get_contents($url);
103 $user_res = json_decode($userInfo,true);
104 // echo $userInfo;
105 return $user_res;
106 // $this->openid = $user_res['openid'];
107 // $this->nickname = base64_encode($user_res['nickname']);
108 // $this->headimgurl = $user_res['headimgurl'];
109 // $this->sex = $user_res['sex'];
110 // // dump($user_res);
111 // //獲取都用戶信息然后判斷是否新用戶 存表
112 // session('openid',$this->openid);
113 // if(!empty($_SERVER['HTTP_REFERER'])){
114 // $this->redirect($_SERVER['HTTP_REFERER']);
115 // }
116 // else{
117 // $this->redirect($this->_domain.'/index/index/index');
118 // }
119
120 }else{
121 return($data);
122 // $this->getUserOpenId();
123 exit();
124 }
125 }
126
127
128
129
130 public function getSignPackage() {
131 $jsapiTicket = $this->getJsApiTicket();
132
133 // 注意 URL 一定要動態獲取,不能 hardcode.
134 $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
135 $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
136
137 $timestamp = time();
138 $nonceStr = $this->createNonceStr();
139
140 // 這里參數的順序要按照 key 值 ASCII 碼升序排序
141 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
142
143 $signature = sha1($string);
144
145 $signPackage = array(
146 "appId" => $this->appId,
147 "nonceStr" => $nonceStr,
148 "timestamp" => $timestamp,
149 "url" => $url,
150 "signature" => $signature,
151 "rawString" => $string
152 );
153 return $signPackage;
154 }
155
156 private function createNonceStr($length = 16) {
157 $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
158 $str = "";
159 for ($i = 0; $i < $length; $i++) {
160 $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
161 }
162 return $str;
163 }
164
165 private function getJsApiTicket() {
166 // jsapi_ticket 應該全局存儲與更新,以下代碼以寫入到文件中做示例
167 if(file_get_contents("jsapi_ticket.json")){
168 $data = json_decode(file_get_contents("jsapi_ticket.json"));
169 }else{
170 $data = null;
171 }
172
173
174 if ($data->expire_time < time()) {
175 $accessToken = $this->getAccessToken();
176 // 如果是企業號用以下 URL 獲取 ticket
177 // $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
178 $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
179 $res = json_decode($this->httpGet($url));
180 $ticket = $res->ticket;
181 if ($ticket) {
182 $data->expire_time = time() + 7000;
183 $data->jsapi_ticket = $ticket;
184 $fp = fopen("jsapi_ticket.json", "w");
185 fwrite($fp, json_encode($data));
186 fclose($fp);
187 }
188 } else {
189 $ticket = $data->jsapi_ticket;
190 }
191
192 return $ticket;
193 }
194
195 private function getAccessToken() {
196 // access_token 應該全局存儲與更新,以下代碼以寫入到文件中做示例
197 $data = json_decode(file_get_contents("access_token.json"));
198 if ($data->expire_time < time()) {
199 // 如果是企業號用以下URL獲取access_token
200 // $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
201 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
202 $res = json_decode($this->httpGet($url));
203 $access_token = $res->access_token;
204 if ($access_token) {
205 $data->expire_time = time() + 7000;
206 $data->access_token = $access_token;
207 $fp = fopen("access_token.json", "w");
208 fwrite($fp, json_encode($data));
209 fclose($fp);
210 }
211 } else {
212 $access_token = $data->access_token;
213 }
214 return $access_token;
215 }
216
217 private function httpGet($url) {
218 $curl = curl_init();
219 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
220 curl_setopt($curl, CURLOPT_TIMEOUT, 500);
221 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
222 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
223 curl_setopt($curl, CURLOPT_URL, $url);
224
225 $res = curl_exec($curl);
226 curl_close($curl);
227
228 return $res;
229 }
230 }
View Code
然后就可以繼續操作了
總結
以上是生活随笔為你收集整理的Fastadmin/thinkphp 微信登录授权 Demo的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么颜色对眼睛好(吃三种颜色的食物对眼睛
- 下一篇: oracle02--多表关联查询