feat: npm部分内容

This commit is contained in:
chenjinsong
2021-07-05 17:40:43 +08:00
parent c35fb070a5
commit c426084934
10 changed files with 251 additions and 156 deletions

View File

@@ -5,7 +5,7 @@
*/
import { get, post } from '@/utils/http'
import { sortByOrderNum } from '@/permission'
import {storageKey} from "@/utils/constants";
import { storageKey, iso36112 } from '@/utils/constants'
export const api = {
// 系统相关
@@ -79,8 +79,12 @@ export async function getI18n () {
return await request
}
export function loadIso36112 () {
get(`${process.env.BASE_URL}geojson/data/countriesWithCapital.json`).then(response => {
sessionStorage.setItem(storageKey.iso36112, JSON.stringify(response))
/* 获得原始的3611-2 json字符串数据 */
export async function getIso36112JsonData (suffix) {
const request = new Promise(resolve => {
get(`${process.env.BASE_URL}geojson/${suffix}`).then(response => {
resolve(JSON.stringify(response))
})
})
return await request
}

View File

@@ -1,7 +1,8 @@
export const defaultPageSize = 20
export const storageKey = {
iso36112: 'cn-iso3611-2',
iso36112Capital: 'cn-iso3611-2-capital',
iso36112WorldLow: 'cn-iso3611-2-world-low',
i18n: 'cn-i18n',
language: 'cn-language',
timezone: 'cn-timezone',
@@ -17,6 +18,11 @@ export const storageKey = {
unsavedChange: 'cn-unsaved-change'
}
export const iso36112 = {
[storageKey.iso36112Capital]: 'data/countriesWithCapital.json',
[storageKey.iso36112WorldLow]: 'worldChinaLow.json'
}
// 统一定义跳转来源
export const fromRoute = {
trafficSummary: 'trafficSummary',

View File

@@ -1,8 +1,8 @@
import { ElMessageBox } from 'element-plus'
import i18n from '@/i18n'
import _ from 'lodash'
import { storageKey } from '@/utils/constants'
import { getCache, setCache } from '@/utils/cache'
import { storageKey, iso36112 } from '@/utils/constants'
import { getIso36112JsonData } from '@/utils/api'
export const tableSort = {
// 是否需要排序
@@ -399,7 +399,46 @@ export function humpToLine (name) {
return name.replace(/([A-Z])/g, '_$1').toLowerCase()
}
export function getCapitalGeo (countryId) {
!getCache('capitalGeo') && setCache('capitalGeo', JSON.parse(sessionStorage.getItem(storageKey.iso36112)))
return getCache('capitalGeo')[countryId]
// 加载geo数据
export function loadGeoData (key) {
const keys = []
if (key) {
keys.push(key)
} else {
keys.push(storageKey.iso36112Capital)
keys.push(storageKey.iso36112WorldLow)
}
keys.forEach(async k => {
if (!localStorage.getItem(k)) {
const data = await getIso36112JsonData(iso36112[k])
if (data) {
localStorage.setItem(k, JSON.stringify(data))
}
}
})
return localStorage.getItem(key)
}
/* 返回geodata对象 */
export function getGeoData (key) {
const data = localStorage.getItem(key)
if (data) {
return JSONParse(data)
} else {
return JSONParse(loadGeoData(key))
}
}
export function getCapitalGeo (countryId) {
const data = getGeoData(storageKey.iso36112Capital)
return data[countryId]
}
export function JSONParse (data) {
const firstParse = JSON.parse(data)
if (typeof firstParse === 'string') {
return JSON.parse(firstParse)
} else {
return firstParse
}
}