import { showNotify } from 'vant';

/**
 * 请求错误
 * @param {} err 
 */
export function requestFailed(err) {
    console.error(err);
    const errorMessage = ((err.response || {}).data || {}).message || '请求出现错误，请稍后再试';
    showNotify({
        type: 'danger',
        message: `错误: ${errorMessage}`,
        duration: 3000,
    });
}

/**
 * 请求正常
 * @param {} response 
 */
export function requestSuccess(response) {
    if (response.errorCode !== 0) {
        showNotify({
            type: 'danger',
            message: `错误: ${response.errorMessage}`,
            duration: 3000,
        });
    }
}

/**
 * 错误提示
 * @param {} response 
 */
export function notificationError(message, title) {
    showNotify({
        type: 'danger',
        message: title ? `${title}: ${message}` : message,
        duration: 3000,
    });
}


/**
 * 成功提示
 * @param {} response 
 */
export function notificationSuccess(message, title) {
    showNotify({
        type: 'success',
        message: title ? `${title}: ${message}` : message,
        duration: 3000,
    });
}

// 获取设备ID
export const getQueryString = () => {
    let urlStr = window.location.href.split('?')[1];
    const urlSearchParams = new URLSearchParams(urlStr);
    const result = Object.fromEntries(urlSearchParams.entries());
    return result;
};