2021年12月4日星期六

util.js

import {toast} from "react-toastify";
import Router from "next/router";
import {getPathSearchParamObj} from "./window";
import $ from 'jquery'

export const getQuery = (name) => {
    let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    let r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURI(r[2]);
    return null;
}
export const isEmpty = (value) => {
    return value === '' || value === undefined
}

export const successTime = 2000
export const warningTime = 5000
export const errorTime = 5000
export const infoTime = 2000
export const resCode = (code, msg, tid) => {
    if (isEmpty(tid)) {
        switch (code) {
            case 3: // error
                toast.error(msg, {autoClose: errorTime})
                return true
            case 2: // warning
                toast.warning(msg, {autoClose: warningTime})
                return true
            case 1: // info
                toast.info(msg, {autoClose: infoTime})
                return false
            case 0:
                toast.success(msg, {autoClose: successTime})
                return false
            case -1:
                toast.error(msg, {autoClose: errorTime})
                return true
            case -2:
                toast.warning(msg, {autoClose: warningTime})
                Router.push('/login')
                return true
        }
    } else {
        switch (code) {
            case 3: // error
                toast.update(tid, {autoClose: errorTime, render: msg, type: "error", isLoading: false})
                return true
            case 2: // warning
                toast.update(tid, {autoClose: warningTime, render: msg, type: 'warning', isLoading: false})
                return true
            case 1: // info
                toast.update(tid, {autoClose: infoTime, render: msg, type: 'info', isLoading: false})
                return false
            case 0:
                toast.update(tid, {autoClose: successTime, render: msg, type: 'success', isLoading: false})
                return false
            case -1:
                toast.update(tid, {autoClose: errorTime, render: msg, type: 'error', isLoading: false})
                return true
            case -2:
                toast.update(tid, {autoClose: warningTime, render: msg, type: 'warning', isLoading: false})
                Router.push('/login')
                return true
        }
    }
}

export const refresh = () => {
    Router.push(location.pathname + '?' + $.param(getPathSearchParamObj()))
}

没有评论:

发表评论

login.js