有时候pc的网站需要写个微信登陆,不需要引入微信的任何东西,直接原生获取到微信的登陆信息,进行微信登陆(appid和秘钥换成自己的)
class WxLoginController extends CommonController
{
private $APP_ID = '&&&&&&&&&&&&&&&&&&&&&&&&' ; //APP_ID
private $APP_SECRET = '&&&&&&&&&&&&&&&&&&&&&'; //$APP_SECRET
//获取Access Token
private function getAccessToken($code){
$ACCESS_TOKEN = file_get_contents('https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$this->APP_ID.'&secret='.$this->APP_SECRET.'&code='.$code.'&grant_type=authorization_code');
return $ACCESS_TOKEN;
}
//获取用户信息方法
private function getUserInfo($access_token,$openid){
return(file_get_contents('https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid));
}
//获取登录;
public function login(Request $request){
$code = request('code');
//获取access_token等信息
$access_token=$this->getAccessToken($code);
$assess_tokenArr = json_decode($access_token,true);
//获取用户信息
$userinfo = $this->getUserInfo($assess_tokenArr['access_token'],$assess_tokenArr['openid']);
$wxinfo = json_decode($userinfo,true);
$user = Users::where('wx_unionid',$wxinfo['unionid'])->first();
if($user){
if(empty($user->pic)){
$user->pic = $wxinfo['headimgurl'];
}
$wxinfo['nickname'] = $this->removeEmoji($wxinfo['nickname']);
if(!Users::where('nickname',$wxinfo['nickname'])->first()){
$user->nickname = $wxinfo['nickname'];
}
Redis::hdel('wap_token:'.$user->wap_token,'wapUserInfo');
$tokenKey = str_random(64);
$user->wx_unionid = $wxinfo['unionid'];
$user->login_ip = $request->ip();
$user->login_time = time();
$user->wap_token = $tokenKey;
unset($user->password);
if(empty($user['phone'])){
$checkphone = 0;
}else{
$checkphone = 1;
}
if ($user->save()) {
unset($user->wap_token);
if (Redis::hmset('wap_token:' . $tokenKey, ['wapUserInfo' => $user]) == 'OK' && Redis::expire('wap_token:' . $tokenKey, 7200)) {
$vdata = [
'status' => 1,
'msg' => '登录成功',
'wap_token' => $tokenKey,
'checkphone'=>$checkphone
];
}
}
}else{
$data['wx_unionid'] = $wxinfo['unionid'];
$data['reg_time'] = time();
$data['pic'] = $wxinfo['headimgurl'];
$data['wap_token'] = str_random(64);
$data['username'] = 'mooby_'.uniqid();
$wxinfo['nickname'] = $this->removeEmoji($wxinfo['nickname']);
if(!Users::where('nickname',$wxinfo['nickname'])->first()) {
$data['nickname'] = $wxinfo['nickname'];
}else{
$data['nickname'] = substr(md5(microtime(true)), 0, 8);
}
$data['login_ip'] = $request->ip();
$data['login_time'] = time();
if($insert_id = Users::create($data)){
$userinfo = Users::find($insert_id->id);
unset($userinfo->password);
if (Redis::hmset('wap_token:' . $userinfo->wap_token, ['wapUserInfo' => $userinfo]) == 'OK' && Redis::expire('wap_token:' . $userinfo->wap_token, 7200)) {
$vdata = [
'msg' => '登录成功',
'status' => 1,
'wap_token'=>$userinfo->wap_token,
'checkphone'=>0,
];
}else{
$vdata = [
'msg' => '请求失败',
'status' => -1
];
}
}
}
return response()->json($vdata);
}
function removeEmoji($nickname) {
$clean_text = "";
// Match Emoticons
$regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u';
$clean_text = preg_replace($regexEmoticons, '', $clean_text);
// Match Miscellaneous Symbols and Pictographs
$regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u';
$clean_text = preg_replace($regexSymbols, '', $clean_text);
// Match Transport And Map Symbols
$regexTransport = '/[\x{1F680}-\x{1F6FF}]/u';
$clean_text = preg_replace($regexTransport, '', $clean_text);
// Match Miscellaneous Symbols
$regexMisc = '/[\x{2600}-\x{26FF}]/u';
$clean_text = preg_replace($regexMisc, '', $clean_text);
// Match Dingbats
$regexDingbats = '/[\x{2700}-\x{27BF}]/u';
$clean_text = preg_replace($regexDingbats, '', $clean_text);
return $clean_text;
}
//微信绑定手机
public function wxBindPhone(){
if(request('code') == 1) {
$user = Users::where('phone',request('phone'))->first();
$userinfo = json_decode(Redis::hget('wap_token:' . request('wap_token'), 'wapUserInfo'), true);
if(!empty($user)){
if (Users::where('id', $user['id'])->update(['wx_unionid' => $userinfo['wx_unionid']])){
$user = Users::where('id', $user['id'])->first();
Users::where('id',$userinfo['id'])->delete();
if (Redis::hmset('wap_token:' . request('wap_token'), ['wapUserInfo' => $user]) == 'OK') {
$vdata = [
'status' => 1,
'msg' => '绑定手机成功',
];
}
} else {
$vdata = [
'msg' => '您未修改任何信息',
'status' => -1
];
}
}else {
if (Users::where('id', $userinfo['id'])->update(['phone' => request('phone'), 'area_code' => request('area_code')])) {
$user = Users::where('id', $userinfo['id'])->first();
if (Redis::hmset('wap_token:' . request('wap_token'), ['wapUserInfo' => $user]) == 'OK') {
$vdata = [
'status' => 1,
'msg' => '绑定手机成功',
];
}
} else {
$vdata = [
'msg' => '您未修改任何信息',
'status' => -1
];
}
}
}else{
$vdata = [
'status' => -1,
'msg' => '验证码错误',
];
}
return response()->json($vdata);
}
}