12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import Storage from './../store/storage.js';
- const ENV = require('./../.env.js')
- const HttpCodes = {
- UNAUTHORIZED: 401, //登录失效
- }
- class request {
- static request(method, url, data = null, that = null) {
- let promise = new Promise(function(resolve, reject) {
- let _url
- if (process.env.NODE_ENV !== 'production') {
- _url = ENV.APP_DEV_URL + url
- } else {
- _url = ENV.APP_PROD_URL + url
- }
- const param = {
- url: _url,
- method: method,
- data: data,
- header: {
- 'Authorization': 'Bearer ' + Storage.getToken(),
- 'Content-Type': 'application/json',
- },
- success(res) {
- if (res.statusCode === 200) {
- if (res.data.code === 200) {
- resolve(res.data)
- } else if (res.data.code == 401) {
- uni.navigateTo({
- url: '/pages/login'
- })
- } else if (res.data.code == 6401) {
- uni.request({
- url: ENV.APP_DEV_URL + '/api/applet/refresh',
- method: 'GET',
- success: (res) => {
- console.log(res, 135)
- Storage.removeToken()
- }
- })
- } else {
- resolve(res)
- }
- } else {
- resolve(res)
- }
- },
- fail(res) {
- resolve(res)
- }
- }
- uni.request(param)
- }).catch((res) => {
- if (res.statusCode === 200) {
- if (res.data.code !== 200) {
- return res.data
- }
- } else {
- console.log('服务器错误:', res)
- return res;
- }
- })
- return promise
- }
- static get(url, data, that) {
- return this.request('GET', url, data, that)
- }
- static post(url, data, that) {
- return this.request('POST', url, data, that)
- }
- static put(url, data, that) {
- return this.request('PUT', url, data, that)
- }
- static delete(url, data, that) {
- return this.request('DELETE', url, data, that)
- }
- }
- export default request
|