1. 概述
本文档提供基于SHA256加密的用户认证API使用指南,包含用户注册、登录功能,支持完整的用户状态跟踪。
注意: 所有密码均使用SHA256哈希存储,请勿传输明文密码到非HTTPS端点。
2. 基础信息
2.1 API地址
基础URL: https://fujii.cn/web/api/IO_users.php
2.2 请求格式
- 方法:POST
- Content-Type: application/x-www-form-urlencoded
- 字符编码:UTF-8
2.3 响应格式
{
"status": "success|error",
"message": "操作结果描述",
"data": { ... }, // 成功时返回的数据
"timestamp": 1630000000
}
3. 接口详情
3.1 用户注册
Endpoint: ?action=register
| 参数 | 类型 | 必填 | 说明 | 示例 |
|---|---|---|---|---|
| username | string | 是 | 4-20位,支持中文 | 张三 |
| password | string | 是 | 至少8位字符 | mypassword123 |
| string | 是 | 有效邮箱地址 | user@example.com |
成功响应示例:
{
"status": "success",
"message": "Registration successful",
"data": {
"username": "张三",
"email": "user@example.com"
},
"timestamp": 1630000000
}
错误状态码:
- 400 - 参数缺失或格式错误
- 409 - 用户名/邮箱已存在
- 500 - 服务器内部错误
3.2 用户登录
Endpoint: ?action=login
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| username | string | 是 | 注册时的用户名 |
| password | string | 是 | 注册时的密码 |
成功响应示例:
{
"status": "success",
"message": "Login successful",
"data": {
"user_id": 123,
"visit_count": 5,
"last_login": "2023-01-01 12:00:00"
},
"timestamp": 1630000000
}
错误状态码:
- 401 - 用户名或密码错误
- 403 - 账户已被锁定
4. 示例代码
4.1 使用Fetch API注册
async function register() {
const response = await fetch('https://fujii.cn/web/api/IO_users.php', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
action: 'register',
username: '张三',
password: 'mypassword123',
email: 'user@example.com'
})
});
const result = await response.json();
console.log(result);
}
4.2 使用axios登录
axios.post('https://fujii.cn/web/api/IO_users.php',
new URLSearchParams({
action: 'login',
username: '张三',
password: 'mypassword123'
}), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
})
.then(response => {
console.log('登录成功:', response.data);
});
5. 数据库结构
CREATE TABLE `users` (
`id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
`username` VARCHAR(20) UNIQUE NOT NULL,
`password` VARCHAR(64) NOT NULL COMMENT 'SHA256哈希',
`email` VARCHAR(255) UNIQUE NOT NULL,
`visit_count` INT UNSIGNED DEFAULT 0,
`last_visit_time` DATETIME,
`last_visit_ip` VARCHAR(45),
`account_locked` DATETIME DEFAULT NULL,
`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;