微信網頁授權,微信登錄,oauth2


微信官方文檔: http://mp.weixin.qq.com/wiki

微信公眾平台OAuth2.0授權詳細步驟如下:

1. 用戶關注微信公眾賬號。
2. 微信公眾賬號提供用戶請求授權頁面URL。
3. 用戶點擊授權頁面URL,將向服務器發起請求
4. 服務器詢問用戶是否同意授權給微信公眾賬號(scope為snsapi_base時無此步驟)
5. 用戶同意(scope為snsapi_base時無此步驟)
6. 服務器將CODE通過回調傳給微信公眾賬號
7. 微信公眾賬號獲得CODE
8. 微信公眾賬號通過CODE向服務器請求Access Token
9. 服務器返回Access Token和OpenID給微信公眾賬號
10. 微信公眾賬號通過Access Token向服務器請求用戶信息(scope為snsapi_base時無此步驟)
11. 服務器將用戶信息回送給微信公眾賬號(scope為snsapi_base時無此步驟)

授權頁面:oauth.php(引導用戶點入...)

 1 <?php
 2 require(dirname(__FILE__) . '/api.class.php');
 3 require(dirname(__FILE__) . '/wechat.class.php');
 4 
 5 //多微信帳號支持
 6 $weixinconfig = $db->getRow ( "SELECT * FROM " . $GLOBALS['ecs']->table('weixin_config') . " WHERE `id` = 1" );
 7 //多微信帳號支持
 8 $id = intval($_GET['id']);//1
 9 $oid = intval($_GET['oid']);//4
10 
11 if($id > 0){
12     $otherconfig = $db->getRow ( "SELECT * FROM " . $GLOBALS['ecs']->table('weixin_config') . " WHERE `id` = $id" );
13     if($otherconfig){
14         $weixinconfig['token'] = $otherconfig['token'];
15         $weixinconfig['appid'] = $otherconfig['appid'];
16         $weixinconfig['appsecret'] = $otherconfig['appsecret'];
17     }
18 }
19 $weixin = new core_lib_wechat($weixinconfig);
20 if($_GET['code']){
21     //echo $_GET['code'];die();
22     $json = $weixin->getOauthAccessToken();
23     //print_r($json);exit();
24     if($json['openid']){
25 
26         $wxuser = $GLOBALS['db']->getRow("select ecuid,unionid,uid,access_token from " . $GLOBALS['ecs']->table('weixin_user') . " where fake_id='{$json['openid']}'");
27         //print_r($wxuser);exit();
28         //如果用戶unionid多平台登錄,自動綁定
29         if($wxuser['unionid']==""){
30             $wxuserinof = $weixin->getUserInfo($json['openid']);
31             $sql = "update ".$GLOBALS['ecs']->table('weixin_user')." set `unionid`='".$wxuserinof['unionid']."' where uid='".$wxuser['uid']."'";
32             $GLOBALS['db']->query($sql);
33         }
34         $ecuid = $wxuser['ecuid'];
35 
36         if($ecuid > 0){
37             $username = $GLOBALS['db']->getOne("select user_name from ".$GLOBALS['ecs']->table('users')." where user_id='{$ecuid}'");
38             $GLOBALS['user']->set_session($username);
39             $GLOBALS['user']->set_cookie($username,1);
40             update_user_info();  //更新用戶信息
41             recalculate_price(); //重新計算購物車中的商品價格
42         }
43     }
44     
45     $url = $api->dir."/mobile/user.php";
46     if($oid > 0){
47         $url = $db->getOne ( "SELECT weburl FROM " . $GLOBALS['ecs']->table('weixin_oauth') . "
48  WHERE `oid` = $oid" );
49         $db->query("update " . $GLOBALS['ecs']->table('weixin_oauth') . "
50  set click=click+1 WHERE `oid` = $oid ");
51     }
52     header("Location:$url");exit;
53 }
54 $url = $GLOBALS['ecs']->url()."/oauth.php?id={$id}&oid={$oid}";
55 $url = $weixin->getOauthRedirect($url,1,'snsapi_base');
56 header("Location:$url");exit;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM