charleslo1 / weapp-cookie

一行代码让微信、头条、百度、支付宝小程序支持 cookie,兼容 uni-app 🍪🚀 One line of code allows weapp to support cookie(wx weixin wxapp cookie)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

set-cookie max-age:0 不起作用

lanyaosmy opened this issue · comments

Cookie构造函数,在max-age为0时会赋值为null

constructor (props) {
    this.name = props.name || ''
    this.value = props.value || ''
    // other
    this.domain = props.domain || ''
    this.path = props.path || '/'
    this.expires = props.expires ? new Date(props.expires) : null
    // 这一行在max-age为0时会设为null
    this.maxAge = props.maxAge ? parseInt(props.maxAge) : null
    this.httpOnly = !!props.httpOnly
    // 记录时间
    this.dateTime = props.dateTime ? new Date(props.dateTime) : new Date()
  }

所以在判断是否过期的方法中,会返回默认false,导致cookie还会携带

     /**
   * 验证 cookie 是否还有效
   * @return {Boolean} 是否有效
   */
  isExpired () {
    // maxAge 为 0,无效
    if (this.maxAge === 0) {
      
      return true
    }
    // 存活秒数超出 maxAge,无效
    if (this.maxAge > 0) {
      let seconds = (Date.now() - this.dateTime.getTime()) / 1000
      return seconds > this.maxAge
    }
    // expires 小于当前时间,无效
    if (this.expires && this.expires < new Date()) {
      return true
    }
    return false
  }

代码问题
this.maxAge = props.maxAge ? Number(props.maxAge) : props.maxAge === 0 ? 0 : null;