第二阶段完善代理商激活码
This commit is contained in:
@@ -5,6 +5,7 @@ namespace app\admin\controller\api;
|
||||
|
||||
use app\manager\model\Member as MemberModel;
|
||||
use think\admin\Controller;
|
||||
use think\facade\Db;
|
||||
|
||||
|
||||
/**
|
||||
@@ -82,4 +83,208 @@ class Member extends Controller
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 激活码验证并激活
|
||||
* @return void
|
||||
*/
|
||||
public function activate()
|
||||
{
|
||||
try {
|
||||
// 验证激活码
|
||||
$code = input('code');
|
||||
if (empty($code)) {
|
||||
return json(['code' => 400, 'msg' => '激活码不能为空']);
|
||||
}
|
||||
|
||||
// 获取设备信息
|
||||
$machineId = input('machine_id', '');
|
||||
if (empty($machineId)) {
|
||||
return json(['code' => 400, 'msg' => '设备ID不能为空']);
|
||||
}
|
||||
|
||||
// 收集设备详细信息
|
||||
$deviceInfo = [
|
||||
'machine_id' => $machineId,
|
||||
'os' => input('os', ''), // 操作系统信息
|
||||
'device_name' => input('device_name', ''), // 设备名称
|
||||
'ip' => $this->request->ip(), // IP地址
|
||||
'user_agent' => $this->request->header('user-agent', ''), // UA信息
|
||||
'location' => input('location', '') // 地理位置(如果有)
|
||||
];
|
||||
|
||||
// 获取激活码信息
|
||||
$activation = Db::name('cursor_activation_codes')
|
||||
->where('code', $code)
|
||||
->where('is_used', 0)
|
||||
->find();
|
||||
|
||||
if (empty($activation)) {
|
||||
return json(['code' => 400, 'msg' => '激活码无效或已被使用']);
|
||||
}
|
||||
|
||||
// 开启事务
|
||||
Db::startTrans();
|
||||
try {
|
||||
// 标记新激活码为已使用
|
||||
Db::name('cursor_activation_codes')
|
||||
->where('id', $activation['id'])
|
||||
->update([
|
||||
'is_used' => 1,
|
||||
'used_at' => date('Y-m-d H:i:s'),
|
||||
'used_by' => $machineId,
|
||||
'device_info' => json_encode($deviceInfo)
|
||||
]);
|
||||
|
||||
Db::commit();
|
||||
|
||||
// 获取该设备所有的激活记录
|
||||
$activations = Db::name('cursor_activation_codes')
|
||||
->where('used_by', '=', $machineId)
|
||||
->where('is_used', '=', 1)
|
||||
->order('used_at desc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
// 计算总天数
|
||||
$totalDays = array_sum(array_column($activations, 'days'));
|
||||
|
||||
// 计算最终到期时间
|
||||
$now = time();
|
||||
$firstActivationTime = strtotime($activations[count($activations)-1]['used_at']); // 第一次激活时间
|
||||
$expireTime = $firstActivationTime + ($totalDays * 24 * 3600); // 总天数转换为秒数
|
||||
$daysLeft = ceil(($expireTime - $now) / 86400);
|
||||
|
||||
// 返回成功信息
|
||||
$result = [
|
||||
'code' => 200,
|
||||
'msg' => '激活成功',
|
||||
'data' => [
|
||||
'expire_time' => date('Y-m-d H:i:s', $expireTime),
|
||||
'total_days' => $totalDays,
|
||||
'added_days' => $activation['days'],
|
||||
'days_left' => $daysLeft,
|
||||
'machine_id' => $machineId,
|
||||
'device_info' => $deviceInfo,
|
||||
'activation_time' => date('Y-m-d H:i:s'),
|
||||
'activation_records' => array_map(function($record) {
|
||||
return [
|
||||
'code' => $record['code'],
|
||||
'days' => $record['days'],
|
||||
'activation_time' => $record['used_at'],
|
||||
'device_info' => json_decode($record['device_info'], true)
|
||||
];
|
||||
}, $activations)
|
||||
]
|
||||
];
|
||||
|
||||
return json($result);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
throw $e;
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取会员状态
|
||||
*/
|
||||
public function status()
|
||||
{
|
||||
try {
|
||||
// 获取设备ID
|
||||
$machineId = input('machine_id', '');
|
||||
if (empty($machineId)) {
|
||||
return json(['code' => 400, 'msg' => '设备ID不能为空']);
|
||||
}
|
||||
|
||||
// 获取该设备所有的激活记录
|
||||
$activations = Db::name('cursor_activation_codes')
|
||||
->where('used_by', '=', $machineId)
|
||||
->where('is_used', '=', 1)
|
||||
->order('used_at desc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
if (empty($activations)) {
|
||||
return json([
|
||||
'code' => 401,
|
||||
'msg' => '未激活',
|
||||
'data' => [
|
||||
'status' => 'inactive',
|
||||
'expire_time' => '',
|
||||
'total_days' => 0,
|
||||
'days_left' => 0,
|
||||
'activation_records' => []
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
// 计算总天数
|
||||
$totalDays = array_sum(array_column($activations, 'days'));
|
||||
|
||||
// 计算最终到期时间
|
||||
$now = time();
|
||||
$firstActivationTime = strtotime($activations[count($activations)-1]['used_at']); // 第一次激活时间
|
||||
$expireTime = $firstActivationTime + ($totalDays * 24 * 3600); // 总天数转换为秒数
|
||||
$daysLeft = ceil(($expireTime - $now) / 86400);
|
||||
|
||||
// 判断是否过期
|
||||
if ($daysLeft <= 0) {
|
||||
return json([
|
||||
'code' => 401,
|
||||
'msg' => '已过期',
|
||||
'data' => [
|
||||
'status' => 'expired',
|
||||
'expire_time' => date('Y-m-d H:i:s', $expireTime),
|
||||
'total_days' => $totalDays,
|
||||
'days_left' => 0,
|
||||
'machine_id' => $machineId,
|
||||
'activation_records' => array_map(function($record) {
|
||||
return [
|
||||
'code' => $record['code'],
|
||||
'days' => $record['days'],
|
||||
'activation_time' => $record['used_at'],
|
||||
'device_info' => json_decode($record['device_info'], true)
|
||||
];
|
||||
}, $activations)
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
// 返回正常状态
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '正常',
|
||||
'data' => [
|
||||
'status' => 'active',
|
||||
'expire_time' => date('Y-m-d H:i:s', $expireTime),
|
||||
'total_days' => $totalDays,
|
||||
'days_left' => $daysLeft,
|
||||
'machine_id' => $machineId,
|
||||
'activation_records' => array_map(function($record) {
|
||||
return [
|
||||
'code' => $record['code'],
|
||||
'days' => $record['days'],
|
||||
'activation_time' => $record['used_at'],
|
||||
'device_info' => json_decode($record['device_info'], true)
|
||||
];
|
||||
}, $activations)
|
||||
]
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => $e->getMessage()
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user