-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
72 lines (64 loc) · 2.2 KB
/
index.php
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
<?php
/**
* author:frank
* date:2015-02-09
* email:[email protected]
*/
session_start() ;
include(__dir__."/include/config.php");
include(__dir__."/models/Users.php");
include(__dir__."/libraries/weixin.ex.class.php");
//是否登录
//没有登录进行网页登录,保存用户信息到session及数据库
//已登录显示内容
$code = $_GET['code'] ;
$state = $_GET['state'] ;
//echo "state:".$state ;
//echo "<br/>";
//echo "code:".$code ;
//echo "<br/>";
$user = $_SESSION['user'] ;
if(empty($user)&&!empty($code)&&!empty($state)){
$wxClient = new WeiXinClientV1(AppId,AppSecret);
$token = $wxClient->oauth->getAccessTokenByCode($code);
$openid = $token['openid'] ;
$wxuser = $wxClient->findUser($openid) ;
$userModel = new Users() ;
$u = $userModel->findByOpenId($openid) ;
//如果用户不存在,保存
if(!empty($u)){
$userModel->save($wxuser) ;
}
$_SESSION['user'] = $wxuser ;
}
//var_dump($user) ;
//unset($_SESSION['user']) ;
if(empty($user)){
//header("Location:".snsapi_url($scope="snsapi_userinfo",AppId,REDIRECT_URI,"test")) ;
}
/**
* echo snsapi_url($scope="snsapi_userinfo",AppId,REDIRECT_URI,"test") ;
*
* @param [type] $scope [snsapi_base snsapi_userinfo]
* @param [type] $appid [description]
* @param [type] $redirect_uri [description]
* @param [type] $state [description]
*/
function snsapi_url($scope="snsapi_userinfo",$appid,$redirect_uri,$state){
//http%3A%2F%2Fwww.naifenyun.com%2Fwechat%2Fcallback
$redirect_uri = urlencode($redirect_uri) ;
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid ;
$url .= "&redirect_uri={$redirect_uri}&response_type=code&scope={$scope}&state={$state}#wechat_redirect" ;
return $url ;
}
echo snsapi_url($scope="snsapi_userinfo",AppId,REDIRECT_URI,"test") ;
?>
<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
<script>
$(function(){
var d = {'nickName':'frank','headUrl':'http://wx.qlogo.cn/mmopen/g3MonUZtNHkdmzicIlibx6iaFqAc56vxLSUfpb6n5WKSYVY0ChQKkiaJSgQ1dZuTOgvLLrhJbERQQ4eMsv84eavHiaiceqxibJxCfHe/46','roleId':'1','greetId':'2'} ;
//$.post('cardsAction.php?action=save',d,function(data){
// console.log(data) ;
//}) ;
}) ;
</script>