User.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * @Created by PhpStorm
  4. * @author: Kirin
  5. * @day: 2024/12/16
  6. * @time: 10:43
  7. */
  8. namespace app\controller\api\user;
  9. use app\common\ApiBaseController;
  10. use app\Request;
  11. use app\services\user\UserLevelServices;
  12. use app\services\user\UserServices;
  13. use app\validate\api\LoginValidate;
  14. use think\exception\ValidateException;
  15. /**
  16. * 用户基本类
  17. * Class User
  18. * @package app\controller\api\user
  19. */
  20. class User extends ApiBaseController
  21. {
  22. public function __construct(Request $request, UserServices $services)
  23. {
  24. parent::__construct($request);
  25. $this->service = $services;
  26. }
  27. public function info()
  28. {
  29. //用户信息
  30. $user = $this->request->user();
  31. if (is_object($user)) $user = $user->toArray();
  32. /** @var UserLevelServices $levelService */
  33. $levelService = app()->make(UserLevelServices::class);
  34. //用户等级
  35. $level = $levelService->getUserLevelInfo($user['uid']);
  36. $user['level_info'] = $level;
  37. return $this->success('ok', $user);
  38. }
  39. public function setAccount()
  40. {
  41. $user = $this->request->user();
  42. if ($user['change_account_time'] > strtotime(date('Y-01-01', time()) && $user['change_account_time'] < strtotime(date('Y-12-31', time())))) {
  43. $this->error('今年已经修改过账号,不能再修改了');
  44. }
  45. $new = $this->request->post('new_account');
  46. if ($this->service->be(['account' => $new])) $this->error('账号已存在');
  47. try {
  48. validate(LoginValidate::class)->check([
  49. 'account' => $new,
  50. ]);
  51. } catch (ValidateException $e) {
  52. return $this->error($e->getError());
  53. }
  54. $res = $this->service->update($user['uid'], ['account' => $new, 'change_account_time' => time()]);
  55. if ($res) {
  56. return $this->success('修改成功');
  57. } else {
  58. return $this->error('修改失败');
  59. }
  60. }
  61. }