当前位置:   article > 正文

layui引入cookie_layui cookie

layui cookie

jquery-cookie文件包下载地址:链接:https://pan.baidu.com/s/1ZCIFVBtuxREeMgL8hlzUHQ   提取码:abcd 

1.思路:

  1. layui.define('jquery', function (exports) {   
  2. //依赖jQuery   
  3. var jQuery = layui.jquery;  
  4. //插件内容  
  5. //将jquery.cookie.js文件的内容复制到此处 ,参加信息化项目jquery-cookie文件夹下的cookie.js文件/root/assets/module/jquery-cookie/src/cookie  
  6. exports('jquery.cookie', null); //'jquery.cookie文件名随意取名' 
  7. });  
  8. //使用方法  
  9. layui.use('jquery.cookie', function () {}); 

2.引入代码

  1. //引入cookie
  2. layui.use(['layer', 'form', 'jquery'], function () {
  3. var layer = layui.layer,
  4. $ = layui.jquery,
  5. form = layui.form;
  6. //这样就可以正常使用jquery了
  7. $("#btn").click(function () {
  8. layer.msg('hello world')
  9. });
  10. });
  11. layui.define(["jquery"], function (exports) {
  12. let jQuery = layui.jquery;
  13. (function ($) {
  14. /*!
  15. * jQuery Cookie Plugin v1.4.1
  16. * https://github.com/carhartl/jquery-cookie
  17. *
  18. * Copyright 2013 Klaus Hartl
  19. * Released under the MIT license
  20. */
  21. (function (factory) {
  22. if (typeof define === 'function' && define.amd) {
  23. // AMD
  24. define(['jquery'], factory);
  25. } else if (typeof exports === 'object') {
  26. // CommonJS
  27. factory(require('jquery'));
  28. } else {
  29. // Browser globals
  30. factory(jQuery);
  31. }
  32. }(function ($) {
  33. var pluses = /\+/g;
  34. function encode(s) {
  35. return config.raw ? s : encodeURIComponent(s);
  36. }
  37. function decode(s) {
  38. return config.raw ? s : decodeURIComponent(s);
  39. }
  40. function stringifyCookieValue(value) {
  41. return encode(config.json ? JSON.stringify(value) : String(value));
  42. }
  43. function parseCookieValue(s) {
  44. if (s.indexOf('"') === 0) {
  45. // This is a quoted cookie as according to RFC2068, unescape...
  46. s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
  47. }
  48. try {
  49. // Replace server-side written pluses with spaces.
  50. // If we can't decode the cookie, ignore it, it's unusable.
  51. // If we can't parse the cookie, ignore it, it's unusable.
  52. s = decodeURIComponent(s.replace(pluses, ' '));
  53. return config.json ? JSON.parse(s) : s;
  54. } catch (e) {
  55. }
  56. }
  57. function read(s, converter) {
  58. var value = config.raw ? s : parseCookieValue(s);
  59. return $.isFunction(converter) ? converter(value) : value;
  60. }
  61. var config = $.cookie = function (key, value, options) {
  62. // Write
  63. if (value !== undefined && !$.isFunction(value)) {
  64. options = $.extend({}, config.defaults, options);
  65. if (typeof options.expires === 'number') {
  66. var days = options.expires, t = options.expires = new Date();
  67. t.setTime(+t + days * 864e+5);
  68. }
  69. return (document.cookie = [
  70. encode(key), '=', stringifyCookieValue(value),
  71. options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
  72. options.path ? '; path=' + options.path : '',
  73. options.domain ? '; domain=' + options.domain : '',
  74. options.secure ? '; secure' : ''
  75. ].join(''));
  76. }
  77. // Read
  78. var result = key ? undefined : {};
  79. // To prevent the for loop in the first place assign an empty array
  80. // in case there are no cookies at all. Also prevents odd result when
  81. // calling $.cookie().
  82. var cookies = document.cookie ? document.cookie.split('; ') : [];
  83. for (var i = 0, l = cookies.length; i < l; i++) {
  84. var parts = cookies[i].split('=');
  85. var name = decode(parts.shift());
  86. var cookie = parts.join('=');
  87. if (key && key === name) {
  88. // If second argument (value) is a function it's a converter...
  89. result = read(cookie, value);
  90. break;
  91. }
  92. // Prevent storing a cookie that we couldn't decode.
  93. if (!key && (cookie = read(cookie)) !== undefined) {
  94. result[name] = cookie;
  95. }
  96. }
  97. return result;
  98. };
  99. config.defaults = {};
  100. $.removeCookie = function (key, options) {
  101. if ($.cookie(key) === undefined) {
  102. return false;
  103. }
  104. // Must not alter options, thus extending a fresh object...
  105. $.cookie(key, '', $.extend({}, options, {expires: -1}));
  106. return !$.cookie(key);
  107. };
  108. }));
  109. })(jQuery);
  110. exports('cookie', null);
  111. });

3.在服务端把数据写入cookie 

  1. //用户角色
  2. List<UserRoleVo> userRoleList = authorizationService.getUserRoles(userId);
  3. String canSee = "";
  4. for (UserRoleVo userRoleVo: userRoleList){
  5. if ("项目群管理员".equals(userRoleVo.getName())){
  6. canSee = "true";
  7. }
  8. }
  9. //创建Cookie
  10. Cookie cookie = new Cookie("canSee", canSee);
  11. //设置Cookie的最大生命周期,否则浏览器关闭后Cookie即失效
  12. cookie.setMaxAge(Integer.MAX_VALUE);
  13. //将Cookie加到response中
  14. response.addCookie(cookie);

4.在当前页使用 

  1. layui.define(function (exports) {
  2. layui.extend({ //设定模块别名
  3. cookie: '{/}/root/assets/module/jquery-cookie/src/cookie',
  4. admin: '{/}/root/assets/module/lib/admin'
  5. }).use(['admin', 'jquery', 'carousel', 'layer', 'form', 'cookie', 'element', 'table', 'laydate'], function () {
  6. var $ = layui.jquery
  7. , admin = layui.admin
  8. , carousel = layui.carousel
  9. , layer = layui.layer
  10. , form = layui.form
  11. , cookie = layui.cookie
  12. , element = layui.element
  13. , jquery = layui.jquery
  14. , laydate = layui.laydate
  15. , table = layui.table;
  16. var canSee = $.cookie('canSee');
  17. if (canSee != "true") {
  18. //页面初始化要执行的方法
  19. } else {
  20. //页面初始化要执行的方法
  21. }
  22. exports('console', {})
  23. });

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/149737
推荐阅读
相关标签
  

闽ICP备14008679号