dever 6 лет назад
Родитель
Сommit
894eb0c083
4 измененных файлов с 131 добавлено и 231 удалено
  1. 4 5
      config/wechat.php
  2. 0 92
      database/user.php
  3. 0 111
      database/wechat.php
  4. 127 23
      lib/Client.php

+ 4 - 5
config/wechat.php

@@ -45,13 +45,12 @@ $config['token'] = array
 # 第三步 获取用户信息
 $config['user'] = array
 (
-	'url' => 'https://api.weixin.qq.com/sns/oauth2/access_token',
+	'url' => 'https://api.weixin.qq.com/sns/userinfo',
 	'param' => array
 	(
-		'appid' => 'appid',
-		'secret' => 'secret',
-		'code' => 'code',
-		'grant_type' => 'authorization_code',
+		'access_token' => 'access_token',
+		'openid' => 'openid',
+		'lang' => 'zh_CN',
 	)
 );
 

+ 0 - 92
database/user.php

@@ -1,92 +0,0 @@
-<?php
-
-return array
-(
-	# 表名
-	'name' => 'user',
-	# 显示给用户看的名称
-	'lang' => '用户绑定列表',
-	'order' => 10,
-	'menu' => false,
-	# 数据结构
-	'struct' => array
-	(
-		'id' 		=> array
-		(
-			'type' 		=> 'int-11',
-			'name' 		=> 'ID',
-			'default' 	=> '',
-			'desc' 		=> '',
-			'match' 	=> 'is_numeric',
-			//'search'	=> 'order',
-			'order'		=> 'desc',
-			'list'		=> true,
-		),
-		
-		'account_id'		=> array
-		(
-			'type' 		=> 'int-11',
-			'name' 		=> '所属账户',
-			'default' 	=> '1',
-			'desc' 		=> '所属账户',
-			'match' 	=> 'is_numeric',
-			'update'	=> 'select',
-			'list'		=> '{account_id} > 0 ? Dever::load("oauth/account-one#name", {account_id}) : "未知"',
-		),
-
-		'uid'		=> array
-		(
-			'type' 		=> 'int-11',
-			'name' 		=> '用户',
-			'default' 	=> '',
-			'desc' 		=> '',
-			'match' 	=> 'is_numeric',
-		),
-
-		'unionid'		=> array
-		(
-			'type' 		=> 'varchar-50',
-			'name' 		=> 'unionid-第三方唯一id',
-			'default' 	=> '',
-			'desc' 		=> 'unionid',
-			'match' 	=> 'is_string',
-			//'update'	=> 'text',
-			'search'	=> 'fulltext',
-			'list'		=> true,
-		),
-
-		'state'		=> array
-		(
-			'type' 		=> 'tinyint-1',
-			'name' 		=> '状态',
-			'default' 	=> '1',
-			'desc' 		=> '请选择状态',
-			'match' 	=> 'is_numeric',
-		),
-		
-		'cdate'		=> array
-		(
-			'type' 		=> 'int-11',
-			'name' 		=> '录入时间',
-			'match' 	=> array('is_numeric', time()),
-			'desc' 		=> '',
-			# 只有insert时才生效
-			'search'	=> 'date',
-			'insert'	=> true,
-			'list'		=> 'date("Y-m-d H:i:s", {cdate})',
-		),
-	),
-	
-	'manage' => array
-	(
-		'insert' => false,
-		'edit' => false,
-		'delete' => false,
-	),
-	
-	# request 请求接口定义
-	'request' => array
-	(
-		
-	),
-);

+ 0 - 111
database/wechat.php

@@ -1,111 +0,0 @@
-<?php
-
-return array
-(
-	# 表名
-	'name' => 'wechat',
-	# 显示给用户看的名称
-	'lang' => '微信openid',
-	'order' => 10,
-	'menu' => false,
-	# 数据结构
-	'struct' => array
-	(
-		'id' 		=> array
-		(
-			'type' 		=> 'int-11',
-			'name' 		=> 'ID',
-			'default' 	=> '',
-			'desc' 		=> '',
-			'match' 	=> 'is_numeric',
-			//'search'	=> 'order',
-			'order'		=> 'desc',
-			'list'		=> true,
-		),
-		
-		'account_id'		=> array
-		(
-			'type' 		=> 'int-11',
-			'name' 		=> '所属账户',
-			'default' 	=> '1',
-			'desc' 		=> '所属账户',
-			'match' 	=> 'is_numeric',
-			'update'	=> 'select',
-			'list'		=> '{account_id} > 0 ? Dever::load("oauth/account-one#name", {account_id}) : "未知"',
-		),
-
-		'uid'		=> array
-		(
-			'type' 		=> 'int-11',
-			'name' 		=> '用户',
-			'default' 	=> '',
-			'desc' 		=> '',
-			'match' 	=> 'is_numeric',
-		),
-
-		'user_id'		=> array
-		(
-			'type' 		=> 'int-11',
-			'name' 		=> '用户',
-			'default' 	=> '',
-			'desc' 		=> '',
-			'match' 	=> 'is_numeric',
-		),
-
-		'openid'		=> array
-		(
-			'type' 		=> 'varchar-50',
-			'name' 		=> 'openid-微信的唯一用户id',
-			'default' 	=> '',
-			'desc' 		=> 'openid',
-			'match' 	=> 'is_string',
-			//'update'	=> 'text',
-			'search'	=> 'fulltext',
-			'list'		=> true,
-		),
-
-		'session_key'		=> array
-		(
-			'type' 		=> 'varchar-50',
-			'name' 		=> 'session_key',
-			'default' 	=> '',
-			'desc' 		=> 'session_key',
-			'match' 	=> 'is_string',
-			//'update'	=> 'text',
-		),
-
-		'state'		=> array
-		(
-			'type' 		=> 'tinyint-1',
-			'name' 		=> '状态',
-			'default' 	=> '1',
-			'desc' 		=> '请选择状态',
-			'match' 	=> 'is_numeric',
-		),
-		
-		'cdate'		=> array
-		(
-			'type' 		=> 'int-11',
-			'name' 		=> '录入时间',
-			'match' 	=> array('is_numeric', time()),
-			'desc' 		=> '',
-			# 只有insert时才生效
-			'search'	=> 'date',
-			'insert'	=> true,
-			'list'		=> 'date("Y-m-d H:i:s", {cdate})',
-		),
-	),
-	
-	'manage' => array
-	(
-		'insert' => false,
-		'edit' => false,
-		'delete' => false,
-	),
-	
-	# request 请求接口定义
-	'request' => array
-	(
-		
-	),
-);

+ 127 - 23
lib/Client.php

@@ -4,8 +4,9 @@ namespace Oauth\Lib;
 
 use Dever;
 use Dever\Session\Oper as Save;
+use Passport\Src\Lib\Base;
 
-class Client
+class Client extends Base
 {
 	/**
 	 * @desc account
@@ -17,7 +18,7 @@ class Client
 	 * @desc save
 	 * @var object
 	 */
-	private $save = null;
+	protected $session = null;
 
 	/**
 	 * @desc request
@@ -33,6 +34,7 @@ class Client
 
 	public function __construct()
 	{
+		parent::__construct();
 		$this->initSave();
 		$this->initRequest();
 		$this->initAccount();
@@ -42,7 +44,7 @@ class Client
 
 	private function initSave()
 	{
-		$this->save = new Save(DEVER_PROJECT, 'session');
+		$this->session = new Save(DEVER_PROJECT, 'session');
 	}
 
 	private function initRequest()
@@ -52,14 +54,14 @@ class Client
 
 	private function initAccount()
 	{
-		$this->account = (isset($this->request['account']) && $this->request['account']) ? $this->request['account'] : $this->save->get('oauth_account');
-		$this->save->add('oauth_account', $this->account);
+		$this->account = (isset($this->request['account']) && $this->request['account']) ? $this->request['account'] : $this->session->get('oauth_account');
+		$this->session->add('oauth_account', $this->account);
 	}
 
 	private function initRefer()
 	{
-		$this->refer = (isset($this->request['refer']) && $this->request['refer']) ? $this->request['refer'] : $this->save->get('oauth_refer');
-		$this->save->add('oauth_refer', $this->refer);
+		$this->refer = (isset($this->request['refer']) && $this->request['refer']) ? $this->request['refer'] : $this->session->get('oauth_refer');
+		$this->session->add('oauth_refer', $this->refer);
 	}
 
 	private function initConfig()
@@ -76,15 +78,24 @@ class Client
 	 */
 	public function auth()
 	{
+		$info = $this->info();
+		if ($info) {
+			if ($this->refer) {
+				$refer = urldecode($this->refer);
+				Dever::location($refer);
+			} else {
+				return true;
+			}
+		}
 		$id = Dever::id();
-		$this->save->add('oauth_id', $id);
+		$this->session->add('oauth_id', $id);
 		$this->param('auth', 'appid', $this->config['appid']);
-		$this->param('auth', 'redirect_uri', Dever::url('request/callback?account=' . $this->account, 'oauth'));
+		$this->param('auth', 'redirect_uri', Dever::url('request.callback?account=' . $this->account, 'oauth'));
 		$this->param('auth', 'state', $id);
 		$this->param('auth', 'response_type');
 		$this->param('auth', 'scope');
 		
-		print_r($this->config['auth']);die;
+		//print_r($this->config['auth']);die;
 
 		$url = $this->config['auth']['url'] . '?' . http_build_query($this->config['auth']['param']);
 
@@ -96,15 +107,16 @@ class Client
 	 */
 	public function callback($url = '')
 	{
+		$this->js = true;
 		if ((isset($this->request['js']) && $this->request['js'])) {
-			$this->_js = false;
+			$this->js = false;
 		}
-		if ($this->_js == true && $url) {
+		if ($this->js == true && $url) {
 			return $this->output($url);
 		} else {
-			$id = $this->save->get('oauth_id');
+			$id = $this->session->get('oauth_id');
 			if (isset($this->config['token']['param'])) {
-				if (!$this->save->get('oauth_refresh')) {
+				if (!$this->session->get('oauth_refresh')) {
 					$this->param('token', 'code');
 					$this->param('token', 'appid', $this->config['appid']);
 					$this->param('token', 'secret', $this->config['appsecret']);
@@ -124,25 +136,28 @@ class Client
 					$this->response('token', 'scope');
 					$this->response('token', 'errcode');
 					$this->response('token', 'errmsg');
+
 				} else {
 					# 由于refresh token是长期有效的,所以这里无需再次获取了。之后通过这个refresh获取access token就行了
 					return;
 				}
 			}
 			
-			# 进入绑定流程吧
-			$this->bind($data);
+			if (isset($this->config['token']['response'])) {
+				# 进入绑定流程吧
+				$this->bind();
+			}
 		}
 	}
 	
 	/**
-	 * @desc 重新获取token
+	 * @desc 重新获取token 暂时不用
 	 * @author leo(suwi.bin)
 	 * @date 2012-08-27
 	 */
 	protected function refresh()
 	{
-		$data = $this->request();
+		$data = $this->request;
 		$state = false;
 		if(isset($data['token_refresh']) && $data['token_refresh'])
 		{
@@ -159,7 +174,7 @@ class Client
 				$update['token_code'] = $return['access_token'];
 				$update['token_type'] = $return['token_type'];
 				$update['token_time'] = $return['expires_in'];
-				$state = $this->_update($update, $data['id']);
+				$state = $this->update($update, $data['id']);
 			}
 		}
 		return $state;
@@ -172,7 +187,7 @@ class Client
 	{
 		$data = $this->config['token']['response'];
 		
-		$get = $this->request();
+		$get = $this->request;
 		
 		$id = false;
 		
@@ -181,15 +196,104 @@ class Client
 			$id = $get['id'];
 		}
 		
-		$this->_update($data, $id);
-		
+		$this->update($data, $id);
 		# 跳转吧,从哪来去哪吧
 		if($this->refer)
 		{
-			$refer = base64_decode($this->refer);
+			$refer = urldecode($this->refer);
 			Dever::location($refer);
 		}
 	}
+
+	/**
+	 * @desc 绑定数据
+	 */
+	private function update($data, $id)
+	{
+		$uid = false;
+        $info = Dever::load('passport/wechat_oauth-one', array('option_openid' => $data['openid']));
+
+        $this->param('user', 'access_token', $data['access_token']);
+		$this->param('user', 'openid', $data['openid']);
+
+		$userinfo = Dever::json_decode(Dever::curl($this->config['user']['url'], $this->config['user']['param']));
+		if (!$userinfo) {
+			return;
+		}
+		$user['source_type'] = 'service';
+        $user['bind'] = 1;
+        $user['temp'] = 2;
+        $user['username'] = $userinfo['nickname'];
+        if ($userinfo['headimgurl']) {
+            //$update['set_avatar'] = $this->sessionAvatar($pic);
+          	$user['avatar'] = $userinfo['headimgurl'];
+        }
+        
+        if ($userinfo['city']) {
+            $user['city'] = $userinfo['city'];
+        }
+
+        if ($userinfo['province']) {
+            $user['province'] = $userinfo['province'];
+        }
+
+        if ($userinfo['country']) {
+            $user['country'] = $userinfo['country'];
+        }
+
+        if ($userinfo['country'] && $userinfo['province'] && $userinfo['city']) {
+        	$user['area'] = $userinfo['country'] .','. $userinfo['province'] .','. $userinfo['city'];
+    	}
+
+        if (!$info) {
+            if (isset($data['unionid']) && $data['unionid']) {
+                $info = Dever::load('passport/wechat_oauth-one', array('unionid' => $data['unionid']));
+                if (!$info) {
+                    $uid = false;
+                } else {
+                    $uid = $info['uid'];
+                }
+                $wechat['unionid'] = $data['unionid'];
+            }
+            if (!$uid) {
+                $uid = Dever::load('passport/user-insert', $user);
+            } else {
+                $user['where_id'] = $uid;
+                Dever::load('passport/user-update', $user);
+            }
+
+            $wechat['access_token'] = $data['access_token'];
+            $wechat['openid'] = $data['openid'];
+            $wechat['expires_in'] = $data['expires_in'];
+            $wechat['refresh_token'] = $data['refresh_token'];
+            $wechat['account_id'] = $this->account;
+            $wechat['uid'] = $uid;
+            $id = Dever::load('passport/wechat_oauth-insert', $wechat);
+        } else {
+            $uid = $info['uid'];
+            if (isset($data['unionid']) && $data['unionid']) {
+                $wechat['unionid'] = $data['unionid'];
+            }
+            
+            $wechat['access_token'] = $data['access_token'];
+            $wechat['openid'] = $data['openid'];
+            $wechat['expires_in'] = $data['expires_in'];
+            $wechat['refresh_token'] = $data['refresh_token'];
+            $wechat['where_id'] = $info['id'];
+            $wechat['account_id'] = $this->account;
+            $id = $info['id'];
+            Dever::load('passport/wechat_oauth-update', $wechat);
+
+            $user['where_id'] = $uid;
+            Dever::load('passport/user-update', $user);
+        }
+
+        $user = Dever::load('passport/user-one', $uid);
+
+        $this->save($user);
+
+        return $uid;
+	}
 	
 	/**
 	 * @desc 输出js内容