123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- import common from './common.js'
- export default {
- TYPE_FRAME: 'FRAME',
- TYPE_BIZ: 'BIZ',
- TYPE_USER: 'USER',
- TYPE_PERSON_BIZ: 'BIZ_PERSON',
- // ------------------------------ User -----------------------------------
- uid: function () {
- return this.currUser() ? this.currUser().id : null
- },
- setUser: function (value) {
- uni.setStorage({
- key: 'laocui_user_token',
- data: value,
- success: function () {}
- });
- },
- setUserInfo(value) {
- uni.setStorage({
- key: 'laocui_user_info',
- data: JSON.stringify(value),
- success: function () {}
- });
- },
- currUser: function () {
- return common.castEval(localStorage.getItem(this.userKey()) || '')
- },
- removeUser: function () {
- uni.removeStorage(this.userKey())
- },
- // ------------------------------ Biz -----------------------------------
- bid: function () {
- return this.currBiz() ? this.currBiz().id : null
- },
- setBiz: function (value) {
- sessionStorage.setItem(this.bizKey(), JSON.stringify(value))
- },
- removeBiz: function () {
- sessionStorage.removeItem(this.bizKey())
- },
- currBiz: function () {
- return common.castEval(sessionStorage.getItem(this.bizKey()) || null)
- },
- // ------------------------------ Token -----------------------------------
- setToken: function (token) {
- localStorage.setItem(this.tokenKey(), token)
- },
- getToken: function () {
- return localStorage.getItem(this.tokenKey()) || null
- },
- removeToken: function () {
- localStorage.removeItem(this.tokenKey())
- },
- // ------------------------------ key -----------------------------------
- // bizKey: function() {
- // const key = this.hashCode(window.location.host + '_biz_' + this.getUserType())
- // return key
- // },
- //
- // userKey: function() {
- // const key = this.hashCode(window.location.host + '_user_' + this.getUserType())
- // return key
- // },
- //
- // tokenKey: function() {
- // const key = this.hashCode(window.location.host + '_token_' + this.getUserType())
- // return key
- // },
- // Frame
- setLoginInfo: function (value) {
- localStorage.setItem('KEY_ADMIN_USER_LOGIN_INFO', JSON.stringify(value))
- },
- getLoginInfo: function () {
- return common.castEval(localStorage.getItem('KEY_ADMIN_USER_LOGIN_INFO') || null)
- },
- removeLoginInfo: function () {
- localStorage.removeItem('KEY_ADMIN_USER_LOGIN_INFO')
- },
- // Biz
- setBizLoginInfo: function (value) {
- localStorage.setItem('KEY_BIZ_USER_LOGIN_INFO', JSON.stringify(value))
- },
- getBizLoginInfo: function () {
- return common.castEval(localStorage.getItem('KEY_BIZ_USER_LOGIN_INFO') || null)
- },
- removeBizLoginInfo: function () {
- localStorage.removeItem('KEY_BIZ_USER_LOGIN_INFO')
- },
- setUserType: function (value) {
- localStorage.setItem('KEY_USER_TYPE', value)
- },
- getUserType: function () {
- return localStorage.getItem('KEY_USER_TYPE') || 'TYPE_FRAME'
- },
- hashCode: function (str) {
- let hash = 0
- if (str.length === 0) return hash
- for (let i = 0; i < str.length; i++) {
- const char = str.charCodeAt(i)
- hash = ((hash << 5) - hash) + char
- hash = hash & hash // Convert to 32bit integer
- }
- return hash
- },
- /*
- 定时缓存
- */
- setTimingLocalStorage: function (key, value, ttl_ms) {
- var data = {
- value: value,
- expirse: new Date(ttl_ms).getTime()
- }
- localStorage.setItem(key, JSON.stringify(data))
- },
- getTimingLocalStorage: function (key) {
- var data = JSON.parse(localStorage.getItem(key))
- if (data !== null) {
- if (data.expirse == null || (data.expirse != null && data.expirse < new Date().getTime())) {
- localStorage.removeItem(key)
- } else {
- return JSON.parse(data.value)
- }
- }
- return null
- }
- }
|