uniapp封装request请求

uniapp封装request请求

request.js

export default function(config) {
    // 将配置参数中的地址结构出来
    const {baseURL} = config;
  
    // 真正插件
    return function (Vue) {
        Vue.prototype.http= async function (params) {
            // 结构请求相关参数
            const {url ,method, data,headers} = params
            // 显示加载框
            uni.showLoading({
                title:'加载中...',
                mask: 'true'
            })
            // 真正请求
            const header = {
                "content-type": "application/x-www-form-urlencoded",
                "ticket": uni.getStorageSync("ticket"),
                "cookie": "JSESSIONID=" + uni.getStorageSync("sessionid")
            };
            const res = await uni.request({
                url: baseURL+url,
                method,
                data,
                header: headers || header
            })
            uni.hideLoading();
            return res[1]
        }
    }
}

main.js

import Vue from 'vue'
import App from './App'
// 导入uni.request请求路径的封装
import request from '@/utils/request'
Vue.config.productionTip = false
App.mpType = 'app'
// 插件
const plugin = request({
  // baseURL:'http://********:8080/TSY'
  baseURL: 'https://***.***com/TSY'
});

Vue.use(plugin)
const app = new Vue({
  ...App
})
app.$mount()

使用请求

 this.http({
            url:'/api/pubshare/testPaper/getListJsonData',
            method:'post',
            data:{},
        }).then(res => {
            console.log('试卷',res)
            if(res.type == 'success'){
              this.shijuan = res.datas
            }
        }).catch(err =>{
        uni.showToast({
        title:'请求失败'
})
    })
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值