找回密码
 立即注册
查看: 44|回复: 0

4,发起请求演变过程,uni.showToast

[复制链接]

193

主题

6

精华

197

金币

技术维护QQ:515138

积分
424
发表于 2026-2-2 21:13:55 | 显示全部楼层 |阅读模式
https://uniapp.dcloud.net.cn/api/request/request.html
第一步:
  1. // 生命周期 钩子函数,打开页面启动
  2.         onLoad(() => {
  3.                 uni.request({
  4.                     url: 'http://localhost:9999/base/sex',
  5.                     data: {
  6.                         text: 'uni.request'
  7.                     },
  8.                     header: {
  9.                         'custom-header': 'hello' //自定义请求头信息
  10.                     },
  11.                     success: (res) => {
  12.                         console.log(res.data);
  13.                         this.text = 'request success';
  14.                     }
  15.                 });
  16.         })
复制代码
演变成:
  1. // 生命周期 钩子函数,打开页面启动
  2.         onLoad(() => {
  3.                 uni.request({
  4.                     url: 'http://localhost:9999/base/sex',
  5.                     success: (res) => {
  6.                         console.log(res.data);
  7.                     }
  8.                 });
  9.         })
复制代码
再演变:
  1. // 加载性别数据
  2.         const sexs = ref([])
  3.         onLoad(() => {
  4.                 uni.request({
  5.                         url: 'http://localhost:9999/base/sex',
  6.                         success: (res) => {
  7.                                 console.log(res.data);
  8.                                 sexs.value = res.data.data.map(item => {
  9.                                         return {
  10.                                                 text: item.paramName,
  11.                                                 value: item.paramValue
  12.                                         }
  13.                                 });
  14.                         }
  15.                 });
  16.         })
复制代码
新建request.js文件:
  1. const request = () => {
  2.         uni.request({
  3.                 url: 'http://localhost:9999/base/sex',
  4.                 success: (res) => {
  5.                         console.log(res.data);
  6.                         sexs.value = res.data.data.map(item => {
  7.                                 return {
  8.                                         text: item.paramName,
  9.                                         value: item.paramValue
  10.                                 }
  11.                         });
  12.                 }
  13.         });
  14. }
  15. export default request;
复制代码

去除业务层代码改造如下:
  1. const request = (option) => {
  2.         uni.request({
  3.                 url: 'http://localhost:9999/base/sex',
  4.                 success: (res) => {
  5.                         console.log(res.data);
  6.                 }
  7.         });
  8. }
  9. export default request;
复制代码

地址变量改造:
  1. const apiBaseUrl ="http://localhost:9999"
  2. const request = (option) => {
  3.         uni.request({
  4.                 url: apiBaseUrl + option.url,
  5.                 success: (res) => {
  6.                         console.log(res.data);
  7.                 }
  8.         });
  9. }
  10. export default request;
复制代码
传值处理:
  1. const apiBaseUrl = "http://localhost:9999"
  2. const request = (option) => {
  3.         uni.request({
  4.                 url: apiBaseUrl + option.url,
  5.                 data: option.data || {},
  6.                 header: option.header || {},
  7.                 method: option.method || 'GET',
  8.                 success: (res) => {
  9.                         console.log(res.data);
  10.                 }
  11.         });
  12. }
  13. export default request;
复制代码
uni.showToast
https://uniapp.dcloud.net.cn/api/ui/prompt.html
整理成功失败返回:
  1. import {
  2.         resolve
  3. } from "dns";
  4. const apiBaseUrl = "http://localhost:9999"
  5. const request = (option) => {
  6.         // return 返回值,成功resolve reject
  7.         return new Promise((resolve, reject) => {
  8.                 uni.request({
  9.                         url: apiBaseUrl + option.url,
  10.                         data: option.data || {},
  11.                         header: option.header || {},
  12.                         method: option.method || 'GET',
  13.                         success: (res) => {
  14.                                 // 根据后端code判断是否成功
  15.                                 if (res.data.code == 20000) {
  16.                                         // 通过resolve数据带出去
  17.                                         resolve(res.data)
  18.                                 } else {
  19.                                         console.log("-----> 操作失败:", res.data);
  20.                                         // 定义错误信息,后端传值 res.data.message,如果没值显示 "未知错误"
  21.                                         let errInfo = res.data?.message || '未知错误'
  22.                                         uni.showToast({
  23.                                                 icon: 'none',
  24.                                                 title: errInfo,
  25.                                                 duration: 3000 // 显示时常
  26.                                         })
  27.                                         // 休眠三秒后执行
  28.                                         setTimeout(function() {
  29.                                                 if (res.data.code == 20003) {
  30.                                                         uni.reLaunch({
  31.                                                                 url: '/pages/login'
  32.                                                         })
  33.                                                 }
  34.                                                 // 错误的抛出去
  35.                                                 reject(new Error(res.statusCode))
  36.                                         }, 3000);
  37.                                 }
  38.                         },
  39.                         fail: (err) => {
  40.                                 let errorInfo = '';
  41.                                 if (err.errMsg.includes('timeout')) {
  42.                                         errorInfo = '请求超时,请稍后重试'
  43.                                 } else if (err.errMsg.includes('abort')) {
  44.                                         errorInfo = '请求数据错误,请稍后重试'
  45.                                 } else {
  46.                                         errorInfo = '网络请求错误'
  47.                                 }
  48.                                 uni.showToast({
  49.                                         title: errorInfo,
  50.                                         duration: 5000,
  51.                                         icon: "none"
  52.                                 });
  53.                                 reject(new Error(errorInfo))
  54.                         }
  55.                 });
  56.         });
  57. }
  58. export default request;
复制代码
注册页面调用:
  1.         // 调用request 后面.then表示成功回调
  2.         import request from '@/utils/request.js'
  3.         onLoad(() => {
  4.                 request({
  5.                         url: '/base/sex',
  6.                 }).then(res => {
  7.                         sexs.value = res.data.data.map(item => {
  8.                                 return {
  9.                                         text: item.paramName,
  10.                                         value: item.paramValue
  11.                                 }
  12.                         });
  13.                 })
  14.         })
复制代码

新增config/config.js
  1. const config = {
  2.         apiBaseUrl: "http://localhost:9999"
  3. }
  4. export default config;
复制代码
改造后:
  1. import config from '@/config/config.js'
  2. // 老的代码
  3. // const apiBaseUrl = "http://localhost:9999"
  4. const request = (option) => {
  5.         // return 返回值,成功resolve reject
  6.         return new Promise((resolve, reject) => {
  7.                 uni.request({
  8.                         url: config.apiBaseUrl + option.url,
  9.                         data: option.data || {},
  10.                         header: option.header || {},
  11.                         method: option.method || 'GET',
  12.                         success: (res) => {
  13.                                 // 根据后端code判断是否成功
  14.                                 if (res.data.code == 20000) {
  15.                                         // 通过resolve数据带出去
  16.                                         resolve(res.data)
  17.                                 } else {
  18.                                         console.log("-----> 操作失败:", res.data);
  19.                                         // 定义错误信息,后端传值 res.data.message,如果没值显示 "未知错误"
  20.                                         let errInfo = res.data?.message || '未知错误'
  21.                                         uni.showToast({
  22.                                                 icon: 'none',
  23.                                                 title: errInfo,
  24.                                                 duration: 3000 // 显示时常
  25.                                         })
  26.                                         // 休眠三秒后执行
  27.                                         setTimeout(function() {
  28.                                                 if (res.data.code == 20003) {
  29.                                                         uni.reLaunch({
  30.                                                                 url: '/pages/login'
  31.                                                         })
  32.                                                 }
  33.                                                 // 错误的抛出去
  34.                                                 reject(new Error(res.statusCode))
  35.                                         }, 3000);
  36.                                 }
  37.                         },
  38.                         fail: (err) => {
  39.                                 let errorInfo = '';
  40.                                 if (err.errMsg.includes('timeout')) {
  41.                                         errorInfo = '请求超时,请稍后重试'
  42.                                 } else if (err.errMsg.includes('abort')) {
  43.                                         errorInfo = '请求数据错误,请稍后重试'
  44.                                 } else {
  45.                                         errorInfo = '网络请求错误'
  46.                                 }
  47.                                 uni.showToast({
  48.                                         title: errorInfo,
  49.                                         duration: 5000,
  50.                                         icon: "none"
  51.                                 });
  52.                                 reject(new Error(errorInfo))
  53.                         }
  54.                 });
  55.         });
  56. }
  57. export default request;
复制代码

增加api/base.js
  1. import request from '@/utils/request.js'
  2. export default{
  3.         getParamListByBaseName(baseName){
  4.                 return request({
  5.                         url: `/base/${baseName}`
  6.                 })
  7.         }
  8. }
复制代码
改造后:
  1. // 调用api接口
  2.         import baseApi from '@/api/base.js'
  3.         onLoad(() => {
  4.                 baseApi.getParamListByBaseName('sex').then(res => {
  5.                         console.log('baseApi 请求。。。。。。。', res);
  6.                         sexs.value = res.data.map(item => {
  7.                                 return {
  8.                                         text: item.paramName,
  9.                                         value: item.paramValue
  10.                                 }
  11.                         });
  12.                 })
  13.         })
复制代码

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
网站建设,公众号小程序开发,系统定制,软件App开发,技术维护【联系我们】手机/微信:17817817816 QQ:515138

QQ|Archiver|自丢网 ( 粤ICP备2024252464号-1 )

GMT+8, 2026-2-18 11:30

专注于网站建设,公众号小程序制作,商城小程序,系统定制,软件App开发

【联系我们】手机/微信:17817817816 QQ:515138

快速回复 返回顶部 返回列表