1. Create http.js in the root directory

Const commonUrl = "Access back end interface "; 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: If (res.statusCode == 200) {resolve(res.data); if (res.statusCode == 200) {resolve(res.data); } else {// The request to the server succeeded, but there is no code because the server does not return data. // then execute //; // then execute //; resolve(res.data.msg); }}, error: function(e) {reject(reject); }}); }); return promise; 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(reject); }}); }); return promise; } module.exports = { post: postRequest, get: getRequest // imgUrl: commonImgUrl };Copy the code

2. Perform global mount in main.js

import Vue from 'vue' import store from './store' import App from './App' import myRequest from './util/http.js' // Prototype.$store = Store vue.prototype.$myRequest = myRequest vue.config. productionTip = false app.mpType =  'app' const app = new Vue({ ... App }) app.$mount()Copy the code

3. Cite cases

Async fy() {let data = {pageCount: 10} const res = await this $myRequest. Get ('/Purchase/app/getHCProduct ', data); this.goodsList = res.HCProductList; },Copy the code