UsersController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Exceptions\ApiException;
  4. use App\Http\Controllers\Api\Controller;
  5. use App\Models\Course;
  6. use App\Models\User;
  7. use App\Http\Requests\Api\UserRequest;
  8. use App\Models\UserCourses;
  9. use App\Services\User\UserService;
  10. use App\Transformers\UserTransformer;
  11. use Dingo\Api\Http\Request;
  12. use Illuminate\Support\Facades\Cache;
  13. /**
  14. * 注册相关
  15. * Created on 2020/8/14 11:23
  16. * Create by Wpeishi
  17. */
  18. class UsersController extends Controller
  19. {
  20. /**
  21. * 注册
  22. * @param UserRequest $request
  23. * @return \Dingo\Api\Http\Response
  24. */
  25. public function store(UserRequest $request)
  26. {
  27. $verifyData = self::verificationCode($request);
  28. if ($request->password != $request->repassword) {
  29. throw new ApiException('两次输入密码不一致');
  30. }
  31. $user = User::create([
  32. 'phone' => $verifyData['phone'],
  33. 'password' => bcrypt($request->password),
  34. 'user_type' => $request->user_type ?? 0,
  35. ]);
  36. // 清除验证码缓存
  37. Cache::forget($request->verification_key);
  38. if (!$user) {
  39. throw new ApiException('注册失败,请重试');
  40. }
  41. return $this->response->item($user, new UserTransformer())
  42. ->setMeta([
  43. 'access_token' => \Auth::guard('api')->fromUser($user),
  44. 'token_type' => 'Bearer',
  45. 'expires_in' => \Auth::guard('api')->factory()->getTTL() * 60
  46. ])
  47. ->setStatusCode(201);
  48. }
  49. /**
  50. * 获取用户信息
  51. * @return array
  52. * @return \Dingo\Api\Http\Response
  53. */
  54. public function me()
  55. {
  56. $user = $this->auth->user();
  57. // $user_data = UserService::getUserInfo($user->id);
  58. $UserCourses = UserCourses::query()->where(['user_id' => $user->id])->orderByDesc('id')->with('course')->get();
  59. $course_data = [];
  60. foreach ($UserCourses as $k => $v) {
  61. $course_data[] = [
  62. 'subject_name' => $v->course->name,
  63. 'bar' => $v->bar
  64. ];
  65. }
  66. $data = [
  67. 'id' => $user->id,
  68. 'nick_name' => $user->nick_name,
  69. 'phone' => $user->phone,
  70. 'avatar_url' => $user->avatar_url,
  71. 'school' => $user->school,
  72. 'make_num' => $user->make_num,
  73. 'user_type' => $user->user_type,
  74. 'carbon_emission' => $user->carbon_emission,
  75. 'course_data' => $course_data
  76. ];
  77. return apiJson($data);
  78. }
  79. /**
  80. * 验证码路由校验 校验
  81. * @param Request $request
  82. * @return array
  83. *
  84. */
  85. public function isVerification(Request $request)
  86. {
  87. self::verificationCode($request);
  88. return apiJsonMsg('验证码正确!');
  89. }
  90. /**
  91. * 忘记密码
  92. * @param Request $request
  93. * @return mixed
  94. */
  95. public function forgetPassword(Request $request)
  96. {
  97. $verifyData = self::verificationCode($request);
  98. $query = User::query()->where(['phone' => $verifyData['phone']])->first();
  99. if (!$query) {
  100. throw new ApiException('用户不存在');
  101. }
  102. if ($request->password != $request->repassword) {
  103. throw new ApiException('两次输入密码不一致');
  104. }
  105. $user = $query->update([
  106. 'password' => bcrypt($request->password),
  107. ]);
  108. if (!$user) {
  109. throw new ApiException('修改失败,请重试');
  110. }
  111. // 清除验证码缓存
  112. Cache::forget($request->verification_key);
  113. return apiJsonMsg('修改成功请前往登录!', 200, $verifyData['phone']);
  114. }
  115. /**
  116. * 校验验证码是否正确
  117. * @param $request
  118. * @return mixed
  119. */
  120. public static function verificationCode($request)
  121. {
  122. $verifyData = Cache::get($request->verification_key);
  123. if (!$verifyData) {
  124. throw new ApiException('验证码已失效');
  125. }
  126. if (!hash_equals($verifyData['code'], $request->verification_code)) {
  127. throw new ApiException('验证码错误');
  128. }
  129. return $verifyData;
  130. }
  131. }