zoukankan      html  css  js  c++  java
  • 微信网页授权,微信登录,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;
  • 相关阅读:
    Scala 学习之 aggregate函数
    CentOS6.5+hadoop-2.5.2+hbase-1.0.1.1+zookeeper-3.4.6之zookeeper配置
    swiper.min.js.map访问404解决办法
    CentOS6.5上搭建hadoop 2.5.2 笔记
    Java 多线程编程中单例的实现
    关于 nohup 执行命令以后 需要再按回车才能起效的解决办法
    java定时任务之 Timer
    关于solr异常:org.apache.solr.client.solrj.SolrServerException: IOException occured when talking to server at: http:192.168.0.11/solr/的解决
    关于dubbo服务产生异常之:Caused by: com.alibaba.dubbo.remoting.TimeoutException: Waiting server-side response timeout by scan timer.
    关于maven使用产生的异常之:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile
  • 原文地址:https://www.cnblogs.com/boundless-sky/p/6056664.html
Copyright © 2011-2022 走看看