ProfileController.php
12.4 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Powerless < wzxaini9@gmail.com>
// +----------------------------------------------------------------------
namespace app\user\controller;
use cmf\lib\Storage;
use think\Validate;
use think\Image;
use cmf\controller\UserBaseController;
use app\user\model\UserModel;
use think\Db;
use think\Request;
class ProfileController extends UserBaseController
{
function _initialize()
{
parent::_initialize();
}
//用户中心
public function percenter()
{
$uid = session('user.id');
$data = Db::name('user')->where(['id' => $uid])->find();
$this->assign('data', $data);
if ($data['user_type'] == '3') {
$yyxs = Db::name('yuyue')
->where(['teach_id' => $uid, 'status' => 3])
->field(' SUM(yuyue_end - yuyue_begin) as yyxs ')
->select()->toArray();
$yyxs = $yyxs ? $yyxs[0]['yyxs'] : '0';
$this->assign("yyxs", $yyxs);
return $this->fetch('percenter_coach');
} else {
$yyxs = Db::name('yuyue')
->where(['uid' => $uid, 'status' => 3])
->field(' SUM(yuyue_end - yuyue_begin) as yyxs ')
->select()->toArray();
$yyxs = $yyxs ? $yyxs[0]['yyxs'] : '0';
$this->assign("yyxs", $yyxs);
return $this->fetch('percenter_stu');
}
}
//用户个人资料
public function perinfo()
{
$uid = session('user.id');
$data = Db::name('user')->where(['id' => $uid])->find();
$this->assign('data', $data);
return $this->fetch('perinfo_stu');
}
//修改用户个人资料
public function perinfo_ins()
{
$file = $this->request->file('headimgurl');
if ($file) {
$result = $file->validate([
'ext' => 'jpg,jpeg,png',
'size' => 1024 * 1024
])->move('.' . DS . 'upload' . DS . 'avatar' . DS);
if ($result) {
$avatarSaveName = str_replace('//', '/', str_replace('\\', '/', $result->getSaveName()));
$avatar = '/upload/avatar/' . $avatarSaveName;
}
$data['headimgurl'] = $avatar;
}
$data1 = $this->request->post();
// $sres = cmf_check_verification_code($data1['mobile'],$data1['sms_code']);
//
// if($sres!=''){
// return json(['code'=>'0','msg'=>$sres]);
// }
$data['user_nickname'] = $data1['user_nickname'];
$data['mobile'] = $data1['mobile'];
$rree = Db::name('user')->where(['id' => session('user.id')])->update($data);
if ($rree) {
return json(['code' => '1', 'msg' => '保存成功']);
} else {
return json(['code' => '0', 'msg' => '您未做更改']);
}
}
/**
* 会员中心首页
*/
public function center()
{
$user = cmf_get_current_user();
$this->assign($user);
$userId = cmf_get_current_user_id();
$userModel = new UserModel();
$user = $userModel->where('id', $userId)->find();
$this->assign('user', $user);
return $this->fetch();
}
/**
* 编辑用户资料
*/
public function edit()
{
$user = cmf_get_current_user();
$this->assign($user);
return $this->fetch('edit');
}
/**
* 编辑用户资料提交
*/
public function editPost()
{
if ($this->request->isPost()) {
$validate = new Validate([
'user_nickname' => 'max:32',
'sex' => 'between:0,2',
'birthday' => 'dateFormat:Y-m-d|after:-88 year|before:-1 day',
'user_url' => 'url|max:64',
'signature' => 'max:128',
]);
$validate->message([
'user_nickname.max' => lang('NICKNAME_IS_TO0_LONG'),
'sex.between' => lang('SEX_IS_INVALID'),
'birthday.dateFormat' => lang('BIRTHDAY_IS_INVALID'),
'birthday.after' => lang('BIRTHDAY_IS_TOO_EARLY'),
'birthday.before' => lang('BIRTHDAY_IS_TOO_LATE'),
'user_url.url' => lang('URL_FORMAT_IS_WRONG'),
'user_url.max' => lang('URL_IS_TO0_LONG'),
'signature.max' => lang('SIGNATURE_IS_TO0_LONG'),
]);
$data = $this->request->post();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
$editData = new UserModel();
if ($editData->editData($data)) {
$this->success(lang('EDIT_SUCCESS'), "user/profile/center");
} else {
$this->error(lang('NO_NEW_INFORMATION'));
}
} else {
$this->error(lang('ERROR'));
}
}
/**
* 个人中心修改密码
*/
public function password()
{
$user = cmf_get_current_user();
$this->assign($user);
return $this->fetch();
}
/**
* 个人中心修改密码提交
*/
public function passwordPost()
{
if ($this->request->isPost()) {
$validate = new Validate([
'old_password' => 'require|min:6|max:32',
'password' => 'require|min:6|max:32',
'repassword' => 'require|min:6|max:32',
]);
$validate->message([
'old_password.require' => lang('old_password_is_required'),
'old_password.max' => lang('old_password_is_too_long'),
'old_password.min' => lang('old_password_is_too_short'),
'password.require' => lang('password_is_required'),
'password.max' => lang('password_is_too_long'),
'password.min' => lang('password_is_too_short'),
'repassword.require' => lang('repeat_password_is_required'),
'repassword.max' => lang('repeat_password_is_too_long'),
'repassword.min' => lang('repeat_password_is_too_short'),
]);
$data = $this->request->post();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
$login = new UserModel();
$log = $login->editPassword($data);
switch ($log) {
case 0:
$this->success(lang('change_success'));
break;
case 1:
$this->error(lang('password_repeat_wrong'));
break;
case 2:
$this->error(lang('old_password_is_wrong'));
break;
default :
$this->error(lang('ERROR'));
}
} else {
$this->error(lang('ERROR'));
}
}
// 用户头像编辑
public function avatar()
{
$user = cmf_get_current_user();
$this->assign($user);
return $this->fetch();
}
// 用户头像上传
public function avatarUpload()
{
$file = $this->request->file('file');
$result = $file->validate([
'ext' => 'jpg,jpeg,png',
'size' => 1024 * 1024
])->move('.' . DS . 'upload' . DS . 'avatar' . DS);
if ($result) {
$avatarSaveName = str_replace('//', '/', str_replace('\\', '/', $result->getSaveName()));
$avatar = 'avatar/' . $avatarSaveName;
session('avatar', $avatar);
return json_encode([
'code' => 1,
"msg" => "上传成功",
"data" => ['file' => $avatar],
"url" => ''
]);
} else {
return json_encode([
'code' => 0,
"msg" => $file->getError(),
"data" => "",
"url" => ''
]);
}
}
// 用户头像裁剪
public function avatarUpdate()
{
$avatar = session('avatar');
if (!empty($avatar)) {
$w = $this->request->param('w', 0, 'intval');
$h = $this->request->param('h', 0, 'intval');
$x = $this->request->param('x', 0, 'intval');
$y = $this->request->param('y', 0, 'intval');
$avatarPath = "./upload/" . $avatar;
$avatarImg = Image::open($avatarPath);
$avatarImg->crop($w, $h, $x, $y)->save($avatarPath);
$result = true;
if ($result === true) {
$storage = new Storage();
$result = $storage->upload($avatar, $avatarPath, 'image');
$userId = cmf_get_current_user_id();
Db::name("user")->where(["id" => $userId])->update(["avatar" => $avatar]);
session('user.avatar', $avatar);
$this->success("头像更新成功!");
} else {
$this->error("头像保存失败!");
}
}
}
/**
* 绑定手机号或邮箱
*/
public function binding()
{
$user = cmf_get_current_user();
$this->assign($user);
return $this->fetch();
}
/**
* 绑定手机号
*/
public function bindingMobile()
{
if ($this->request->isPost()) {
$validate = new Validate([
'username' => 'require|number|unique:user,mobile',
'verification_code' => 'require',
]);
$validate->message([
'username.require' => '手机号不能为空',
'username.number' => '手机号只能为数字',
'username.unique' => '手机号已存在',
'verification_code.require' => '验证码不能为空',
]);
$data = $this->request->post();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
$errMsg = cmf_check_verification_code($data['username'], $data['verification_code']);
if (!empty($errMsg)) {
$this->error($errMsg);
}
$userModel = new UserModel();
$log = $userModel->bindingMobile($data);
switch ($log) {
case 0:
$this->success('手机号绑定成功');
break;
default :
$this->error('未受理的请求');
}
} else {
$this->error("请求错误");
}
}
/**
* 绑定邮箱
*/
public function bindingEmail()
{
if ($this->request->isPost()) {
$validate = new Validate([
'username' => 'require|email|unique:user,user_email',
'verification_code' => 'require',
]);
$validate->message([
'username.require' => '邮箱地址不能为空',
'username.email' => '邮箱地址不正确',
'username.unique' => '邮箱地址已存在',
'verification_code.require' => '验证码不能为空',
]);
$data = $this->request->post();
if (!$validate->check($data)) {
$this->error($validate->getError());
}
$errMsg = cmf_check_verification_code($data['username'], $data['verification_code']);
if (!empty($errMsg)) {
$this->error($errMsg);
}
$userModel = new UserModel();
$log = $userModel->bindingEmail($data);
switch ($log) {
case 0:
$this->success('邮箱绑定成功');
break;
default :
$this->error('未受理的请求');
}
} else {
$this->error("请求错误");
}
}
}