1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- import http from './interface'
-
-
- export const $http = (url, method, data, json, isloading=true) => {
- //设置请求前拦截器
- http.interceptor.request = (config) => {
- if(isloading){
- uni.showLoading({
- title:'加载中...'
- })
- }
-
- config.header = {
- 'content-type': json ? 'application/json' : 'application/x-www-form-urlencoded',
- "token": uni.getStorageSync('token'),
- "userId": uni.getStorageSync('userInfo')?JSON.parse(uni.getStorageSync('userInfo')).id:'',
- }
- }
- //设置请求结束后拦截器
- http.interceptor.response = async (response) => {
- //判断返回状态 执行相应操作
- if(isloading){
- uni.hideLoading()
- }
-
- if (response?.data?.code === 401 || response?.data?.msg.indexOf('未授权') > -1 || response?.data?.msg.indexOf('重新登录') > -1) {
- return uni.showModal({
- title: '温馨提示',
- content:'当前登录已失效,是否返回重新登录',
- success: (res) => {
- if (res.confirm) {
- uni.clearStorageSync();
- uni.reLaunch({
- url: '/pages/login'
- })
- }
- }
- })
- }
-
- // 请根据后端规定的状态码判定
- if (response.data.code === 300) {//token失效
- // return response.data = await doRequest(response, url)//动态刷新token,并重新完成request请求
- }else{
- if(response.data.code==10021&&response.data.msg){
- uni.showToast({
- title:response.data.msg,
- icon:'none',
- duration:1500
- })
- }
- }
- return response;
- }
- return http.request({
- method: method,
- url: url,
- dataType: 'json',
- data,
- })
- }
- function postJson(url, data, isloading=true) {
- return $http(url, 'POST', data, isloading)
- }
- function get(url, data, isloading=true) {
-
- return $http(url, 'GET', data, true, isloading)
- }
- function post(url, data, isloading=true) {
- return $http(url, 'POST', data, true, isloading)
- }
- function put(url, data, isloading=true) {
- return $http(url, 'PUT', data, true, isloading)
- }
- function del(url, data, isloading=true) {
- return $http(url, 'DELETE', data, true, isloading)
- }
- export default {
- postJson,
- get,
- post,
- put,
- del
- }
|