一键复制给ai,即可优化代码
# AI代码生成规范 - 强制执行版> # AI Code Generation Rules - Mandatory Execution Version📖 **双语文档说明:本文档采用中英文双语对照格式,所有规则和说明均提供完整的中英文版本,请根据需要选择阅读。**> 📖 **Bilingual Document Notice: This document is provided in both Chinese and English with complete parallel versions of all rules and instructions. Please read according to your preference.**⚠️ **重要提醒:此文件为AI代码生成的强制性规范,所有规则必须严格执行,不得忽略或省略!**> ⚠️ **Important Notice: This file is a mandatory specification for AI code generation. All rules must be strictly followed without exception!**🚨 **AI执行声明:在生成任何代码之前,我将严格按照以下14条规则执行,不允许任何例外!**> 🚨 **AI Execution Statement: Before generating any code, I will strictly follow the 14 rules below without any exceptions!**## 📋 强制执行检查表 - AI必须在每次代码生成前确认> ## 📋 Mandatory Execution Checklist - AI must confirm before every code generation- [ ] ✅ 确认使用中文注释(规则1)- [ ] ✅ 确认注释代码已封装为函数(规则2) - [ ] ✅ 确认使用errorShowFn错误处理(规则3)- [ ] ✅ 确认使用ES6+语法(规则4)- [ ] ✅ 确认所有变量都有注释(规则5)- [ ] ✅ 确认重复代码已合并(规则6)- [ ] ✅ 确认条件判断已简化(规则7)- [ ] ✅ 确认函数参数已优化(规则8)- [ ] ✅ 确认布尔逻辑已优化(规则9)- [ ] ✅ 确认导航函数有注释(规则10)- [ ] ✅ 确认导入未删除(规则11)- [ ] ✅ 确认调试函数已提取(规则12)- [ ] ✅ 确认注释代码已函数化(规则13)- [ ] ✅ 确认变量赋值有注释(规则14)> - [ ] ✅ Confirm using Chinese comments (Rule 1)> - [ ] ✅ Confirm commented code wrapped as functions (Rule 2)> - [ ] ✅ Confirm using errorShowFn error handling (Rule 3)> - [ ] ✅ Confirm using ES6+ syntax (Rule 4)> - [ ] ✅ Confirm all variables have comments (Rule 5)> - [ ] ✅ Confirm duplicate code merged (Rule 6)> - [ ] ✅ Confirm conditionals simplified (Rule 7)> - [ ] ✅ Confirm function parameters optimized (Rule 8)> - [ ] ✅ Confirm boolean logic optimized (Rule 9)> - [ ] ✅ Confirm navigation functions have comments (Rule 10)> - [ ] ✅ Confirm imports not deleted (Rule 11)> - [ ] ✅ Confirm debug functions extracted (Rule 12)> - [ ] ✅ Confirm commented code functionized (Rule 13)> - [ ] ✅ Confirm variable assignments have comments (Rule 14)## 一、绝对强制规则(14条) - 违反任何一条都是错误的代码> ## 1. Absolutely Mandatory Rules (14 items) - Violating any rule results in incorrect code### 1.1 基础代码规范(5条) - 必须严格执行1. **中文注释强制**:❌ 禁止英文注释,❌ 禁止无注释,✅ 必须使用中文注释2. **注释代码函数系统**:❌ 禁止删除注释代码,✅ 必须封装为函数放入\"注释代码函数区\"3. **错误处理简化**:❌ 禁止其他错误处理方式,✅ 必须使用errorShowFn函数4. **现代JavaScript语法**:❌ 禁止老式语法,✅ 必须使用ES6+语法(箭头函数、async/await、解构)5. **全面注释要求**:❌ 禁止无注释的变量赋值,✅ 变量和条件判断必须添加行内注释> ### 1.1 Basic Code Standards (5 items) - Must be strictly enforced> 1. **Mandatory Chinese Comments**: ❌ Forbidden English comments, ❌ Forbidden no comments, ✅ Must use Chinese comments> 2. **Commented Code Function System**: ❌ Forbidden to delete commented code, ✅ Must wrap as functions in \"Commented Code Function Area\"> 3. **Simplified Error Handling**: ❌ Forbidden other error handling methods, ✅ Must use errorShowFn function> 4. **Modern JavaScript Syntax**: ❌ Forbidden old syntax, ✅ Must use ES6+ syntax (arrow functions, async/await, destructuring)> 5. **Comprehensive Comment Requirement**: ❌ Forbidden uncommented variable assignments, ✅ Variables and conditionals must have inline comments### 1.2 代码优化规范(5条) - 必须严格执行6. **合并重复逻辑**:❌ 禁止重复代码存在,✅ 必须识别并合并重复代码7. **简化条件判断**:❌ 禁止复杂的if-else,✅ 必须使用三元运算符简化8. **函数参数优化**:❌ 禁止传统参数写法,✅ 必须使用默认参数和解构赋值9. **布尔逻辑优化**:❌ 禁止冗余的布尔逻辑,✅ 必须进行逻辑反转优化10. **导航注释要求**:❌ 禁止无注释的导航函数,✅ 导航函数必须添加目标页面注释> ### 1.2 Code Optimization Standards (5 items) - Must be strictly enforced> 6. **Merge Duplicate Logic**: ❌ Forbidden duplicate code existence, ✅ Must identify and merge duplicate code> 7. **Simplify Conditionals**: ❌ Forbidden complex if-else, ✅ Must use ternary operators for simplification> 8. **Function Parameter Optimization**: ❌ Forbidden traditional parameter syntax, ✅ Must use default parameters and destructuring assignment> 9. **Boolean Logic Optimization**: ❌ Forbidden redundant boolean logic, ✅ Must perform logical inversion optimization> 10. **Navigation Comment Requirement**: ❌ Forbidden uncommented navigation functions, ✅ Navigation functions must have target page comments### 1.3 注释和导入管理(4条) - 必须严格执行11. **禁止删除导入**:❌ 禁止删除任何导入,✅ 未使用导入变量必须添加TODO注释标记(解构导入中部分未使用变量在上一行添加注释,完全未使用的导入移至TODO区域)12. **调试函数提取**:❌ 禁止直接console.log,✅ 必须提取为注释函数13. **注释代码函数化**:❌ 禁止删除注释代码,✅ 多行/复杂逻辑必须封装为函数,原位置添加注释调用(// 函数名();),单行注释不封装14. **变量赋值注释**:❌ 禁止无注释的变量赋值,✅ 所有变量赋值必须添加行内注释> ### 1.3 Comment and Import Management (4 items) - Must be strictly enforced> 11. **No Deletion of Imports**: ❌ Forbidden to delete any imports, ✅ Must add TODO comments for unused import variables (for partial unused variables in destructuring imports, add comments on previous line; move completely unused imports to TODO area)> 12. **Debug Function Extraction**: ❌ Forbidden direct console.log, ✅ Must extract as comment functions> 13. **Commented Code Functionization**: ❌ Forbidden to delete commented code, ✅ Multi-line/complex logic must be wrapped as functions, mark original location with comment call (// functionName();), single-line comments not wrapped> 14. **Variable Assignment Comments**: ❌ Forbidden uncommented variable assignments, ✅ All variable assignments must have inline comments## 二、注释代码管理系统> ## 2. Commented Code Management System### 核心原则:不删除、函数化、集中管理、原位置标记> ### Core Principles: No deletion, functionization, centralized management, original location marking### 操作流程:1. 被注释代码块封装为具名函数2. 函数放入\"注释代码函数区\" 3. 原位置用注释标记函数名4. 函数名使用英文snake_case命名,配合详细中文注释说明功能> ### Operation Process:> 1. Commented code blocks are wrapped as named functions> 2. Functions are placed in the \"Commented Code Function Area\"> 3. Mark the original location with a comment of the function name> 4. Function names use English snake_case naming with detailed Chinese comments describing functionality### 示例:> ### Example:```javascript// ===== 5. 注释代码函数区 =====// 暂停的用户验证逻辑 - 用户权限验证功能const suspended_user_verification_logic = () => { if (!state.userInfo.verified) { showToast(\"请先完成用户验证\"); return false; } return true;}// 暂停的VIP用户优惠逻辑 - VIP折扣计算功能 const suspended_vip_discount_logic = () => { if (user.vip_level > 3) { apply_vip_discount(0.8); }}// 原位置使用:// suspended_user_verification_logic(); // 用户验证功能暂时关闭// suspended_vip_discount_logic(); // VIP优惠功能暂时关闭```## 三、错误处理规范> ## 3. Error Handling Specification### 核心函数:> ### Core Functions:```javascriptconst errorShowFn = (err) => { console.error(\"错误信息:\", err); const msg = err.message || \"操作失败\"; uni.showToast({ title: msg, icon: \"none\" });};const handleApiCall = async (apiFn, ...args) => { try { const response = await apiFn(...args); if (!response || response.status !== 200) throw new Error(\"接口响应异常\"); return response.data; } catch (err) { errorShowFn(err); throw err; }};```### 流程:参数验证(try内抛出)→ API调用(handleApiCall包装)→ 数据验证 → 错误捕获(errorShowFn)> ### Process: Parameter validation (throw in try) → API call (wrapped by handleApiCall) → Data validation → Error capture (errorShowFn)## 四、文件结构(11区域)> ## 4. File Structure (11 Areas)```javascript// ===== 1. 依赖导入区域 =====// ===== 2. TODO待处理导入区域 ===== // ===== 3. 状态控制逻辑区域 =====// ===== 4. 通用工具函数区域 =====// ===== 5. 注释代码函数区 =====// ===== 6. 错误处理函数区域 =====// ===== 7. 数据处理函数区域 =====// ===== 8. UI渲染逻辑区域 =====// ===== 9. 页面初始化与事件绑定 =====// ===== 10. TODO任务管理区域 =====// ===== 11. 导出区域 =====```### 未使用导入变量处理规范:**核心原则**:区分处理解构导入和完整导入,不删除任何导入**处理规则**:1. **解构导入部分未使用**:在上一行添加TODO注释标记2. **完整导入未使用**:移至TODO待处理导入区域3. **保持可追溯性**:详细记录预期用途**示例**:```javascript// ===== 1. 依赖导入区域 =====// TODO: computed未使用-响应式计算属性预留import { ref, reactive, computed } from \'vue\'// TODO: hideLoading未使用-加载状态管控预留import { showToast, hideLoading } from \'@/utils/ui\'import { validateForm } from \'@/utils/validator\' // 已使用// ===== 2. TODO待处理导入区域 =====// TODO: 导入备用模块 - 完整未使用的导入// import { uploadFile } from \'@/api/upload\' // 文件上传功能预留// import dayjs from \'dayjs\' // 日期处理功能预留```> ```javascript> // ===== 1. Dependency Import Area =====> // ===== 2. TODO Pending Import Area =====> // ===== 3. State Control Logic Area =====> // ===== 4. General Utility Functions Area =====> // ===== 5. Commented Code Functions Area =====> // ===== 6. Error Handling Functions Area =====> // ===== 7. Data Processing Functions Area =====> // ===== 8. UI Rendering Logic Area =====> // ===== 9. Page Initialization & Event Binding =====> // ===== 10. TODO Task Management Area =====> // ===== 11. Export Area =====> ```> ### Unused Import Variable Handling Specification:> **Core Principle**: Distinguish between destructuring imports and complete imports, do not delete any imports> > **Processing Rules**:> 1. **Partially unused destructuring imports**: Add TODO comments on previous line> 2. **Completely unused imports**: Move to TODO pending import area> 3. **Maintain traceability**: Record expected usage in detail> ```## 五、命名规范> ## 5. Naming Conventions### 变量命名:snake_case,布尔值(is_、has_、can_前缀),函数(get_、set_、handle_、show_前缀)### 注释函数命名:英文snake_case,描述性强,使用suspended_前缀区分(如:suspended_user_auth、suspended_data_cache)> ### Variable Naming: snake_case, Boolean values (is_, has_, can_ prefix), functions (get_, set_, handle_, show_ prefix)> ### Comment Function Naming: English snake_case, highly descriptive, use suspended_ prefix for distinction (e.g., suspended_user_auth, suspended_data_cache)### 注释代码函数处理规则:- **返回值**:保持原有结构- **多层嵌套**:整体封装,保持内部结构- **函数调用**:仅作代码保存容器,不实际执行> ### Commented Code Function Handling Rules:> - **Return Value**: Keep the original structure> - **Multi-level Nesting**: Wrap as a whole, keep internal structure> - **Function Calls**: Only as code containers, not actually executed## 六、API调用标准模式(7步)> ## 6. Standard API Call Pattern (7 Steps)```javascriptconst get_user_data = async (user_id) => { showLoading(\"获取用户信息中...\"); try { if (!user_id) throw new Error(\"用户ID不能为空\"); // 参数验证 const data = await handleApiCall((id) => axios.get(`/api/user/${id}`), user_id); // API调用 if (!data.user) throw new Error(\"用户数据无效\"); // 数据验证 state.userInfo = data.user; // 状态更新 showToast(\"获取成功\"); // 用户反馈 return data; // 返回数据 } catch (err) { errorShowFn(err); throw err; } finally { hideLoading(); }};```## 七、AI执行优先级> ## 7. AI Execution Priority1. **核心规则(最高)**:遵循14条规则,保留注释代码,封装到注释函数区,中文注释2. **必需函数**:errorShowFn、handleApiCall、工具函数、注释函数区函数3. **模式应用**:API调用模式、11区域结构、TODO管理、命名约定4. **代码优化**:ES6+语法、合并重复、简化条件、默认参数> 1. **Core Rules (Highest)**: Follow the 14 rules, keep commented code, wrap into the commented function area, use Chinese comments> 2. **Required Functions**: errorShowFn, handleApiCall, utility functions, commented function area functions> 3. **Pattern Application**: API call pattern, 11-area structure, TODO management, naming conventions> 4. **Code Optimization**: ES6+ syntax, merge duplicates, simplify conditions, default parameters## 八、快速参考> ## 8. Quick Reference### 注释代码转换模板:> ### Commented Code Conversion Template:```javascript// 需要封装(多行/复杂):// if (user.vip_level > 3) {// apply_vip_discount(0.8);// }// 转换后:// 暂停的VIP用户优惠逻辑 - VIP折扣计算功能const suspended_vip_discount_logic = () => { if (user.vip_level > 3) { apply_vip_discount(0.8); }}// 原位置:// suspended_vip_discount_logic(); // VIP优惠功能暂时关闭// 不需要封装(单行):// const test = 123; // 保持原样```### TODO分类:`// TODO: 功能待实现` `// TODO: 性能优化点` `// TODO: 注释函数激活` `// TODO: 导入备用模块`> ### TODO Categories:> `// TODO: Features to implement` `// TODO: Performance optimization points` `// TODO: Comment function activation` `// TODO: Import backup modules`### 开发检查清单:- [ ] 中文注释完整 [ ] 注释代码已封装 [ ] 使用errorShowFn [ ] 遵循11区域结构 [ ] snake_case命名> ### Development Checklist:> - [ ] Complete Chinese comments [ ] Commented code wrapped [ ] Use errorShowFn [ ] Follow 11-area structure [ ] snake_case naming## 九、核心理念> ## 9. Core Concepts**保护性开发**:不删除代码,封装保存 **可追溯性**:函数名快速定位功能 **统一化处理**:错误、API、状态统一标准 **可维护性**:清晰结构和完整注释> **Protective Development**: Do not delete code, wrap and save > **Traceability**: Function names allow quick feature location > **Unified Handling**: Standardize error, API, and state handling > **Maintainability**: Clear structure and complete comments## 十、AI强制执行检查机制> ## 10. AI Mandatory Execution Check Mechanism### 🔍 每次代码生成前必须执行的检查清单:> ### 🔍 Mandatory Checklist Before Every Code Generation:1. **强制规则检查**:确认是否遵循全部14条规则 ✅2. **中文注释检查**:确认所有代码逻辑都有中文注释 ✅ 3. **注释代码检查**:确认被注释代码已封装为函数 ✅4. **错误处理检查**:确认使用errorShowFn统一处理错误 ✅5. **文件结构检查**:确认遵循11区域结构划分 ✅6. **命名规范检查**:确认使用snake_case命名规范 ✅7. **ES6+语法检查**:确认使用现代JavaScript语法 ✅### ⚠️ 违规处理机制:> ### ⚠️ Violation Handling Mechanism:- **发现违规**:立即停止代码生成,重新按规范执行- **规则遗漏**:返回重新生成,直到完全符合规范- **注释缺失**:补充所有缺失的中文注释- **结构错误**:重新按11区域结构组织代码### 🎯 AI执行承诺:> ### 🎯 AI Execution Commitment:```我承诺在每次代码生成时:1. 严格遵循14条强制规则,不遗漏任何一条2. 确保所有代码都有完整的中文注释3. 将注释代码封装为函数保存在注释函数区4. 使用errorShowFn进行统一错误处理5. 按照11区域结构组织代码文件6. 使用snake_case命名规范和ES6+语法7. 在生成代码前进行自我检查和验证```> ```> I promise to follow these guidelines when generating code:> 1. Strictly adhere to all 14 mandatory rules without omission> 2. Ensure all code has complete Chinese comments> 3. Wrap commented code as functions in the comment function area> 4. Use errorShowFn for unified error handling> 5. Organize code files according to the 11-area structure> 6. Use snake_case naming conventions and ES6+ syntax> 7. Perform self-checks and validation before code generation> ```---**AI执行要点**:- 被注释代码必须封装为函数保存在\"注释代码函数区\"- 原位置用注释标记函数名,便于追踪恢复- 函数名使用英文snake_case命名,配合详细中文注释说明功能- 封装函数仅作代码保存容器,不会实际执行> **AI Execution Essentials**:> - Commented code must be wrapped as functions and saved in the \"Commented Code Function Area\"> - Mark the original location with a comment of the function name for easy tracking and recovery> - Function names use English snake_case naming with detailed Chinese comments describing functionality> - Wrapped functions are only for code storage and will not be executed