token.js 944 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. * @Author: your name
  3. * @Date: 2020-05-28 10:28:16
  4. * @LastEditTime: 2020-05-29 11:15:12
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: \admin-frame\public\worker\worker.js
  8. */
  9. let time;
  10. onmessage = function(e) {
  11. const data = e.data;
  12. time = e.data.time / 1000 - 6;
  13. setInterval(jwtTime, 6000, data);
  14. };
  15. function jwtTime({ token }) {
  16. var base64Url = token.split('.')[1];
  17. var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
  18. var jsonPayload = decodeURIComponent(
  19. atob(base64)
  20. .split('')
  21. .map(function(c) {
  22. return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
  23. })
  24. .join('')
  25. );
  26. const exp = JSON.parse(jsonPayload).exp;
  27. time = Number(time + 6);
  28. if (time > exp) {
  29. postMessage({
  30. errcode: -1,
  31. errmsg: '登录过期',
  32. });
  33. } else {
  34. postMessage({
  35. errcode: 0,
  36. errmsg: '未过期',
  37. });
  38. }
  39. }