AuthController.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Exceptions\AlertException;
  4. use App\Http\Controllers\Controller;
  5. use App\Http\Controllers\Core\Auth;
  6. use App\Models\PartnerModel;
  7. use App\Models\User\UserModel;
  8. use App\Models\User\AuthKey;
  9. use App\Services\Auth\PhoneService;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Cache;
  12. use Illuminate\Support\Facades\DB;
  13. use Ixudra\Curl\Facades\Curl;
  14. class AuthController extends Controller
  15. {
  16. /**
  17. * 验证当前手机号-发送验证码
  18. * @return array
  19. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  20. * @throws AlertException
  21. */
  22. public function sendVerify2Current()
  23. {
  24. $uid = Auth::auth();
  25. $user = UserModel::findOrFail($uid);
  26. if (empty($user->phone)) {
  27. throw new AlertException('你还没有绑定手机号', 101);
  28. }
  29. $ps = new PhoneService();
  30. if ($ps->sendVerifyCode($user->phone)) {
  31. return array(
  32. 'code' => 200,
  33. 'message' => 'success'
  34. );
  35. }
  36. throw new AlertException('发送失败', 102);
  37. }
  38. /**
  39. * 验证当前手机号-检验验证码
  40. * @param Request $request
  41. * @return array
  42. * @throws AlertException
  43. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  44. */
  45. public function checkVerify2Current(Request $request)
  46. {
  47. $this->validate($request, ['code' => 'required']);
  48. $uid = Auth::auth();
  49. $user = UserModel::findOrFail($uid);
  50. if (empty($user->phone)) {
  51. throw new AlertException('你还没有绑定手机号', 101);
  52. }
  53. $code = $request->post('code');
  54. if (Cache::get("smsverifycode:{$user->phone}") == $code) {
  55. return [
  56. 'code' => 200,
  57. 'message' => 'success',
  58. ];
  59. } else {
  60. return [
  61. 'code' => 401,
  62. 'message' => '验证码错误',
  63. ];
  64. }
  65. }
  66. /**
  67. * 换绑手机号-发送验证码
  68. * @param Request $request
  69. * @return array
  70. * @throws AlertException
  71. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  72. */
  73. public function sendVerify2New(Request $request)
  74. {
  75. $this->validate($request, ['phone' => 'required']);
  76. $phone = $request->post('phone');
  77. Auth::auth();
  78. $ps = new PhoneService();
  79. if ($ps->sendVerifyCode($phone)) {
  80. return array(
  81. 'code' => 200,
  82. 'message' => 'success'
  83. );
  84. }
  85. throw new AlertException('发送失败', 102);
  86. }
  87. /**
  88. * 换绑手机号-检验验证码
  89. * @param Request $request
  90. * @return array
  91. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  92. */
  93. public function checkVerify2New(Request $request)
  94. {
  95. $this->validate($request, ['phone' => 'required', 'code' => 'required']);
  96. $phone = $request->post('phone');
  97. $code = $request->post('code');
  98. $uid = Auth::auth();
  99. if (Cache::get("smsverifycode:{$phone}") == $code) {
  100. $puser = UserModel::where('phone', $phone)->first();
  101. if (!collect($puser)->isEmpty()) {
  102. $ack = uniqid();
  103. Cache::put("fpdx:checkverify2new:{$ack}", $puser->phone, 1);
  104. return array(
  105. 'code' => 201,
  106. 'message' => '该手机号已绑定了其他账号',
  107. 'data' => array(
  108. 'nickname' => $puser->nickname,
  109. 'ack' => $ack
  110. )
  111. );
  112. }
  113. UserModel::where('uid', $uid)->update(['phone' => $phone]);
  114. return [
  115. 'code' => 200,
  116. 'message' => 'success',
  117. ];
  118. } else {
  119. return [
  120. 'code' => 401,
  121. 'message' => '验证码错误',
  122. ];
  123. }
  124. }
  125. /**
  126. * 绑定手机号
  127. * @param Request $request
  128. * @return array
  129. */
  130. public function bindPhone(Request $request)
  131. {
  132. $this->validate($request, ['phone' => 'required']);
  133. $phone = $request->post('phone');
  134. $result = Curl::to(env("AUTH_URL") . "/api/auth/bind")
  135. ->withHeader("authorization", $request->header("authorization"))
  136. ->withData([
  137. 'authkey' => $phone,
  138. 'authtype' => 'phone'
  139. ])->asJsonResponse(true)->put();
  140. return $result;
  141. }
  142. /**
  143. * 换绑冲突手机号
  144. * @param Request $request
  145. * @return array
  146. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  147. * @throws AlertException
  148. */
  149. public function changePhone(Request $request)
  150. {
  151. throw new AlertException("暂停换绑手机号");
  152. $uid = Auth::auth();
  153. $this->validate($request, ['ack' => 'required']);
  154. $ack = $request->post('ack');
  155. $phone = Cache::get("fpdx:checkverify2new:{$ack}");
  156. if (empty($phone)) {
  157. throw new AlertException("验证码失效,请重新验证", 202);
  158. }
  159. $puser = UserModel::where('phone', $phone)->firstOrFail();
  160. DB::beginTransaction();
  161. try {
  162. $puser->phone = null;
  163. $puser->save();
  164. PartnerModel::where('id', $puser->partner_id)->update(['is_sell' => 0]);
  165. UserModel::where('uid', $uid)->update(['phone' => $phone]);
  166. DB::commit();
  167. return response([
  168. 'code' => 200,
  169. 'message' => 'success'
  170. ]);
  171. } catch (\Exception $exception) {
  172. DB::rollBack();
  173. throw $exception;
  174. }
  175. }
  176. }