http.api.js 1.0 KB

123456789101112131415161718192021222324252627282930313233
  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. httpMap[key] = (data = {}, config = {}) => http[apiurl[key]?.type](apiurl[key]?.url, {params:data}, config);
  13. } else if (apiurl[key]?.type == 'delete') {
  14. httpMap[key] = (data = {}, config = {}) => http[apiurl[key]?.type](apiurl[key]?.url + `${queryParams(data)}`, {}, config);
  15. } else{
  16. httpMap[key] = (params = {}, config = {}) => http[apiurl[key]?.type](apiurl[key]?.url, params, config);
  17. }
  18. });
  19. // 将各个定义的接口名称,统一放进对象挂载到vm.$u.api(因为vm就是this,也即this.$u.api)下
  20. vm.$u.api = {
  21. ...httpMap
  22. };
  23. }
  24. export default {
  25. install
  26. }