parent
b8ed38e6d7
commit
507d51b96d
@ -0,0 +1,4 @@ |
|||||||
|
import logic from "./logic" |
||||||
|
import resize from "./resize" |
||||||
|
|
||||||
|
export {logic, resize} |
||||||
@ -0,0 +1,42 @@ |
|||||||
|
import Decimal from 'decimal.js' |
||||||
|
|
||||||
|
/*基于decimal.js的运算封装*/ |
||||||
|
export function plus(...val) { |
||||||
|
if (val.length === 0) return 0 |
||||||
|
else if (val.length === 1) return new Decimal(val[0]).toNumber() |
||||||
|
let f = new Decimal(val[0]) |
||||||
|
for (let i = 1; i < val.length; i++) { |
||||||
|
f = f.add(new Decimal(val[i])) |
||||||
|
} |
||||||
|
return f.toNumber() |
||||||
|
} |
||||||
|
|
||||||
|
export function sub(...val) { |
||||||
|
if (val.length === 0) return 0 |
||||||
|
else if (val.length === 1) return new Decimal(val[0]).toNumber() |
||||||
|
let f = new Decimal(val[0]) |
||||||
|
for (let i = 1; i < val.length; i++) { |
||||||
|
f = f.sub(new Decimal(val[i])) |
||||||
|
} |
||||||
|
return f.toNumber() |
||||||
|
} |
||||||
|
|
||||||
|
export function mul(...val) { |
||||||
|
if (val.length === 0) return 0 |
||||||
|
else if (val.length === 1) return new Decimal(val[0]).toNumber() |
||||||
|
let f = new Decimal(val[0]) |
||||||
|
for (let i = 1; i < val.length; i++) { |
||||||
|
f = f.mul(new Decimal(val[i])) |
||||||
|
} |
||||||
|
return f.toNumber() |
||||||
|
} |
||||||
|
|
||||||
|
export function div(...val) { |
||||||
|
if (val.length === 0) return 0 |
||||||
|
else if (val.length === 1) return new Decimal(val[0]).toNumber() |
||||||
|
let f = new Decimal(val[0]) |
||||||
|
for (let i = 1; i < val.length; i++) { |
||||||
|
f = f.div(new Decimal(val[i])) |
||||||
|
} |
||||||
|
return f.toNumber() |
||||||
|
} |
||||||
@ -0,0 +1,19 @@ |
|||||||
|
import pako from 'pako' |
||||||
|
|
||||||
|
export function unzip(b64Data) { |
||||||
|
let strData = atob(b64Data) |
||||||
|
// Convert binary string to character-number array
|
||||||
|
let charData = strData.split('').map(x => x.charCodeAt(0)) |
||||||
|
// Turn number array into byte-array
|
||||||
|
let binData = new Uint8Array(charData) |
||||||
|
// unzip
|
||||||
|
let data = pako.inflate(binData) |
||||||
|
// Convert gunzipped byteArray back to ascii string:
|
||||||
|
strData = String.fromCharCode.apply(null, new Uint16Array(data)) |
||||||
|
return decodeURIComponent(strData) |
||||||
|
} |
||||||
|
|
||||||
|
export function zip(str) { |
||||||
|
let binaryString = pako.gzip(encodeURIComponent(str), {to: 'string'}) |
||||||
|
return btoa(binaryString) |
||||||
|
} |
||||||
@ -1,29 +0,0 @@ |
|||||||
import {sessionUserKey} from '@/config' |
|
||||||
import {isEmpty, unzip, zip} from "@/utils" |
|
||||||
|
|
||||||
export function isUserExist() { |
|
||||||
return !isEmpty(sessionStorage.getItem(sessionUserKey)) |
|
||||||
} |
|
||||||
|
|
||||||
export function getUser() { |
|
||||||
let obj = sessionStorage.getItem(sessionUserKey) |
|
||||||
if (isEmpty(obj)) return {} |
|
||||||
try { |
|
||||||
obj = JSON.parse(unzip(obj)) |
|
||||||
} |
|
||||||
catch (e) { |
|
||||||
console.error('用户数据异常!', e) |
|
||||||
obj = {} |
|
||||||
removeUser() |
|
||||||
} |
|
||||||
return obj |
|
||||||
} |
|
||||||
|
|
||||||
export function setUser(user) { |
|
||||||
if (isEmpty(user)) return removeUser() |
|
||||||
sessionStorage.setItem(sessionUserKey, zip(JSON.stringify(user))) |
|
||||||
} |
|
||||||
|
|
||||||
function removeUser() { |
|
||||||
sessionStorage.removeItem(sessionUserKey) |
|
||||||
} |
|
||||||
Loading…
Reference in new issue