dever 5 years ago
parent
commit
4b34592add
7 changed files with 261 additions and 148 deletions
  1. 17 1
      database/combine.php
  2. 14 0
      database/user.php
  3. 35 23
      lib/Base.php
  4. 30 0
      lib/Cron.php
  5. 7 0
      src/Api.php
  6. 0 10
      src/App.php
  7. 158 114
      src/Applet.php

+ 17 - 1
database/combine.php

@@ -1,5 +1,9 @@
 <?php
-
+$status = array
+(
+	1 => '未执行',
+	2 => '已执行',
+);
 return array
 (
 	# 表名
@@ -46,6 +50,18 @@ return array
 			'list'		=> true,
 		),
 
+		'status'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '合并任务执行状态',
+			'default' 	=> '2',
+			'desc' 		=> '合并任务执行状态',
+			'match' 	=> 'is_numeric',
+			'option' 	=> $status,
+			'update'	=> 'radio',
+			'list'		=> true,
+		),
+
 		'state'		=> array
 		(
 			'type' 		=> 'tinyint-1',

+ 14 - 0
database/user.php

@@ -383,5 +383,19 @@ return array
 			),
 			'type' => 'one',
 		),
+
+		# 此处定义是为跳出auth
+		'updates' => array
+		(
+			'set' => array
+			(
+				'state' => 'yes',
+			),
+			'where' => array
+			(
+				'id' => array('yes', 'in'),
+			),
+			'type' => 'update',
+		),
 	),
 );

+ 35 - 23
lib/Base.php

@@ -102,7 +102,7 @@ class Base
             if (isset($data['unionid']) && $data['unionid']) {
                 $wechat['unionid'] = $data['unionid'];
                 # 判断用户是否存在,是否需要合并
-                //$wechat['uid'] = $this->combine($uid, $data['unionid']);
+                $wechat['uid'] = $this->combine($uid, $data['unionid']);
             }
             
             $wechat['access_token'] = $data['access_token'];
@@ -205,6 +205,12 @@ class Base
         if ($user_wechat) {
             # 合并去
             $drop = array();
+            $total = count($user_wechat);
+            if ($total <= 1) {
+                return $cur;
+            }
+
+            $new = false;
             foreach ($user_wechat as $k => $v) {
                 if (!isset($v['uid'])) {
                     $v['uid'] = $v['id'];
@@ -213,36 +219,49 @@ class Base
                     $user = Dever::db('passport/user')->one($v['uid']);
                 }
 
-                if ($cur != $v['uid']) {
-                    if ($user['bind'] == 1 || $user['temp'] == 2 || $user['avatar'] || $user['mobile'] || $v['uid'] < $cur) {
-                        $drop[$cur] = $cur;
-                        $cur = $v['uid'];
+                if (!$new) {
+                    if ($user['bind'] == 1) {
+                        $new = $v['uid'];
+                    } elseif ($user['temp'] == 2) {
+                        $new = $v['uid'];
+                    } elseif ($user['avatar']) {
+                        $new = $v['uid'];
+                    } elseif ($user['mobile']) {
+                        $new = $v['uid'];
+                    } elseif ($v['uid'] < $cur) {
+                        $cur = $new = $v['uid'];
                     } else {
                         $drop[$v['uid']] = $v['uid'];
                     }
+                } else {
+                    $drop[$v['uid']] = $v['uid'];
                 }
             }
+            $cur = $new;
 
-            $test = Dever::input('test');
-
-            if ($test == 1) {
+            if (Dever::input('test') == 1) {
                 print_r($cur);
                 print_r($drop);die;
             }
             if ($drop) {
+                ksort($drop);
+                $combine = array();
+                $combine['status'] = 1;
+                $combine['new_uid'] = $cur;
+                $combine['old_uid'] = implode(',', $drop);
+                $info = Dever::db('passport/combine')->one($combine);
+                if (!$info) {
+                    Dever::db('passport/combine')->insert($combine);
+                }
+
+                Dever::load('passport/user-updates', array('set_state' => 2, 'where_id' => $combine['old_uid']));
+
+                $update = array();
                 foreach ($drop as $k => $v) {
                     if ($cur == $v) {
                         unset($drop[$k]);
                         continue;
                     }
-                    $insert['new_uid'] = $cur;
-                    $insert['old_uid'] = $v;
-                    $info = Dever::db('passport/combine')->one($insert);
-                    if (!$info) {
-                        Dever::db('passport/combine')->insert($insert);
-                    }
-                    Dever::load('passport/user-update', array('set_state' => 2, 'where_id' => $v));
-                    Dever::load('passport/user-update', array('set_state' => 1, 'where_id' => $cur));
 
                     $new = Dever::db('passport/user')->one($cur);
                     $old = Dever::db('passport/user')->one($v);
@@ -272,13 +291,6 @@ class Base
                         $update['where_id'] = $cur;
                         Dever::load('passport/user-update', $update);
                     }
-                    
-                }
-                $combine = Dever::project('combine');
-                if ($combine && $drop) {
-                    $drop = implode(',', $drop);
-                    Dever::load('combine/lib/core.handle?uid=' . $cur . '&drop=' . $drop);
-                    //Dever::daemon('lib/core.handle?uid=' . $cur . '&drop=' . $drop, 'combine');
                 }
             }
         }

+ 30 - 0
lib/Cron.php

@@ -0,0 +1,30 @@
+<?php
+namespace Passport\Lib;
+
+use Dever;
+
+class Cron
+{
+	# 定时合并
+    public function combine()
+    {
+        $combine = Dever::project('combine');
+        if ($combine) {
+        	$where['status'] = 1;
+        	$data = Dever::db('passport/combine')->state($where);
+
+        	if ($data) {
+        		foreach ($data as $k => $v) {
+        			$drop = implode(',', $v['old_uid']);
+            		Dever::load('combine/lib/core.handle?uid=' . $v['new_uid'] . '&drop=' . $drop);
+
+            		$update['where_id'] = $v['id'];
+            		$update['status'] = 2;
+            		Dever::db('passport/combine')->update($update);
+        		}
+        	}
+            //Dever::daemon('lib/core.handle?uid=' . $cur . '&drop=' . $drop, 'combine');
+        }
+        return 'ok';
+    }
+}

+ 7 - 0
src/Api.php

@@ -28,4 +28,11 @@ class Api
         
         return $info;
     }
+
+    # 测试合并
+    public function test_combine()
+    {
+        $base = new Base;
+        $base->combine(3, 321313);
+    }
 }

+ 0 - 10
src/App.php

@@ -16,16 +16,6 @@ class App extends Base
         return $uid;
     }
 
-    public function comcomuid()
-    {
-        return;
-        $uid = Dever::input('uid');
-        $olduid = Dever::input('olduid');
-        $uid = $this->combine($uid, $olduid, 'id');
-
-        return $uid;
-    }
-
     /**
      * 更新用户信息
      *

+ 158 - 114
src/Applet.php

@@ -8,13 +8,14 @@ use Passport\Lib\Base;
 
 class Applet extends Base
 {
-  	public function init()
+    public function init()
     {
-      	$uid = $this->check();
+        $uid = $this->check();
         $user = Dever::load('passport/user-one', $uid);
 
         return $user;
     }
+
     /**
      * 用户绑定 生成用户信息
      *
@@ -22,67 +23,16 @@ class Applet extends Base
      */
     public function bind()
     {
-        $token = Dever::project('token');
-        $applet = array();
-
-        $system = Dever::input('system', 1);
-
-        $config = Dever::config('base', 'project')->applet;
-
-        if ($token) {
-            $applet = Dever::db('token/project')->one($system);
-        }
-
-        if (!$applet) {
-            if (isset($config['project']) && $config['project']) {
-                $applet = Dever::db($config['project'])->one($system);
-            } else {
-                $applet = $config;
-            }
-        }
+        $create = Dever::input('create', 1);
+        $data = $this->getLoginInfo();
 
-        if (!$applet) {
-            Dever::alert('错误的appid');
+        if ($create == 1) {
+            $data = $this->create($data);
         }
 
-        $appid = $applet['appid'];
-        $secret = $applet['secret'];
-        $url = $config['url'];
-        $code = Dever::input('code');
-
-        $url .= '?appid=' . $appid;
-        $url .= '&secret=' . $secret;
-        $url .= '&js_code=' . $code;
-        $url .= '&grant_type=authorization_code';
-
-        $data = Dever::curl($url);
-        Dever::log($data, 'passport_applet');
-        if (strstr($data, 'errcode')) {
-            Dever::alert($data);
-        }
-
-        //YzJkOThpRFhwZ1lQTF9mZl9hLVZjZnFXemJVenlYcDQ3d3JWekk0b1I4NjBBQ0Naejg4a0VQa0U=
-        //$data = '{"session_key":"aNAXk7nG\/DRYI\/G0KzJRsw==","openid":"oIZ895RZs2ZkywasoZIv6WavPZlQ"}';
-        $data = Dever::json_decode($data);
-
-        $user = $this->create($data);
-
-        return $user;
+        return $data;
     }
 
-    /*
-    public function test_sms()
-    {
-        $uid = Dever::input('uid');
-        $mobile = Dever::input('mobile');
-        $state = Dever::config('base', 'project')->regSendSms;
-        if ($state) {
-            Dever::setInput('skin', $state);
-            $this->send($mobile, $uid);
-        }
-    }
-    */
-
     /**
      * 生成用户,返回uid
      *
@@ -101,7 +51,6 @@ class Applet extends Base
                     $uid = false;
                 } else {
                     # 判断用户是否存在,是否需要合并
-                    $uid = $info['uid'];
                     $uid = $this->combine($info['uid'], $data['unionid']);
                 }
                 $wechat['unionid'] = $data['unionid'];
@@ -127,7 +76,6 @@ class Applet extends Base
         } else {
             $uid = $info['uid'];
             if (isset($data['unionid']) && $data['unionid']) {
-                //$wechat['unionid'] = $data['unionid'];
                 # 判断用户是否存在,是否需要合并
                 $wechat['uid'] = $this->combine($uid, $data['unionid']);
             }
@@ -144,67 +92,25 @@ class Applet extends Base
         $result['uid'] = $uid;
         $result['signature'] = Dever::login($uid);
       
-      	if ($user['mobile']) {
-        	$result['mobile'] = $user['mobile'];
+        if ($user['mobile']) {
+            $result['mobile'] = $user['mobile'];
         }
 
         return $result;
     }
 
     /**
-     * 更新用户信息 手机号
+     * 一次性登录:通过code或者openid、sessionkey来注册用户,此时已经授权,可以直接拿到unioinid
      *
      * @return mixed
      */
-    public function mobile()
+    public function login()
     {
-        $uid = $this->check();
-        $vid = Dever::input('vid');
-        $result = array();
-        $mobile = $phoneNumber = '';
+        $data = $this->getLoginInfo();
 
-        $iv = Dever::input('iv');
-        $encryptedData = Dever::input('encryptedData');
-        
-        if ($iv && $encryptedData) {
-            $vinfo = Dever::load('passport/wechat-one', $vid);
-            $data = $this->decryptData($vinfo['session_key']);
-            if ($data && isset($data->purePhoneNumber) && isset($data->phoneNumber)) {
-                $mobile = $data->purePhoneNumber;
-                $phoneNumber = $data->phoneNumber;
-            }
-        }
-
-        if ($mobile && $phoneNumber && $uid) {
-            $uid = $this->combine($uid, $mobile, 'mobile');
-
-            $info = Dever::load('passport/user-one', $uid);
-            $result['mobile'] = $mobile;
-            if ($info) {
-
-                if (!$info['mobile']) {
-                    Dever::score($uid, 'bind_mobile', '绑定手机号');
-                }
-                $update['set_mobile'] = $mobile;
-                $update['set_bind'] = 1;
-                $update['where_id'] = $uid;
-                Dever::load('passport/user-update', $update);
-
-                $state = Dever::config('base', 'project')->regSendSms;
-                if ($state) {
-                    Dever::setInput('skin', $state);
-                    $this->send($mobile, $uid);
-                }
-            } else {
-                Dever::alert('无效的用户id,请重新登录');
-            }
-        }
+        $data += $this->getWechatData($data['session_key']);
 
-        
-
-        $result['vid'] = $vid;
-        $result['uid'] = $uid;
-        $result['signature'] = Dever::login($uid);
+        $result = $this->create($data);
 
         return $result;
     }
@@ -230,7 +136,8 @@ class Applet extends Base
         if ($vid) {
             $vinfo = Dever::load('passport/wechat-one', $vid);
 
-            $unionid = $this->unionid($vinfo['session_key']);
+            $data = $this->getWechatData($vinfo['session_key']);
+            $unionid = $data['unionid'];
             if ($unionid) {
                 $vinfo = Dever::load('passport/wechat-one', array('option_unionid' => $unionid));
                 if (!$vinfo) {
@@ -274,7 +181,7 @@ class Applet extends Base
             
             if ($pic) {
                 //$update['set_avatar'] = $this->saveAvatar($pic);
-              	$update['set_avatar'] = $pic;
+                $update['set_avatar'] = $pic;
             }
             
             if ($city) {
@@ -309,6 +216,64 @@ class Applet extends Base
         return $result;
     }
 
+    /**
+     * 更新用户信息 手机号
+     *
+     * @return mixed
+     */
+    public function mobile()
+    {
+        $uid = $this->check();
+        $vid = Dever::input('vid');
+        $result = array();
+        $mobile = $phoneNumber = '';
+
+        $iv = Dever::input('iv');
+        $encryptedData = Dever::input('encryptedData');
+        
+        if ($iv && $encryptedData) {
+            $vinfo = Dever::load('passport/wechat-one', $vid);
+            $data = $this->getWechatData($vinfo['session_key']);
+            if ($data && $data['mobile']) {
+                $mobile = $data['mobile'];
+                $phoneNumber = $data['phone'];
+            }
+        }
+
+        if ($mobile && $phoneNumber && $uid) {
+            $uid = $this->combine($uid, $mobile, 'mobile');
+
+            $info = Dever::load('passport/user-one', $uid);
+            $result['mobile'] = $mobile;
+            if ($info) {
+
+                if (!$info['mobile']) {
+                    Dever::score($uid, 'bind_mobile', '绑定手机号');
+                }
+                $update['set_mobile'] = $mobile;
+                $update['set_bind'] = 1;
+                $update['where_id'] = $uid;
+                Dever::load('passport/user-update', $update);
+
+                $state = Dever::config('base', 'project')->regSendSms;
+                if ($state) {
+                    Dever::setInput('skin', $state);
+                    $this->send($mobile, $uid);
+                }
+            } else {
+                Dever::alert('无效的用户id,请重新登录');
+            }
+        }
+
+        
+
+        $result['vid'] = $vid;
+        $result['uid'] = $uid;
+        $result['signature'] = Dever::login($uid);
+
+        return $result;
+    }
+
     private function unionid($session_key)
     {
         $data = $this->decryptData($session_key);
@@ -319,13 +284,37 @@ class Applet extends Base
         return false;
     }
 
+    private function getWechatData($session_key)
+    {
+        $result = array();
+        $data = $this->decryptData($session_key);
+        if ($data && isset($data->openId)) {
+            $result['openid'] = $data->openId;
+            $result['unionid'] = '';
+            $result['mobile'] = '';
+            $result['phone'] = '';
+            if (isset($data->unionId)) {
+                $result['unionid'] = $data->unionId;
+            }
+            if (isset($data->purePhoneNumber)) {
+                $result['mobile'] = $data->purePhoneNumber;
+            }
+
+            if (isset($data->phoneNumber)) {
+                $result['phone'] = $data->phoneNumber;
+            }
+        }
+        
+        return $result;
+    }
+
     private function decryptData($session_key)
     {
-    	$iv = Dever::input('iv');
+        $iv = Dever::input('iv');
         $encryptedData = Dever::input('encryptedData');
 
         if (!$iv || !$encryptedData) {
-        	return false;
+            return false;
         }
         if (strlen($session_key) != 24) {
             return false;
@@ -347,11 +336,66 @@ class Applet extends Base
             return false;
         }
         /*
-      	$applet = Dever::config('base', 'project')->applet;
+        $applet = Dever::config('base', 'project')->applet;
         $appid = $applet['appid'];
         if($dataObj->watermark->appid != $appid) {
             return false;
         }*/
         return $dataObj;
     }
+
+    private function getApplet()
+    {
+        $applet = Dever::config('base', 'project')->applet;
+        if (Dever::project('token')) {
+            $system = Dever::input('system', 1);
+            $project = Dever::db('token/project')->one($system);
+
+            if (!$project) {
+                if (isset($config['project']) && $config['project']) {
+                    $project = Dever::db($config['project'])->one($system);
+                }
+            }
+
+            if ($project) {
+                $applet['appid'] = $project['appid'];
+                $applet['secret'] = $project['secret'];
+            }
+        }
+
+        if (!$applet || !$applet['appid'] || !$applet['secret']) {
+            Dever::alert('错误的appid');
+        }
+
+        return $applet;
+    }
+
+    private function getLoginInfo()
+    {
+        $applet = $this->getApplet();
+        $appid = $applet['appid'];
+        $secret = $applet['secret'];
+        $url = $applet['url'];
+        $code = Dever::input('code');
+        if (!$applet || !$applet['appid'] || !$applet['secret']) {
+            Dever::alert('错误的appid');
+        }
+
+        $url .= '?appid=' . $appid;
+        $url .= '&secret=' . $secret;
+        $url .= '&js_code=' . $code;
+        $url .= '&grant_type=authorization_code';
+
+        $data = Dever::curl($url);
+        Dever::log($data, 'passport_applet');
+        if (strstr($data, 'errcode')) {
+            Dever::alert($data);
+        }
+
+        //YzJkOThpRFhwZ1lQTF9mZl9hLVZjZnFXemJVenlYcDQ3d3JWekk0b1I4NjBBQ0Naejg4a0VQa0U=
+        //$data = '{"session_key":"aNAXk7nG\/DRYI\/G0KzJRsw==","openid":"oIZ895RZs2ZkywasoZIv6WavPZlQ"}';
+        $data = Dever::json_decode($data);
+
+        return $data;
+    }
 }