175 lines
5.3 KiB
Plaintext
175 lines
5.3 KiB
Plaintext
// @ts-nocheck
|
|
import type { CascaderOption, KeysType } from './type';
|
|
// #ifndef UNI-APP-X
|
|
type UTSJSONObject = Record<string, any>
|
|
// #endif
|
|
// #ifdef UNI-APP-X
|
|
const Object = UTSJSONObject
|
|
// #endif
|
|
import { t } from '@/utils/i18n'
|
|
|
|
export function parseKeys(keys : UTSJSONObject | null, uniCloud:boolean) : KeysType {
|
|
const _labelKey = uniCloud ? 'name' : `${keys?.['label'] ?? 'label'}`
|
|
const _valueKey = uniCloud ? 'code' : `${keys?.['value'] ?? 'value'}`
|
|
const _childrenKey = `${keys?.['children'] ?? 'children'}`
|
|
return {
|
|
label: _labelKey,
|
|
value: _valueKey,
|
|
children: _childrenKey,
|
|
} as KeysType
|
|
}
|
|
|
|
export function parseOptions(options : UTSJSONObject[], keys : KeysType) : UTSJSONObject[] {
|
|
return options.map((item) : UTSJSONObject => {
|
|
// #ifndef APP-ANDROID
|
|
const obj = {
|
|
[keys.label]: item[keys.label],
|
|
[keys.value]: item[keys.value],
|
|
[keys.children]: item[keys.children],
|
|
}
|
|
// #endif
|
|
// #ifdef APP-ANDROID
|
|
const obj = {}
|
|
item.toMap().forEach((v, key) => {
|
|
if (key == keys.label || key == keys.value || key == keys.children) {
|
|
obj[key] = v
|
|
}
|
|
})
|
|
// #endif
|
|
return obj;
|
|
});
|
|
}
|
|
|
|
|
|
export function getIndexesByValue(options : UTSJSONObject[], value : string | null, keys : KeysType) : number[] | null {
|
|
if (value == null) return null
|
|
for (let i = 0, size = options.length; i < size; i += 1) {
|
|
const opt = options[i];
|
|
if (opt[keys.value] == value) {
|
|
return [i];
|
|
}
|
|
const children = opt[keys.children] as UTSJSONObject[] | null
|
|
if (children != null) {
|
|
const res = getIndexesByValue(children, value, keys)
|
|
if (res != null) {
|
|
return [i, ...res]
|
|
}
|
|
}
|
|
}
|
|
return null
|
|
}
|
|
|
|
/**
|
|
* 在数组的指定位置插入或更新值。
|
|
* 如果指定的索引小于数组的长度,则更新该位置的值。
|
|
* 如果指定的索引大于或等于数组的长度,则将值添加到数组的末尾。
|
|
*
|
|
* @param {number[]} arr - 要操作的数字数组。
|
|
* @param {number} index - 要插入或更新值的索引位置。
|
|
* @param {number} value - 要插入或更新的值。
|
|
*/
|
|
export function pushAt<T>(arr : T[], index : number, value : T) {
|
|
// #ifdef APP-ANDROID
|
|
if (index < arr.length) {
|
|
arr[index] = value;
|
|
} else {
|
|
arr.push(value);
|
|
}
|
|
// #endif
|
|
// #ifndef APP-ANDROID
|
|
arr[index] = value;
|
|
// #endif
|
|
};
|
|
|
|
/**
|
|
* 将给定的字符串按照每两个字符进行分割,返回分割后的字符串数组。
|
|
*
|
|
* @param {string} str - 需要被分割的原始字符串。
|
|
* @returns {string[]} 返回一个数组,其中每个元素都是原始字符串中连续的两个字符组成的子串。
|
|
*/
|
|
export function splitEveryTwo(str : string) : string[] {
|
|
const result : string[] = [];
|
|
for (let i = 0; i < str.length; i += 2) {
|
|
result.push(str.substring(i, i + 2));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
export function getIndexByValue(options : UTSJSONObject[], value : string) : number {
|
|
return Math.max(options.findIndex(item => item['code'] == value), 0)
|
|
}
|
|
|
|
export function updateChildren(parent : UTSJSONObject[], child : UTSJSONObject[], selectedIndexes : number[]) {
|
|
if (selectedIndexes.length == 0 && parent.length == 0) {
|
|
parent.concat(child)
|
|
// parent = child
|
|
} else if (selectedIndexes.length > 1) {
|
|
const i = selectedIndexes.shift()!;
|
|
updateChildren(parent[i]['children'] as UTSJSONObject[], child, selectedIndexes)
|
|
} else if (selectedIndexes.length == 1) {
|
|
const i = selectedIndexes.shift()!;
|
|
// #ifdef APP-ANDROID || APP-IOS
|
|
parent[i].set('children', child)
|
|
// #endif
|
|
// #ifndef APP-ANDROID || APP-IOS
|
|
parent[i]['children'] = child
|
|
// #endif
|
|
}
|
|
}
|
|
|
|
export function getUniCloudArea(where : UTSJSONObject, cache : Map<string, UTSJSONObject[]>) : Promise<UTSJSONObject[]> {
|
|
return new Promise((resolve) => {
|
|
const db = uniCloud.databaseForJQL()
|
|
const collection = db.collection('opendb-city-china')
|
|
const type = (where['type'] ?? 4) as number;
|
|
const code = where['parent_code'] as string|null
|
|
if (code != null && cache.has(code)) {
|
|
resolve(cache.get(code)!)
|
|
return
|
|
}
|
|
uni.showLoading({
|
|
title: t('Mobile.Page.loading')
|
|
})
|
|
collection.where(where).get().then(res => {
|
|
uni.hideLoading()
|
|
// 省市 就加上children 县就直接返回
|
|
const cursor = type > 1 ? res.data : res.data.map((item) : UTSJSONObject => {
|
|
return Object.assign(item, { children: [] as UTSJSONObject[] })
|
|
})
|
|
if (code != null) {
|
|
cache.set(code, cursor)
|
|
}
|
|
resolve(cursor)
|
|
}).catch(err => {
|
|
uni.hideLoading()
|
|
uni.showToast({
|
|
icon: 'error',
|
|
title: t('Mobile.Common.networkRetry')
|
|
})
|
|
})
|
|
|
|
})
|
|
}
|
|
|
|
export function pickUniCloudArea(columns : UTSJSONObject[], level : number, value : string, selectedIndexes : number[], cache:Map<string, UTSJSONObject[]>) {
|
|
const first = selectedIndexes[0]
|
|
// 获取当前项
|
|
const item = selectedIndexes.reduce((p : UTSJSONObject | null, c : number, i : number) : UTSJSONObject | null => {
|
|
// #ifdef APP-ANDROID || APP-IOS
|
|
return i == 0 ? p : p?.getArray<UTSJSONObject>('children')?.[c]
|
|
// #endif
|
|
// #ifndef APP-ANDROID || APP-IOS
|
|
return i == 0 ? p : p?.['children']?.[c]
|
|
// #endif
|
|
}, columns[first])
|
|
|
|
const code = item?.[`code`] as string | null
|
|
const children = item?.[`children`] as UTSJSONObject[] | null
|
|
|
|
if (item != null && code == value && children != null && children.length == 0) {
|
|
getUniCloudArea({ type: level + 1, parent_code: code }, cache).then(res => {
|
|
updateChildren(columns, res, selectedIndexes)
|
|
})
|
|
}
|
|
} |