-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserModel.php
More file actions
92 lines (78 loc) · 2.22 KB
/
UserModel.php
File metadata and controls
92 lines (78 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2017/10/30 0030
* Time: 16:25
*/
class UserModel
{
protected $db;
public function __construct()
{
$config = require_once './config.php';
$this->db = DBServer::getInstance($config['db']);
}
/**
* @param $type
* @param $value
* @return array
*/
public function checkUserExists($type,$value)
{
$sql = "select * from users where $type='$value'";
$record = $this->db->fetchAll($sql);
if($record == false) return stdOutPut('','ok',1);
else return stdOutPut('',"this $type has been registered",0);
}
/**
* @param $type
* @param $value
* @return array
* 检查是否存在用户
*/
public function createUser($type,$value)
{
$name = $_REQUEST['name'];
$created_at = date('Y-m-d H:i:s',time());
$password = $this->getPassword($_REQUEST['password']);
$sql = "insert into users(name,$type,password,created_at)value('$name','$value','$password','$created_at')";
$result = $this->db->exec($sql);
if($result)
{
$sql = "select * from users where $type='$value'";
$record = $this->db->fetchRow($sql);
return stdOutPut($record,'ok',1);
}
else return stdOutPut('','register failed',0);
}
/**
* @param $pasword
* @return bool|string
* 密码加密
*/
public function getPassword($pasword)
{
return password_hash($pasword,PASSWORD_DEFAULT);
}
public function checkPassword($password,$hash)
{
return password_verify($password,$hash);
}
/**
* @param $type
* @param $account
* @param $password
* @return array
* 根据用户名和密码登录
*/
public function login($type,$account,$password)
{
$sql = "select * from users where $type='$account'";
$record = $this->db->fetchRow($sql);
if($record == false) return stdOutPut('','this account do not exists',0);
if(!$this->checkPassword($password,$record['password']))
return stdOutPut('','account or password wrong',0);
return stdOutPut($record,'ok',1);
}
}