http.api.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import {
  2. apiurl
  3. } from "./apiurl.js"
  4. import queryParams from "../utils/queryParams.js"
  5. // 此处第二个参数vm,就是我们在页面使用的this,你可以通过vm获取vuex等操作,更多内容详见uView对拦截器的介绍部分:
  6. const install = (Vue, vm) => {
  7. let httpMap = {}
  8. const http = uni.$u.http
  9. // 循环请求路径对象生成对应的方式请求
  10. Object.keys(apiurl).forEach((key) => {
  11. if(apiurl[key]?.type=='get'){
  12. if(apiurl[key].addUrl){
  13. httpMap[key] = (data = {}, config = {}) => http[apiurl[key]?.type](apiurl[key]?.url+data.code, {}, config);
  14. }else {
  15. httpMap[key] = (data = {}, config = {}) => http[apiurl[key]?.type](apiurl[key]?.url, {params:data}, config);
  16. }
  17. } else if (apiurl[key]?.type == 'delete') {
  18. httpMap[key] = (data = {}, config = {}) => http[apiurl[key]?.type](apiurl[key]?.url + `${queryParams(data)}`, {}, config);
  19. } else{
  20. httpMap[key] = (params = {}, config = {}) => http[apiurl[key]?.type](apiurl[key]?.url, params, config);
  21. }
  22. });
  23. // 将各个定义的接口名称,统一放进对象挂载到vm.$u.api(因为vm就是this,也即this.$u.api)下
  24. vm.$u.api = {
  25. ...httpMap
  26. };
  27. }
  28. export default {
  29. install
  30. }