配置域名配置config.js
// config 域名配置
const config = {
host: 'https://test.17m.club/assets',
}
module.exports = config
请求拦截配置request.js
const config = require('../config/index.js')
import utils from '../utils/index.js'
const http = function(method, url, data, header = {}) {
const token = utils.storage.getItem('token');
if (token){
header.Authorization = `bearer ${token}`;
}
if (!header['Content-Type']) {
header['Content-Type'] = 'application/json';
}
return new Promise(function (resolve, reject) {
wx.request({
method: method,
url: config.host + url,
data: data,
header: header,
success: function (res) {
if (res.statusCode === 200) {
reslve(res.data);
} else {
reject(error)
// utils.format.showErr(res.data.msg)
}
},
error: function (error) {
utils.format.showErr('网络请求错误')
reject(error)
}
});
});
}
export const post = (url, data) => {
return http('post', url, data)
}
export const get = (url, data) => {
return http('get', url, data)
}
请求index文档 index.js
import {post, get} from './request'
//登录
export const wxLogin = (params) => post('/api/oauth/wx', params)
最后我们可以在app.js引用,然后在APP中注册,单页面引用时需要调用getApp()方法即可