import { App } from 'vue' ; import { $g } from './config' ; class MiStorage { type { string } prefix : string ; type { Storage } instance ! : Storage ; constructor ( type ? ) { this . prefix = $g . prefix ; this . change ( type ) ; } param keys param prefix return { * } get ( keys , prefix ? ) { let data = { } ; if ( Array . isArray ( keys ) ) { for ( let i = 0 , len = keys . length ; i < len ; i ++ ) { const key = keys [ i ] ; const item = `${ prefix ?? this . prefix }${ key }` ; data [ key ] = JSON . parse ( this . instance . getItem ( item ) ) ; } } else { data = null ; const key = `${ prefix ?? this . prefix }${ keys }` ; if ( keys ) data = JSON . parse ( this . instance . getItem ( key ) ) ; } return data ; } param key param value param prefix set ( key , value , prefix ? ) { const item = `${ prefix ?? this . prefix }${ key }` ; this . instance . setItem ( item , JSON . stringify ( value ) ) ; } param keys del ( keys , prefix ? ) { if ( Array . isArray ( keys ) ) { for ( let i = 0 , len = keys . length ; i < len ; i ++ ) { const key = keys [ i ] ; const item = `${ prefix ?? this . prefix }${ key }` ; this . instance . removeItem ( item ) ; } } else { const item = `${ this . prefix }${ keys }` ; this . instance . removeItem ( item ) ; } } param type change ( type ? ) { this . instance = type === 'local' ? localStorage : type === 'session' ? sessionStorage : localStorage ; return this ; } } export const $storage = new MiStorage ( ) ; const storage = { install ( app ) { app . config . globalProperties . $storage = $storage ; return app ; } } ; export default storage ;