微信公众号授权登录一(laravel)

 
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use Session;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Models\AdminuserModel;
use App\Http\Models\ConfigModel;
use Overtrue\Wechat\Auth;
use DB;
/**
 * 进行用户授权登录
 * @param Request $req
 * @return 跳转到过来的网页
 */
public function login(Request $req)
{
    if ($req->session()->get('user')) {
        return redirect('/home/index');
    }
    $auth = new Auth($this->wx_id,$this->wx_secect);
    $user = $auth->authorize();
    $req->session()->put('user', $user->all());
    $this->add_userinfo($req,$req->session()->get('user'));
    return back();
}
 
 /**
     * 增加用户信息
     * @param $info 用户的信息
     *
     */
    public function add_userinfo($req,$info)
    {
        $data = array(
            'openid' => $info['openid'],
            'nickname' => $info['nickname'],
            'create_time' => time(),
//            'province' => $info['province'],
//            'city' => $info['city'],
//            'sex' => $info['sex'],
            'headimgurl' => $info['headimgurl'],
            'status' => 0,//默认未关注公众号
        );
         $user_info = AdminuserModel::where('openid',$info['openid'])->first();
         if(empty($user_info->openid)){
             AdminuserModel::create($data);
             $user_info = AdminuserModel::where('openid',$info['openid'])->first();
             $user_id = $user_info->id;
             $req->session()->put('user_id',$user_id);
         }
    }

 

 

 

/**
 * 用户退出登录
 *
 */
public function logout(Request $req){
    $req->session()->forget('user');//清除session
    $req->session()->forget('user_id');
    echo 'logout ok';
}

 

你可能感兴趣的:(跳坑,微信公众号授权登录)