Skip to content

Commit 9157604

Browse files
committed
add wechat-login filter
1 parent ce14a8a commit 9157604

8 files changed

+211
-1
lines changed

app/config/app.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
|
7979
*/
8080

81-
'key' => 'wlSG2aL4Vk16s1PrhfpEBfkCZLzdTPc2',
81+
'key' => 'qp5JdmiuFHufqJ3UCYv5nhe9441hfcVW',
8282

8383
'cipher' => MCRYPT_RIJNDAEL_128,
8484

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php namespace Wechat;
2+
3+
use View;
4+
use BaseController;
5+
use Request;
6+
use Response;
7+
use Input;
8+
use Session;
9+
use MemberOpenid;
10+
use Member;
11+
use Redirect;
12+
13+
class PublicController extends BaseController {
14+
15+
protected $member;
16+
protected $memberOpenid;
17+
18+
public function __construct(Member $member, MemberOpenid $memberOpenid)
19+
{
20+
$this->member = $member;
21+
$this->memberOpenid = $memberOpenid;
22+
$this->beforeFilter('wechat.userinfo', ['only' => ['login']]);
23+
}
24+
25+
public function login()
26+
{
27+
$openid = Session::get('openid');
28+
$memberOpenid = $this->memberOpenid->whereOpenid($openid)->first();
29+
if (!$memberOpenid) {
30+
$wechatUserinfo = Session::get('wechat_userinfo');
31+
$this->member->nickname = $wechatUserinfo['nickname'];
32+
$this->member->head_img_url = $wechatUserinfo['headimgurl'];
33+
$this->member->save();
34+
$this->memberOpenid->member_id = $this->member->id;
35+
$this->memberOpenid->openid = $openid;
36+
$this->memberOpenid->save();
37+
$memberOpenid = $this->memberOpenid;
38+
}
39+
Session::put('member_id', $memberOpenid->member_id);
40+
$redirectUrl = Session::pull('wechat-login-before-url', '/');
41+
return Redirect::to($redirectUrl);
42+
}
43+
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
6+
class CreateMembersTable extends Migration {
7+
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::create('members', function(Blueprint $table)
16+
{
17+
$table->increments('id');
18+
$table->integer('wechat_account_id')->unsigned()->nullable();
19+
$table->string('nickname');
20+
$table->string('head_img_url');
21+
$table->timestamps();
22+
$table->foreign('wechat_account_id')->references('id')->on('wechat_accounts');
23+
});
24+
}
25+
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::drop('members');
35+
}
36+
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
6+
class CreateMemberOpenidsTable extends Migration {
7+
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::create('member_openids', function(Blueprint $table)
16+
{
17+
$table->increments('id');
18+
$table->integer('wechat_account_id')->unsigned()->nullable();
19+
$table->string('member_id');
20+
$table->string('openid')->unqiue();
21+
$table->timestamps();
22+
$table->foreign('wechat_account_id')->references('id')->on('wechat_accounts');
23+
});
24+
}
25+
26+
27+
/**
28+
* Reverse the migrations.
29+
*
30+
* @return void
31+
*/
32+
public function down()
33+
{
34+
Schema::drop('member_openids');
35+
}
36+
37+
}

app/filters.php

+9
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,12 @@
8888
throw new Illuminate\Session\TokenMismatchException;
8989
}
9090
});
91+
92+
Route::filter('wechat-login', function()
93+
{
94+
if (!Session::has('openid')) {
95+
Session::put('wechat-login-before-url', Request::fullUrl());
96+
return Redirect::route('wechat.login', ['openid' => Input::get('openid')]);
97+
}
98+
});
99+

app/models/Member.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
use Xjchen\BasicAdmin\Externals\Ardent;
4+
use Xjchen\BasicAdmin\Traits\LimitedWechatAccountTrait;
5+
use Xjchen\LimitedTrait\LimitedTrait;
6+
7+
class Member extends Ardent {
8+
9+
use LimitedTrait, LimitedWechatAccountTrait;
10+
protected $limitedColumns = ['wechat_account_id'];
11+
12+
protected $table = 'members';
13+
14+
protected $guarded = array('id');
15+
16+
public $forceEntityHydrationFromInput = true;
17+
18+
protected $perPage = 6;
19+
20+
public static $rules = array(
21+
);
22+
23+
public static $customAttributes = [
24+
'nickname' => '昵称',
25+
'head_img_url' => '头像链接'
26+
];
27+
28+
protected $appends = [];
29+
30+
public function openids()
31+
{
32+
return $this->hasMany('MemberOpenid', 'member_id');
33+
}
34+
35+
public function getNicknameAttribute($value)
36+
{
37+
return json_decode($value);
38+
}
39+
40+
public function setNicknameAttribute($value)
41+
{
42+
$this->attributes['nickname'] = json_encode($value);
43+
}
44+
45+
}

app/models/MemberOpenid.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Xjchen\BasicAdmin\Externals\Ardent;
4+
use Xjchen\BasicAdmin\Traits\LimitedWechatAccountTrait;
5+
use Xjchen\LimitedTrait\LimitedTrait;
6+
7+
class MemberOpenid extends Ardent {
8+
9+
use LimitedTrait, LimitedWechatAccountTrait;
10+
protected $limitedColumns = ['wechat_account_id'];
11+
12+
protected $table = 'member_openids';
13+
14+
protected $guarded = array('id');
15+
16+
public $forceEntityHydrationFromInput = true;
17+
18+
protected $perPage = 6;
19+
20+
public static $rules = array(
21+
);
22+
23+
public static $customAttributes = [
24+
];
25+
26+
protected $appends = [];
27+
28+
public function member()
29+
{
30+
return $this->belongsTo('Member', 'member_id');
31+
}
32+
33+
}

app/routes.php

+5
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@
1515
{
1616
return View::make('hello');
1717
});
18+
19+
Route::get('wechat-login', [
20+
'as' => 'wechat.login',
21+
'uses' => 'Wechat\PublicController@login'
22+
]);

0 commit comments

Comments
 (0)