123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace app\controller\api\user;
- use app\common\ApiBaseController;
- use app\Request;
- use app\services\user\UserLevelServices;
- use app\services\user\UserServices;
- use app\validate\api\LoginValidate;
- use think\exception\ValidateException;
- class User extends ApiBaseController
- {
- public function __construct(Request $request, UserServices $services)
- {
- parent::__construct($request);
- $this->service = $services;
- }
- public function info()
- {
-
- $user = $this->request->user();
- if (is_object($user)) $user = $user->toArray();
-
- $levelService = app()->make(UserLevelServices::class);
-
- $level = $levelService->getUserLevelInfo($user['uid']);
- $user['level_info'] = $level;
- return $this->success('ok', $user);
- }
- public function setAccount()
- {
- $user = $this->request->user();
- if ($user['change_account_time'] > strtotime(date('Y-01-01', time()) && $user['change_account_time'] < strtotime(date('Y-12-31', time())))) {
- $this->error('今年已经修改过账号,不能再修改了');
- }
- $new = $this->request->post('new_account');
- if ($this->service->be(['account' => $new])) $this->error('账号已存在');
- try {
- validate(LoginValidate::class)->check([
- 'account' => $new,
- ]);
- } catch (ValidateException $e) {
- return $this->error($e->getError());
- }
- $res = $this->service->update($user['uid'], ['account' => $new, 'change_account_time' => time()]);
- if ($res) {
- return $this->success('修改成功');
- } else {
- return $this->error('修改失败');
- }
- }
-
- public function changeMobile()
- {
- $user = $this->request->user();
- [$new_phone, $old_captcha, $new_captcha] = $this->request->postMore([['new_phone', ''], ['old_captcha', ''], ['new_captcha', '']], true);
-
- check_sms_captcha($user['phone'], 'change', $old_captcha);
- check_sms_captcha($new_phone, 'change_new', $new_captcha);
- $res = $this->service->update($user['uid'], ['phone' => $new_phone]);
- if ($res) {
- return $this->success('修改成功');
- } else {
- return $this->error('修改失败');
- }
- }
- public function edit()
- {
- $user = $this->request->user();
- $data = $this->request->postMore([
- ['nickname', ''],
- ['avatar', '']
- ]);
- $data = array_filter($data);
- if (!$data) {
- return $this->error('没有修改任何信息');
- }
- $res = $this->service->update($user['uid'], $data);
- if ($res) {
- return $this->success('修改成功');
- } else {
- return $this->error('修改失败');
- }
- }
- }
|