封装uni-app的网络请求

爱被打了一巴掌 2023-10-04 17:21 113阅读 0赞

方式一:
使用原生uni.request请求

  1. const BASE_URL = 'http://xxx'
  2. export const request = (options) => {
  3. //加载loading
  4. uni.showLoading({
  5. title:'加载中'
  6. })
  7. let header = {
  8. "Content-Type": "application/json",
  9. "Authorization": uni.getStorageSync('token')
  10. }
  11. console.info(options)
  12. return new Promise((resolve, reject) => {
  13. uni.request({
  14. // 开发者服务器接口地址(请求服务器地址 + 具体接口名)
  15. url: BASE_URL + options.url,
  16. // 请求方式(若不传,则默认为 GET )
  17. method: options.method || 'GET',
  18. // 请求参数(若不传,则默认为 {} )
  19. data: options.data || {
  20. },
  21. // 请求头(若不传,则默认为 header )
  22. header: options.header || header,
  23. // 请求成功
  24. success: (res) => {
  25. // 此判断可根据自己需要更改
  26. if (res.statusCode != 200) {
  27. let msg = error.data.message || error.data
  28. if(msg==null || msg==undefined) msg = '服务器异常'
  29. uni.showToast({
  30. title: msg,
  31. icon: 'none'
  32. })
  33. if(res.statusCode == 401){
  34. setTimeout(() => {
  35. // 跳转到登录页
  36. uni.navigateTo({
  37. url: "/pages/login/login"
  38. })
  39. }, 1000)
  40. return
  41. }
  42. }
  43. resolve(res.data)
  44. },
  45. // 请求失败
  46. fail: (err) => {
  47. uni.showToast({
  48. title: '请求接口失败!',
  49. icon: 'none'
  50. })
  51. reject(err)
  52. },
  53. //请求结束之后,执行的回调函数(成功或失败都会执行)
  54. complete() {
  55. //隐藏loading
  56. uni.hideLoading()
  57. }
  58. })
  59. })
  60. }
  61. export default {
  62. request
  63. }

方式二(推荐使用):
使用luch-request进行封装(小程序不支持axios),把luch的相关文件放到项目中,如
在这里插入图片描述
新建一个js,内容如下:

  1. import Request from '@/utils/luch/index.js'
  2. const BASE_URL = 'http://xxx'
  3. const request = new Request();
  4. /* 设置全局配置 */
  5. request.setConfig((config) => {
  6. config.baseURL = BASE_URL
  7. return config
  8. })
  9. /* 请求以前拦截器 */
  10. request.interceptors.request.use((config) => {
  11. config.header["Content-Type"] = "application/json"
  12. config.header = {
  13. ...config.header
  14. }
  15. // 权限认证
  16. config.header.Authorization = uni.getStorageSync("token")
  17. return config
  18. })
  19. request.interceptors.response.use(async (res) => {
  20. /* 请求以后拦截器 */
  21. if (res.statusCode != 200) {
  22. return Promise.reject(res)
  23. }
  24. return res.data
  25. }, (error) => {
  26. // 请求错误
  27. console.log(error)
  28. if (error.statusCode == 401) {
  29. // 登录态失效则清除token:没有携带token、token没法再刷新
  30. let msg = error.data.message || error.data
  31. if(msg==null || msg==undefined) msg = '当前登录状态过期,请重新登录'
  32. uni.clearStorageSync()
  33. uni.showToast({
  34. title: msg,
  35. icon: 'none',
  36. success() {
  37. setTimeout(() => {
  38. // 跳转到登录页
  39. uni.navigateTo({
  40. url: "/pages/login/login"
  41. })
  42. }, 1000)
  43. }
  44. })
  45. }else{
  46. let msg = error.data.message || error.data
  47. uni.showToast({
  48. title: msg,
  49. icon: 'none',
  50. duration: 2000
  51. })
  52. }
  53. return Promise.reject(error)
  54. })
  55. export {
  56. request
  57. }

main.js中引入

  1. import {
  2. request} from '@/utils/request.js'
  3. Vue.prototype.$request = request

使用

  1. //get方法
  2. this.$request.get(url).then(res=>{
  3. console.info(res)
  4. })
  5. //put方法
  6. this.$request.put(rul, param).then(res => {
  7. console.info(res)
  8. })
  9. //delete方法
  10. this.$request.delete(url, param).then(res => {
  11. console.info(res)
  12. })
  13. //post方法
  14. this.$request.post(url,this.param).then(res=>{
  15. console.info(res)
  16. })
  17. //上传文件
  18. this.$request.upload(url,{
  19. filePath: tempFilePaths[0],
  20. name: 'file',
  21. }).then(res => {
  22. console.log(res)
  23. })

发表评论

表情:
评论列表 (有 0 条评论,113人围观)

还没有评论,来说两句吧...

相关阅读