123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <?php
- namespace App\Http\Controllers\Api\V1;
- use App\Http\Controllers\Controller;
- use App\Http\Requests\Api\AuthorizationRequest;
- use App\Exceptions\ApiException;
- use App\Models\Experience;
- use App\Models\User;
- use App\Models\UserTicket;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Auth;
- /**
- * 登录相关
- * Created on 2020/8/14 11:22
- * Create by Wpeishi
- */
- class AuthorizationsController extends Controller
- {
- /**
- * 微信授权登录
- * @param Request $request
- * @return array
- */
- public function wxappLogin(Request $request)
- {
- $app = \EasyWeChat::miniProgram();
- $data = $app->auth->session($request->code);
- // $data = [
- // 'openid'=>'oRMan5MEa4mX1cMaqH17yrlwWBwE',
- // 'session_key'=>'P6FJ3Tq5mJeSVxl9gI4nkA==',
- // ];
- //判断code是否过期
- if (isset($data['errcode'])) {
- return apiJsonError('code已过期或不正确');
- }
- $weappOpenid = $data['openid'];
- $weixinSessionKey = $data['session_key'];
- $user = User::query()->where(['open_id' => $weappOpenid])->first();
- if ($user) {
- // 存在更新
- $user->open_id = $weappOpenid;
- $user->session_key = $weixinSessionKey;
- $user->save();
- } else {
- // 新增用户
- $user = User::create([
- 'open_id' => $weappOpenid,
- 'session_key' => $weixinSessionKey,
- ]);
- // 新增用户体验卷 2 张
- UserTicket::insert([[
- 'user_id' => $user->id,
- 'ticket_id' => 1,
- 'type' => 0,
- 'grant_time' => date('Y-m-d H:i:s'),
- 'status' => 0,
- ], [
- 'user_id' => $user->id,
- 'ticket_id' => 1,
- 'type' => 0,
- 'grant_time' => date('Y-m-d H:i:s'),
- 'status' => 0,
- ]]);
- }
- $user = User::UpdateOrCreate(['open_id' => $weappOpenid], [
- 'open_id' => $weappOpenid,
- 'session_key' => $weixinSessionKey,
- ]);
- $ttl = $request->out_time ?? config('jwt.ttl'); # 设置token 过期时间
- if (!$token = Auth::guard('api')->setTTL($ttl)->tokenById($user->id)) {
- throw new ApiException('token-获取出错');
- }
- $data['token'] = $this->respondWithToken($token);
- return apiJson($data);
- }
- /**
- * 登录
- * @param AuthorizationRequest $request
- * @return array
- */
- public function store()
- {
- // $credentials['phone'] = $request->phone;
- // $credentials['password'] = $request->password;
- $user = User::query()->where(['id' => 31])->first();
- // return $user;
- $ttl = $request->out_time ?? config('jwt.ttl'); # 设置token 过期时间
- if (!$token = Auth::guard('api')->setTTL($ttl)->tokenById($user->id)) {
- throw new ApiException('用户名或密码错误');
- }
- return apiJson($this->respondWithToken($token));
- }
- /**
- * 刷新token
- * @return array
- */
- public function update()
- {
- $token = Auth::guard('api')->refresh();
- return apiJson($this->respondWithToken($token));
- }
- /**
- * 注销
- * @return array
- */
- public function destroy()
- {
- Auth::guard('api')->logout();
- return apiJsonMsg('注销成功');
- }
- /**
- * 返回 token 格式
- * @param $token
- * @return mixed
- */
- protected function respondWithToken($token)
- {
- return [
- 'access_token' => $token,
- 'token_type' => 'Bearer',
- 'expires_in' => Auth::guard('api')->factory()->getTTL() * 60 // token 过期时间,单位秒
- ];
- }
- }
|