ProfileController.php
6.5 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\user\controller;
use cmf\controller\RestUserBaseController;
use think\Db;
use think\Validate;
class ProfileController extends RestUserBaseController
{
// 用户密码修改
public function changePassword()
{
$validate = new Validate([
'old_password' => 'require',
'password' => 'require',
'confirm_password' => 'require|confirm:password'
]);
$validate->message([
'old_password.require' => '请输入您的旧密码!',
'password.require' => '请输入您的新密码!',
'confirm_password.require' => '请输入确认密码!',
'confirm_password.confirm' => '两次输入的密码不一致!'
]);
$data = $this->request->param();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
$userId = $this->getUserId();
$userPassword = Db::name("user")->where('id', $userId)->value('user_pass');
if (!cmf_compare_password($data['old_password'], $userPassword)) {
$this->error('旧密码不正确!');
}
Db::name("user")->where('id', $userId)->update(['user_pass' => cmf_password($data['password'])]);
$this->success("密码修改成功!");
}
// 用户绑定邮箱
public function bindingEmail()
{
$validate = new Validate([
'email' => 'require|email|unique:user,user_email',
'verification_code' => 'require'
]);
$validate->message([
'email.require' => '请输入您的邮箱!',
'email.email' => '请输入正确的邮箱格式!',
'email.unique' => '正确账号已存在!',
'verification_code.require' => '请输入数字验证码!'
]);
$data = $this->request->param();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
$userId = $this->getUserId();
$userEmail = Db::name("user")->where('id', $userId)->value('user_email');
if (!empty($userEmail)) {
$this->error("您已经绑定邮箱!");
}
$errMsg = cmf_check_verification_code($data['email'], $data['verification_code']);
if (!empty($errMsg)) {
$this->error($errMsg);
}
Db::name("user")->where('id', $userId)->update(['user_email' => $data['email']]);
$this->success("绑定成功!");
}
// 用户绑定手机号
public function bindingMobile()
{
$validate = new Validate([
'mobile' => 'require|unique:user,mobile',
'verification_code' => 'require'
]);
$validate->message([
'mobile.require' => '请输入您的手机号!',
'mobile.unique' => '手机号已经存在!',
'verification_code.require' => '请输入数字验证码!'
]);
$data = $this->request->param();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
if (!preg_match('/(^(13\d|15[^4\D]|17[013678]|18\d)\d{8})$/', $data['mobile'])) {
$this->error("请输入正确的手机格式!");
}
$userId = $this->getUserId();
$mobile = Db::name("user")->where('id', $userId)->value('mobile');
if (!empty($mobile)) {
$this->error("您已经绑定手机!");
}
$errMsg = cmf_check_verification_code($data['mobile'], $data['verification_code']);
if (!empty($errMsg)) {
$this->error($errMsg);
}
Db::name("user")->where('id', $userId)->update(['mobile' => $data['mobile']]);
$this->success("绑定成功!");
}
/**
* 用户基本信息获取及修改
* @param 请求为GET 获取信息
* @param [string] $[field] [要获取的一个或多个字段名] 可选
* @return 带参数,返回某个或多个字段信息。不带参数,返回所有信息
* @param 请求为POST 修改信息
*/
public function userInfo($field = '')
{
//判断请求为GET,获取信息
if ($this->request->isGet()) {
$userId = $this->getUserId();
$fieldStr = 'user_type,user_login,mobile,user_email,user_nickname,avatar,signature,user_url,sex,birthday,score,coin,user_status,user_activation_key,create_time,last_login_time,last_login_ip';
if (empty($field)) {
$userData = Db::name("user")->field($fieldStr)->find($userId);
} else {
$fieldArr = explode(',', $fieldStr);
$postFieldArr = explode(',', $field);
$mixedField = array_intersect($fieldArr, $postFieldArr);
if (empty($mixedField)) {
$this->error('您查询的信息不存在!');
}
if (count($mixedField) > 1) {
$fieldStr = implode(',', $mixedField);
$userData = Db::name("user")->field($fieldStr)->find($userId);
} else {
$userData = Db::name("user")->where('id', $userId)->value($mixedField);
}
}
$this->success('获取成功!', $userData);
}
//判断请求为POST,修改信息
if ($this->request->isPost()) {
$userId = $this->getUserId();
$fieldStr = 'user_nickname,avatar,signature,user_url,sex,birthday';
$data = $this->request->post();
if (empty($data)) {
$this->error('修改失败,提交表单为空!');
}
if (!empty($data['birthday'])) {
$data['birthday'] = strtotime($data['birthday']);
}
$upData = Db::name("user")->where('id', $userId)->field($fieldStr)->update($data);
if ($upData !== false) {
$this->success('修改成功!');
} else {
$this->error('修改失败!');
}
}
}
}