| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- //decode by http://www.yunlu99.com/
- namespace Biz;
- /**
- * token
- */
- class TokenManager {
- public function __construct() {
- $this->model = lm('account', 'Commons');
- }
- function publish($identity) {
- $token = $this->getToken();
- $this->saveToken($identity, $token);
- // echo 'tokne';
- $_SESSION['globalToken'] = $token;
- return $token;
- }
- function refreshToken() {
- return isset($_SESSION['globalToken']) ? $_SESSION['globalToken'] : '';
- }
- function check($token) {
- $user = lm('account_detailed', 'Commons')->where('token', $token)->first();
- if ($user) {
- return $user;
- }
- return 0;
- }
- function doLogout() {
- $_SESSION['uinfo'] = null;
- $_SESSION['globalToken'] = null;
- }
- /**
- * 登录操作
- *
- * @param $token
- * @return int
- * @throws \Exception
- */
- function doLogin($token) {
- $user = $this->check($token);
- $userInfo = $this->model->join('account_detailed', 'account.identity', '=', 'account_detailed.account_identity')->where('account.identity', $user->account_identity)->get();
- if (empty($userInfo) || !$user) {
- return false;
- }
- lm('account_detailed', 'Commons')->updateLoginStatus($user->identity);
- $userInfoArray = $userInfo->toArray();
- \InitSess::init()->iniSession($token);
- $_SESSION['uinfo'] = $userInfoArray[0];
- return $userInfoArray;
- }
- /**
- * 获取token
- *
- * @return string
- */
- public function getToken() {
- $enc = GenEncryption();
- $time = time();
- $token = substr(uniqid($enc . $time), 0, 35);
- return $token;
- }
- /**
- * 更新token记录
- *
- * @param $identity
- * @param $token
- * @throws \Exception
- */
- public function saveToken($identity, $token)
- {
- lm('account_detailed', 'Commons')->where('account_identity', $identity)->update(array('token' => $token));
- $accountToken = lm('account_token', 'Commons')->where('account_identity', $identity)->first();
- // $data = ['last_time' => date('Y-m-d H:i:s'), 'effective_time' => strtotime('now') + 3600];
- $data = ['last_time' => date('Y-m-d H:i:s'), 'effective_time' => time()];
- if ($accountToken) {
- lm('account_token', 'Commons')->where('account_identity', $identity)->update($data);
- } else {
- // echo 567;
- $data['account_identity'] = $identity;
- lm('account_token', 'Commons')->insert($data);
- }
- }
- /**
- * @param $token
- * @return mixed
- * @throws \Exception
- */
- public function getAccountInfo($token)
- {
- $userInfo = lm('account_detailed', 'Commons')->join('account', 'account_detailed.account_identity', '=', 'account.identity')->where('account_detailed.token', $token)->first();
- \InitSess::init()->iniSession($token);
- return $userInfo;
- }
- /**
- * @param $token
- * @return mixed
- * @throws \Exception
- */
- public function getAgentInfo($token)
- {
- $userInfo = lm('agent_detailed', 'Commons')->where('agent_token', $token)->first();
- return $userInfo;
- }
- }
|