提交代码
This commit is contained in:
1 parent
aacdbdad81
commit
a0e5c9a024
11 files changed
+6086
-26
No files matched your search
@@ -0,0 +1,413 @@
|
||||
# 移动端API调用标准规范
|
||||
|
||||
> 📌 **重要提示**: 移动端与Web端的API调用方式完全不同,请严格遵守本规范!
|
||||
|
||||
## 🚫 常见错误
|
||||
|
||||
### ❌ 不要这样做(Web端方式)
|
||||
```javascript
|
||||
// 错误1:使用 this.$http
|
||||
const res = await this.$http.get('/api/path');
|
||||
const res = await this.$http.post('/api/path', data);
|
||||
const res = await this.$http.upload('/api/upload', options);
|
||||
|
||||
// 错误2:使用 getDicts
|
||||
import { getDicts } from '@/api/system/dict/data.js';
|
||||
const res = await getDicts('dict_type');
|
||||
this.dictData = res.data; // Web端返回格式
|
||||
|
||||
// 错误3:使用 this.$u.toast
|
||||
this.$u.toast('提示信息');
|
||||
```
|
||||
|
||||
## ✅ 正确做法(移动端方式)
|
||||
|
||||
### 1. HTTP请求
|
||||
|
||||
```javascript
|
||||
// 导入
|
||||
import { request } from '@/common/request.js';
|
||||
|
||||
// GET请求(无参数)
|
||||
const res = await request({
|
||||
url: '/api/path',
|
||||
method: 'GET',
|
||||
});
|
||||
|
||||
// GET请求(带参数)
|
||||
const res = await request({
|
||||
url: '/api/path',
|
||||
method: 'GET',
|
||||
params: {
|
||||
key1: 'value1',
|
||||
key2: 'value2'
|
||||
},
|
||||
});
|
||||
|
||||
// POST请求
|
||||
const res = await request({
|
||||
url: '/api/path',
|
||||
method: 'POST',
|
||||
data: {
|
||||
key1: 'value1',
|
||||
key2: 'value2'
|
||||
},
|
||||
});
|
||||
|
||||
// PUT请求
|
||||
const res = await request({
|
||||
url: '/api/path',
|
||||
method: 'PUT',
|
||||
data: { ... },
|
||||
});
|
||||
|
||||
// DELETE请求
|
||||
const res = await request({
|
||||
url: '/api/path',
|
||||
method: 'DELETE',
|
||||
});
|
||||
```
|
||||
|
||||
### 2. 字典数据获取
|
||||
|
||||
```javascript
|
||||
// 导入
|
||||
import { getDict, getDictLabel, getDictTagType } from '@/common/dict.js';
|
||||
|
||||
// 获取字典数据(直接返回数组)
|
||||
const dictData = await getDict('dict_type'); // 返回: [{label, value, ...}, ...]
|
||||
|
||||
// 批量获取字典
|
||||
const [dict1, dict2] = await Promise.all([
|
||||
getDict('dict_type_1'),
|
||||
getDict('dict_type_2'),
|
||||
]);
|
||||
|
||||
// 获取字典标签
|
||||
const label = getDictLabel(dictData, '1'); // 返回: '标签名'
|
||||
|
||||
// 获取字典Tag类型(用于u-tag组件)
|
||||
const tagType = getDictTagType(dictData, '1'); // 返回: 'success', 'warning', 'error' 等
|
||||
```
|
||||
|
||||
### 3. 字典展示(u-tag)
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<!-- 基础用法 -->
|
||||
<u-tag :type="getDictTagType(xg_dict_type, value)">
|
||||
{{ getDictLabel(xg_dict_type, value) }}
|
||||
</u-tag>
|
||||
|
||||
<!-- 带降级处理 -->
|
||||
<u-tag :type="getDictTagType(xg_dict_type, value, tagTypeFallback)">
|
||||
{{ getDictLabel(xg_dict_type, value) }}
|
||||
</u-tag>
|
||||
|
||||
<!-- 选择器显示 -->
|
||||
<u-picker
|
||||
:show="showPicker"
|
||||
:columns="dictColumns"
|
||||
keyName="text"
|
||||
@confirm="handleConfirm"
|
||||
@cancel="showPicker = false"
|
||||
></u-picker>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { getDict, getDictLabel, getDictTagType } from '@/common/dict.js';
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
xg_dict_type: [],
|
||||
value: '1',
|
||||
showPicker: false,
|
||||
|
||||
// 降级映射(当字典未加载时使用)
|
||||
tagTypeFallback: {
|
||||
'1': 'success',
|
||||
'2': 'warning',
|
||||
'3': 'error',
|
||||
},
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
// 转换为选择器格式
|
||||
dictColumns() {
|
||||
return [
|
||||
this.xg_dict_type.map(item => ({
|
||||
text: item.label,
|
||||
value: item.value,
|
||||
}))
|
||||
];
|
||||
},
|
||||
},
|
||||
async mounted() {
|
||||
// 加载字典
|
||||
this.xg_dict_type = await getDict('xg_dict_type');
|
||||
},
|
||||
methods: {
|
||||
getDictLabel,
|
||||
getDictTagType,
|
||||
|
||||
handleConfirm(e) {
|
||||
if (e && e.value && e.value.length > 0) {
|
||||
this.value = e.value[0].value;
|
||||
}
|
||||
this.showPicker = false;
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
```
|
||||
|
||||
### 4. 文件上传
|
||||
|
||||
```javascript
|
||||
// 导入
|
||||
import { uploadFile } from '@/utils/upload.js';
|
||||
|
||||
// 选择并上传图片
|
||||
uni.chooseImage({
|
||||
count: 1,
|
||||
success: async (res) => {
|
||||
const filePath = res.tempFilePaths[0];
|
||||
|
||||
// 上传文件
|
||||
const result = await uploadFile(filePath, (progress) => {
|
||||
console.log('上传进度:', progress);
|
||||
// 更新进度条
|
||||
this.uploadProgress = progress;
|
||||
});
|
||||
|
||||
if (result && result.code === 200 && result.data) {
|
||||
// 上传成功
|
||||
this.ossId = result.data.ossId;
|
||||
this.fileUrl = result.data.url;
|
||||
}
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### 5. Toast提示
|
||||
|
||||
```javascript
|
||||
// 普通提示
|
||||
uni.showToast({
|
||||
title: '提示信息',
|
||||
icon: 'none'
|
||||
});
|
||||
|
||||
// 成功提示
|
||||
uni.showToast({
|
||||
title: '操作成功',
|
||||
icon: 'success'
|
||||
});
|
||||
|
||||
// 失败提示
|
||||
uni.showToast({
|
||||
title: '操作失败',
|
||||
icon: 'error'
|
||||
});
|
||||
|
||||
// 加载中
|
||||
uni.showLoading({
|
||||
title: '加载中...',
|
||||
mask: true
|
||||
});
|
||||
|
||||
// 关闭加载
|
||||
uni.hideLoading();
|
||||
```
|
||||
|
||||
## 📊 完整示例
|
||||
|
||||
```vue
|
||||
<template>
|
||||
<view class="page-container">
|
||||
<!-- 字典展示 -->
|
||||
<u-tag :type="getDictTagType(xg_status_dict, formData.status)">
|
||||
{{ getDictLabel(xg_status_dict, formData.status) }}
|
||||
</u-tag>
|
||||
|
||||
<!-- 字典选择器 -->
|
||||
<u-form-item label="状态" prop="status">
|
||||
<view @click="showStatusPicker = true">
|
||||
<u-input
|
||||
:value="getDictLabel(xg_status_dict, formData.status)"
|
||||
placeholder="请选择状态"
|
||||
readonly
|
||||
suffix-icon="arrow-down"
|
||||
/>
|
||||
</view>
|
||||
<u-picker
|
||||
:show="showStatusPicker"
|
||||
:columns="statusColumns"
|
||||
keyName="text"
|
||||
@confirm="handleStatusConfirm"
|
||||
@cancel="showStatusPicker = false"
|
||||
></u-picker>
|
||||
</u-form-item>
|
||||
|
||||
<!-- 提交按钮 -->
|
||||
<u-button type="primary" @click="handleSubmit">提交</u-button>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { request } from '@/common/request.js';
|
||||
import { getDict, getDictLabel, getDictTagType } from '@/common/dict.js';
|
||||
import { uploadFile } from '@/utils/upload.js';
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
formData: {
|
||||
title: '',
|
||||
status: '1',
|
||||
},
|
||||
xg_status_dict: [],
|
||||
showStatusPicker: false,
|
||||
};
|
||||
},
|
||||
computed: {
|
||||
statusColumns() {
|
||||
return [
|
||||
this.xg_status_dict.map(item => ({
|
||||
text: item.label,
|
||||
value: item.value,
|
||||
}))
|
||||
];
|
||||
},
|
||||
},
|
||||
async mounted() {
|
||||
await this.init();
|
||||
},
|
||||
methods: {
|
||||
getDictLabel,
|
||||
getDictTagType,
|
||||
|
||||
async init() {
|
||||
// 加载字典
|
||||
this.xg_status_dict = await getDict('xg_status');
|
||||
|
||||
// 加载数据
|
||||
await this.loadData();
|
||||
},
|
||||
|
||||
async loadData() {
|
||||
try {
|
||||
const res = await request({
|
||||
url: '/api/getData',
|
||||
method: 'GET',
|
||||
params: { id: this.id },
|
||||
});
|
||||
|
||||
if (res.code === 200) {
|
||||
this.formData = res.data;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载数据失败:', error);
|
||||
uni.showToast({
|
||||
title: '加载失败',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
handleStatusConfirm(e) {
|
||||
if (e && e.value && e.value.length > 0) {
|
||||
this.formData.status = e.value[0].value;
|
||||
}
|
||||
this.showStatusPicker = false;
|
||||
},
|
||||
|
||||
async handleSubmit() {
|
||||
try {
|
||||
uni.showLoading({ title: '提交中...', mask: true });
|
||||
|
||||
const res = await request({
|
||||
url: '/api/submit',
|
||||
method: 'POST',
|
||||
data: this.formData,
|
||||
});
|
||||
|
||||
uni.hideLoading();
|
||||
|
||||
if (res.code === 200) {
|
||||
uni.showToast({
|
||||
title: '提交成功',
|
||||
icon: 'success'
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
uni.navigateBack();
|
||||
}, 1500);
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: res.msg || '提交失败',
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
uni.hideLoading();
|
||||
console.error('提交失败:', error);
|
||||
uni.showToast({
|
||||
title: '提交失败',
|
||||
icon: 'error'
|
||||
});
|
||||
}
|
||||
},
|
||||
},
|
||||
};
|
||||
</script>
|
||||
```
|
||||
|
||||
## 🔑 核心差异对照表
|
||||
|
||||
| 功能 | Web端 | 移动端 |
|
||||
|------|-------|--------|
|
||||
| **HTTP请求** | `this.$http.get('/api')` | `request({ url: '/api', method: 'GET' })` |
|
||||
| **字典获取** | `getDicts('type')` 返回 `{data:[]}` | `getDict('type')` 返回 `[...]` |
|
||||
| **字典标签** | `selectDictLabel(dict, value)` | `getDictLabel(dict, value)` |
|
||||
| **Tag类型** | 手动映射 | `getDictTagType(dict, value)` |
|
||||
| **文件上传** | `this.$http.upload()` | `uploadFile(path, callback)` |
|
||||
| **Toast提示** | `this.$u.toast('msg')` | `uni.showToast({ title: 'msg' })` |
|
||||
| **加载提示** | `this.$u.loading.show()` | `uni.showLoading({ title: '...' })` |
|
||||
|
||||
## 📝 注意事项
|
||||
|
||||
1. **导入路径固定**:
|
||||
- HTTP请求: `@/common/request.js`
|
||||
- 字典方法: `@/common/dict.js`
|
||||
- 文件上传: `@/utils/upload.js`
|
||||
|
||||
2. **字典数据格式**:
|
||||
- 移动端 `getDict()` 直接返回数组,不需要 `.data`
|
||||
- 字典项格式: `{ label, value, elTagType, elTagClass, remark }`
|
||||
|
||||
3. **Tag类型自动转换**:
|
||||
- `danger` → `error`
|
||||
- `default` → `primary`
|
||||
|
||||
4. **文件上传进度**:
|
||||
- 进度回调返回 0-100 的数字
|
||||
- Web端可能是 `progress.progress`,移动端直接是数字
|
||||
|
||||
5. **错误处理**:
|
||||
- 统一使用 `try-catch`
|
||||
- 错误时使用 `uni.showToast` 提示
|
||||
|
||||
## 🔗 相关文档
|
||||
|
||||
- `mobile/src/common/request.js` - HTTP请求源码
|
||||
- `mobile/src/common/dict.js` - 字典方法源码
|
||||
- `mobile/src/utils/upload.js` - 文件上传源码
|
||||
- `.cursor/cursorrules-mobile.md` - 移动端开发规范
|
||||
|
||||
---
|
||||
|
||||
**最后更新**: 2025-01-24
|
||||
**适用范围**: 移动端工程(Vue 2.6.14 + uni-app 2.x)
|
||||
|
||||
@@ -12,6 +12,7 @@ export const ENCRYPT_CONFIG = {
|
||||
TENANT_ID: '000000',
|
||||
// 加密请求头
|
||||
ENCRYPT_HEADER: 'encrypt-key',
|
||||
//TODO 开发环境需要修改为false
|
||||
// 是否启用加密
|
||||
ENCRYPT_ENABLED: true,
|
||||
};
|
||||
@@ -36,8 +37,9 @@ export const REQUEST_CONFIG = {
|
||||
? 'http://localhost/login?redirect=/index'
|
||||
: 'https://hzdyjs.camasort.com/login?redirect=/index', // 生产环境前端域名
|
||||
|
||||
//TODO 生产环境需要修改为true
|
||||
// 是否启用加密
|
||||
encryptEnabled: true,
|
||||
encryptEnabled: false,
|
||||
// 防重复提交间隔时间(ms)
|
||||
repeatSubmitInterval: 500,
|
||||
};
|
||||
|
||||
Reference in new issue
Block a user