/**
 * 设置cookie
 * @param name cookie的名称
 * @param value cookie的值
 * @param day cookie的过期时间
 */
function setCookie(name, value, day) {
  if(day !== 0){     //当设置的时间等于0时，不设置expires属性，cookie在浏览器关闭后删除
    var expires = day * 24 * 60 * 60 * 1000
    var date = new Date(+new Date()+expires)
    document.cookie = name + "=" + value + ";expires=" + date.toUTCString() + ';path=/'
    // document.cookie = name + "=" + value + ";expires=" + date.toUTCString()
  }else{
    document.cookie = name + "=" + value + ';path=/'
  }
}

/**
 * 获取对应名称的cookie
 * @param cookieName cookie的名称
 * @returns {null} 不存在时，返回null
 */
function getCookie(cookieName) {
  const strCookie = document.cookie
  const cookieList = strCookie.split(';')
  for(let i = 0; i < cookieList.length; i++) {
    const arr = cookieList[i].split('=')
    if (cookieName === arr[0].trim()) {
      return arr[1]
    }
  }
  return null
}

/**
 * 删除cookie
 * @param name cookie的名称
 */
function delCookie(name) {
  setCookie(name, ' ', -1)
}

export {
  setCookie, getCookie, delCookie
}