当前位置:   article > 正文

uni-app开发小程序网络请求封装_uniapp 封装请求 .env

uniapp 封装请求 .env
  1. export const apiResquest = (prams) => { //prams 为我们需要调用的接口API的参数 下面会贴具体代码
  2. uni.showLoading({
  3. title: '加载中...',
  4. mask: true
  5. });
  6. // 判断请求类型
  7. let headerData = {
  8. 'content-type': 'application/json',
  9. 'system-type': 3,
  10. 'Authorization': uni.getStorageSync('token') ? "Bearer " + uni.getStorageSync('token') : ''
  11. }
  12. let dataObj = null
  13. dataObj = prams.query
  14. return new Promise((resolve, reject) => {
  15. let ip = process.env.VUE_APP_BASE_URL;
  16. const token = uni.getStorageSync("token");
  17. let whiteurl = ['file/add_file', 'user/login', 'get_dictionary_value_info', 'user/reset_password',
  18. 'user/info', 'file/get_file_info', 'user/edit', 'apiResquest', 'user/reset_pwd', 'region',
  19. 'auth/department/front','user/list/salesman'
  20. ]
  21. let url = ''
  22. const addip = '/linker/we_chat'
  23. if (whiteurl.indexOf(prams.url) != -1) {
  24. url = ip + '/' + prams.url; //请求的网络地址和局地的api地址组合
  25. } else {
  26. url = ip + addip + '/' + prams.url; //请求的网络地址和局地的api地址组合
  27. }
  28. console.log("请求的url地址");
  29. console.log(url);
  30. console.log("请求的参数->",dataObj);
  31. return uni.request({
  32. url: url,
  33. data: dataObj,
  34. method: prams.method || 'GET',
  35. header: headerData,
  36. success(res) {
  37. resolve(res.data);
  38. },
  39. fail(err) {
  40. reject(err);
  41. },
  42. complete(res) {
  43. console.log('res------------>', res);
  44. console.log("请求params->", dataObj);
  45. console.log("url->", url);
  46. uni.hideLoading({
  47. success: (response) => {
  48. // 成功
  49. },
  50. complete: response => {
  51. if (res.data.code != 200) {
  52. uni.showToast({
  53. title: res.data.msg,
  54. duration: 2000,
  55. icon: "none"
  56. })
  57. setTimeout(() => {
  58. if (res.data.code == 401) {
  59. uni.clearStorageSync("token")
  60. uni.clearStorageSync("userInfo")
  61. uni.clearStorageSync("routes")
  62. uni.showModal({
  63. title: '提示',
  64. content: `是否登录?`,
  65. cancelText: "取消", // 取消按钮的文字
  66. confirmText: "确认", // 确认按钮的文字
  67. showCancel: true, // 是否显示取消按钮,默认为 true
  68. confirmColor: '#f55850',
  69. cancelColor: '#39B54A',
  70. success: (res) => {
  71. if (res.confirm) {
  72. uni.switchTab({
  73. url: "/pages/login/login"
  74. })
  75. }
  76. }
  77. })
  78. }
  79. if (res.data.code == 400) {
  80. console.log("参数错误");
  81. }
  82. if (res.code == 500) {
  83. console.log("权限问题");
  84. }
  85. }, 1000)
  86. }
  87. }
  88. })
  89. }
  90. })
  91. })
  92. }
  93. // 公共图片上传网络请求封装
  94. export const uploadFile = (prams) => {
  95. return new Promise((resolve, reject) => {
  96. let ip = process.env.VUE_APP_BASE_URL;
  97. uni.uploadFile({
  98. url: ip + prams.url, //请求地址
  99. filePath: prams.data, //图片路径
  100. name: 'file',
  101. header: {
  102. 'system-type': 3,
  103. 'Authorization': uni.getStorageSync('token') ? "Bearer " + uni.getStorageSync(
  104. 'token') : ''
  105. },
  106. timeout: 2000,
  107. // formData: data,
  108. success: (res) => {
  109. if (JSON.parse(res.data).code == 200) {
  110. resolve(JSON.parse(res.data))
  111. }
  112. },
  113. fail: (error) => {
  114. console.log(error); //抛出异常
  115. if (error.errMsg == "uploadFile:fail timeout") {
  116. uni.showToast({
  117. title: '请求超时',
  118. mask: true,
  119. icon: 'error'
  120. })
  121. }
  122. },
  123. complete: () => {
  124. uni.hideLoading()
  125. }
  126. })
  127. })
  128. }
  1. import {
  2. data,
  3. methods
  4. } from 'uview-ui/libs/mixin/mixin'
  5. import {
  6. apiResquest
  7. } from './request.js'
  8. //修改日程 添加日程 删除日程
  9. export const calendar_app = (query, method) => {
  10. return apiResquest({
  11. url: 'calendar_app',
  12. method: method,
  13. query: {
  14. ...query
  15. }
  16. })
  17. }

.vue中使用

  1. import {
  2. calendar_app,
  3. userInfo,
  4. person_data,
  5. collect_data
  6. } from '../../api/index.js'

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/318506
推荐阅读
相关标签
  

闽ICP备14008679号