You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.5 KiB
PHP

<?php
namespace App\Http\Controllers\API\Mp;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use JWT;
class LoginController
{
//微信登录授权获取openid
public function wxGetOpenid()
{
$code = request('code');
$url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . config('app.globals.WxAppid') . "&secret=" . config('app.globals.WxAppSecret') . "&js_code=" . $code . "&grant_type=authorization_code";
$response = Http::get($url);
if ($response->successful()) {
$data = $response->json(); // 获取响应的 JSON 数据
// var_dump($data);
$h5url=null;
if (isset($data['openid'])) {
$openid = $data['openid'];
$user = DB::table('weixin_user')->where(['openid'=>$openid])->first();
if(!$user){
$insert=DB::table('weixin_user')->insert([
'openid'=>$openid,
'status'=>0,
]);
}else{
if($user->status==1){
$h5url="https://www.baidu.com";
}
}
return \Yz::Return(true, '获取openid成功', ['openid' => $openid, 'member' => $user,'url'=>$h5url]);
} else {
return \Yz::echoError1('获取openid失败');
}
} else {
return \Yz::echoError1('获取openid失败');
}
}
}