// 图片域名
let globalImageHost = ''
let localhostName = location.href.split('#')[0] // 获取锚点之前的链接
if (localhostName.indexOf('localhost') > -1) { //  开发环境
    globalImageHost = 'localhost'
} else if (localhostName.indexOf('https://gatewaytest.vschool100.com') > -1) { // 测试环境
    globalImageHost = 'http://test.vschool100.com:8888'
} else {
    globalImageHost = 'https://image.vschool100.cn'
}

/**
 *  格式化日期
 *  返回 类似 2019-03-18 格式的字符串
 * **/

let formatDate = (year, month, day) => {
    let y = year
    let m = month
    if (m < 10) m = '0' + m
    let d = day
    if (d < 10) d = '0' + d
    return y + '-' + m + '-' + d
}

/**
 * 获取星期
 * @param Date
 * @returns String
 */

let getWeek = (date = new Date()) => {
    const weekArr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
    return weekArr[date.getDay()]
}
// 日期格式化
// now 为时间 例如 new Date()
// type为1：YYYY-MM-DD； type为2： YYYY-MM-DD HH:mm
// txt为1：显示的格式为2019-10-01； txt为2：2019年10月01日
let currentTime = (now, type, txt) => {
    // let now = new Date()
    let year = now.getFullYear() // 年
    let month = now.getMonth() + 1 // 月
    let day = now.getDate() // 日
    let hh = now.getHours() // 时
    let mm = now.getMinutes() // 分
    let clock = txt === 1 ? year + '-' : year + '年'
    if (month < 10) {
        clock += '0'
    }
    clock += txt === 1 ? month + '-' : month + '月'
    if (day < 10) {
        clock += '0'
    }
    clock += txt === 1 ? day + ' ' : day + '日'
    if (type === 1) {
        return (clock)
    }
    if (hh < 10) {
        clock += '0'
    }
    clock += txt === 1 ? hh + ':' : hh + '时'
    if (mm < 10) clock += '0'
    clock += txt === 1 ? mm : mm + '秒'
    return (clock)
}

let exportHostName = () => {
    let [exportHostName, nodeEnv] = ['', process.env.NODE_ENV]
    if (nodeEnv === 'development') {
        exportHostName = 'localhost:8080'
    } else if (nodeEnv === 'testing') {
        exportHostName = 'https://test.vschool100.com'
    } else if (nodeEnv === 'production') {
        exportHostName = 'https://oa.vschool100.com'
    }
    return exportHostName
}

const formatParams = params => '?' + Object.keys(params).map(item => item + '=' + params[item]).join('&')

export default {
    globalImageHost,
    formatDate,
    getWeek,
    exportHostName,
    currentTime,
    formatParams
}
