auth.js 4.3 KB

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