123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- import {
- getToken
- } from '@/utils/auth'
- import {
- getLocale
- } from '@/utils/index'
- import {
- $http
- } from '@escook/request-miniprogram'
- import store from '@/store'
- class Request {
- constructor(options = {}) {
-
- this.baseUrl = options.baseUrl || ''
-
- this.url = options.url || ''
-
- this.method = 'GET'
-
- this.data = null
-
- this.header = options.header || {}
- this.beforeRequest = null
- this.afterRequest = null
- }
- get(url, data = {}) {
- this.method = 'GET'
- this.url = this.baseUrl + url
- this.data = data
- return this._()
- }
- post(url, data = {}) {
- this.method = 'POST'
- this.url = this.baseUrl + url
- this.data = data
- return this._()
- }
- put(url, data = {}) {
- this.method = 'PUT'
- this.url = this.baseUrl + url
- this.data = data
- return this._()
- }
- delete(url, data = {}) {
- this.method = 'DELETE'
- this.url = this.baseUrl + url
- this.data = data
- return this._()
- }
- _() {
-
- this.header = {}
-
- this.beforeRequest && typeof this.beforeRequest === 'function' && this.beforeRequest(this)
-
- return new Promise((resolve, reject) => {
- let weixin = wx
-
- if (typeof uni !== 'undefined') {
- weixin = uni
- }
- if (process.env.NODE_ENV !== 'development') {
- console.log("请求地址:" + this.url, this.data)
- }
- weixin.request({
- url: this.url,
- method: this.method,
- data: this.data,
- header: this.header,
- success: res => {
- if (this.afterRequest) {
- this.afterRequest(res)
- }
- },
- fail: err => {
- reject(err)
- },
- complete: res => {
- resolve(res.data)
- },
- })
- })
- }
- }
- const request = new Request()
- request.baseUrl = process.env.BASE_API
- request.config = {
- retry: 3,
- retryDelay: 1000,
- timeout: 50000,
- }
- request.beforeRequest = _request => {
- _request.header['Language-Set'] = getLocale()
- _request.header['X-Requested-With'] = 'XMLHttpRequest'
- const apiToken = getToken()
- if (apiToken) {
- _request.header.token = `${apiToken}`
- }
- }
- request.afterRequest = response => {
- if (process.env.NODE_ENV !== 'development') {
- console.log("返回数据", response)
- }
- if (response.header && response.header['authorization']) {
-
- const token = response.header['authorization'].replace('Bearer ', '')
- if (token !== store.getters.token) {
- store.commit('SET_TOKEN', token)
- }
- }
- const res = response.data
- if (res.code !== 0) {
- if (res.code === 401) {
- if (res.message === 'Token has expired') {
- res.message = '登录超时'
- }
- store.dispatch('fedLogOut').then(() => {
- uni.navigateTo({
- url: '/pages/user/login'
- })
- })
- } else {
- uni.showModal({
- content: res.message || 'Error',
- success(r) {
- if (r.confirm) {
- } else if (r.cancel) {
- }
- }
- })
- }
- return Promise.reject(new Error(res.message || 'Error'))
- } else {
- return res
- }
- }
- export default request
- export const get = (url) => (params,baseUrl=null) => {
- if (baseUrl) {
- request.baseUrl = baseUrl
- }
- return request.get(url, params)
- }
- export const post = (url) => (params,baseUrl=null) => {
- if (baseUrl) {
- request.baseUrl = baseUrl
- }
- return request.post(url, params)
- }
- export const link = (url) => {
- return process.env.BASE_API + url
- }
- export const upload = (url) => (params) => {
- return new Promise((resolve, reject) => {
- uni.uploadFile({
- url: process.env.BASE_API + url,
- filePath: params.filePath,
- name: params.name || 'file',
- header: {
- 'X-Requested-With': 'XMLHttpRequest',
- 'token': getToken(),
- 'Content-Type': 'application/json;charset=UTF-8'
- },
- success(res) {
- if (res.data) {
- try {
- res.data = JSON.parse(res.data)
- resolve(res.data)
- return
- } catch(e) {}
- }
- uni.showToast({
- icon: 'error',
- title: '上传文件失败'
- })
- },
- fail(error) {
- uni.showToast({
- icon: 'none',
- title: error.message
- })
- reject(error)
- }
- })
- })
- }
|