auth.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import common from './common.js'
  2. export default {
  3. TYPE_FRAME: 'FRAME',
  4. TYPE_BIZ: 'BIZ',
  5. TYPE_USER: 'USER',
  6. TYPE_PERSON_BIZ: 'BIZ_PERSON',
  7. // ------------------------------ User -----------------------------------
  8. uid: function () {
  9. return this.currUser() ? this.currUser().id : null
  10. },
  11. setUser: function (value) {
  12. uni.setStorage({
  13. key: 'laocui_user_token',
  14. data: value,
  15. success: function () {}
  16. });
  17. },
  18. setUserInfo(value) {
  19. uni.setStorage({
  20. key: 'laocui_user_info',
  21. data: JSON.stringify(value),
  22. success: function () {}
  23. });
  24. },
  25. currUser: function () {
  26. return common.castEval(localStorage.getItem(this.userKey()) || '')
  27. },
  28. removeUser: function () {
  29. uni.removeStorage(this.userKey())
  30. },
  31. // ------------------------------ Biz -----------------------------------
  32. bid: function () {
  33. return this.currBiz() ? this.currBiz().id : null
  34. },
  35. setBiz: function (value) {
  36. sessionStorage.setItem(this.bizKey(), JSON.stringify(value))
  37. },
  38. removeBiz: function () {
  39. sessionStorage.removeItem(this.bizKey())
  40. },
  41. currBiz: function () {
  42. return common.castEval(sessionStorage.getItem(this.bizKey()) || null)
  43. },
  44. // ------------------------------ Token -----------------------------------
  45. setToken: function (token) {
  46. localStorage.setItem(this.tokenKey(), token)
  47. },
  48. getToken: function () {
  49. return localStorage.getItem(this.tokenKey()) || null
  50. },
  51. removeToken: function () {
  52. localStorage.removeItem(this.tokenKey())
  53. },
  54. // ------------------------------ key -----------------------------------
  55. // bizKey: function() {
  56. // const key = this.hashCode(window.location.host + '_biz_' + this.getUserType())
  57. // return key
  58. // },
  59. //
  60. // userKey: function() {
  61. // const key = this.hashCode(window.location.host + '_user_' + this.getUserType())
  62. // return key
  63. // },
  64. //
  65. // tokenKey: function() {
  66. // const key = this.hashCode(window.location.host + '_token_' + this.getUserType())
  67. // return key
  68. // },
  69. // Frame
  70. setLoginInfo: function (value) {
  71. localStorage.setItem('KEY_ADMIN_USER_LOGIN_INFO', JSON.stringify(value))
  72. },
  73. getLoginInfo: function () {
  74. return common.castEval(localStorage.getItem('KEY_ADMIN_USER_LOGIN_INFO') || null)
  75. },
  76. removeLoginInfo: function () {
  77. localStorage.removeItem('KEY_ADMIN_USER_LOGIN_INFO')
  78. },
  79. // Biz
  80. setBizLoginInfo: function (value) {
  81. localStorage.setItem('KEY_BIZ_USER_LOGIN_INFO', JSON.stringify(value))
  82. },
  83. getBizLoginInfo: function () {
  84. return common.castEval(localStorage.getItem('KEY_BIZ_USER_LOGIN_INFO') || null)
  85. },
  86. removeBizLoginInfo: function () {
  87. localStorage.removeItem('KEY_BIZ_USER_LOGIN_INFO')
  88. },
  89. setUserType: function (value) {
  90. localStorage.setItem('KEY_USER_TYPE', value)
  91. },
  92. getUserType: function () {
  93. return localStorage.getItem('KEY_USER_TYPE') || 'TYPE_FRAME'
  94. },
  95. hashCode: function (str) {
  96. let hash = 0
  97. if (str.length === 0) return hash
  98. for (let i = 0; i < str.length; i++) {
  99. const char = str.charCodeAt(i)
  100. hash = ((hash << 5) - hash) + char
  101. hash = hash & hash // Convert to 32bit integer
  102. }
  103. return hash
  104. },
  105. /*
  106. 定时缓存
  107. */
  108. setTimingLocalStorage: function (key, value, ttl_ms) {
  109. var data = {
  110. value: value,
  111. expirse: new Date(ttl_ms).getTime()
  112. }
  113. localStorage.setItem(key, JSON.stringify(data))
  114. },
  115. getTimingLocalStorage: function (key) {
  116. var data = JSON.parse(localStorage.getItem(key))
  117. if (data !== null) {
  118. if (data.expirse == null || (data.expirse != null && data.expirse < new Date().getTime())) {
  119. localStorage.removeItem(key)
  120. } else {
  121. return JSON.parse(data.value)
  122. }
  123. }
  124. return null
  125. }
  126. }