使用 POSTMAN 用 code 登录报错
环境和代码都是根据之前的课程写的,Homestead,laravel6.x,小程序登录的代码如下:
<?php
namespace App\Http\Controllers\Api;
use Auth;
use App\Models\User;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use App\Transformers\DataTransformer;
use Illuminate\Auth\AuthenticationException;
use App\Http\Requests\Api\AuthorizationRequest;
use App\Http\Requests\Api\SocialAuthorizationRequest;
use App\Http\Requests\Api\WeappAuthorizationRequest;
class AuthorizationsController extends Controller
{
public function weappStore(WeappAuthorizationRequest $request)
{
$code = $request->code;
$miniProgram = \EasyWeChat::miniProgram();
$data = $miniProgram->auth->session($code);
if (isset($data['errcode'])) {
return $this->response->errorUnauthorized('code 不正确');
}
$user = User::where('weapp_openid', $data['openid'])->first();
$attributes['weixin_session_key'] = $data['session_key'];
if (!$user) {
// 找不到 openid 对应的用户要求用户提交
if (!$request->username) {
return $this->response->errorForbidden('用户不存在');
}
$username = $request->username;
filter_var($username, FILTER_VALIDATE_EMAIL) ?
$credentials['email'] = $username :
$credentials['phone'] = $username;
$credentials['password'] = $request->password;
if (!Auth::guard('api')->once($credentials)) {
return $this->response->errorUnauthorized('用户名或密码错误');
}
$user = Auth::guard('api')->getUser();
$attributes['weapp_openid'] = $data['openid'];
}
$user->update($attributes);
$token = Auth::guard('api')->fromUser($user);
return $this->respondWithToken($token)->setStatusCode(201);
}
}
接受值都正常,问题应该出现在
if (isset($data['errcode'])) {
return $this->response->errorUnauthorized('code 不正确');
}
请问这个response为什么不能用呢?
推荐文章: