Vestmore_GO/php_model/Token.php

197 lines
5.4 KiB
PHP
Raw Normal View History

2024-04-11 06:57:47 +00:00
<?php
/**
* Token类存储APP和微信小程序用户登录产生的数据并负责较验用户的状态。
*
* @author Evan
*/
class Model_Customer_Token extends \application\Model\Base\BaseModel
{
private $_customerTokenTable;
private $_redisKey = 'token_';
public $db;
public $redis;
public function __construct()
{
$this->db = \system\engine\Registry::get('db');
// $this->redis = \system\engine\Registry::get('redis');
$this->redis = false;
$config = \system\engine\Registry::get('config');
$this->tableFullName = $this->_customerTokenTable = $config->database->prefix
. 'customer_token';
}
/**
* 删除指定的登录数据
*
* @param Array $data <p>登录认证数据</p>
*
*/
public function insertToken($data)
{
$this->deleteToken($data['token']);
if (!empty($data['customer_id']) && !empty($data['platform'])) {
$this->db->query('DELETE FROM ' . $this->_customerTokenTable . ' WHERE customer_id = '
. $this->db->quote($data['customer_id']) . ' AND platform = ' . $this->db->quote($data['platform']));
}
$this->db->insert($this->_customerTokenTable, $data);
}
/**
* 更新指定的登录数据
*
* @param String $token <p>用户登录时生成的token字符串</p>
* @param Array $data <p>其它需要记录的数据</p>
*
*/
public function updateTokenData($token, $data = array())
{
$where = $this->db->quoteInto('token = ?', $token);
$this->db->update($this->_customerTokenTable, array(
'data' => json_encode($data)
), $where);
}
/**
* 更新登录数据的生命期
*
* @param String $token <p>用户登录时生成的token字符串</p>
* @param Int $expiry <p>过期时间Unix时间戳</p>
*
*/
public function updateTokenExpiry($token, $expiry)
{
$where = $this->db->quoteInto('token = ?', $token);
$this->db->update($this->_customerTokenTable, array(
'expiry' => $expiry
), $where);
}
/**
* 更新登录数据的会员ID
*
* @param String $token <p>用户登录时生成的token字符串</p>
* @param Int $customer_id <p>已登录会员的ID</p>
*
*/
public function updateTokenCustomerId($token, $customer_id)
{
$where = $this->db->quoteInto('token = ?', $token);
$this->db->update($this->_customerTokenTable, array(
'customer_id' => $customer_id
), $where);
}
/**
* 删除指定的登录数据
*
* @param String $token <p>用户登录时生成的token字符串</p>
*
*/
public function deleteToken($token)
{
$where = $this->db->quoteInto('token = ?', $token);
$this->db->delete($this->_customerTokenTable, $where);
}
/**
*获取用户的登录数据
*
* @param String $token <p>用户登录时生成的token字符串</p>
*
* @return Array 一个包含登录数据的数组
*/
public function getToken($token)
{
return $this->db->fetchRow('SELECT * FROM ' . $this->_customerTokenTable
. ' WHERE token = ' . $this->db->quote($token));
}
/**
* 较验Token是否有效
*
* @param String $token <p>用户登录时生成的token字符串</p>
*
* @return Mix 返回一组会员数据代表验证通过,<b>FALSE</b>代表验证不通过。
*/
public function checkToken($token)
{
$result = $this->db->fetchRow('SELECT * FROM ' . $this->_customerTokenTable
. ' WHERE token = ' . $this->db->quote($token));
if ($result) {
if ($result['expiry'] > time()) {
return $result;
} else {
$this->deleteToken($token);
return FALSE;
}
} else {
return FALSE;
}
}
/**
*获取用户的登录数据
*
* @param String $customer_id <p>用户ID</p>
* @param String $current_token <p>用户当前有效的token字符串</p>
* @param String $platform <p>登录平台ID</p>
* @return Boolean 一个包含登录数据的数组
*/
public function clearDuplicateToken($customer_id, $current_token, $platform = 1)
{
$results = $this->db->fetchAll('SELECT * FROM ' . $this->_customerTokenTable
. ' WHERE customer_id = ' . $this->db->quote($customer_id) . 'AND platform = ' . $this->db->quote($platform));
foreach ($results as $result) {
if ($result['token'] != $current_token) {
$this->updateTokenCustomerId($result['token'], 0);
}
}
return false;
}
public function getTokenByCustomerId($customer_id)
{
return $this->db->fetchOne('SELECT token FROM ' . $this->_customerTokenTable
. ' WHERE customer_id = ' . $this->db->quote($customer_id));
}
public function getTokenDataByCustomerId($customer_id)
{
$token_data = $this->db->fetchOne('SELECT data FROM ' . $this->_customerTokenTable
. ' WHERE customer_id = ' . $this->db->quote($customer_id));
return json_decode($token_data, true);
}
}