Skip to content

Commit e1874de

Browse files
authored
Merge pull request #13 from hanfengmi/master
user
2 parents a7d39fe + f0ccc9f commit e1874de

File tree

9 files changed

+184
-45
lines changed

9 files changed

+184
-45
lines changed

application/config/config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@
324324
| https://codeigniter.com/user_guide/libraries/encryption.html
325325
|
326326
*/
327-
$config['encryption_key'] = '';
327+
$config['encryption_key'] = 'PHP-ENC-KEY-IN-TIME';
328328

329329
/*
330330
|--------------------------------------------------------------------------

application/controllers/Admin.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@ public function index()
2020
$this->load->model('admin_model');
2121
$product = $this->admin_model->getProductData();
2222
$catgory = $this->admin_model->getCatgoryData();
23+
$user = $this->admin_model->getUserData();
2324
$data['products'] = $product;
2425
$data['catgory'] = $catgory;
26+
$data['user'] = $user;
2527
// var_dump($product,$catgory)
2628
// $this->load->view('home',['products' => $products]);
2729

application/controllers/Login.php

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function index(){
1515
$this->load->helper('cookie');
1616
$this->load->library('layout');
1717
$this->layout->view('login');
18+
1819
}
1920

2021
public function Login() {
@@ -29,38 +30,53 @@ public function Login() {
2930
$user = $this->Login_model->login($post['email'], $post['pwd']);
3031
// $url = current_url();
3132
$response = array('status'=>'0','msg'=>'failed','data'=>'');
33+
34+
if($user['status'] == 2){
35+
// 登录成功构造JWT, 加上当前时间戳。
36+
$token['email'] = $user['data']->email;
37+
$token['role'] = $user['data']->role;
38+
$token['time'] = time();
39+
$jwtToken = $this->objOfJwt->GenerateToken($token);
40+
$cookie = array(
41+
'name' => 'auth',
42+
'value' => $jwtToken,
43+
'expire' => 60*60*24*3,
44+
'path' => NULL,
45+
'domain' => NULL,
46+
'secure' => FALSE,
47+
'prefix' => NULL,
48+
'httponly' => TRUE
49+
);
50+
// set cookie + set session
51+
$this->input->set_cookie($cookie);
52+
$_SESSION['nonces'] = md5(rand(1,10000));
53+
// $decodeToken = $this->objOfJwt->DecodeToken($jwtToken);
54+
// echo $decodeToken;
55+
// var_dump($_COOKIE);
56+
57+
$response = array('status'=>'2','msg'=>'success','data'=>$user['data']);
58+
echo json_encode($response);
59+
}else {
60+
$response = array('status'=>'0','msg'=>'failed','data'=>'pwd or email error');
61+
echo json_encode($response);
62+
}
3263

33-
if(sizeof($user)>0){
34-
// 登录成功构造JWT, 加上当前时间戳。
35-
$token['email'] = $user[0]->email;
36-
$token['role'] = $user[0]->role;
37-
$token['time'] = time();
38-
$jwtToken = $this->objOfJwt->GenerateToken($token);
39-
$cookie = array(
40-
'name' => 'auth',
41-
'value' => $jwtToken,
42-
'expire' => 60*60*24*3,
43-
'path' => NULL,
44-
'domain' => NULL,
45-
'secure' => FALSE,
46-
'prefix' => NULL,
47-
'httponly' => TRUE
48-
);
49-
// set cookie + set session
50-
$this->input->set_cookie($cookie);
51-
$_SESSION['nonces'] = md5(rand(1,10000));
52-
// $decodeToken = $this->objOfJwt->DecodeToken($jwtToken);
53-
// echo $decodeToken;
54-
// var_dump($_COOKIE);
64+
}
5565

56-
$response = array('status'=>'2','msg'=>'success','data'=>$user[0]);
57-
echo json_encode($response);
58-
}else{
59-
$response = array('status'=>'0','msg'=>'failed','data'=>$user);
60-
// echo json_encode($user);
61-
// $this->layout->view('item');
62-
echo json_encode($response);
63-
}
66+
public function SignUp() {
67+
$response = array('status'=>'0','msg'=>'failed');
68+
$post = $this->input->post(NULL, TRUE);
69+
$this->load->model('Login_model');
70+
71+
$pwd = password_hash($post['pwd'], PASSWORD_DEFAULT);
72+
$user = $this->Login_model->signUp($post['email'], $pwd);
73+
if($user['status'] == 2){
74+
$response = array('status'=>'2','msg'=>'success','data'=>$user['data']);
75+
}else {
76+
$response = array('status'=>'0','msg'=>'failed','data'=>$user['data']);
77+
}
78+
// $user = password_verify('123123', $pwd); 验证 返回true || false
79+
echo json_encode($response);
6480
}
6581

6682
public function LogOut() {

application/database/data.sq3

0 Bytes
Binary file not shown.

application/models/Admin_model.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,11 @@ public function getCatgoryData(){
2222
return $query->result();
2323

2424
}
25+
26+
public function getUserData(){
27+
// return array("Volvo","BMW","SAAB");
28+
$query = $this->db->query('select * from users;');
29+
return $query->result();
30+
31+
}
2532
}

application/models/Login_model.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,48 @@ public function __construct(){
55
// $this->load->database(); // 配置文件中已经自动加载
66
}
77

8+
public function signUp($email, $pwd){
9+
$data = array(
10+
'email'=> $email,
11+
'pwd'=> $pwd,
12+
'name'=>'example',
13+
'role'=>1
14+
);
15+
$res = array('status'=>'0','msg'=>'failed','data'=>null);
16+
try{
17+
$sql = "SELECT * FROM users WHERE email = ? ";
18+
$query = $this->db->query($sql, array($email));
19+
$user = $query->result();
20+
if(sizeof($user)>0){
21+
$res = array('status'=>'0','msg'=>'failed','data'=>'Email has been registered');
22+
return $res;
23+
}else {
24+
$insert = $this->db->insert( 'users' , $data );
25+
$res = array('status'=>'2','msg'=>'success','data'=>$this->db->insert_id());
26+
return $res;
27+
}
28+
}catch(PDOEXCEPTION $e){
29+
echo $e->getMessage();
30+
}
31+
}
832

933
public function login($email, $pwd){
1034
try{
11-
$sql = "SELECT * FROM users WHERE email = ? AND pwd = ?";
35+
$sql = "SELECT * FROM users WHERE email = ? ";
1236
// use query bindings to prevent against injection.
13-
$query = $this->db->query($sql, array($email, $pwd));
14-
return $query->result();
37+
$query = $this->db->query($sql, array($email));
38+
$res = array('status'=>'0','msg'=>'failed','data'=>null);
39+
$user = $query->result();
40+
if(sizeof($user)>0 && password_verify($pwd, $user[0]->pwd)){
41+
$res = array('status'=>'2','msg'=>'success','data'=>$user[0]);
42+
return $res;
43+
} else {
44+
$res = array('status'=>'0','msg'=>'failed','data'=>null);
45+
return $res;
46+
};
47+
1548
}catch(PDOEXCEPTION $e){
1649
echo $e->getMessage();
1750
}
1851
}
19-
2052
}

application/views/admin.php

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
<!-- /.container-fluid-->
7171
</div>
7272

73-
<div id="show-table">
73+
<div id="show-table" class="container-fluid">
7474
<div class="product-table col-md-6">
7575
<table class="table table-hover">
7676
<thead>
@@ -140,8 +140,48 @@
140140
</table>
141141

142142
</div>
143+
<div class="user-table col-md-6 table-responsive">
144+
<table class="table table-hover">
145+
<thead>
146+
<tr>
147+
<th>userid</th>
148+
<th>name</th>
149+
<th>email</th>
150+
<th>pwd</th>
151+
<th>role</th>
152+
</tr>
153+
</thead>
154+
<tbody class="category-tbody">
155+
<?php
156+
if($data['user']){
157+
foreach($data['user'] as $row) {
158+
$user_id = $row->userid;
159+
$user_name = $row->name;
160+
$user_email = $row->email;
161+
$user_pwd = $row->pwd;
162+
$user_role = $row->role;
163+
echo
164+
'
165+
<tr>
166+
<td>' . $user_id . '</td>
167+
<td>' . $user_name . '</td>
168+
<td>' . $user_email . '</td>
169+
<td>' . $user_pwd . '</td>
170+
<td>' . $user_role . '</td>
171+
</tr>
172+
';
173+
}
174+
}
175+
?>
176+
</tbody>
177+
178+
</table>
179+
</div>
143180
</div>
144181

182+
183+
184+
145185
<!-- /.content-wrapper-->
146186
<footer class="sticky-footer">
147187
<div class="container">

application/views/header.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'
1818
<p>guest</p>
1919
<button type="button" class="logIn">logIn</button>
20+
<button type="button" class="logIn">signUp</button>
2021
';
2122
}
2223
?>
@@ -119,8 +120,9 @@ function findProd(prod) {
119120
window.location.href = url.split('item')[0]+'login'
120121
}
121122
})
122-
}
123123

124+
},
125+
124126
}
125127
header.init();
126128
</script>

application/views/login.php

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<div class="content-wrapper">
44
<div class="col-md-6">
5-
<h3> Login </h3>
5+
<h3 class="log-title"> Login </h3>
66
<form id="login">
77
<div class="form-group">
88
<label>Email</label>
@@ -12,7 +12,8 @@
1212
<label>Pwd</label>
1313
<input id="pwd" type="password" class="form-control" name="pwd">
1414
</div>
15-
<button type="button" class="btn btn-primary" value="Submit">Submit</button>
15+
<button type="button" class="btn btn-primary btn-login" >Login</button>
16+
<button type="button" class="btn btn-primary btn-signUp" >SignUp</button>
1617
</form>
1718
</div>
1819
</div>
@@ -22,9 +23,11 @@
2223
var logIn = {
2324
init:function(){
2425
this.login();
26+
this.signUp();
2527
},
2628
login:function(){
27-
$('#login').on('click','.btn-primary',function(){
29+
$('#login').on('click','.btn-login',function(){
30+
var pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
2831
var formValue = $('#login').serializeArray();
2932
var values = {};
3033
for (x in formValue) {
@@ -35,17 +38,22 @@
3538
return false;
3639
}
3740
}
41+
if (!pattern.test(values['email'])){
42+
alert('请填写正确邮箱')
43+
return false;
44+
}
3845
$.ajax({
3946
type: "post",
4047
data: values,
4148
url: "./Login/Login",
4249
dataType: 'json',
43-
// xhrFields: { withCredentials: true },
4450
success: function(data) {
45-
if(data.data.role == 0){
46-
window.location.href = window.location.href.split('login')[1]+'admin';
47-
} else if(data.data.role == 1) {
48-
window.location.href = window.location.href.split('login')[1]+'home';
51+
if(data.status == 2){
52+
if(data.data.role == 0){
53+
window.location.href = window.location.href.split('login')[1]+'admin';
54+
} else if(data.data.role == 1) {
55+
window.location.href = window.location.href.split('login')[1]+'home';
56+
}
4957
}else {
5058
alert('email or pwd error')
5159
}
@@ -55,6 +63,38 @@
5563
}
5664
});
5765
})
66+
},
67+
signUp:function(){
68+
$('#login').on('click','.btn-signUp',function(){
69+
var pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
70+
var formValue = $('#login').serializeArray();
71+
var values = {};
72+
for (x in formValue) {
73+
if (formValue[x].value) {
74+
values[formValue[x].name] = formValue[x].value;
75+
} else {
76+
alert(formValue[x].name + ' is required')
77+
return false;
78+
}
79+
}
80+
if (!pattern.test(values['email'])){
81+
alert('请填写正确邮箱')
82+
return false;
83+
}
84+
$.ajax({
85+
type: "post",
86+
data: values,
87+
url: "./Login/SignUp",
88+
dataType: 'json',
89+
success: function(data) {
90+
if(data.status == 2){
91+
alert('success');
92+
}else {
93+
alert(data.data);
94+
}
95+
}
96+
})
97+
})
5898
}
5999
}
60100
logIn.init();

0 commit comments

Comments
 (0)