uniStorageTool.uts 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
import { SetStorageOptions, SetStorageSuccess, GetStorageOptions, GetStorageSuccess } from "./interface.uts"


const STORAGE_DATA_TYPE = '__TYPE'
const STORAGE_KEYS = 'uni-storage-keys'

/**
 * add since 2023-09-04 过滤原生数据类型对齐 web typeOf 用于兼容旧数据
 */
function filterNativeType(src:string):string{
	if(src == "Double" || src == "Float" || src == "Long" || src == "Int" || src == "Short" || src == "Byte" || src == "UByte" || src == "UShort" || src == "UInt" || src == "ULong"){
		return "number"
	}
	
	return src
}


function parseValue(value: any): any | null {
	const types = ['object', 'string', 'number', 'boolean', 'undefined']
	const object = typeof value == 'string' ? JSON.parse(value as string) : value
	if (object == null) {
		return null
	} else {
		const type = typeof object
		if (types.indexOf(type) >= 0) {
			// @ts-expect-error   
			if (object instanceof UTSJSONObject || object instanceof Map<string,any>) { 
				// @ts-expect-error   
				const map = (object instanceof UTSJSONObject) ? (object as UTSJSONObject).toMap() : (object as Map<string,any>) 
				if (map.size  == 2 && map.has('data')  && map.has('type')) {
					let dataType:string = ""
					if(map.get("type") ==  null){
						dataType = ""
					}else{
						dataType = map.get("type") as string
					}
					if (filterNativeType(typeof map.get('data')) == dataType && dataType != 'string') {
						return map.get('data')
					}else if (typeof map.get('data') == dataType && dataType == 'string') {
						const regex = /^\d{4}-\d{2}-\d{2}T\d{2}\:\d{2}\:\d{2}\.\d{3}Z$/    
						// @ts-expect-error
						if (type == 'object' && regex.test(map.get('data') as string)) {
							let dateStr = map.get('data') as string
							return new Date(dateStr)
						}
						return map.get('data')
					}
				} else if (map.size >= 1){
					return ''
				}
			}
		}
		return null
	}
}

function praseGetStorage(type: string, value: string): any | null {
	let data: any | null = value
	if (type != 'string' || (type == "string" && value == '{"type":"undefined"}')) {
		// 兼容H5和V3初期历史格式
		let object = JSON.parse(value)
		
		if (object == null) {
			return data
		}
		
		const result = parseValue(object)
		if (result != null) {
			data = result
		} else if (type != null){
			// 兼容App端历史格式
			data = object
			if (typeof object == 'string') {
				object = JSON.parse(object as string)
				const objectType = typeof object
				if (objectType == 'number' && type == 'date') {
					let dateNum = object as number
					data = new Date(dateNum)
				} else if (objectType == (['null', 'array'].indexOf(type) < 0 ? type : 'object')) {
					data = object
				}
			}
		}
	}
	return data
}

function uni_setStorageSync(key: string, data: any, saveItemHandler: (key: string, data: string) => void, removeItemHandler: (key: string) => void) {
	let type = filterNativeType(typeof data)
	let value: string | null = null
	 value = (type == 'string') ? (data as string) : JSON.stringify({type: type, data: data})
	if (type == 'string' && parseValue(data) != null) {
		saveItemHandler(key + STORAGE_DATA_TYPE, type)
	} else{
		removeItemHandler(key + STORAGE_DATA_TYPE)
	}
	
	if(value ==  null){
		value = ""
	}
	
	saveItemHandler(key, value!)
}

function uni_setStorageAsync(options: SetStorageOptions, saveItemAsyncHandler: (key: string, data: string) => void, removeItemAsyncHandler: (key: string) => void) {
	const type = filterNativeType(typeof options.data)
	let value: string | null = null
	value = (type == 'string') ? (options.data as string) : JSON.stringify({type: type, data: options.data})
	if (value == null) {
		let fail = new UniError("uni-storage", -1, "data can not be stringify")
		options.fail?.(fail) 
		options.complete?.(fail)
	} else{
		if (type == 'string' && parseValue(options.data) != null) {
			saveItemAsyncHandler(options.key + STORAGE_DATA_TYPE, type)
		} else{
			removeItemAsyncHandler(options.key + STORAGE_DATA_TYPE)
		}
		if(value == null){
			value = ""
		}
		saveItemAsyncHandler(options.key, value!)
		let success: SetStorageSuccess = {
		}
		options.success?.(success)
		options.complete?.(success)  
	}

}

function uni_getStorageSync(key: string, getItemHandler: ((key: string) => string | null)): any | null {
	let value = getItemHandler(key)
	let typeOrigin = getItemHandler(key + STORAGE_DATA_TYPE)
	if(typeOrigin == null){
		typeOrigin = ""
	}
	
	const type = typeOrigin!.toLowerCase()
	if (typeof value != "string") {
		return ''
	}
	if(value == null){
		value = ""
	}
	return praseGetStorage(type, value!)
}

function uni_getStorageAsync(options: GetStorageOptions, getItemAsyncHandler: ((key: string) => string | null), includesKey: (key: string) => boolean) {
	let ret = includesKey(options.key);
	if (!ret) {
		let fail = new UniError("uni-storage", -2, "getStorage:fail data not found")
		options.fail?.(fail);
		options.complete?.(fail);
		return;
	}
	let value = getItemAsyncHandler(options.key)
	if(value == null){
		value = ""
	}
	let typeOrigin = getItemAsyncHandler(options.key + STORAGE_DATA_TYPE)
	if(typeOrigin == null){
		typeOrigin = ""
	}
	const type = typeOrigin!.toLowerCase()
	
	if (typeof value != "string") {
		let success: GetStorageSuccess = {
			data: ""
		}
		options.success?.(success)
		options.complete?.(success)
	}else {
		const data = praseGetStorage(type, value!)
		let success: GetStorageSuccess = {
			data: data
		}
		options.success?.(success)
		options.complete?.(success)
	}
}

export {
	STORAGE_DATA_TYPE,
	STORAGE_KEYS,
	uni_setStorageSync,
	uni_setStorageAsync,
	uni_getStorageSync,
	uni_getStorageAsync,
}