dever 5 years ago
parent
commit
af73047a05
12 changed files with 697 additions and 516 deletions
  1. 12 0
      database/app.php
  2. 114 0
      database/system.php
  3. 23 1
      database/user.php
  4. 14 0
      database/wechat.php
  5. 404 336
      lib/Base.php
  6. 107 107
      lib/Code.php
  7. 2 15
      src/Api.php
  8. 5 2
      src/App.php
  9. 13 52
      src/Applet.php
  10. 1 1
      src/Login.php
  11. 1 1
      src/Reg.php
  12. 1 1
      src/User.php

+ 12 - 0
database/app.php

@@ -55,6 +55,18 @@ return array
 			//'list'		=> true,
 		),
 
+		'system_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '所属项目',
+			'default' 	=> '1',
+			'desc' 		=> '所属项目',
+			'match' 	=> 'is_numeric',
+			//'option' 	=> $level,
+			//'update'	=> 'select',
+			//'list'		=> true,
+		),
+
 		'state'		=> array
 		(
 			'type' 		=> 'tinyint-1',

+ 114 - 0
database/system.php

@@ -0,0 +1,114 @@
+<?php
+
+return array
+(
+	# 表名
+	'name' => 'system',
+	# 显示给用户看的名称
+	'lang' => '项目管理',
+	# 后台菜单排序
+	'order' => 1,
+	# 数据结构
+	'struct' => array
+	(
+		'id' 		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> 'ID',
+			'default' 	=> '',
+			'desc' 		=> '',
+			'match' 	=> 'is_numeric',
+			'search'	=> 'order',
+			'list'		=> true,
+			'order'		=> 'desc',
+		),
+		
+		'name'		=> array
+		(
+			'type' 		=> 'varchar-32',
+			'name' 		=> '项目名称',
+			'default' 	=> '',
+			'desc' 		=> '项目名称',
+			'match' 	=> 'is_string',
+			'update'	=> 'text',
+			'search'	=> 'fulltext',
+			'list'		=> true,
+		),
+		
+		'reorder'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '排序(数值越大越靠前)',
+			'default' 	=> '1',
+			'desc' 		=> '请输入排序',
+			'match' 	=> 'option',
+			'update'	=> 'text',
+			'search'	=> 'order',
+			'list'		=> true,
+			'order'		=> 'desc',
+			'edit'		=> 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时才生效
+			'insert'	=> true,
+			'list'		=> 'date("Y-m-d H:i:s", {cdate})',
+		),
+	),
+
+	# 默认值
+	'default' => array
+	(
+		'col' => 'name,state,cdate',
+		'value' => array
+		(
+			'"默认项目",1,' . time(),
+		),
+	),
+
+	'manage' => array
+	(
+		'insert' => false,
+		'edit' => false,
+
+		# 自定义快捷新增和编辑
+		'button' => array
+		(
+			'新增' => array('fast'),
+		),
+		# 快捷更新
+		'list_button' => array
+		(
+			'edit' => array('编辑'),
+		),
+	),
+
+	'request' => array
+	(
+		'getAll' => array
+		(
+			# 匹配的正则或函数 选填项
+			'option' => array
+			(
+				'state' => 1,
+			),
+			'type' => 'all',
+			'order' => array('reorder' => 'desc','id' => 'desc'),
+			'col' => '*',
+		),
+	),
+);

+ 23 - 1
database/user.php

@@ -13,6 +13,16 @@ $level = function()
 	return Dever::db('passport/level')->state();
 };
 */
+$system = function()
+{
+	$array = array();
+	$info = Dever::db('passport/system')->state();
+	if($info)
+	{
+		$array += $info;
+	}
+	return $array;
+};
 
 $sex = array
 (
@@ -108,6 +118,18 @@ return array
 			'list'		=> true,
 		),
 
+		'system_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '所属项目',
+			'default' 	=> '1',
+			'desc' 		=> '所属项目',
+			'match' 	=> 'is_numeric',
+			'option' 	=> $system,
+			//'update'	=> 'select',
+			//'list'		=> true,
+		),
+
 		'source_type'		=> array
 		(
 			'type' 		=> 'varchar-100',
@@ -234,7 +256,7 @@ return array
 
 		'level'		=> array
 		(
-			'type' 		=> 'tinyint-1',
+			'type' 		=> 'int-11',
 			'name' 		=> '用户等级',
 			'default' 	=> '1',
 			'desc' 		=> '请选择用户等级',

+ 14 - 0
database/wechat.php

@@ -4,6 +4,8 @@ $type = array
 (
 	1 => '小程序',
 	2 => '微信公众号',
+	3 => 'IOS微信客户端',
+	4 => '安卓微信客户端',
 );
 return array
 (
@@ -28,6 +30,18 @@ return array
 			'list'		=> true,
 		),
 
+		'system_id'		=> array
+		(
+			'type' 		=> 'int-11',
+			'name' 		=> '所属项目',
+			'default' 	=> '1',
+			'desc' 		=> '所属项目',
+			'match' 	=> 'is_numeric',
+			//'option' 	=> $level,
+			//'update'	=> 'select',
+			//'list'		=> true,
+		),
+
 		'uid'		=> array
 		(
 			'type' 		=> 'int-11',

+ 404 - 336
src/Lib/Base.php → lib/Base.php

@@ -1,336 +1,404 @@
-<?php
-namespace Passport\Src\Lib;
-
-use Dever;
-use Upload\Src\Lib\Img;
-use Dever\Routing\Uri;
-use Dever\Session\Oper as Save;
-use Dever\String\Encrypt;
-
-class Base
-{
-    const NAME = 'dever_passport_v2';
-
-    const CODE = 'dever_code';
-
-    const MCODE = 'dever_mcode';
-
-    protected $save;
-
-    public function __construct()
-    {
-        $this->save = new Save(false, 'cookie');
-    }
-
-    # 合并用户
-    protected function combine($uid, $unionid, $col = 'unionid')
-    {
-        return;
-        $user_wechat = Dever::load('passport/wechat-state', array($col => $unionid));
-        if ($user_wechat) {
-            # 合并去
-            $cur = $uid;//1 1,2
-            $drop = array();
-            foreach ($user_wechat as $k => $v) {
-                if ($v['uid'] < $cur) {
-                    $drop[$cur] = $cur;
-                    $cur = $v['uid'];
-                } elseif ($v['uid'] > $cur) {
-                    $drop[$v['uid']] = $v['uid'];
-                }
-            }
-            if ($drop) {
-                foreach ($drop as $k => $v) {
-                    Dever::load('passport/user-update', array('set_state' => 2, 'where_id' => $v));
-                }
-                $combine = Dever::project('combine');
-                if ($combine) {
-                    Dever::load('combine/lib/core')->handle($cur, $drop);
-                }
-            }
-        }
-    }
-
-    /**
-     * 检测用户有效性
-     *
-     * @return mixed
-     */
-    public function check($state = true)
-    {
-        $signature = Dever::input('signature');
-        $user = Dever::loginResult($signature, $state);
-        if ($state && !isset($user['uid'])) {
-            Dever::alert('user error');
-        }
-        if (isset($user['uid']) && $user['uid']) {
-            return $user['uid'];
-        }
-        return -1;
-    }
-
-    public function getToken($request = array())
-    {
-        return http_build_query(Dever::token($request));
-    }
-
-    public function getUrl($method, $request = array())
-    {
-        return Dever::proxy($method, $this->getToken($request));
-    }
-
-    protected function info()
-    {
-        $data = $this->save->get(self::NAME);
-        return $data;
-    }
-
-    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-one', $param);
-
-            if ($check) {
-                if (time() - $check['cdate'] < Dever::config('base', 'project')->mobileCode['time']) {
-                    Dever::alert('请不要在一分钟之内申请多次验证码,请您稍后再试');
-                }
-            }
-        }
-
-        $total = Dever::config('base', 'project')->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);
-
-        # 启动发送
-        $this->send($mobile, $insert['add_code'], $id);
-
-        $this->save->add(self::MCODE, $mobile . '_' . $code->mcode, Dever::config('base', 'project')->mobileCode['timeout']);
-
-        return $code->mcode;
-    }
-
-    public function send($mobile, $code, $id = false)
-    {
-        if (Dever::project('sms')) {
-            $send['skin'] = 'code';
-            $send['mobile'] = $mobile;
-            $send['param'] = array
-            (
-                'code' => $code
-            );
-            $send['param'] = Dever::json_encode($send['param']);
-            return Dever::load('sms/api.send', $send);
-        }
-        $url = Dever::config('base', 'project')->mobileCode['url'];
-
-        if (!$url) {
-            return;
-        }
-
-        $content = Dever::config('base', 'project')->mobileCode['body'];
-
-        $content = $this->replace($content, $mobile, $code);
-
-        parse_str($content, $param);
-
-        $type = Dever::config('base', 'project')->mobileCode['method'];
-        $json = Dever::config('base', 'project')->mobileCode['json'];
-        $header = Dever::config('base', 'project')->mobileCode['header'];
-
-        return Dever::curl($url, $param, $type, $json, $header);
-    }
-
-    private function replace($content, $mobile = '', $code = '')
-    {
-        $skin = Dever::config('base', 'project')->mobileCode['skin'];
-        $skin_key = Dever::input('skin', 'code');
-        if (isset($skin[$skin_key])) {
-            $skin = $skin[$skin_key];
-        } else {
-            $skin = array_shift($skin);
-        }
-
-        $config = array('{code}', '{mobile}', '{sign}', '{skin}', '{param}');
-        $replace = array($code, $mobile, Dever::config('base', 'project')->mobileCode['sign'], $skin);
-        return str_replace($config, $replace, $content);
-    }
-
-    protected function refer($state = false)
-    {
-        $refer = Dever::input('refer');
-
-        $project = 'main';
-
-        if ($refer) {
-            $refer = Encrypt::decode($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);
-            }
-            */
-            
-        } else {
-            $refer = Dever::url('home', $project);
-        }
-
-        $param = Dever::input('param');
-
-        if ($param) {
-            $refer .= '&' . $param;
-        }
-
-        if ($state) {
-            return $refer;
-        }
-        Dever::out($refer);
-    }
-
-    protected function save($user)
-    {
-        if (is_numeric($user)) {
-            $user = Dever::load('passport/user-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']);
-
-            if (Dever::config('base', 'project')->regAction) {
-                Dever::load(Dever::config('base', 'project')->regAction, $user);
-            }
-
-            return $user;
-        }
-    }
-
-    public function createRefer()
-    {
-        return 'refer=' . Encrypt::encode(Dever::url(false, 'main'));
-    }
-
-    protected function saveSex($sex)
-    {
-        if ($sex || $sex == 0) {
-            $config_sex = Dever::config('base', 'project')->sex;
-            if (isset($config_sex[$sex])) {
-                $sex = $config_sex[$sex];
-            } else {
-                if ($sex == '男') {
-                    $sex = 1;
-                } elseif ($sex == '女') {
-                    $sex = 2;
-                } elseif ($sex == '未知') {
-                    $sex = 3;
-                }
-            }
-        }
-        return $sex;
-    }
-
-    protected function saveAvatar($value)
-    {
-        if (!$value) {
-            return '';
-        }
-        $value = Dever::pic($value);
-        $base = Dever::data();
-        $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 Img();
-
-            $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);
-            if ($file) {
-                $result = str_replace('/upload/', '/', Dever::config('host')->uploadRes . $path . $name . $ext);
-            } else {
-                $result = $value;
-            }
-        }
-
-        return $result;
-    }
-}
+<?php
+namespace Passport\Lib;
+
+use Dever;
+use Upload\Src\Lib\Img;
+use Dever\Routing\Uri;
+use Dever\Session\Oper as Save;
+use Dever\String\Encrypt;
+
+class Base
+{
+    const NAME = 'dever_passport_v2';
+
+    const CODE = 'dever_code';
+
+    const MCODE = 'dever_mcode';
+
+    protected $save;
+
+    public function __construct()
+    {
+        $this->save = new Save(false, 'cookie');
+    }
+
+    /**
+     * 更新用户信息 绑定用户手机号
+     *
+     * @return mixed
+     */
+    public function bind_mobile()
+    {
+        $uid = $this->check();
+        //$code = Dever::input('mcode');
+        $mobile = Dever::load('passport/reg')->checkMobileExists();
+        if ($mobile && $uid) {
+            $uid = $this->combine($uid, $mobile, 'mobile');
+            $info = Dever::load('passport/user-one', $uid);
+            $result['mobile'] = $mobile;
+            if ($info) {
+                $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,请重新登录');
+            }
+        }
+
+        if (!$info['mobile']) {
+            Dever::score($uid, 'bind_mobile', '绑定手机号');
+        }
+
+        $result['uid'] = $uid;
+        $result['signature'] = Dever::login($uid);
+
+        return $result;
+    }
+
+    public function createUsername($uid, $username, $update = false)
+    {
+        if (!$username || $username == '临时用户') {
+            $username = $uid + 100000;
+            $username = 'JM' . $username;
+
+            if ($update) {
+                Dever::db('passport/user')->update(array('where_id' => $uid, 'username' => $username));
+            }
+        }
+        
+        return $username;
+    }
+
+    # 合并用户
+    public function combine($uid, $unionid, $col = 'unionid')
+    {
+    	$cur = $uid;
+        if ($col == 'mobile') {
+            $user_wechat = Dever::load('passport/user-all', array($col => $unionid));
+        } else {
+            $user_wechat = Dever::load('passport/wechat-state', array($col => $unionid));
+        }
+
+        if ($user_wechat) {
+            # 合并去
+            $drop = array();
+            foreach ($user_wechat as $k => $v) {
+                if (!isset($v['uid'])) {
+                    $v['uid'] = $v['id'];
+                }
+                if ($v['uid'] < $cur) {
+                    $drop[$cur] = $cur;
+                    $cur = $v['uid'];
+                } elseif ($v['uid'] > $cur) {
+                    $drop[$v['uid']] = $v['uid'];
+                }
+            }
+            if ($drop) {
+                foreach ($drop as $k => $v) {
+                    Dever::load('passport/user-update', array('set_state' => 2, 'where_id' => $v));
+                }
+                $combine = Dever::project('combine');
+                if ($combine) {
+                    $drop = implode(',', $drop);
+                    Dever::load('combine/lib/core.handle?uid=' . $cur . '&drop=' . $drop);
+                    //Dever::daemon('lib/core.handle?uid=' . $cur . '&drop=' . $drop, 'combine');
+                }
+            }
+        }
+
+        echo 11;die;
+
+        return $cur;
+    }
+
+    /**
+     * 检测用户有效性
+     *
+     * @return mixed
+     */
+    public function check($state = true)
+    {
+        $signature = Dever::input('signature');
+        $user = Dever::loginResult($signature, $state);
+        if ($state && !isset($user['uid'])) {
+            Dever::alert('user error');
+        }
+        if (isset($user['uid']) && $user['uid']) {
+            return $user['uid'];
+        }
+        return -1;
+    }
+
+    public function getToken($request = array())
+    {
+        return http_build_query(Dever::token($request));
+    }
+
+    public function getUrl($method, $request = array())
+    {
+        return Dever::proxy($method, $this->getToken($request));
+    }
+
+    protected function info()
+    {
+        $data = $this->save->get(self::NAME);
+        return $data;
+    }
+
+    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-one', $param);
+
+            if ($check) {
+                if (time() - $check['cdate'] < Dever::config('base', 'project')->mobileCode['time']) {
+                    Dever::alert('请不要在一分钟之内申请多次验证码,请您稍后再试');
+                }
+            }
+        }
+
+        $total = Dever::config('base', 'project')->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);
+
+        # 启动发送
+        $this->send($mobile, $insert['add_code'], $id);
+
+        $this->save->add(self::MCODE, $mobile . '_' . $code->mcode, Dever::config('base', 'project')->mobileCode['timeout']);
+
+        return $code->mcode;
+    }
+
+    public function send($mobile, $code, $id = false)
+    {
+        if (Dever::project('sms')) {
+            $send['skin'] = 'code';
+            $send['mobile'] = $mobile;
+            $send['param'] = array
+            (
+                'code' => $code
+            );
+            $send['param'] = Dever::json_encode($send['param']);
+            print_r($send);die;
+            return Dever::load('sms/api.send', $send);
+        }
+        $url = Dever::config('base', 'project')->mobileCode['url'];
+
+        if (!$url) {
+            return;
+        }
+
+        $content = Dever::config('base', 'project')->mobileCode['body'];
+
+        $content = $this->replace($content, $mobile, $code);
+
+        parse_str($content, $param);
+
+        $type = Dever::config('base', 'project')->mobileCode['method'];
+        $json = Dever::config('base', 'project')->mobileCode['json'];
+        $header = Dever::config('base', 'project')->mobileCode['header'];
+
+        return Dever::curl($url, $param, $type, $json, $header);
+    }
+
+    private function replace($content, $mobile = '', $code = '')
+    {
+        $skin = Dever::config('base', 'project')->mobileCode['skin'];
+        $skin_key = Dever::input('skin', 'code');
+        if (isset($skin[$skin_key])) {
+            $skin = $skin[$skin_key];
+        } else {
+            $skin = array_shift($skin);
+        }
+
+        $config = array('{code}', '{mobile}', '{sign}', '{skin}', '{param}');
+        $replace = array($code, $mobile, Dever::config('base', 'project')->mobileCode['sign'], $skin);
+        return str_replace($config, $replace, $content);
+    }
+
+    protected function refer($state = false)
+    {
+        $refer = Dever::input('refer');
+
+        $project = 'main';
+
+        if ($refer) {
+            $refer = Encrypt::decode($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);
+            }
+            */
+            
+        } else {
+            $refer = Dever::url('home', $project);
+        }
+
+        $param = Dever::input('param');
+
+        if ($param) {
+            $refer .= '&' . $param;
+        }
+
+        if ($state) {
+            return $refer;
+        }
+        Dever::out($refer);
+    }
+
+    protected function save($user)
+    {
+        if (is_numeric($user)) {
+            $user = Dever::load('passport/user-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']);
+
+            if (Dever::config('base', 'project')->regAction) {
+                Dever::load(Dever::config('base', 'project')->regAction, $user);
+            }
+
+            return $user;
+        }
+    }
+
+    public function createRefer()
+    {
+        return 'refer=' . Encrypt::encode(Dever::url(false, 'main'));
+    }
+
+    protected function saveSex($sex)
+    {
+        if ($sex || $sex == 0) {
+            $config_sex = Dever::config('base', 'project')->sex;
+            if (isset($config_sex[$sex])) {
+                $sex = $config_sex[$sex];
+            } else {
+                if ($sex == '男') {
+                    $sex = 1;
+                } elseif ($sex == '女') {
+                    $sex = 2;
+                } elseif ($sex == '未知') {
+                    $sex = 3;
+                }
+            }
+        }
+        return $sex;
+    }
+
+    protected function saveAvatar($value)
+    {
+        if (!$value) {
+            return '';
+        }
+        $value = Dever::pic($value);
+        $base = Dever::data();
+        $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 Img();
+
+            $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);
+            if ($file) {
+                $result = str_replace('/upload/', '/', Dever::config('host')->uploadRes . $path . $name . $ext);
+            } else {
+                $result = $value;
+            }
+        }
+
+        return $result;
+    }
+}

+ 107 - 107
src/Lib/Code.php → lib/Code.php

@@ -1,107 +1,107 @@
-<?php
-namespace Passport\Src\Lib;
-
-use Dever;
-
-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()
-    {
-        $len = isset(Dever::config('base', 'project')->mobileCode['length']) ? Dever::config('base', 'project')->mobileCode['length'] : 4;
-        $this->createRand($len);
-    }
-
-    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);
-    }
-}
+<?php
+namespace Passport\Lib;
+
+use Dever;
+
+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()
+    {
+        $len = isset(Dever::config('base', 'project')->mobileCode['length']) ? Dever::config('base', 'project')->mobileCode['length'] : 4;
+        $this->createRand($len);
+    }
+
+    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);
+    }
+}

+ 2 - 15
src/Api.php

@@ -2,25 +2,12 @@
 namespace Passport\Src;
 
 use Dever;
+use Passport\Lib\Base;
 
-class Api
+class Api extends Base
 {
     private $user;
 
-    public function createUsername($uid, $username, $update = false)
-    {
-        if (!$username || $username == '临时用户') {
-            $username = $uid + 100000;
-            $username = 'JM' . $username;
-
-            if ($update) {
-                Dever::db('passport/user')->update(array('where_id' => $uid, 'username' => $username));
-            }
-        }
-        
-        return $username;
-    }
-
     public function info($uid)
     {
         $info = Dever::db('passport/user')->one($uid);

+ 5 - 2
src/App.php

@@ -2,7 +2,7 @@
 namespace Passport\Src;
 
 use Dever;
-use Passport\Src\Lib\Base;
+use Passport\Lib\Base;
 
 class App extends Base
 {
@@ -79,6 +79,7 @@ class App extends Base
     {
     	$mobile = Dever::input('mobile');
     	$code = Dever::input('code');
+        $system = Dever::input('system', 1);
     	$source_type = Dever::input('source_type', 'applet');
     	$send['skin'] = 'code';
     	$send['mobile'] = $mobile;
@@ -100,17 +101,19 @@ class App extends Base
             $user['add_bind'] = 1;
             $user['add_temp'] = 2;
             $user['add_source_type'] = $source_type;
+            $user['add_system_id'] = $system;
             $uid = Dever::load('passport/user-insert', $user);
 
             Dever::score($uid, 'mobile_reg', '手机号注册');
         } else {
-        	$uid = $info['id'];
+            $uid = $this->combine($info['id'], $mobile, 'mobile');
         }
 
         # 记录app登录日志
         $appData = array('uid' => $uid, 'source_type' => $source_type);
         $app = Dever::db('passport/app')->one($appData);
         if (!$app) {
+            $appData['system_id'] = $system;
         	Dever::db('passport/app')->insert($appData);
         }
 

+ 13 - 52
src/Applet.php

@@ -2,7 +2,7 @@
 namespace Passport\Src;
 
 use Dever;
-use Passport\Src\Lib\Base;
+use Passport\Lib\Base;
 
 # 小程序
 
@@ -58,45 +58,6 @@ class Applet extends Base
         return $user;
     }
 
-    /**
-     * 更新用户信息 绑定用户手机号
-     *
-     * @return mixed
-     */
-    public function bind_mobile()
-    {
-        $uid = $this->check();
-        //$code = Dever::input('mcode');
-        $mobile = Dever::load('passport/reg')->checkMobileExists();
-        if ($mobile && $uid) {
-            $info = Dever::load('passport/user-one', $uid);
-            $result['mobile'] = $mobile;
-            if ($info) {
-                $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,请重新登录');
-            }
-        }
-
-        if (!$info['mobile']) {
-            Dever::score($uid, 'bind_mobile', '绑定手机号');
-        }
-
-        $result['uid'] = $uid;
-        $result['signature'] = Dever::login($uid);
-
-        return $result;
-    }
-
     /*
     public function test_sms()
     {
@@ -118,6 +79,7 @@ class Applet extends Base
     private function create($data)
     {
         $uid = false;
+        $system = Dever::input('system', 1);
         $info = Dever::load('passport/wechat-one', array('option_openid' => $data['openid']));
 
         if (!$info) {
@@ -126,7 +88,8 @@ class Applet extends Base
                 if (!$info) {
                     $uid = false;
                 } else {
-                    $uid = $info['uid'];
+                    # 判断用户是否存在,是否需要合并
+                    $uid = $this->combine($info['uid'], $data['unionid']);
                 }
                 $wechat['unionid'] = $data['unionid'];
             }
@@ -135,23 +98,25 @@ class Applet extends Base
                 $user['temp'] = 1;
                 $user['username'] = '临时用户';
                 $user['source_type'] = 'applet';
+                $user['system_id'] = $system;
 
                 $uid = Dever::load('passport/user-insert', $user);
 
-                Dever::load('passport/api')->createUsername($uid, $user['username'], true);
+                Dever::load('passport/lib/base')->createUsername($uid, $user['username'], true);
             }
              
             $wechat['openid'] = $data['openid'];
             $wechat['session_key'] = $data['session_key'];
             $wechat['uid'] = $uid;
             $wechat['type'] = 1;
+            $wechat['system_id'] = $system;
             $id = Dever::load('passport/wechat-insert', $wechat);
         } else {
             $uid = $info['uid'];
             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['session_key'] = $data['session_key'];
@@ -198,6 +163,8 @@ class Applet extends Base
         }
 
         if ($mobile && $phoneNumber && $uid) {
+            $uid = $this->combine($uid, $mobile, 'mobile');
+
             $info = Dever::load('passport/user-one', $uid);
             $result['mobile'] = $mobile;
             if ($info) {
@@ -257,15 +224,9 @@ class Applet extends Base
                     Dever::load('passport/wechat-update', $wechat);
                 } elseif ($vinfo && $vinfo['id'] != $vid) {
                     $wechat['set_unionid'] = $unionid;
-                    //$wechat['set_uid'] = $vinfo['uid'];
+                    $uid = $this->combine($uid, $unionid);
+                    $wechat['set_uid'] = $uid;
                     $wechat['where_id'] = $vid;
-
-                    # 删除user信息
-                    if ($uid != $vinfo['uid']) {
-                        //Dever::load('passport/user-update', array('set_state' => 2, 'where_id' => $uid));
-                    }
-                    
-                    //$uid = $vinfo['uid'];
                     Dever::load('passport/wechat-update', $wechat);
                 }
                 
@@ -281,7 +242,7 @@ class Applet extends Base
             }
             $update['temp'] = 2;
 
-            $update['set_username'] = Dever::load('passport/api')->createUsername($uid, $name);
+            $update['set_username'] = Dever::load('passport/lib/base')->createUsername($uid, $name);
             
             if ($sex == 1) {
                 $update['set_sex'] = 1;

+ 1 - 1
src/Login.php

@@ -2,7 +2,7 @@
 namespace Passport\Src;
 
 use Dever;
-use Passport\Src\Lib\Base;
+use Passport\Lib\Base;
 
 class Login extends Base
 {

+ 1 - 1
src/Reg.php

@@ -2,7 +2,7 @@
 namespace Passport\Src;
 
 use Dever;
-use Passport\Src\Lib\Base;
+use Passport\Lib\Base;
 
 # 所有手机发送短信操作,后续要独立成一个组件
 

+ 1 - 1
src/User.php

@@ -2,7 +2,7 @@
 namespace Passport\Src;
 
 use Dever;
-use Passport\Src\Lib\Base;
+use Passport\Lib\Base;
 
 class User extends Base
 {