rabin 6 years ago
commit
0788d888dc
11 changed files with 1102 additions and 0 deletions
  1. 14 0
      LICENSE
  2. 2 0
      README.md
  3. 22 0
      config/base.php
  4. 94 0
      database/code.php
  5. 232 0
      database/user.php
  6. 8 0
      index.php
  7. 217 0
      src/Lib/Base.php
  8. 104 0
      src/Lib/Code.php
  9. 76 0
      src/Login.php
  10. 228 0
      src/Reg.php
  11. 105 0
      src/User.php

+ 14 - 0
LICENSE

@@ -0,0 +1,14 @@
+Apache License
+Copyright 2016-2017 Dever(dever.cc)
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.

+ 2 - 0
README.md

@@ -0,0 +1,2 @@
+# passport
+

+ 22 - 0
config/base.php

@@ -0,0 +1,22 @@
+<?php
+
+return array
+(
+	# 站点使用邮箱还是手机作为主账号,如果是手机,请填写手机验证码的配置接口
+	'account' => 'email',
+	'mobileCode' => array
+	(
+		# 发送短信的接口
+		'url' => '',
+		# 短信签名
+		'sign' => '【DEVER】',
+		# 短信签名类型,1为放置在内容之前,2为放置在内容之后
+		'signType' => 2,
+		# 短信内容
+		'msg' => '您好,您的验证码为{code},十分钟之内有效。',
+		# 一天之内的最大发送次数,默认为10次
+		'total' => 10,
+		# 不允许多久之内重复发送,默认为60秒
+		'time' => 60,
+	),
+);

+ 94 - 0
database/code.php

@@ -0,0 +1,94 @@
+<?php
+
+# 定义几个常用的选项
+$option = array
+(
+	1 => '发送中',
+	2 => '已发送',
+);
+
+$mobile = Dever::rule('mobile');
+
+return array
+(
+	# 表名
+	'name' => 'code',
+	# 显示给用户看的名称
+	'lang' => '短信记录表',
+	'menu' => false,
+	# 数据结构
+	'struct' => array
+	(
+	
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			'search'	=> 'order',
+			//'list'		=> true,
+		),
+
+		'mobile'		=> array
+		(
+			'type' 		=> 'varchar-32',
+			'name' 		=> '手机号',
+			'default' 	=> '',
+			'desc' 		=> '请输入用户手机号',
+			'match' 	=> $mobile,
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'code'		=> array
+		(
+			'type' 		=> 'varchar-32',
+			'name' 		=> '验证码',
+			'default' 	=> '',
+			'desc' 		=> '验证码',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'day'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '申请时间',
+			'default' 	=> '',
+			'desc' 		=> '申请时间',
+			'match' 	=> 'is_numeric',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'state'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '状态',
+			'default' 	=> '1',
+			'desc' 		=> '请选择状态',
+			'match' 	=> 'is_numeric',
+			'option' 	=> $option,
+			//'update'	=> 'radio',
+			'list'		=> true,
+		),
+		
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '申请时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			# 只有insert时才生效
+			'insert'	=> true,
+			'search'	=> 'date',
+			'list'		=> 'date("Y-m-d H:i:s", {cdate})',
+		),
+	),
+);

+ 232 - 0
database/user.php

@@ -0,0 +1,232 @@
+<?php
+
+# 定义几个常用的选项
+$option = array
+(
+	1 => '正常',
+	2 => '冻结',
+);
+
+$level = array
+(
+	1 => '普通',
+	2 => '元老',
+	3 => '小编',
+	10 => '管理员',
+);
+
+$weixin = array
+(
+	1 => '已关注',
+	2 => '未关注',
+);
+
+$bind = array
+(
+	1 => '已绑定',
+	2 => '未绑定',
+);
+
+$email = Dever::rule('email');
+$mobile = Dever::rule('mobile');
+
+
+return array
+(
+	'email' => $email,
+	'mobile' => $mobile,
+	# 表名
+	'name' => 'user',
+	# 显示给用户看的名称
+	'lang' => '用户管理',
+	'order' => 10,
+	# 数据结构
+	'struct' => array
+	(
+	
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '用户ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			'search'	=> 'order',
+			'list'		=> true,
+		),
+
+		'email'		=> array
+		(
+			'type' 		=> 'varchar-150',
+			'name' 		=> '邮箱',
+			'default' 	=> '',
+			'desc' 		=> '请输入邮箱',
+			'match' 	=> $email,
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+		
+		'username'		=> array
+		(
+			'type' 		=> 'varchar-50',
+			'name' 		=> '用户名',
+			'default' 	=> '',
+			'desc' 		=> '请输入用户名',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+
+		'mobile'		=> array
+		(
+			'type' 		=> 'varchar-32',
+			'name' 		=> '手机号',
+			'default' 	=> '',
+			'desc' 		=> '请输入用户手机号',
+			'match' 	=> $mobile,
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+		
+		'password'		=> array
+		(
+			'type' 		=> 'varchar-50',
+			'name' 		=> '密码',
+			'default' 	=> '',
+			'desc' 		=> '请输入密码',
+			'match' 	=> 'option',
+			'update'	=> 'password',
+			'callback'	=> 'md5',
+		),
+		
+		'avatar'		=> array
+		(
+			'type' 		=> 'varchar-150',
+			'name' 		=> '头像',
+			'default' 	=> '',
+			'desc' 		=> '请选择头像',
+			'match' 	=> 'option',
+			'update'	=> 'image',
+			'key' 		=> '1',
+			'place'		=> '150',
+		),
+		
+		'info'		=> array
+		(
+			'type' 		=> 'varchar-255',
+			'name' 		=> '介绍-选填,如果想清空该介绍,请输入null。',
+			'default' 	=> '',
+			'desc' 		=> '请输入介绍',
+			'match' 	=> 'option',
+			'update'	=> 'editor',
+		),
+
+		'level'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '用户等级',
+			'default' 	=> '1',
+			'desc' 		=> '请选择用户等级',
+			'match' 	=> 'is_numeric',
+			//'option' 	=> $level,
+			//'update'	=> 'radio',
+			//'list'		=> true,
+		),
+
+		'login_date'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '最近登录',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			'default' 	=> '0',
+			//'list'		=> 'date("Y-m-d H:i:s", {login_date})',
+		),
+
+		'reg_date'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '注册时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			'default' 	=> '0',
+		),
+
+		'bind'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '是否绑定手机',
+			'default' 	=> '2',
+			'desc' 		=> '请选择是否绑定手机',
+			'match' 	=> 'is_numeric',
+			//'option' 	=> $bind,
+			//'update'	=> 'radio',
+		),
+
+		'state'		=> array
+		(
+			'type' 		=> 'tinyint-1',
+			'name' 		=> '状态',
+			'default' 	=> '1',
+			'desc' 		=> '请选择状态',
+			'match' 	=> 'is_numeric',
+			'option' 	=> $option,
+			'update'	=> 'radio',
+		),
+		
+		'cdate'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '注册时间',
+			'match' 	=> array('is_numeric', time()),
+			'desc' 		=> '',
+			# 只有insert时才生效
+			'insert'	=> true,
+			//'list'		=> 'date("Y-m-d H:i:s", {cdate})',
+		),
+	),
+
+	# 更新表结构
+	/*
+	'alter' => array
+	(
+		1 => array
+		(
+			array('add', 'bind', 'bind', 'tinyint-1 2 是否绑定手机'),
+		),
+		'version' => 1,
+	),
+	*/
+	
+	# request 请求接口定义
+	'request' => array
+	(
+		# 此处定义是为跳出auth
+		'login' => array
+		(
+			'option' => array
+			(
+				'username' => 'yes',
+				'mobile' => 'yes',
+				'email' => 'yes',
+				'password' => 'yes',
+			),
+			'type' => 'one',
+		),
+
+		# 验证手机号是否被使用 
+		'checkMobile' => array
+		(
+			# 匹配的正则或函数 选填项
+			'where' => array
+			(
+				'mobile' => 'yes',
+				'id' => array('yes', '!='),
+			),
+			'type' => 'one',
+		),
+	),
+);

+ 8 - 0
index.php

@@ -0,0 +1,8 @@
+<?php
+
+define('DEVER_APP_NAME', 'passport');
+define('DEVER_APP_LANG', '用户管理');
+define('DEVER_APP_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR);
+define('DEVER_MANAGE_ORDER', -10);
+define('DEVER_MANAGE_ICON', 'glyphicon glyphicon-wrench');
+include(DEVER_APP_PATH . '../boot.php');

+ 217 - 0
src/Lib/Base.php

@@ -0,0 +1,217 @@
+<?php
+namespace Passport\Src\Lib;
+
+use Dever;
+use Dever\Img\Handle;
+use Dever\Routing\Uri;
+use Dever\Session\Oper as Save;
+use Dever\String\Encrypt;
+
+class Base
+{
+    const NAME = 'dever';
+
+    const CODE = 'dever_code';
+
+    const MCODE = 'dever_mcode';
+
+    protected $save;
+
+    public function __construct()
+    {
+        $this->save = new Save(false, 'cookie');
+    }
+
+    protected function info()
+    {
+        return $this->save->get(self::NAME);
+    }
+
+    protected function code($code = false)
+    {
+        if ($code) {
+            $save = $this->save->get(self::CODE);
+            return $code == $save;
+        }
+
+        $code = new Code();
+        $code->create();
+
+        $this->save->add(self::CODE, $code->code, 600);
+    }
+
+    protected function mcode($mobile, $code = false)
+    {
+        if ($code) {
+            $save = $this->save->get(self::MCODE);
+            return $mobile . '_' . $code == $save;
+        }
+
+        $day = date('Ymd', time());
+
+        # 检测当前手机号最新一次发送时间,不允许一分钟之内发送
+        $param['option_day'] = $day;
+        $param['option_mobile'] = $mobile;
+
+        # 检测当前手机号今天已经发送多少验证码了
+        $info = Dever::load('passport/code-total', $param);
+
+        if ($info >= 1) {
+            $check = Dever::load('passport/code-check', $param);
+
+            if ($check) {
+                if (time() - $check['cdate'] < Dever::config('base', 'passport')->mobileCode['time']) {
+                    Dever::alert('请不要在一分钟之内申请多次验证码,请您稍后再试');
+                }
+            }
+        }
+
+        $total = Dever::config('base', 'passport')->mobileCode['total'];
+        if ($info >= $total) {
+            Dever::alert('很抱歉,您已经申请获取验证码超过' . $total . '次,今天您已经无法获取验证码了,请您明天再来');
+        }
+
+        $code = new Code();
+        $code->createM();
+
+        # 记录当前的验证码
+        $insert['add_mobile'] = $mobile;
+        $insert['add_day'] = $day;
+        $insert['add_code'] = $code->mcode;
+        $id = Dever::load('passport/code-insert', $insert);
+
+        # 发送验证码到手机上
+        //$text = '您好,您的验证码为' . $code->mcode . ',十分钟之内有效。';
+        $text = str_replace('{code}', $code->mcode, Dever::config('base', 'passport')->mobileCode['msg']);
+
+        # 启动发送
+        $this->send($mobile, $text, $id);
+
+        $this->save->add(self::MCODE, $mobile . '_' . $code->mcode, 600);
+
+        return $code->mcode;
+    }
+
+    private function send($mobile, $content, $id)
+    {
+        $url = Dever::config('base', 'passport')->mobileCode['url'];
+
+        if (Dever::config('base', 'passport')->mobileCode['signType'] == 1) {
+            $content = Dever::config('base', 'passport')->mobileCode['sign'] . $content;
+        } else {
+            $content .= Dever::config('base', 'passport')->mobileCode['sign'];
+        }
+
+        $param['phone'] = $mobile;
+        $param['message'] = $content;
+        $param['seqid'] = $id + 1000;
+        $param['smspriority'] = 1;
+
+        $url .= http_build_query($param);
+
+        Dever::curl($url);
+    }
+
+    protected function refer()
+    {
+        $refer = Dever::input('refer');
+
+        $project = 'main';
+
+        if ($refer) {
+            $url = parse_url(Encrypt::decode($refer));
+            $url['path'] = preg_replace('/^\//', '', $url['path']);
+            if (!isset($url['query'])) {
+                $url['query'] = '';
+            }
+
+            if (Uri::$type == '?') {
+                $refer = Dever::url(str_replace($url['path'] . Uri::$type, '', $url['query']), $project);
+            } else {
+                $refer = Dever::url($url['path'] . '?' . $url['query'], $project);
+            }
+
+            Dever::out($refer);
+        } else {
+            Dever::out(Dever::url('home', $project));
+        }
+    }
+
+    protected function save($user)
+    {
+        if (is_numeric($user)) {
+            $user = Dever::load('service/company-one', $user);
+        }
+
+        if ($user && is_array($user)) {
+            if (Dever::mobile()) {
+                $time = 30;
+            } else {
+                $time = 7;
+            }
+            $this->save->add(self::NAME, $user, 3600 * 24 * $time);
+
+            $user['signature'] = Dever::login($user['id']);
+
+            return $user;
+        }
+    }
+
+    public function createRefer()
+    {
+        return 'refer=' . Encrypt::encode(Dever::url());
+    }
+
+    protected function saveAvatar($value)
+    {
+        $base = DEVER_PATH . 'data/upload/';
+        $date = explode('-', date("Y-m-d"));
+        if (is_array($value)) {
+            $user = $value[1];
+            $value = $value[0];
+
+            if (is_array($value)) {
+                $name = md5($value['tmp_name'] . rand(0, 100) . microtime());
+                $path = 'avatar/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
+                $ext = '.jpg';
+                $temp = Dever::path($base, $path . $name . $ext);
+
+                move_uploaded_file($value['tmp_name'], $temp);
+
+                $value = $temp;
+            }
+        }
+        $result = Dever::$global['host']['img'] . 'chead.jpg';
+        if ($value) {
+            # 裁图
+            $handle = new Handle();
+
+            $size = '200_200_2';
+
+            $user = isset($user) && $user ? $user : $this->init();
+            if ($user && isset($user['id']) && $user['id']) {
+                $name = md5($user['id']);
+            } else {
+                $name = md5($value . rand(0, 100) . microtime());
+            }
+
+            $path = 'avatar/' . $date[0] . '/' . $date[1] . '/' . $date[2] . '/';
+            $ext = '.jpg';
+            $file = Dever::path($base, $path . $name . $ext);
+            $temp = $file . '.temp.jpg';
+
+            //$value = Dever::curl($value);
+            //file_put_contents($temp, file_get_contents($value));
+
+            $file = $handle->thumb($value, $size, true, $file);
+            //print_r($file);die;
+            if ($file) {
+                $result = Dever::$global['host']['image'] . $path . $name . $ext;
+            } else {
+                $result = $value;
+            }
+        }
+
+        return $result;
+    }
+}

+ 104 - 0
src/Lib/Code.php

@@ -0,0 +1,104 @@
+<?php
+namespace Passport\Src\Lib;
+
+class Code
+{
+    private $width;
+
+    private $height;
+
+    public $code;
+
+    public $mcode;
+
+    public $string;
+
+    private $image;
+
+    public function __construct($width = '160', $height = '30')
+    {
+        $this->width = $width;
+        $this->height = $height;
+    }
+
+    public function create()
+    {
+        header("Content-type: image/png");
+        $this->createCode();
+        $this->createImage();
+        $this->createPix();
+        $this->write();
+        imagepng($this->image);
+        imagedestroy($this->image);
+    }
+
+    public function createM()
+    {
+        $this->createRand(4);
+    }
+
+    private function createImage()
+    {
+        $this->image = @imagecreate($this->width, $this->height);
+        $back = imagecolorallocate($this->image, 255, 255, 255);
+        $border = imagecolorallocate($this->image, 0, 0, 0);
+        imagefilledrectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $back);
+        imagerectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $border);
+    }
+
+    private function createPix()
+    {
+        for ($i = 0; $i <= 200; $i++) {
+            imagesetpixel($this->image, rand(2, $this->width), rand(2, $this->height), imagecolorallocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255)));
+        }
+    }
+
+    public function createRand($length)
+    {
+        $this->mcode = null;
+        $str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
+        $str = '0123456789';
+        $max = strlen($str) - 1;
+
+        for ($i = 0; $i < $length; $i++) {
+            $this->mcode .= $str[rand(0, $max)]; //rand($min,$max)生成介于min和max两个数之间的一个随机整数
+        }
+    }
+
+    private function createCode()
+    {
+        $cal = array
+            (
+            array('+', '+'),
+            array('-', '-'),
+            //array('*', '乘以'),
+            //array('/', '除以'),
+        );
+        $index = array_rand($cal);
+
+        $m1 = rand(1, 100);
+        $m2 = rand(1, 100);
+
+        $this->string = $m1 . $cal[$index][1] . $m2 . '';
+
+        $this->code = '$this->code = ' . $m1 . $cal[$index][0] . $m2 . ';';
+
+        eval($this->code);
+    }
+
+    private function write()
+    {
+        $length = strlen($this->string);
+        for ($i = 0; $i < $length; $i++) {
+            $bg_color = imagecolorallocate($this->image, rand(0, 255), rand(0, 128), rand(0, 255));
+            $x = floor($this->width / $length) * $i;
+            $y = rand(0, $this->height - 15);
+            imagechar($this->image, rand(5, 5), $x + 5, $y, $this->string[$i], $bg_color);
+        }
+    }
+
+    public function __destruct()
+    {
+        unset($this->width, $this->height, $this->image);
+    }
+}

+ 76 - 0
src/Login.php

@@ -0,0 +1,76 @@
+<?php
+namespace Passport\Src;
+
+use Dever;
+use Passport\Src\Lib\Base;
+
+class Login extends Base
+{
+    public function test()
+    {
+        return Dever::login(-1);
+    }
+
+    public function quit()
+    {
+        if ($this->info()) {
+            $this->save->un(self::NAME);
+        }
+
+        $refer = isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] ? $_SERVER['HTTP_REFERER'] : Dever::url('home', 'main');
+
+        Dever::location($refer);
+    }
+
+    public function action()
+    {
+        $account = Dever::config('base', 'passport')->account;
+
+        $param['option_' . $account] = Dever::input($account);
+
+        $password = md5(Dever::input('password'));
+
+        $user = Dever::load('passport/user-login', $param);
+
+        if ($user && $password == $user['password']) {
+            $this->save($user);
+
+            $this->refer();
+        } else {
+            Dever::alert('您的账号或密码错误');
+        }
+    }
+
+    public function check()
+    {
+        if ($this->info()) {
+            $home = Dever::url('', 'main');
+            return Dever::location($home);
+        }
+    }
+
+    public function get()
+    {
+        return $this->info();
+    }
+
+    public function url()
+    {
+        return Dever::url('login?' . $this->createRefer(), 'main');
+    }
+
+    public function location()
+    {
+        return Dever::location($this->url());
+    }
+
+    public function oauth($url = false)
+    {
+        if (!$url) {
+            $url = Dever::url();
+        }
+        $link = Dever::url('get.request?refer=' . urlencode($url), 'oauth');
+
+        return $link;
+    }
+}

+ 228 - 0
src/Reg.php

@@ -0,0 +1,228 @@
+<?php
+namespace Passport\Src;
+
+use Dever;
+use Passport\Src\Lib\Base;
+
+class Reg extends Base
+{
+    /**
+     * 获取图形验证码
+     * @return mixed
+     */
+    public function getCode()
+    {
+        return $this->code();
+    }
+
+    /**
+     * 获取手机随机验证码
+     * @return mixed
+     */
+    public function getMCode()
+    {
+        $mobile = $this->checkMobileExists(1);
+
+        $code = $this->mcode($mobile);
+
+        //Dever::alert('验证码已发送至您的手机,请注意查收,十分钟之内有效');
+
+        return '验证码已发送至您的手机,请注意查收,十分钟之内有效:' . $code;
+    }
+
+    /**
+     * 检测图形验证码
+     * @return mixed
+     */
+    private function checkCode()
+    {
+        $code = Dever::input('code');
+        $code = $this->code($code);
+        if (!$code) {
+            Dever::alert('请您填写正确的验证码');
+        }
+    }
+
+    /**
+     * 检测手机验证码
+     * @return mixed
+     */
+    private function checkMCode($mobile)
+    {
+        $code = Dever::input('mcode');
+        $code = $this->mcode($mobile, $code);
+        if (!$code) {
+            Dever::alert('请您填写正确的验证码');
+        }
+    }
+
+    /**
+     * 验证手机号
+     * @return mixed
+     */
+    private function checkMobile()
+    {
+        $rule = Dever::rule('mobile');
+
+        $mobile = Dever::input('mobile');
+
+        if (!$mobile) {
+            Dever::alert('手机号不能为空');
+        }
+        if (!preg_match($rule, $mobile)) {
+            Dever::alert('请填写正确的手机号');
+        }
+
+        return $mobile;
+    }
+
+    /**
+     * 验证手机号是否注册
+     * @return mixed
+     */
+    public function checkMobileExists($state = false)
+    {
+        $param['option_mobile'] = $this->checkMobile();
+
+        if ($state && $state == 1) {
+
+        } else {
+            $this->checkMCode($param['option_mobile']);
+        }
+
+        $user = Dever::load('passport/user-one', $param);
+
+        if ($user) {
+            Dever::alert('该手机号已经注册');
+        }
+
+        return $param['option_mobile'];
+    }
+
+    /**
+     * 验证邮箱
+     * @return mixed
+     */
+    private function checkEmail()
+    {
+        $rule = Dever::rule('email');
+
+        $email = Dever::input('email');
+
+        if (!$email) {
+            Dever::alert('邮箱不能为空');
+        }
+        if (!preg_match($rule, $email)) {
+            Dever::alert('请填写正确的邮箱');
+        }
+
+        return $email;
+    }
+
+    /**
+     * 验证邮箱是否注册
+     * @return mixed
+     */
+    public function checkEmailExists($state = false)
+    {
+        $param['option_email'] = $this->checkEmail();
+
+        if ($state && $state == 1) {
+
+        } else {
+            $this->checkCode($param['option_email']);
+        }
+
+        $user = Dever::load('passport/user-one', $param);
+
+        if ($user) {
+            Dever::alert('该邮箱已经注册');
+        }
+
+        return $param['option_email'];
+    }
+
+    public function action()
+    {
+        $account = Dever::config('base', 'passport')->account;
+        $baccount = ucfirst($account);
+        $method = 'check' . $baccount . 'Exists';
+
+        $param['option_' . $account] = $this->$method();
+
+        if ($param['option_' . $account]) {
+            $param['add_' . $account] = $param['option_' . $account];
+            $param['add_username'] = Dever::input('username');
+            $param['add_password'] = Dever::input('password');
+            $cpassword = Dever::input('cpassword');
+
+            if (!$param['add_username']) {
+                Dever::alert('昵称不能为空');
+            }
+
+            if (!$param['add_password']) {
+                Dever::alert('密码不能为空');
+            }
+
+            if ($cpassword != $param['add_password']) {
+                Dever::alert('确认密码不正确');
+            }
+
+            $id = Dever::load('passport/user-insert', $param);
+
+            if ($id > 0) {
+                $this->save($id);
+                $this->refer();
+            } else {
+                Dever::alert('注册失败');
+            }
+        }
+    }
+
+    public function forget()
+    {
+        $account = Dever::config('base', 'passport')->account;
+        $baccount = ucfirst($account);
+        $method = 'check' . $baccount . 'Exists';
+
+        $param['option_' . $account] = $this->$method();
+
+        $this->checkCode($param['option_' . $account]);
+
+        $user = Dever::load('passport/user-one', $param);
+
+        if (!$user) {
+            Dever::alert('该账号还未注册,请先注册');
+        } else {
+            $param['set_password'] = Dever::input('password');
+            $cpassword = Dever::input('cpassword');
+
+            if (md5($param['set_password']) == $user['password']) {
+                Dever::alert('您的新密码和旧密码相同');
+            }
+
+            if (!$param['set_password']) {
+                Dever::alert('新密码不能为空');
+            }
+
+            if ($param['set_password'] != $cpassword) {
+                Dever::alert('确认密码不正确');
+            }
+            $id = $param['where_id'] = $user['id'];
+
+            Dever::load('passport/user-update', $param);
+
+            if ($id > 0) {
+                return $this->save($id);
+                $this->refer();
+            } else {
+                Dever::alert('修改失败');
+            }
+        }
+    }
+
+    public function url()
+    {
+        return Dever::url('reg?' . $this->createRefer(), 'main');
+    }
+}

+ 105 - 0
src/User.php

@@ -0,0 +1,105 @@
+<?php
+namespace Passport\Src;
+
+use Dever;
+use Passport\Src\Lib\Base;
+
+class User extends Base
+{
+    private $user;
+    public function __construct()
+    {
+        parent::__construct();
+        $this->init();
+    }
+
+    public function init()
+    {
+        if (!$this->user) {
+            $this->user = $this->info();
+
+            $url = Dever::url();
+            if (!$this->user && (strpos($url, 'my/') !== false || strpos($url, 'forum/post') !== false)) {
+                return Dever::load('passport/login.location');
+            }
+
+            if ($this->user) {
+                $this->user['msgnum'] = 0;
+            }
+        }
+
+        return $this->user;
+    }
+
+    public function data()
+    {
+        if ($this->user) {
+            $this->user['link'] = Dever::url('user/info', 'main');
+            $this->user['link'] = Dever::url('forum/list?uid=' . $this->user['id'], 'main');
+        }
+        return $this->user;
+    }
+
+    public function check()
+    {
+        if (!$this->user) {
+            Dever::alert('请先登录', true);
+        }
+    }
+
+    public function updateInfo()
+    {
+        $this->check();
+
+        //$update['set_mobile'] = Dever::input('mobile');
+        $update['set_username'] = Dever::input('username');
+        $update['set_avatar'] = Dever::input('avatar') ? Dever::input('avatar') . '?v=' . time() : $this->user['avatar'];
+        $update['where_id'] = $this->user['id'];
+        $sex = Dever::input('sex');
+
+        if ($sex == '男') {
+            $update['set_sex'] = 1;
+        } elseif ($sex == '女') {
+            $update['set_sex'] = 2;
+        } else {
+            $update['set_sex'] = 0;
+        }
+
+        if ($update['set_avatar']) {
+            $update['set_avatar'] = $this->saveAvatar($update['set_avatar']);
+        }
+
+        Dever::load('passport/user-update', $update);
+
+        $this->user = $this->save($this->user['id']);
+
+        return true;
+    }
+
+    public function updateAvatar()
+    {
+        $this->check();
+
+        $update['set_avatar'] = Dever::input('avatar') ? Dever::input('avatar') . '?v=' . time() : false;
+        $update['where_id'] = $this->user['id'];
+
+        if ($update['set_avatar']) {
+            $update['set_avatar'] = $this->saveAvatar($update['set_avatar']);
+
+            Dever::load('passport/user-update', $update);
+
+            $this->user = $this->save($this->user['id']);
+        }
+
+        return true;
+    }
+
+    public function updateWeixin()
+    {
+        $this->check();
+
+        Dever::load('passport/user-update', array('where_id' => $this->user['id'], 'set_weixin' => 1));
+
+        return true;
+    }
+}