cookie.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. var setupDefaults = require('./setupDefaults')
  2. var staticDocument = require('./staticDocument')
  3. var staticDecodeURIComponent = require('./staticDecodeURIComponent')
  4. var staticEncodeURIComponent = require('./staticEncodeURIComponent')
  5. var isArray = require('./isArray')
  6. var isObject = require('./isObject')
  7. var isDate = require('./isDate')
  8. var isUndefined = require('./isUndefined')
  9. var includes = require('./includes')
  10. var keys = require('./keys')
  11. var assign = require('./assign')
  12. var arrayEach = require('./arrayEach')
  13. var helperNewDate = require('./helperNewDate')
  14. var helperGetDateTime = require('./helperGetDateTime')
  15. var getWhatYear = require('./getWhatYear')
  16. var getWhatMonth = require('./getWhatMonth')
  17. var getWhatDay = require('./getWhatDay')
  18. function toCookieUnitTime (unit, expires) {
  19. var num = parseFloat(expires)
  20. var nowdate = helperNewDate()
  21. var time = helperGetDateTime(nowdate)
  22. switch (unit) {
  23. case 'y': return helperGetDateTime(getWhatYear(nowdate, num))
  24. case 'M': return helperGetDateTime(getWhatMonth(nowdate, num))
  25. case 'd': return helperGetDateTime(getWhatDay(nowdate, num))
  26. case 'h':
  27. case 'H': return time + num * 60 * 60 * 1000
  28. case 'm': return time + num * 60 * 1000
  29. case 's': return time + num * 1000
  30. }
  31. return time
  32. }
  33. function toCookieUTCString (date) {
  34. return (isDate(date) ? date : new Date(date)).toUTCString()
  35. }
  36. /**
  37. * cookie操作函数
  38. * @param {String/Array/Object} name 键/数组/对象
  39. * @param {String} value 值
  40. * @param {Object} options 参数
  41. * @param {String} name: 键
  42. * @param {Object} value: 值
  43. * @param {String} path: 路径
  44. * @param {String} domain: 作用域
  45. * @param {Boolean} secure: 设置为安全的,只能用https协议
  46. * @param {Number} expires: 过期时间,可以指定日期或者字符串,默认天
  47. */
  48. function cookie (name, value, options) {
  49. if (staticDocument) {
  50. var opts, expires, values, result, cookies, keyIndex
  51. var inserts = []
  52. var args = arguments
  53. if (isArray(name)) {
  54. inserts = name
  55. } else if (args.length > 1) {
  56. inserts = [assign({ name: name, value: value }, options)]
  57. } else if (isObject(name)) {
  58. inserts = [name]
  59. }
  60. if (inserts.length > 0) {
  61. arrayEach(inserts, function (obj) {
  62. opts = assign({}, setupDefaults.cookies, obj)
  63. values = []
  64. if (opts.name) {
  65. expires = opts.expires
  66. values.push(staticEncodeURIComponent(opts.name) + '=' + staticEncodeURIComponent(isObject(opts.value) ? JSON.stringify(opts.value) : opts.value))
  67. if (expires) {
  68. if (isNaN(expires)) {
  69. // UTCString || Unit
  70. expires = expires.replace(/^([0-9]+)(y|M|d|H|h|m|s)$/, function (text, num, unit) {
  71. return toCookieUTCString(toCookieUnitTime(unit, num))
  72. })
  73. } else if (/^[0-9]{11,13}$/.test(expires) || isDate(expires)) {
  74. // Date || now
  75. expires = toCookieUTCString(expires)
  76. } else {
  77. // day
  78. expires = toCookieUTCString(toCookieUnitTime('d', expires))
  79. }
  80. opts.expires = expires
  81. }
  82. arrayEach(['expires', 'path', 'domain', 'secure'], function (key) {
  83. if (!isUndefined(opts[key])) {
  84. values.push(opts[key] && key === 'secure' ? key : (key + '=' + opts[key]))
  85. }
  86. })
  87. }
  88. staticDocument.cookie = values.join('; ')
  89. })
  90. return true
  91. } else {
  92. result = {}
  93. cookies = staticDocument.cookie
  94. if (cookies) {
  95. arrayEach(cookies.split('; '), function (val) {
  96. keyIndex = val.indexOf('=')
  97. result[staticDecodeURIComponent(val.substring(0, keyIndex))] = staticDecodeURIComponent(val.substring(keyIndex + 1) || '')
  98. })
  99. }
  100. return args.length === 1 ? result[name] : result
  101. }
  102. }
  103. return false
  104. }
  105. function hasCookieItem (key) {
  106. return includes(cookieKeys(), key)
  107. }
  108. function getCookieItem (name, key) {
  109. return cookie(name, key)
  110. }
  111. function setCookieItem (name, key, options) {
  112. cookie(name, key, options)
  113. return cookie
  114. }
  115. function removeCookieItem (name, options) {
  116. cookie(name, 0, assign({ expires: -1 }, setupDefaults.cookies, options))
  117. }
  118. function cookieKeys () {
  119. return keys(cookie())
  120. }
  121. assign(cookie, {
  122. has: hasCookieItem,
  123. set: setCookieItem,
  124. setItem: setCookieItem,
  125. get: getCookieItem,
  126. getItem: cookie,
  127. remove: removeCookieItem,
  128. removeItem: removeCookieItem,
  129. keys: cookieKeys,
  130. getJSON: cookie
  131. })
  132. module.exports = cookie