import Taro, { clearStorage } from '@tarojs/taro'; import { CommonRes, CommonResNew } from '../services/common'; import { setGlobalData, getGlobalData } from './global-data'; import { BASE_URL, BASE_URL_ZQ } from './request-enhance'; import { showModal } from './wechat-ui'; type Method = | 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT'; const request = ( url: string, data?: unknown, method: Method = 'POST', headers = {}, isShowLoading = true ): Promise => { const option = { url, data, method, header: { Authorization: Taro.getStorageSync('authorization'), ...headers, }, }; if (isShowLoading) { Taro.showLoading({ title: '加载中..' }); } return Taro.request(option) .then(({ statusCode, data }: { statusCode: number; data: CommonRes }) => { console.log('response data', data); if (isShowLoading) { Taro.hideLoading(); } if (statusCode >= 200 && statusCode < 300) { if (data.code === 1000) { return data.data || '请求成功'; // throw new Error('故意的'); } else if (data.code === 1002) { clearStorage(); if (!getGlobalData('isNavToLogin')) { setGlobalData('isNavToLogin', true); showModal( '提示', '凭证过期需要重新登录以访问更多功能,是否需要登录?' ).then((res) => { if (res.confirm) { Taro.navigateTo({ url: '/pages/login/login' }).then(() => { setTimeout(() => { setGlobalData('isNavToLogin', false); }, 3000); }); } }); } throw new Error(data.msg); } else { throw new Error(data.msg); } } else { const msg = `Error: code ${statusCode}`; throw new Error(msg); } }) .catch((error) => { if (isShowLoading) { Taro.hideLoading(); } Taro.showToast({ title: error.message || 'error(─‿─)1', icon: 'none', duration: 1500, mask: true, }); return null; }); }; const requestNew = ( url: string, data?: unknown, method: Method = 'POST', headers = {'Content-Type':'application/x-www-form-urlencoded'}, isShowLoading = true ): Promise => { const option = { url, data, method, header: { Authorization: Taro.getStorageSync('authorization'), ...headers, }, }; if (isShowLoading) { Taro.showLoading({ title: '加载中..' }); } return Taro.request(option) .then(({ statusCode, data }: { statusCode: number; data: CommonResNew }) => { console.log('response data', data); if (isShowLoading) { Taro.hideLoading(); } if (statusCode >= 200 && statusCode < 300) { if (data.code === 1000) { return data || '请求成功'; // throw new Error('故意的'); } else if (data.code === 1002) { clearStorage(); if (!getGlobalData('isNavToLogin')) { setGlobalData('isNavToLogin', true); showModal( '提示', '凭证过期需要重新登录以访问更多功能,是否需要登录?' ).then((res) => { if (res.confirm) { Taro.navigateTo({ url: '/pages/login/login' }).then(() => { setTimeout(() => { setGlobalData('isNavToLogin', false); }, 3000); }); } }); } throw new Error(data.msg); } else { throw new Error(data.msg); } } else { const msg = `Error: code ${statusCode}`; throw new Error(msg); } }) .catch((error) => { if (isShowLoading) { Taro.hideLoading(); } Taro.showToast({ title: error.message || 'error(─‿─)2', icon: 'none', duration: 1500, mask: true, }); return null; }); }; export const RzRequest = { get(url = '/', data = {}, headers = {}): Promise { return request(BASE_URL + url, data, 'GET', headers); }, post(url = '/', data = {}, isShowLoading = true): Promise { return request(BASE_URL + url, data, 'POST', undefined, isShowLoading); }, postNew(url = '/', data = {}, isShowLoading = true) { let baseUrl = BASE_URL; if (url.includes('ZhuQue')) { baseUrl = BASE_URL_ZQ; } return requestNew(baseUrl + url, data, 'POST', undefined, isShowLoading); }, getNew(url = '/', data = {}, isShowLoading = true) { let baseUrl = BASE_URL; if (url.includes('ZhuQue')) { baseUrl = BASE_URL_ZQ; } return requestNew(baseUrl + url, data, 'GET', undefined, isShowLoading); }, put(url = '/', data = {}): Promise { return request(BASE_URL + url, data, 'PUT'); }, delete(url = '/', data = {}): Promise { return request(BASE_URL + url, data, 'DELETE'); }, };