UserApiController.php
3.7 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
namespace App\Http\Controllers\Api;
use App\Http\Requests\UserUpdate;
use App\Models\UserModel;
use http\Client\Curl\User;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;
class UserApiController extends Controller
{
public function info(Request $request)
{
$user = $request->user();
$user = \Arr::only($user->toArray(), [
'id',
'name',
'email',
'phone',
'account_properties',
'company_name',
'first_name',
'last_name',
]);
return $this->setSuccessData($user);
}
public function update(UserUpdate $request)
{
$data = $request->only([
'account_properties',
'company_name',
'first_name',
'last_name',
]);
$id = $request->user->id;
$data['update_time'] = time();
$result = UserModel::where('id', $id)->update($data);
if (!$result) {
return $this->setError('Update user info failed');
}
return $this->setSuccess('Update user info success');
}
public function changePhone(Request $request)
{
$password = $request->input('password');
$phone = $request->input('phone');
$validator = Validator::make($request->all(), [
'phone' => 'required',
'password' => 'required|max:100'
]);
if ($validator->fails()) {
return $this->setError($validator->errors()->first());
}
$userId = Auth::user()->id;
$hashedPassword = UserModel::where('id', $userId)->value('password');
if (!Hash::check($password, $hashedPassword)) {
return $this->setError('Wrong Password!');
}
$result = UserModel::where('id', $userId)->update(['phone' => $phone]);
if (!$result) {
return $this->setError('Change phone failed');
}
return $this->setSuccess('Change phone success');
}
public function changeEmail(Request $request)
{
$password = $request->input('password');
$email = $request->input('email');
$validator = Validator::make($request->all(), [
'email' => 'required|email',
'password' => 'required|max:100',
'email_code' => 'required'
]);
if ($validator->fails()) {
return $this->setError($validator->errors()->first());
}
$userId = Auth::user()->id;
$hashedPassword = UserModel::where('id', $userId)->value('password');
if (!Hash::check($password, $hashedPassword)) {
return $this->setError('Wrong Password!');
}
//发送邮箱验证码
$redisKey = 'sem_email_code_change_email' . '_' . $email;
$cachedEmailCode = Redis::get($redisKey);
if ($cachedEmailCode != $request->input('email_code')) {
return $this->setError('Email code invalid');
}
//先判断下邮箱是否存在
$existEmail = UserModel::where('email', $email)->exists();
if ($existEmail) {
return $this->setError('Email has been registered');
}
$result = UserModel::where('id', $userId)->update(['email' => $email,'update_time' => time()]);
if (!$result) {
return $this->setError('Change email failed');
}
Redis::del($redisKey);
return $this->setSuccess('Change email success');
}
}