vue-Cookies.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * Vue Cookies v1.7.0
  3. * https://github.com/cmp-cc/vue-cookies
  4. *
  5. * Copyright 2016, cmp-cc
  6. * Released under the MIT license
  7. */
  8. (function() {
  9. var defaultConfig = {
  10. expires : '1d',
  11. path : '; path=/',
  12. domain:'',
  13. secure:'',
  14. sameSite:''
  15. }
  16. var VueCookies = {
  17. // install of Vue
  18. install: function(Vue) {
  19. Vue.prototype.$cookies = this
  20. Vue.$cookies = this
  21. },
  22. config : function(expireTimes,path,domain,secure,sameSite) {
  23. defaultConfig.expires = expireTimes ? expireTimes : '1d';
  24. defaultConfig.path = path ? '; path=' + path : '; path=/';
  25. defaultConfig.domain = domain ? '; domain=' + domain : '';
  26. defaultConfig.secure = secure ? '; Secure' : '';
  27. defaultConfig.sameSite = sameSite ? '; SameSite=' + sameSite : '';
  28. },
  29. get: function(key) {
  30. var value = decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(key).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null
  31. if(value && value.substring(0,1) === "{" && value.substring(value.length-1,value.length) === "}") {
  32. try {
  33. value = JSON.parse(value)
  34. }catch (e) {
  35. return value;
  36. }
  37. }
  38. return value;
  39. },
  40. set: function(key, value, expireTimes, path, domain, secure, sameSite) {
  41. if (!key) {
  42. throw new Error("Cookie name is not find in first argument.")
  43. }else if(/^(?:expires|max\-age|path|domain|secure|SameSite)$/i.test(key)){
  44. throw new Error("Cookie key name illegality, Cannot be set to ['expires','max-age','path','domain','secure','SameSite']\t current key name: " + key);
  45. }
  46. // support json object
  47. if(value && value.constructor === Object) {
  48. value = JSON.stringify(value);
  49. }
  50. var _expires = "";
  51. expireTimes = expireTimes === undefined ? defaultConfig.expires : expireTimes;
  52. if (expireTimes && expireTimes != 0) {
  53. switch (expireTimes.constructor) {
  54. case Number:
  55. if(expireTimes === Infinity || expireTimes === -1) _expires = "; expires=Fri, 31 Dec 9999 23:59:59 GMT";
  56. else _expires = "; max-age=" + expireTimes;
  57. break;
  58. case String:
  59. if (/^(?:\d{1,}(y|m|d|h|min|s))$/i.test(expireTimes)) {
  60. // get capture number group
  61. var _expireTime = expireTimes.replace(/^(\d{1,})(?:y|m|d|h|min|s)$/i, "$1");
  62. // get capture type group , to lower case
  63. switch (expireTimes.replace(/^(?:\d{1,})(y|m|d|h|min|s)$/i, "$1").toLowerCase()) {
  64. // Frequency sorting
  65. case 'm': _expires = "; max-age=" + +_expireTime * 2592000; break; // 60 * 60 * 24 * 30
  66. case 'd': _expires = "; max-age=" + +_expireTime * 86400; break; // 60 * 60 * 24
  67. case 'h': _expires = "; max-age=" + +_expireTime * 3600; break; // 60 * 60
  68. case 'min': _expires = "; max-age=" + +_expireTime * 60; break; // 60
  69. case 's': _expires = "; max-age=" + _expireTime; break;
  70. case 'y': _expires = "; max-age=" + +_expireTime * 31104000; break; // 60 * 60 * 24 * 30 * 12
  71. default: new Error("unknown exception of 'set operation'");
  72. }
  73. } else {
  74. _expires = "; expires=" + expireTimes;
  75. }
  76. break;
  77. case Date:
  78. _expires = "; expires=" + expireTimes.toUTCString();
  79. break;
  80. }
  81. }
  82. document.cookie =
  83. encodeURIComponent(key) + "=" + encodeURIComponent(value) +
  84. _expires +
  85. (domain ? "; domain=" + domain : defaultConfig.domain) +
  86. (path ? "; path=" + path : defaultConfig.path) +
  87. (secure === undefined ? defaultConfig.secure : secure ? "; Secure" : "") +
  88. (sameSite === undefined ? defaultConfig.sameSite : (sameSite ? "; SameSite=" + sameSite : ""));
  89. return this;
  90. },
  91. remove: function(key, path, domain) {
  92. if (!key || !this.isKey(key)) {
  93. return false;
  94. }
  95. document.cookie = encodeURIComponent(key) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (domain ? "; domain=" + domain : defaultConfig.domain) + (path ? "; path=" + path : defaultConfig.path);
  96. return this;
  97. },
  98. isKey: function(key) {
  99. return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(key).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
  100. },
  101. keys: function() {
  102. if(!document.cookie) return [];
  103. var _keys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
  104. for (var _index = 0; _index < _keys.length; _index++) {
  105. _keys[_index] = decodeURIComponent(_keys[_index]);
  106. }
  107. return _keys;
  108. }
  109. }
  110. if (typeof exports == "object") {
  111. module.exports = VueCookies;
  112. } else if (typeof define == "function" && define.amd) {
  113. define([], function() {
  114. return VueCookies;
  115. })
  116. } else if (window.Vue) {
  117. Vue.use(VueCookies);
  118. }
  119. // vue-cookies can exist independently,no dependencies library
  120. if(typeof window!=="undefined"){
  121. window.$cookies = VueCookies;
  122. }
  123. })()