AuthKeyService.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Services\Auth;
  3. use App\Events\LogoutUser;
  4. use App\Exceptions\AlertException;
  5. use App\Models\Appearance\AppearanceModel;
  6. use App\Models\PartnerModel;
  7. use App\Models\User\AuthKey;
  8. use App\Models\User\UserModel;
  9. use App\Services\Service;
  10. class AuthKeyService extends Service
  11. {
  12. public function bindKey(int $uid, string $key, string $type)
  13. {
  14. $m = new AuthKey();
  15. if ($m->where([['uid', $uid], ['auth_type', $type]])->exists()) {
  16. throw new AlertException("你已经绑定了该认证方式", 201);
  17. }
  18. if ($m->where('auth_key', $key)->exists()) {
  19. throw new AlertException("已经被绑定,请先解绑", 400);
  20. }
  21. $m->fill([
  22. 'uid' => $uid,
  23. 'auth_key' => $key,
  24. 'auth_type' => $type,
  25. ]);
  26. if ($m->save()) {
  27. return true;
  28. } else {
  29. throw new AlertException("网络繁忙|E_DB", 500);
  30. }
  31. }
  32. /**
  33. * 注销用户
  34. * @param int $uid
  35. * @return bool
  36. * @throws \Exception
  37. */
  38. public function logout($uid)
  39. {
  40. \DB::beginTransaction();
  41. try {
  42. AuthKey::where('uid', '=', $uid)->delete();
  43. // 删除手机号+注销时间
  44. UserModel::where('uid', $uid)->update(['phone' => null, 'logoff_at' => time()]);
  45. // 下架卡片
  46. PartnerModel::where('uid', $uid)->update([
  47. 'check_photo' => -1,
  48. 'photo_1_check' => -1,
  49. 'photo_2_check' => -1,
  50. 'photo_3_check' => -1,
  51. 'photo_4_check' => -1,
  52. 'voice_check' => -1,
  53. 'intro_check' => -1,
  54. 'is_sell' => 0,
  55. 'is_commit_check' => 0,
  56. 'is_recommend' => 0,
  57. 'is_voice_recommend' => 0,
  58. ]);
  59. // 解绑openid
  60. \DB::table('kddx_user_openid')->where('uid', $uid)->update(['uid' => 0]);
  61. AppearanceModel::where('uid', $uid)->delete();
  62. \DB::commit();
  63. // 用户注销事件
  64. event(new LogoutUser($uid));
  65. return true;
  66. } catch (\Exception $exception) {
  67. \DB::rollBack();
  68. throw $exception;
  69. }
  70. }
  71. }