common.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import constant from './constant'
  2. const vKey = location.host
  3. export default {
  4. hashCode: function (str) {
  5. let hash = 0
  6. if (str.length === 0) return hash
  7. for (let i = 0; i < str.length; i++) {
  8. const char = str.charCodeAt(i)
  9. hash = (hash << 5) - hash + char
  10. hash = hash & hash // Convert to 32bit integer
  11. }
  12. return hash
  13. },
  14. uuid: function (len, radix) {
  15. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('')
  16. var uuid = []
  17. var i
  18. radix = radix || chars.length
  19. if (len) {
  20. // Compact form
  21. for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)]
  22. } else {
  23. // rfc4122, version 4 form
  24. var r
  25. // rfc4122 requires these characters
  26. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-'
  27. uuid[14] = '4'
  28. // Fill in random data. At i==19 set the high bits of clock sequence as
  29. // per rfc4122, sec. 4.1.5
  30. for (i = 0; i < 36; i++) {
  31. if (!uuid[i]) {
  32. r = 0 | (Math.random() * 16)
  33. uuid[i] = chars[i === 19 ? (r & 0x3) | 0x8 : r]
  34. }
  35. }
  36. }
  37. return uuid.join('')
  38. },
  39. setUser: function (value) {
  40. localStorage.setItem(this.UserKey(), JSON.stringify(value))
  41. },
  42. currUser: function () {
  43. const userKey = localStorage.getItem(this.UserKey())
  44. if (userKey) {
  45. const user = JSON.parse(userKey || '')
  46. // console.log('curr user:', user)
  47. if (user) {
  48. return user
  49. }
  50. }
  51. // this.logout()
  52. return null
  53. },
  54. uid: function () {
  55. return this.currUser() ? this.currUser().id : null
  56. },
  57. isAdmin: function () {
  58. return this.uid() === '1'
  59. },
  60. setBiz: function (value) {
  61. localStorage.setItem(this.BizKey(), JSON.stringify(value))
  62. },
  63. currBiz: function () {
  64. return JSON.parse(localStorage.getItem(this.BizKey()) || null)
  65. },
  66. bid: function () {
  67. return this.currBiz() ? this.currBiz().id : null
  68. },
  69. setUserType: function (value) {
  70. localStorage.setItem(vKey + '_' + constant.KEY_USER_TYPE, value)
  71. },
  72. currUserType: function () {
  73. return localStorage.getItem(vKey + '_' + constant.KEY_USER_TYPE) || '1'
  74. },
  75. setCode: function (value) {
  76. localStorage.setItem(constant.KEY_CODE, value)
  77. },
  78. currCode: function () {
  79. return localStorage.getItem(constant.KEY_CODE) || null
  80. },
  81. setMenu: function (value) {
  82. localStorage.setItem(this.MenuKey(), JSON.stringify(value))
  83. },
  84. currMenu: function () {
  85. const menuVal = this.getMenuVal()
  86. if (menuVal && menuVal !== 'undefined') {
  87. return JSON.parse(menuVal)
  88. } else {
  89. return []
  90. }
  91. },
  92. getMenuVal: function () {
  93. return localStorage.getItem(this.MenuKey())
  94. },
  95. removeStorage: function (_key) {
  96. localStorage.removeItem(_key)
  97. },
  98. // ================================ Common Key ==============================================
  99. // UserKey: function() {
  100. // return window.location.host + '_' + constant.KEY_USER + this.currUserType()
  101. // },
  102. //
  103. // MenuKey: function() {
  104. // return window.location.host + '_' + constant.KEY_USER + this.currUserType() + '_' + constant.KEY_USER_MENU
  105. // },
  106. UserKey: function () {
  107. return this.hashCode(window.location.host + '_' + constant.KEY_USER + this.currUserType())
  108. },
  109. BizKey: function () {
  110. return this.hashCode(window.location.host + '_' + constant.KEY_BIZ + this.currUserType())
  111. },
  112. MenuKey: function () {
  113. return this.hashCode(
  114. window.location.host +
  115. '_' +
  116. constant.KEY_USER +
  117. this.currUserType() +
  118. '_' +
  119. constant.KEY_USER_MENU
  120. )
  121. },
  122. replaceFileUrl: function (urlPath) {
  123. if (urlPath != null && urlPath !== '') {
  124. urlPath = urlPath.replaceAll(
  125. '/server/FileController/download/',
  126. constant.BASE_URI + '/FileController/download/'
  127. )
  128. }
  129. return urlPath
  130. },
  131. formatDate: function (fmt, date) {
  132. // author: meizz
  133. var o = {
  134. 'M+': date.getMonth() + 1, // 月份
  135. 'd+': date.getDate(), // 日
  136. 'h+': date.getHours(), // 小时
  137. 'm+': date.getMinutes(), // 分
  138. 's+': date.getSeconds(), // 秒
  139. 'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
  140. S: date.getMilliseconds() // 毫秒
  141. }
  142. if (/(y+)/.test(fmt)) {
  143. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  144. }
  145. for (var k in o) {
  146. if (new RegExp('(' + k + ')').test(fmt)) {
  147. fmt = fmt.replace(
  148. RegExp.$1,
  149. RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
  150. )
  151. }
  152. }
  153. return fmt
  154. }
  155. }