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

登录认证数据

* */ 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 = ' . $data['customer_id'] . ' AND platform = ' . $data['platform']); } $this->db->insert($this->_customerTokenTable, $data); } /** * 更新指定的登录数据 * * @param String $token

用户登录时生成的token字符串

* @param Array $data

其它需要记录的数据

* */ 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

用户登录时生成的token字符串

* @param Int $expiry

过期时间,Unix时间戳

* */ public function updateTokenExpiry($token, $expiry) { $where = $this->db->quoteInto('token = ?', $token); $this->db->update($this->_customerTokenTable, array( 'expiry' => $expiry ), $where); } /** * 更新登录数据的会员ID * * @param String $token

用户登录时生成的token字符串

* @param Int $customer_id

已登录会员的ID

* */ 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

用户登录时生成的token字符串

* */ public function deleteToken($token) { $where = $this->db->quoteInto('token = ?', $token); $this->db->delete($this->_customerTokenTable, $where); } /** *获取用户的登录数据 * * @param String $token

用户登录时生成的token字符串

* * @return Array 一个包含登录数据的数组 */ public function getToken($token) { return $this->db->fetchRow('SELECT * FROM ' . $this->_customerTokenTable . ' WHERE token = ' . $token); } /** * 较验Token是否有效 * * @param String $token

用户登录时生成的token字符串

* * @return Mix 返回一组会员数据代表验证通过,FALSE代表验证不通过。 */ public function checkToken($token) { $result = $this->db->fetchRow('SELECT * FROM ' . $this->_customerTokenTable . ' WHERE token = ' . $token); if ($result) { if ($result['expiry'] > time()) { return $result; } else { $this->deleteToken($token); return FALSE; } } else { return FALSE; } } /** *获取用户的登录数据 * * @param String $customer_id

用户ID

* @param String $current_token

用户当前有效的token字符串

* @param String $platform

登录平台ID

* @return Boolean 一个包含登录数据的数组 */ public function clearDuplicateToken($customer_id, $current_token, $platform = 1) { $results = $this->db->fetchAll('SELECT * FROM ' . $this->_customerTokenTable . ' WHERE customer_id = ' . $customer_id . 'AND platform = ' . $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 = ' . $customer_id); } public function getTokenDataByCustomerId($customer_id) { $token_data = $this->db->fetchOne('SELECT data FROM ' . $this->_customerTokenTable . ' WHERE customer_id = ' . $customer_id); return json_decode($token_data, true); } }