edu/uniapp/src/utils/util.ts

153 lines
4.3 KiB
TypeScript
Raw Normal View History

2022-09-09 06:44:55 +00:00
import { isObject } from '@vue/shared'
/**
* @description ctx
* @param { String } selector '.app' | '#app'
* @param { Boolean } all
* @param { ctx } context
*/
2022-09-09 01:50:39 +00:00
export const getRect = (selector: string, all = false, context?: any) => {
return new Promise((resolve, reject) => {
let query = uni.createSelectorQuery()
if (context) {
query = uni.createSelectorQuery().in(context)
}
query[all ? 'selectAll' : 'select'](selector)
2022-09-09 01:50:39 +00:00
.boundingClientRect(function (rect) {
if (all && Array.isArray(rect) && rect.length) {
return resolve(rect)
}
if (!all && rect) {
return resolve(rect)
}
reject('找不到元素')
})
.exec()
})
2022-09-09 01:50:39 +00:00
}
2022-09-09 06:44:55 +00:00
/**
* @description
*/
export function currentPage() {
const pages = getCurrentPages()
const currentPage = pages[pages.length - 1]
return currentPage || {}
}
/**
* @description
*/
interface Link {
path: string
name?: string
type: string
isTab: boolean
query?: Record<string, any>
}
export enum LinkTypeEnum {
'SHOP_PAGES' = 'shop',
'CUSTOM_LINK' = 'custom'
}
export function navigateTo(link: Link, navigateType: 'navigateTo' | 'reLaunch' = 'navigateTo') {
const url = link.query ? `${link.path}?${objectToQuery(link.query)}` : link.path
2022-09-15 03:40:20 +00:00
navigateType == 'navigateTo' && uni.navigateTo({ url })
navigateType == 'reLaunch' && uni.reLaunch({ url })
2022-09-09 06:44:55 +00:00
}
/**
* @description
* @param {unknown} value
* @return {Boolean}
*/
export const isEmpty = (value: unknown) => {
return value == null && typeof value == 'undefined'
}
/**
* @description Query语法
* @param { Object } params
* @return {string} Query语法
*/
export function objectToQuery(params: Record<string, any>): string {
let query = ''
for (const props of Object.keys(params)) {
const value = params[props]
const part = encodeURIComponent(props) + '='
if (!isEmpty(value)) {
console.log(encodeURIComponent(props), isObject(value))
if (isObject(value)) {
for (const key of Object.keys(value)) {
if (!isEmpty(value[key])) {
const params = props + '[' + key + ']'
const subPart = encodeURIComponent(params) + '='
query += subPart + encodeURIComponent(value[key]) + '&'
}
}
} else {
query += part + encodeURIComponent(value) + '&'
}
}
}
return query.slice(0, -1)
}
2023-03-29 07:52:38 +00:00
2023-04-03 07:13:49 +00:00
/**
* @description
* @param { string } price
* @param { string } take
* @param { string } prec
*/
export function formatPrice({ price, take = 'all', prec = undefined }: any) {
let [integer, decimals = ''] = (price + '').split('.')
// 小数位补
if (prec !== undefined) {
const LEN = decimals.length
for (let i = prec - LEN; i > 0; --i) decimals += '0'
decimals = decimals.substr(0, prec)
}
switch (take) {
case 'int':
return integer
case 'dec':
return decimals
case 'all':
return integer + '.' + decimals
}
}
2023-03-29 07:52:38 +00:00
/**
* @description
* @param { string } task
*/
export function series(...task: Array<(_arg: any) => any>) {
return function (): Promise<any> {
return new Promise((resolve, reject) => {
const iteratorTask = task.values()
const next = (res?: any) => {
const nextTask = iteratorTask.next()
if (nextTask.done) {
resolve(res)
} else {
Promise.resolve(nextTask.value(res)).then(next).catch(reject)
}
}
next()
})
}
}
/**
* @description
* @param {String | Number} value 100
* @param {String} unit px em rem
*/
export const addUnit = (value: string | number, unit = 'rpx') => {
return !Object.is(Number(value), NaN) ? `${value}${unit}` : value
}