2026-03-02 02:11:19 +00:00
|
|
|
|
import { isObject } from '@vue/shared'
|
|
|
|
|
|
import { getToken } from './auth'
|
|
|
|
|
|
import { parseQuery } from 'uniapp-router-next'
|
2022-09-09 06:44:55 +00:00
|
|
|
|
|
2022-09-08 03:27:26 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @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) => {
|
2022-09-08 03:27:26 +00:00
|
|
|
|
return new Promise((resolve, reject) => {
|
2024-10-10 06:10:49 +00:00
|
|
|
|
let qurey = uni.createSelectorQuery()
|
2022-09-08 03:27:26 +00:00
|
|
|
|
if (context) {
|
2024-10-10 06:10:49 +00:00
|
|
|
|
qurey = uni.createSelectorQuery().in(context)
|
2022-09-08 03:27:26 +00:00
|
|
|
|
}
|
2024-10-10 06:10:49 +00:00
|
|
|
|
qurey[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-08 03:27:26 +00:00
|
|
|
|
})
|
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
|
2024-10-10 06:10:49 +00:00
|
|
|
|
canTab: boolean
|
2022-09-09 06:44:55 +00:00
|
|
|
|
query?: Record<string, any>
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export enum LinkTypeEnum {
|
|
|
|
|
|
'SHOP_PAGES' = 'shop',
|
2024-10-10 06:10:49 +00:00
|
|
|
|
'CUSTOM_LINK' = 'custom',
|
|
|
|
|
|
'MINI_PROGRAM' = 'mini_program'
|
2022-09-09 06:44:55 +00:00
|
|
|
|
}
|
2022-09-13 11:07:36 +00:00
|
|
|
|
|
2026-03-02 02:11:19 +00:00
|
|
|
|
export function navigateTo(
|
|
|
|
|
|
link: Link,
|
|
|
|
|
|
navigateType: 'navigateTo' | 'switchTab' | 'reLaunch' = 'navigateTo'
|
|
|
|
|
|
) {
|
2024-10-10 06:10:49 +00:00
|
|
|
|
// 如果是小程序跳转
|
|
|
|
|
|
if (link.type === LinkTypeEnum.MINI_PROGRAM) {
|
|
|
|
|
|
navigateToMiniProgram(link)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-02 02:11:19 +00:00
|
|
|
|
const url = link?.query ? `${link.path}?${objectToQuery(link?.query)}` : link.path
|
2024-10-10 06:10:49 +00:00
|
|
|
|
|
2026-03-02 02:11:19 +00:00
|
|
|
|
;(navigateType == 'switchTab' || link.canTab) && uni.switchTab({ url })
|
|
|
|
|
|
navigateType == 'navigateTo' && uni.navigateTo({ url })
|
|
|
|
|
|
navigateType == 'reLaunch' && uni.reLaunch({ url })
|
2024-10-10 06:10:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @description 小程序跳转
|
|
|
|
|
|
* @param link 跳转信息,由装修数据进行输入
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function navigateToMiniProgram(link: Link) {
|
2026-03-02 02:11:19 +00:00
|
|
|
|
const query = link.query
|
2024-10-10 06:10:49 +00:00
|
|
|
|
// #ifdef H5
|
|
|
|
|
|
window.open(
|
2026-03-02 02:11:19 +00:00
|
|
|
|
`weixin://dl/business/?appid=${query?.appId}&path=${query?.path}&env_version=${
|
|
|
|
|
|
query?.env_version
|
|
|
|
|
|
}&query=${encodeURIComponent(query?.query)}`
|
2024-10-10 06:10:49 +00:00
|
|
|
|
)
|
|
|
|
|
|
// #endif
|
|
|
|
|
|
// #ifdef MP
|
|
|
|
|
|
uni.navigateToMiniProgram({
|
|
|
|
|
|
appId: query?.appId,
|
|
|
|
|
|
path: query?.path,
|
|
|
|
|
|
extraData: parseQuery(query?.query),
|
2026-03-02 02:11:19 +00:00
|
|
|
|
envVersion: query?.env_version
|
2024-10-10 06:10:49 +00:00
|
|
|
|
})
|
|
|
|
|
|
// #endif
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @description 将一个数组分成几个同等长度的数组
|
|
|
|
|
|
* @param { Array } array[分割的原数组]
|
|
|
|
|
|
* @param { Number } size[每个子数组的长度]
|
|
|
|
|
|
*/
|
|
|
|
|
|
export const sliceArray = (array: any[], size: number) => {
|
|
|
|
|
|
const result = []
|
|
|
|
|
|
for (let x = 0; x < Math.ceil(array.length / size); x++) {
|
|
|
|
|
|
const start = x * size
|
|
|
|
|
|
const end = start + size
|
|
|
|
|
|
result.push(array.slice(start, end))
|
|
|
|
|
|
}
|
|
|
|
|
|
return result
|
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
|
|
|
|
|
2024-10-10 06:10:49 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @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
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-04-03 07:13:49 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @description 格式化输出价格
|
|
|
|
|
|
* @param { string } price 价格
|
|
|
|
|
|
* @param { string } take 小数点操作
|
|
|
|
|
|
* @param { string } prec 小数位补
|
|
|
|
|
|
*/
|
2026-03-02 02:11:19 +00:00
|
|
|
|
export function formatPrice({ price, take = 'all', prec = undefined }: any) {
|
|
|
|
|
|
const [integer, decimals = ''] = (price + '').split('.')
|
2023-04-03 07:13:49 +00:00
|
|
|
|
|
|
|
|
|
|
// 小数位补
|
|
|
|
|
|
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()
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|