TOKEN
This commit is contained in:
@@ -2,9 +2,11 @@
|
|||||||
|
|
||||||
namespace app\controller;
|
namespace app\controller;
|
||||||
|
|
||||||
|
use app\model\AppUser;
|
||||||
use EasyWeChat\Factory;
|
use EasyWeChat\Factory;
|
||||||
use EasyWeChat\OfficialAccount\Application;
|
use EasyWeChat\OfficialAccount\Application;
|
||||||
use think\facade\Config;
|
use think\facade\Config;
|
||||||
|
use think\facade\Db;
|
||||||
use think\facade\Request;
|
use think\facade\Request;
|
||||||
use think\facade\Route;
|
use think\facade\Route;
|
||||||
use think\facade\View;
|
use think\facade\View;
|
||||||
@@ -39,7 +41,7 @@ class Wechat
|
|||||||
->domain(true);
|
->domain(true);
|
||||||
$this->initWechat();
|
$this->initWechat();
|
||||||
|
|
||||||
return show(SUCCESS_MESSAGE,SUCCESS_CODE,$this->app->oauth->scopes(['snsapi_userinfo'])->redirect($redirect));
|
return show(SUCCESS_MESSAGE, SUCCESS_CODE, $this->app->oauth->scopes(['snsapi_userinfo'])->redirect($redirect));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -54,16 +56,22 @@ class Wechat
|
|||||||
$user = $this->app->oauth->user();
|
$user = $this->app->oauth->user();
|
||||||
$callback = Request::get('callback');
|
$callback = Request::get('callback');
|
||||||
|
|
||||||
$token = '怎样获得一个 token';
|
$user = AppUser::where('openid', $user->getId())->find();
|
||||||
return redirect($callback.'?token='.$token);
|
|
||||||
|
|
||||||
// $user 可以用的方法:
|
if ($user) {
|
||||||
// $user->getId(); // 对应微信的 OPENID
|
|
||||||
// $user->getNickname(); // 对应微信的 nickname
|
} else {
|
||||||
// $user->getName(); // 对应微信的 nickname
|
$user = AppUser::create([
|
||||||
// $user->getAvatar(); // 头像网址
|
'nickname' => $user->getNickname(),
|
||||||
// $user->getOriginal(); // 原始API返回的结果
|
'avatar' => $user->getAvatar(),
|
||||||
// $user->getToken(); // access_token, 比如用于地址共享时使用
|
'identity' => 1,
|
||||||
|
'openid' => $user->getId(),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
$tokenData = ['userid' => $user->id, 'loginTime' => time(), 'rankStr' => strRand(5)];
|
||||||
|
$token = authcode(json_encode($tokenData), 'ENCODE');
|
||||||
|
|
||||||
|
return redirect($callback.'?token='.$token);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
10
app/model/AppUser.php
Normal file
10
app/model/AppUser.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace app\model;
|
||||||
|
|
||||||
|
use think\Model;
|
||||||
|
|
||||||
|
class AppUser extends Model
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user