Files
verify/backend/scripts/update_dynamic_code_doc.go
admin ea8ffb6c74 fix: 订阅模式登录验证、永久会员类型区分、动态代码HTTP返回值修复、侧边栏滚动位置保持
- 修复订阅模式登录时错误检查余额的问题
- 区分无限余额和永久订阅两种永久会员类型
- 修复动态代码HTTP请求返回值在JS中无法正确访问的问题
- 添加侧边栏滚动位置保持功能
- 移除developer角色相关代码,统一使用admin
- 添加缺失的i18n翻译key
2026-05-01 16:39:31 +08:00

303 lines
6.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"log"
"verification-platform-backend/internal/database"
"verification-platform-backend/internal/model"
)
func main() {
database.Init()
var doc model.Doc
if err := database.DB.Where("slug = ?", "api-get-dynamic-code").First(&doc).Error; err != nil {
log.Printf("未找到文档: %v", err)
return
}
newContent := `# 获取动态代码接口
**注意**:动态代码接口需要用户登录认证,需要在请求头中携带JWT token:
Authorization: Bearer {token}
### 接口地址
GET /api/v1/app/:appKey/dynamic-code
### 请求头
Authorization: Bearer {token}
### 路径参数
| 参数名 | 类型 | 必填 | 说明 |
|--------|------|------|------|
| appKey | string | 是 | 应用密钥 |
### 响应示例
{
"code": 200,
"message": "操作成功",
"data": [
{
"id": 1,
"name": "加法函数",
"key": "add",
"description": "实现数字加法运算",
"status": "active",
"created_at": "2024-01-01T00:00:00Z"
}
]
}
### 字段说明
- id: 动态代码ID
- name: 动态代码名称
- key: 动态代码唯一标识
- description: 描述
- status: 状态(active、inactive
---
## 执行动态代码接口
### 接口地址
POST /api/v1/app/:appKey/dynamic-code/:key/execute
### 请求头
Authorization: Bearer {token}
Content-Type: application/json
### 路径参数
| 参数名 | 类型 | 必填 | 说明 |
|--------|------|------|------|
| appKey | string | 是 | 应用密钥 |
| key | string | 是 | 动态代码标识 |
### 请求体
{
"params": {
"param1": "value1",
"param2": "value2"
}
}
### 参数说明
- params: 参数对象,键值对形式传递,键名即为JavaScript变量名
### 响应示例
{
"code": 200,
"message": "操作成功",
"data": {
"result": "执行结果",
"execution_time": 1
}
}
### 字段说明
- result: 代码执行结果
- execution_time: 执行时间(毫秒)
---
## JavaScript代码编写规范
### 1. 参数使用
动态代码可以直接使用请求参数中传递的变量,无需额外定义:
a + b
请求参数:{"params": {"a": 10, "b": 20}}
返回结果:30
### 2. 函数定义与调用
可以定义函数并在最后一行调用:
function calculate(x, y) {
return Math.sqrt(x * x + y * y);
}
calculate(x, y)
请求参数:{"params": {"x": 3, "y": 4}}
返回结果:5
### 3. 复杂逻辑
支持完整的JavaScript语法,包括控制流、循环、对象操作等:
function processScore(score) {
if (score >= 90) {
return "优秀";
} else if (score >= 60) {
return "及格";
} else {
return "不及格";
}
}
processScore(score)
### 4. 返回值规则
- **最后一个表达式的值即为返回值**
- **不要在全局作用域使用return语句**
- 可以返回任意类型:数字、字符串、布尔值、对象、数组等
### 5. 常见示例
#### 简单计算
price * quantity
#### 字符串拼接
name + "的年龄是" + age + "岁"
#### 条件判断
score >= 60 ? "及格" : "不及格"
#### 对象返回
{
sum: a + b,
product: a * b,
difference: a - b
}
#### 数组操作
function sumArray(numbers) {
let total = 0;
for (let i = 0; i < numbers.length; i++) {
total += numbers[i];
}
return total;
}
sumArray([1, 2, 3, 4, 5])
### 6. 注意事项
1. **全局作用域不能使用return**:return只能在函数内部使用
2. **参数变量直接使用**:无需重新定义
3. **最后一行是返回值**:确保最后一行是需要返回的表达式
4. **支持ES6语法**:可以使用let、const、箭头函数等
5. **内置对象可用**Math、Date、JSON等JavaScript内置对象都可以使用
---
## 错误处理
### 常见错误
#### 1. 语法错误
{
"code": 400,
"message": "代码执行错误: SyntaxError: Unexpected token"
}
#### 2. 运行时错误
{
"code": 400,
"message": "代码执行错误: ReferenceError: x is not defined"
}
#### 3. 全局return错误
{
"code": 400,
"message": "代码执行错误: SyntaxError: Illegal return statement"
}
---
## 完整示例
### 示例1:计算折扣价格
**请求**
POST /api/v1/app/your-app-key/dynamic-code/calculate-discount/execute
{
"params": {
"price": 100,
"discount": 20,
"tax": 10
}
}
**动态代码**
function calculateFinalPrice(price, discount, tax) {
const subtotal = price;
const discountAmount = subtotal * (discount / 100);
const afterDiscount = subtotal - discountAmount;
const taxAmount = afterDiscount * (tax / 100);
return afterDiscount + taxAmount;
}
calculateFinalPrice(price, discount, tax)
**响应**
{
"code": 200,
"message": "操作成功",
"data": {
"result": 88,
"execution_time": 1
}
}
### 示例2:用户信息格式化
**请求**
POST /api/v1/app/your-app-key/dynamic-code/format-user/execute
{
"params": {
"name": "张三",
"age": 25,
"city": "北京"
}
}
**动态代码**
function formatUserInfo(name, age, city) {
const greeting = "你好";
return greeting + ",我是" + name + ",今年" + age + "岁,来自" + city;
}
formatUserInfo(name, age, city)
**响应**
{
"code": 200,
"message": "操作成功",
"data": {
"result": "你好,我是张三,今年25岁,来自北京",
"execution_time": 1
}
}
---
## 性能建议
1. **避免死循环**:确保循环有明确的退出条件
2. **合理使用缓存**:对于重复计算,可以考虑缓存结果
3. **控制代码复杂度**:过复杂的代码可能影响执行效率
4. **使用内置函数**:优先使用JavaScript内置函数,性能更好
---
## 安全建议
1. **不要执行危险操作**:避免访问文件系统、网络请求等
2. **参数验证**:在代码中对参数进行必要的验证
3. **避免敏感信息**:不要在代码中硬编码密钥、密码等敏感信息
4. **限制执行时间**:设置合理的超时时间,防止长时间运行
`
doc.Content = newContent
if err := database.DB.Save(&doc).Error; err != nil {
log.Printf("更新文档失败: %v", err)
return
}
log.Println("动态代码文档更新成功")
}