数据库: - db/schema_phase1.sql 16 张业务表(类目/服务/地址/师傅/订单/流水/取证/报价/评价/券/钱包/提现/结算/消息) - db/seed_phase1.sql 15 类目 + 32 服务项目 + 新人券(幂等) 后端 API (application/api/controller): - Base/Auth/Home/Address/Order/Master/Review - app/common/library/OrderStatus 11 状态机 + 流转矩阵 - application/api/common.php PHP 8.5 兼容垫片(E_DEPRECATED 不再升级为异常) 测试: - tests/e2e_flow.js 用户下单→支付→师傅抢单→履约→验收→结算→评价 全链路 65 项断言 - 含异常用例:越权/重复操作/非法状态流转/2h 未联系自动回流
123 行
3.2 KiB
PHP
可执行文件
123 行
3.2 KiB
PHP
可执行文件
<?php
|
|
|
|
namespace app\common\library\token\driver;
|
|
|
|
use app\common\library\token\Driver;
|
|
|
|
/**
|
|
* Token操作类
|
|
*/
|
|
class Mysql extends Driver
|
|
{
|
|
|
|
/**
|
|
* 默认配置
|
|
* @var array
|
|
*/
|
|
protected $options = [
|
|
'table' => 'user_token',
|
|
'expire' => 2592000,
|
|
'connection' => [],
|
|
];
|
|
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param array $options 参数
|
|
* @access public
|
|
*/
|
|
public function __construct($options = [])
|
|
{
|
|
if (!empty($options)) {
|
|
$this->options = array_merge($this->options, $options);
|
|
}
|
|
if ($this->options['connection']) {
|
|
$this->handler = \think\Db::connect($this->options['connection'])->name($this->options['table']);
|
|
} else {
|
|
$this->handler = \think\Db::name($this->options['table']);
|
|
}
|
|
$time = time();
|
|
$tokentime = cache('tokentime');
|
|
if (!$tokentime || $tokentime < $time - 86400) {
|
|
cache('tokentime', $time);
|
|
$this->handler->where('expiretime', '<', $time)->where('expiretime', '>', 0)->delete();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 存储Token
|
|
* @param string $token Token
|
|
* @param int $user_id 会员ID
|
|
* @param int $expire 过期时长,0表示无限,单位秒
|
|
* @return bool
|
|
*/
|
|
public function set($token, $user_id, $expire = null)
|
|
{
|
|
$expiretime = !is_null($expire) && $expire !== 0 ? time() + $expire : 0;
|
|
$token = $this->getEncryptedToken($token);
|
|
$exists = $this->handler->where('token', $token)->count();
|
|
if ($exists) {
|
|
return false;
|
|
}
|
|
$this->handler->insert(['token' => $token, 'user_id' => $user_id, 'createtime' => time(), 'expiretime' => $expiretime]);
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 获取Token内的信息
|
|
* @param string $token
|
|
* @return array
|
|
*/
|
|
public function get($token)
|
|
{
|
|
$data = $this->handler->where('token', $this->getEncryptedToken($token))->find();
|
|
if ($data) {
|
|
if (!$data['expiretime'] || $data['expiretime'] > time()) {
|
|
//返回未加密的token给客户端使用
|
|
$data['token'] = $token;
|
|
//返回剩余有效时间
|
|
$data['expires_in'] = $this->getExpiredIn($data['expiretime']);
|
|
return $data;
|
|
} else {
|
|
self::delete($token);
|
|
}
|
|
}
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* 判断Token是否可用
|
|
* @param string $token Token
|
|
* @param int $user_id 会员ID
|
|
* @return boolean
|
|
*/
|
|
public function check($token, $user_id)
|
|
{
|
|
$data = $this->get($token);
|
|
return $data && $data['user_id'] == $user_id ? true : false;
|
|
}
|
|
|
|
/**
|
|
* 删除Token
|
|
* @param string $token
|
|
* @return boolean
|
|
*/
|
|
public function delete($token)
|
|
{
|
|
$this->handler->where('token', $this->getEncryptedToken($token))->delete();
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 删除指定用户的所有Token
|
|
* @param int $user_id
|
|
* @return boolean
|
|
*/
|
|
public function clear($user_id)
|
|
{
|
|
$this->handler->where('user_id', $user_id)->delete();
|
|
return true;
|
|
}
|
|
|
|
}
|