# 统一后端平台 - 5分钟快速接入指南
本指南帮助你在 5 分钟内将应用接入统一后端平台。
---
## 🚀 第一步:创建 Casdoor 应用
### 1. 访问 Casdoor 管理后台
```
http://localhost:8000
```
### 2. 创建新应用
1. 点击左侧 `Applications` → `Add Application`
2. 填写应用信息:
```
名称: my-app
显示名称: 我的应用
```
3. 记录 `Client ID` 和 `Client Secret`
---
## 🔐 第二步:集成用户登录
### 复制粘贴代码(React 示例)
```javascript
// auth.js
import axios from 'axios';
const API_BASE = 'http://localhost:9000/api/v1';
const CASDOOR_URL = 'http://localhost:8000';
// 登录
export function login() {
window.location.href = `${CASDOOR_URL}/login/oauth/authorize?client_id=YOUR_CLIENT_ID&redirect_uri=${encodeURIComponent('http://localhost:3000/callback')}&response_type=code`;
}
// 处理回调
export async function handleCallback(code) {
const res = await axios.get(`${CASDOOR_URL}/api/login/oauth/access_token`, {
params: { client_id: 'YOUR_CLIENT_ID', code, grant_type: 'authorization_code' }
});
localStorage.setItem('token', res.data.access_token);
}
// 获取当前用户
export async function getCurrentUser() {
const token = localStorage.getItem('token');
const res = await axios.get(`${API_BASE}/auth/me`, {
headers: { Authorization: `Bearer ${token}` }
});
return res.data;
}
// API 请求客户端
const api = axios.create({ baseURL: API_BASE });
api.interceptors.request.use(config => {
config.headers.Authorization = `Bearer ${localStorage.getItem('token')}`;
return config;
});
export default api;
```
---
## 📊 第三步:保存数据
### 创建数据
```javascript
import api from './auth';
// 保存一篇文章
async function savePost() {
const res = await api.post('/records', {
app_identifier: 'my-app', // 你的应用标识符
collection_type: 'post', // 数据类型
payload: { // 🔥 你的数据(任意结构)
title: '我的文章',
content: '文章内容...',
author: '张三'
},
is_published: true
});
console.log('保存成功:', res.data);
}
```
### 查询数据
```javascript
// 查询所有文章
async function getPosts() {
const res = await api.get('/records', {
params: {
app_identifier: 'my-app',
collection_type: 'post'
}
});
console.log('文章列表:', res.data.items);
}
// 查询我的文章
async function getMyPosts() {
const res = await api.get('/records', {
params: {
app_identifier: 'my-app',
collection_type: 'post',
owner_id: 'current' // 自动使用当前用户 ID
}
});
return res.data.items;
}
```
### 更新数据
```javascript
async function updatePost(id) {
const res = await api.patch(`/records/${id}`, {
payload: {
title: '新标题',
content: '新内容...'
}
});
}
```
### 删除数据
```javascript
async function deletePost(id) {
await api.delete(`/records/${id}`);
}
```
---
## 📁 第四步:上传文件
### 上传图片
```javascript
async function uploadImage(file) {
const formData = new FormData();
formData.append('file', file);
formData.append('app_identifier', 'my-app');
const res = await api.post('/files/upload', formData);
console.log('图片 URL:', res.data.public_url);
return res.data.public_url;
}
// 使用示例
{
const file = e.target.files[0];
const url = await uploadImage(file);
console.log('上传成功:', url);
}} />
```
### 显示图片
```javascript
// 在组件中使用
function PostImage({ imageUrl }) {
return ;
}
```
---
## 🎯 完整示例:TODO 应用
```javascript
import { useState, useEffect } from 'react';
import api from './auth';
export function TodoApp() {
const [todos, setTodos] = useState([]);
const [input, setInput] = useState('');
// 获取待办事项
useEffect(() => {
api.get('/records', {
params: { app_identifier: 'todo-app', collection_type: 'task' }
}).then(res => setTodos(res.data.items));
}, []);
// 添加待办
async function addTodo() {
await api.post('/records', {
app_identifier: 'todo-app',
collection_type: 'task',
payload: { text: input, done: false }
});
setInput('');
// 重新加载...
}
// 标记完成
async function toggleTodo(id, currentDone) {
await api.patch(`/records/${id}`, {
payload: { done: !currentDone }
});
}
return (