AuthorizationsController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Api\AuthorizationRequest;
  5. use App\Exceptions\ApiException;
  6. use App\Models\Experience;
  7. use App\Models\User;
  8. use App\Models\UserTicket;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Auth;
  11. /**
  12. * 登录相关
  13. * Created on 2020/8/14 11:22
  14. * Create by Wpeishi
  15. */
  16. class AuthorizationsController extends Controller
  17. {
  18. /**
  19. * 微信授权登录
  20. * @param Request $request
  21. * @return array
  22. */
  23. public function wxappLogin(Request $request)
  24. {
  25. $app = \EasyWeChat::miniProgram();
  26. $data = $app->auth->session($request->code);
  27. // $data = [
  28. // 'openid'=>'oRMan5MEa4mX1cMaqH17yrlwWBwE',
  29. // 'session_key'=>'P6FJ3Tq5mJeSVxl9gI4nkA==',
  30. // ];
  31. //判断code是否过期
  32. if (isset($data['errcode'])) {
  33. return apiJsonError('code已过期或不正确');
  34. }
  35. $weappOpenid = $data['openid'];
  36. $weixinSessionKey = $data['session_key'];
  37. $user = User::query()->where(['open_id' => $weappOpenid])->first();
  38. if ($user) {
  39. // 存在更新
  40. $user->open_id = $weappOpenid;
  41. $user->session_key = $weixinSessionKey;
  42. $user->save();
  43. } else {
  44. // 新增用户
  45. $user = User::create([
  46. 'open_id' => $weappOpenid,
  47. 'session_key' => $weixinSessionKey,
  48. ]);
  49. // 新增用户体验卷 2 张
  50. UserTicket::insert([[
  51. 'user_id' => $user->id,
  52. 'ticket_id' => 1,
  53. 'type' => 0,
  54. 'grant_time' => date('Y-m-d H:i:s'),
  55. 'status' => 0,
  56. ], [
  57. 'user_id' => $user->id,
  58. 'ticket_id' => 1,
  59. 'type' => 0,
  60. 'grant_time' => date('Y-m-d H:i:s'),
  61. 'status' => 0,
  62. ]]);
  63. }
  64. $user = User::UpdateOrCreate(['open_id' => $weappOpenid], [
  65. 'open_id' => $weappOpenid,
  66. 'session_key' => $weixinSessionKey,
  67. ]);
  68. $ttl = $request->out_time ?? config('jwt.ttl'); # 设置token 过期时间
  69. if (!$token = Auth::guard('api')->setTTL($ttl)->tokenById($user->id)) {
  70. throw new ApiException('token-获取出错');
  71. }
  72. $data['token'] = $this->respondWithToken($token);
  73. return apiJson($data);
  74. }
  75. /**
  76. * 登录
  77. * @param AuthorizationRequest $request
  78. * @return array
  79. */
  80. public function store()
  81. {
  82. // $credentials['phone'] = $request->phone;
  83. // $credentials['password'] = $request->password;
  84. $user = User::query()->where(['id' => 31])->first();
  85. // return $user;
  86. $ttl = $request->out_time ?? config('jwt.ttl'); # 设置token 过期时间
  87. if (!$token = Auth::guard('api')->setTTL($ttl)->tokenById($user->id)) {
  88. throw new ApiException('用户名或密码错误');
  89. }
  90. return apiJson($this->respondWithToken($token));
  91. }
  92. /**
  93. * 刷新token
  94. * @return array
  95. */
  96. public function update()
  97. {
  98. $token = Auth::guard('api')->refresh();
  99. return apiJson($this->respondWithToken($token));
  100. }
  101. /**
  102. * 注销
  103. * @return array
  104. */
  105. public function destroy()
  106. {
  107. Auth::guard('api')->logout();
  108. return apiJsonMsg('注销成功');
  109. }
  110. /**
  111. * 返回 token 格式
  112. * @param $token
  113. * @return mixed
  114. */
  115. protected function respondWithToken($token)
  116. {
  117. return [
  118. 'access_token' => $token,
  119. 'token_type' => 'Bearer',
  120. 'expires_in' => Auth::guard('api')->factory()->getTTL() * 60 // token 过期时间,单位秒
  121. ];
  122. }
  123. }