// HTTP API使用示例 // 注意:鉴权配置已简化,只需要在 http/config/config.js 中配置不需要鉴权的接口即可 // 导入API模块 import { petsApi, assistantApi, adoptionApi, profileApi, commonApi, addNoAuthApis, setEnvironment } from '@/http/index.js' // 或者导入所有API // import api from '@/http/index.js' export default { data() { return { petsList: [], userInfo: {}, loading: false } }, methods: { // 示例1:获取宠物列表(自动鉴权) async loadPets() { try { // 使用默认配置,自动根据接口判断是否需要鉴权 const pets = await petsApi.getPetsList() this.petsList = pets } catch (error) { console.error('获取宠物列表失败:', error) } }, // 示例2:添加不需要鉴权的接口 addCustomNoAuthApis() { // 如果有自定义的接口不需要鉴权,可以这样添加 addNoAuthApis([ '/custom/public-api', '/special/no-auth-endpoint' ]) }, // 示例3:切换环境 switchEnvironment() { // 根据需要切换环境 // #ifdef H5 setEnvironment('development') // H5开发环境 // #endif // #ifdef MP-WEIXIN setEnvironment('production') // 小程序生产环境 // #endif // 或者根据条件动态切换 const isDev = process.env.NODE_ENV === 'development' setEnvironment(isDev ? 'development' : 'production') }, // 示例2:添加宠物 async addNewPet() { try { const petData = { name: '小白', breed: '金毛', age: 2, gender: '公' } const result = await petsApi.addPet(petData, { custom: { auth: true, loading: true, toast: true // 显示成功/失败提示 } }) uni.showToast({ title: '添加成功', icon: 'success' }) // 重新加载列表 this.loadPets() } catch (error) { // 错误已在拦截器中处理,这里可以做额外处理 console.error('添加宠物失败:', error) } }, // 示例3:AI助手对话 async sendMessageToAI() { try { const messageData = { message: '我的猫咪最近不爱吃饭,怎么办?', petId: 123 } const response = await assistantApi.sendMessage(messageData, { custom: { auth: true, loading: true, loadingText: 'AI正在思考中...' } }) console.log('AI回复:', response.reply) } catch (error) { console.error('AI对话失败:', error) } }, // 示例4:用户登录 async userLogin() { try { const loginData = { username: 'user@example.com', password: '123456' } const result = await profileApi.userLogin(loginData, { custom: { auth: false, // 登录接口不需要token loading: true, loadingText: '正在登录...' } }) // 保存token和用户信息 uni.setStorageSync('token', result.token) uni.setStorageSync('userInfo', result.userInfo) uni.showToast({ title: '登录成功', icon: 'success' }) } catch (error) { console.error('登录失败:', error) } }, // 示例5:上传图片 async uploadPetImage() { try { // 选择图片 const chooseResult = await uni.chooseImage({ count: 1, sizeType: ['compressed'], sourceType: ['album', 'camera'] }) const imageData = { filePath: chooseResult.tempFilePaths[0], name: 'petImage', formData: { type: 'pet', petId: 123 } } const uploadResult = await commonApi.uploadImage(imageData, { custom: { auth: true, loading: true, loadingText: '正在上传图片...' } }) console.log('上传成功:', uploadResult.url) uni.showToast({ title: '上传成功', icon: 'success' }) } catch (error) { console.error('上传失败:', error) } }, // 示例6:搜索领养宠物 async searchAdoptionPets() { try { const searchParams = { keyword: '金毛', type: 'dog', age: '1-3', location: '北京' } const pets = await adoptionApi.searchPets(searchParams, { custom: { auth: false, // 搜索不需要登录 loading: true } }) console.log('搜索结果:', pets) } catch (error) { console.error('搜索失败:', error) } }, // 示例7:批量操作 async batchOperations() { try { // 并发执行多个请求 const [pets, userInfo, adoptionPets] = await Promise.all([ petsApi.getPetsList(), profileApi.getUserInfo(), adoptionApi.getAdoptionPets() ]) console.log('批量获取数据成功:', { pets, userInfo, adoptionPets }) } catch (error) { console.error('批量操作失败:', error) } }, // 示例8:自定义错误处理 async customErrorHandling() { try { const result = await petsApi.getPetsList({}, { custom: { auth: true, loading: true, toast: false, // 不显示默认错误提示 catch: true // 允许catch捕获错误 } }) } catch (error) { // 自定义错误处理 if (error.code === 401) { uni.showModal({ title: '提示', content: '登录已过期,请重新登录', success: (res) => { if (res.confirm) { uni.reLaunch({ url: '/pages/login/login' }) } } }) } else { uni.showToast({ title: error.message || '操作失败', icon: 'none' }) } } } } }