add wechat oauth
This commit is contained in:
@@ -19,6 +19,7 @@ use App\PasswordReset;
|
||||
use Mail;
|
||||
use App\AuthLog;
|
||||
use phpCAS;
|
||||
use App\Wechat;
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
||||
@@ -27,6 +28,68 @@ class AuthController extends Controller
|
||||
return View::make('auth.login');
|
||||
}
|
||||
|
||||
public function wx(Request $request)
|
||||
{
|
||||
if(isset($request->data) && $request->sign == substr(md5($request->data . env('WECHAT_KEY')), 8, 16))
|
||||
{
|
||||
$data['wechat'] = json_decode(base64_decode($request->data));
|
||||
$user = User::where('wechat_open_id', $data['wechat']->openId)->first();
|
||||
$request->session()->put('wechat_open_id', $data['wechat']->openId);
|
||||
if(!$user) {
|
||||
$wechat = Wechat::firstOrNew(['open_id' => $data['wechat']->openId]);
|
||||
$wechat->head_img_url = $data['wechat']->headImgUrl;
|
||||
$wechat->nick_name = $data['wechat']->nickName;
|
||||
$wechat->sex = $data['wechat']->sex;
|
||||
$wechat->save();
|
||||
|
||||
return view('auth.wechatGuide')->with($data);
|
||||
}
|
||||
else {
|
||||
if ($user->baned) {
|
||||
$nowdate = time();
|
||||
$banedstart = $user->banedstart;
|
||||
$date = $nowdate-$banedstart;
|
||||
if($date>($user->banedtime)*86400 && $user->banedtime!=-1)
|
||||
{
|
||||
$user->baned = 0;
|
||||
return Redirect::to('/');
|
||||
}
|
||||
// log start
|
||||
$log = new AuthLog();
|
||||
$log->user_id = $user->id;
|
||||
$log->ip = $request->ip();
|
||||
$log->event = "wechat_oauth_login_banned";
|
||||
$log->save();
|
||||
// log end
|
||||
|
||||
if($user->banedtime == -1)
|
||||
return Redirect::to('/login')->withInput()->withErrors('你已经被永久封禁!');
|
||||
return Redirect::to('/login')->withInput()->withErrors('你已经被封禁!'.ceil($user->banedtime-($date/86400)).'天后解禁!');
|
||||
}
|
||||
|
||||
// log start
|
||||
$log = new AuthLog();
|
||||
$log->user_id = $user->id;
|
||||
$log->ip = $request->ip();
|
||||
$log->event = "wechat_oauth_login_success";
|
||||
$log->save();
|
||||
// log end
|
||||
|
||||
$request->session()->put('user_id', $user->id);
|
||||
$request->session()->put('username', $user->username);
|
||||
$request->session()->put('nickname', $user->nickname);
|
||||
if ($user->privilege)
|
||||
$request->session()->put('is_admin', $user->privilege);
|
||||
|
||||
if ($user->registerCompletion() != 0)
|
||||
return Redirect::to('/register/' . $user->registerCompletion());
|
||||
else
|
||||
return Redirect::to('/');
|
||||
}
|
||||
}
|
||||
return Redirect::to("http://api.xms.rmbz.net/open/oauth?path=" . env("APP_URL") . "/wx");
|
||||
}
|
||||
|
||||
public function cas(Request $request)
|
||||
{
|
||||
phpCAS::client(CAS_VERSION_2_0, "sso.neu.cn", 443, "/cas");
|
||||
@@ -53,6 +116,18 @@ class AuthController extends Controller
|
||||
$log->save();
|
||||
// log end
|
||||
|
||||
if($request->session()->has('wechat_open_id')) {
|
||||
$user->wechat_open_id = $request->session()->get('wechat_open_id');
|
||||
$user->save();
|
||||
// log start
|
||||
$log = new AuthLog();
|
||||
$log->user_id = $user->id;
|
||||
$log->ip = $request->ip();
|
||||
$log->event = "wechat_oauth_sso_bind_success";
|
||||
$log->save();
|
||||
// log end
|
||||
}
|
||||
|
||||
$request->session()->put('user_id', $user->id);
|
||||
|
||||
return Redirect::to('/register/' . $user->registerCompletion());
|
||||
@@ -75,10 +150,10 @@ class AuthController extends Controller
|
||||
$log->event = "cas_login_banned";
|
||||
$log->save();
|
||||
// log end
|
||||
|
||||
|
||||
if($user->banedtime == -1)
|
||||
return Redirect::back()->withInput()->withErrors('你已经被永久封禁!');
|
||||
return Redirect::back()->withInput()->withErrors('你已经被封禁!'.ceil($user->banedtime-($date/86400)).'天后解禁!');
|
||||
return Redirect::to('/login')->withInput()->withErrors('你已经被永久封禁!');
|
||||
return Redirect::to('/login')->withInput()->withErrors('你已经被封禁!'.ceil($user->banedtime-($date/86400)).'天后解禁!');
|
||||
}
|
||||
|
||||
// log start
|
||||
@@ -95,6 +170,18 @@ class AuthController extends Controller
|
||||
if ($user->privilege)
|
||||
$request->session()->put('is_admin', $user->privilege);
|
||||
|
||||
if($request->session()->has('wechat_open_id')) {
|
||||
$user->wechat_open_id = $request->session()->get('wechat_open_id');
|
||||
$user->save();
|
||||
// log start
|
||||
$log = new AuthLog();
|
||||
$log->user_id = $user->id;
|
||||
$log->ip = $request->ip();
|
||||
$log->event = "wechat_oauth_sso_bind_success";
|
||||
$log->save();
|
||||
// log end
|
||||
}
|
||||
|
||||
if ($user->registerCompletion() != 0)
|
||||
return Redirect::to('/register/' . $user->registerCompletion());
|
||||
else
|
||||
@@ -144,7 +231,7 @@ class AuthController extends Controller
|
||||
$log->event = "login_banned";
|
||||
$log->save();
|
||||
// log end
|
||||
|
||||
|
||||
if($user->banedtime == -1)
|
||||
return Redirect::back()->withInput()->withErrors('你已经被永久封禁!');
|
||||
return Redirect::back()->withInput()->withErrors('你已经被封禁!'.ceil($user->banedtime-($date/86400)).'天后解禁!');
|
||||
@@ -166,6 +253,18 @@ class AuthController extends Controller
|
||||
if ($user->privilege)
|
||||
$request->session()->put('is_admin', $user->privilege);
|
||||
|
||||
if($request->session()->has('wechat_open_id')) {
|
||||
$user->wechat_open_id = $request->session()->get('wechat_open_id');
|
||||
$user->save();
|
||||
// log start
|
||||
$log = new AuthLog();
|
||||
$log->user_id = $user->id;
|
||||
$log->ip = $request->ip();
|
||||
$log->event = "wechat_oauth_bind_success";
|
||||
$log->save();
|
||||
// log end
|
||||
}
|
||||
|
||||
if ($user->registerCompletion() != 0)
|
||||
return Redirect::to('/register/' . $user->registerCompletion());
|
||||
else
|
||||
@@ -229,6 +328,7 @@ class AuthController extends Controller
|
||||
$request->session()->forget('username');
|
||||
$request->session()->forget('nickname');
|
||||
$request->session()->forget('is_admin');
|
||||
$request->session()->forget('wechat_open_id');
|
||||
|
||||
return Redirect::to('/');
|
||||
}
|
||||
|
||||
@@ -172,7 +172,7 @@ class UserController extends Controller
|
||||
{
|
||||
$data = [];
|
||||
$user_id = $request->session()->get('user_id');
|
||||
$data['user'] = User::find($user_id);
|
||||
$data['user'] = User::with('wechat')->find($user_id);
|
||||
$data['userinfos'] = UserInfo::where('user_id', $user_id)->get();
|
||||
$data['tab'] = isset($request->tab) ? $request->tab : "profile";
|
||||
return View::make('user.user')->with($data);
|
||||
|
||||
@@ -30,6 +30,7 @@ Route::group(['middleware' => ['web']],function () {
|
||||
Route::group(['middleware' => ['authredirect']], function() {
|
||||
Route::get('/login', "AuthController@showLogin");
|
||||
Route::get('/sso', "AuthController@cas");
|
||||
Route::get('/wx', "AuthController@wx");
|
||||
Route::post('/login', "AuthController@login");
|
||||
Route::get('/register', "AuthController@showRegister");
|
||||
Route::post('/register', "AuthController@register");
|
||||
|
||||
@@ -34,4 +34,9 @@ class User extends Model
|
||||
return $this->nickname != "" ? $this->nickname : "一位先锋市场用户";
|
||||
}
|
||||
|
||||
public function wechat()
|
||||
{
|
||||
return $this->belongsTo('App\Wechat', "wechat_open_id", "open_id");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Wechat extends Model
|
||||
{
|
||||
protected $fillable = ['open_id'];
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class CreateWechatsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('wechats', function (Blueprint $table) {
|
||||
$table->string('open_id');
|
||||
$table->primary('open_id');
|
||||
$table->string('head_img_url');
|
||||
$table->string('nick_name');
|
||||
$table->string('sex');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('wechats');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
class AlterUserAddWechatId extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
if (Schema::hasTable('users') && !Schema::hasColumn('users', 'wechat_open_id'))
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->string('wechat_open_id')->nullable()->unique();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
if (Schema::hasTable('users') && Schema::hasColumn('users', 'wechat_open_id'))
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->dropColumn('wechat_open_id');
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
.head-img {
|
||||
border-style: solid;
|
||||
border-width: 0.3em;
|
||||
border-color: #cccccc;
|
||||
border-radius: 15%;
|
||||
margin-bottom: 1em;
|
||||
}
|
||||
|
||||
.nickname {
|
||||
background: #f9fafa;
|
||||
padding: 3px 6px;
|
||||
border-radius: 3px;
|
||||
color: #f4645f;
|
||||
margin: 5px;
|
||||
border: 1px solid #ded9d9;
|
||||
overflow-x: auto;
|
||||
font-size: 0.9em;
|
||||
}
|
||||
|
||||
.content-wrapper {
|
||||
margin-top: 3em;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html class="no-js" lang="zh">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="x-ua-compatible" content="ie=edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
||||
<link rel="stylesheet" href="/css/foundation.css"/>
|
||||
<link rel="stylesheet" href="/css/app.css"/>
|
||||
<link rel="stylesheet" href="/css/cropper.min.css"/>
|
||||
<link rel="stylesheet" href="/css/froala_editor.min.css">
|
||||
<link rel="stylesheet" href="/css/froala_editor.pkgd.min.css">
|
||||
<link rel="stylesheet" href="/css/font-awesome.min.css">
|
||||
<link type="text/css" media="screen" rel="stylesheet" href="/css/responsive-tables.css" />
|
||||
@yield('asset')
|
||||
<script src="/js/jquery.min.js"></script>
|
||||
<script src="/js/vendor/jquery.min.js"></script>
|
||||
<script src="/js/vendor/what-input.min.js"></script>
|
||||
<script src="/js/vendor/foundation.min.js"></script>
|
||||
<script src="/js/cropper.min.js"></script>
|
||||
<!--Pop foundation and cropper's old version jQuery-->
|
||||
<script type="text/javascript">
|
||||
var $jQuery_FOUNDATION = $.noConflict(true);
|
||||
</script>
|
||||
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
|
||||
<script src="/js/jquery.slides.min.js"></script>
|
||||
<script src="/js/app.js"></script>
|
||||
<script src="/js/froala_editor.pkgd.min.js"></script>
|
||||
<title>微信登录 - 先锋市场</title>
|
||||
</head>
|
||||
<body>
|
||||
<div class="content-wrapper row align-middle">
|
||||
<div class="small-10 small-push-1 columns card">
|
||||
<div class="card-section">
|
||||
<form action="/login" method="POST" data-abide novalidate>
|
||||
<center>
|
||||
<img class="head-img" src="{{ $wechat->headImgUrl }}" width="64" height="64"/>
|
||||
</center>
|
||||
<p>您正在使用微信账号 <span class="nickname">{{ $wechat->nickName }}</span> 登录先锋市场,要绑定此账号,点击下面的按钮继续。</p>
|
||||
<div class="row">
|
||||
<a type="submit" class="expanded hollow button" href="/login">我已有账号</a>
|
||||
</div>
|
||||
<div class="row">
|
||||
<a href="/sso" style="float: right" class="expanded button small-centered" href="/sso">校园卡认证注册</a>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<link rel="stylesheet" href="/css/wechat.css"/>
|
||||
@include('layout.footer')
|
||||
</body>
|
||||
</html>
|
||||
@@ -30,6 +30,14 @@
|
||||
<input type="submit" class="hollow button" value="解绑">
|
||||
@endif
|
||||
</form>
|
||||
<form>
|
||||
@if($user->wechat_open_id)
|
||||
<label>微信:已关联 {{$user->wechat->nick_name}}</label>
|
||||
<img class="head-img" src="{{$user->wechat->head_img_url}}" width="64px" height="64px">
|
||||
@else
|
||||
<label>微信:请在东大小秘书中点击“闲置市场”链接来关联微信。</label>
|
||||
@endif
|
||||
</form>
|
||||
<form action="/user/edit/password" method="POST">
|
||||
{{ csrf_field() }}
|
||||
@if($user->password!='')
|
||||
@@ -39,4 +47,6 @@
|
||||
<label>确认新密码<input type="password" name="newPassword_confirmation" placeholder="必填" required></label>
|
||||
<input type="submit" class="hollow button" name="password_submit" value="保存">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<link rel="stylesheet" href="/css/wechat.css"/>
|
||||
Reference in New Issue
Block a user