uniapp封装请求挂载调用

封装require.js

const commonUrl = "https://192.168.1.103:8080/pig/"; //公共路径 
const commonImgUrl = 'https://192.168.1.103:8080/FileUpload/'
    // post请求封装
function postRequest(url, data) {
    var promise = new Promise((resolve, reject) => {
        var that = this;
        var postData = data;
        uni.request({
            url: commonUrl + url,
            data: postData,
            method: "POST",
            header: {
                "content-type": "application/x-www-form-urlencoded",
                token: uni.getStorageSync("token")
            },
            success: function(res) {
                //返回什么就相应的做调整
                if (res.statusCode == 200) {
                    resolve(res.data);
                } else {
                    // 请求服务器成功,但是由于服务器没有数据返回,此时无code。会导致这个空数据
                    //接口后面的then执行
                    // 不下去,导致报错,所以还是要resolve下,这样起码有个返回值,
                    //不会被阻断在那里执行不下去!
                    resolve(res.data.msg);
                }
            },
            error: function(e) {
                reject("网络出错");
            }
        });
    });
    return promise;
}

// get请求封装
function getRequest(url, data) {
    var promise = new Promise((resolve, reject) => {
        var that = this;
        var postData = data;
        uni.request({
            url: commonUrl + url,
            data: postData,
            method: "GET",
            dataType: "json",
            header: {
                "content-type": "application/json"
            },
            success: function(res) {
                if (res.statusCode == 200) {
                    resolve(res.data);
                } else {
                    resolve(res.data);
                }
            },
            error: function(e) {
                reject("网络出错");
            }
        });
    });
    return promise;
}

module.exports = {
    post: postRequest,
    get: getRequest,
    imgUrl: commonImgUrl
};

main.js 引用 + 挂载

import Vue from 'vue'
import App from './App'
import myRequest from '@/utils/require.js'
import store from './store'

Vue.prototype.$store = store
Vue.prototype.$myRequest = myRequest
Vue.prototype.$imgUrl = myRequest.imgUrl
Vue.config.productionTip = false

App.mpType = 'app'


const app = new Vue({
    ...App,
    store
})
app.$mount()

页面调用

async add(){ 
				let data = {
				  attach_type: 1
				}
				const res =  await this.$myRequest.get('home/getAttachInfo.do',data);
				if(res.result == 200){
					this.swiperList = res.data.map(item => ({
							attach_id: item.attach_id,
							attach_path: this.$imgUrl + item.attach_path, 
							buss_id:item.buss_id,
							pic_link:item.pic_link,
							type:item.type
						})) 
					console.log(this.swiperList)
				}
			},
``
  • 6
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值