|
|
há 11 meses atrás | |
|---|---|---|
| .. | ||
| custom-recharge | há 11 meses atrás | |
| ghs | há 11 meses atrás | |
| order | há 11 meses atrás | |
| pay | há 11 meses atrás | |
| payment | há 11 meses atrás | |
| README.md | há 11 meses atrás | |
本项目使用 luch-request 作为 HTTP 请求库,统一管理 API 请求。
api/
├── common/ # 通用 API
│ └── index.js
├── payment/ # 支付相关 API
│ └── index.js
└── README.md # 本文件
// 导入支付相关 API
import { createPaymentOrder, requestPayment } from '@/api/payment'
// 导入通用 API
import { getUserInfo, uploadPic } from '@/api/common'
export default {
methods: {
async handlePayment() {
try {
// 创建支付订单
const orderResult = await createPaymentOrder({
amount: 100,
paymentMethod: 'alipay',
orderId: 'ORDER123',
description: '测试订单'
})
console.log('订单创建成功:', orderResult)
} catch (error) {
console.error('订单创建失败:', error)
}
},
async getUserData() {
try {
const userInfo = await getUserInfo()
console.log('用户信息:', userInfo)
} catch (error) {
console.error('获取用户信息失败:', error)
}
}
}
}
在 config.js 中配置 API 基础 URL:
export const APIHOST = 'https://api.example.com'
export const IMGHOST = 'https://img.example.com'
可以通过环境变量覆盖默认配置:
VUE_APP_API_BASE_URL=https://api.production.com
VUE_APP_IMG_BASE_URL=https://img.production.com
luch-request 会自动处理以下功能:
所有 API 请求都会返回 Promise,可以使用 try-catch 或 .catch() 处理错误:
// 方式1: try-catch
try {
const result = await createPaymentOrder(data)
} catch (error) {
console.error('请求失败:', error)
}
// 方式2: .catch()
createPaymentOrder(data)
.then(result => {
console.log('成功:', result)
})
.catch(error => {
console.error('失败:', error)
})
index.js 文件https 实例导出 API 函数
import https from '@/plugins/luch-request_0.0.7/request'
// GET 请求
export const getData = (params) => {
return https.get('/api/path', params)
}
// POST 请求
export const postData = (data) => {
return https.post('/api/path', data)
}