|
|
|
|
@ -9,6 +9,8 @@ use App\Models\User;
|
|
|
|
|
use App\Models\VipCustomer;
|
|
|
|
|
use App\Models\Visit;
|
|
|
|
|
use EasyWeChat\Factory;
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
|
|
|
|
|
|
class UserController extends CommonController
|
|
|
|
|
@ -41,10 +43,32 @@ class UserController extends CommonController
|
|
|
|
|
// 获取配置信息
|
|
|
|
|
$appid = \config('app.wechat_appid');
|
|
|
|
|
$appSecret = \config('app.wechat_appsecret');
|
|
|
|
|
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $appSecret . "&js_code={$all['code']}&grant_type=authorization_code";
|
|
|
|
|
$userInfo = json_decode(file_get_contents($url), true);
|
|
|
|
|
$url = "https://api.weixin.qq.com/sns/jscode2session";
|
|
|
|
|
$userInfo = null;
|
|
|
|
|
try {
|
|
|
|
|
$response = Http::timeout(10)->get($url, [
|
|
|
|
|
'appid' => $appid,
|
|
|
|
|
'secret' => $appSecret,
|
|
|
|
|
'js_code' => $all['code'],
|
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
|
]);
|
|
|
|
|
$userInfo = $response->json();
|
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
|
Log::error('miniapp login jscode2session request failed', [
|
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
|
'appid' => $appid,
|
|
|
|
|
]);
|
|
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, '微信登录服务不可用']);
|
|
|
|
|
}
|
|
|
|
|
if (!isset($userInfo['openid'])) {
|
|
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, 'code异常']);
|
|
|
|
|
Log::warning('miniapp login jscode2session invalid response', [
|
|
|
|
|
'appid' => $appid,
|
|
|
|
|
'wx_errcode' => $userInfo['errcode'] ?? null,
|
|
|
|
|
'wx_errmsg' => $userInfo['errmsg'] ?? null,
|
|
|
|
|
'response' => $userInfo,
|
|
|
|
|
]);
|
|
|
|
|
$msg = $userInfo['errmsg'] ?? 'code异常';
|
|
|
|
|
return $this->fail([ResponseCode::ERROR_BUSINESS, "code异常:{$msg}"]);
|
|
|
|
|
}
|
|
|
|
|
$openid = $userInfo['openid'];
|
|
|
|
|
//判断该用户是否在数据库
|
|
|
|
|
|