/** * ajax请求相关的服务 */ import baseUrl from './ajax.env' import { msg } from '@/utils/util' class AjaxService { constructor() { this.name = 'AjaxService' } getBaseUrl (url = '') { return url.indexOf('://') > -1 ? url : baseUrl + url } getStorageUser(){ let userInfo ='' uni.getStorage({ key: 'userInfo', success: function (res){ userInfo = res.data } }) return userInfo } getHeaders({ header = {} }) { const GET_LOGIN_STAUS = this.getStorageUser(); console.log(GET_LOGIN_STAUS) let REV_TOKEN_ENV=''; if (GET_LOGIN_STAUS != null) { REV_TOKEN_ENV = GET_LOGIN_STAUS.token }else{ REV_TOKEN_ENV = 'X-token' } header['authorization'] = REV_TOKEN_ENV return header } request(options = {}) { let header = this.getHeaders(options) if (options.header) { header = Object.assign(header, options.header) } let url = this.getBaseUrl(options.url) let { isLoading = true } = options if (isLoading) { wx.showLoading({ title: '加载中' }) } const requestPromise = new Promise((resolve, reject) => { uni.request({ url: url, method: options.method || 'POST', data: options.data || {}, header, success: res => { if (isLoading) wx.hideLoading(); if (res.data.code === 1) { resolve(res.data) } else { reject(res.data) } }, fail: error => { reject(error) msg(error) wx.hideLoading() } }) }) return requestPromise } get(options) { options.method = 'GET' return this.request(options) } post(options) { return this.request(options) } } export default new AjaxService()