2022-09-09 06:44:55 +00:00
|
|
|
|
import { isObject } from '@vue/shared'
|
2022-09-09 06:46:46 +00:00
|
|
|
|
import { getToken } from './auth'
|
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) => {
|
|
|
|
|
|
let qurey = uni.createSelectorQuery()
|
|
|
|
|
|
if (context) {
|
|
|
|
|
|
qurey = uni.createSelectorQuery().in(context)
|
|
|
|
|
|
}
|
2022-09-09 01:50:39 +00:00
|
|
|
|
qurey[all ? 'selectAll' : 'select'](selector)
|
|
|
|
|
|
.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
|
|
|
|
|
|
|
|
|
|
/**
|
2022-09-09 06:46:46 +00:00
|
|
|
|
<<<<<<< HEAD
|
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) {
|
|
|
|
|
|
let url: string
|
|
|
|
|
|
switch (link.type) {
|
|
|
|
|
|
case LinkTypeEnum.SHOP_PAGES:
|
|
|
|
|
|
url = link.query ? `${link.path}?${objectToQuery(link.query)}` : link.path
|
|
|
|
|
|
if (link.isTab) {
|
|
|
|
|
|
uni.switchTab({ url })
|
|
|
|
|
|
} else {
|
|
|
|
|
|
uni.navigateTo({ url })
|
|
|
|
|
|
}
|
|
|
|
|
|
break
|
|
|
|
|
|
case LinkTypeEnum.CUSTOM_LINK:
|
|
|
|
|
|
uni.navigateTo({ url: `/pages/webview/webview?url=${link.path}` })
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @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 {
|
|
|
|
|
|
console.log(part + encodeURIComponent(value), '####')
|
|
|
|
|
|
query += part + encodeURIComponent(value) + '&'
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return query.slice(0, -1)
|
|
|
|
|
|
}
|
2022-09-09 06:27:52 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* @description 上传图片
|
|
|
|
|
|
* @param { String } path 选择的本地地址
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function uploadFile(path: any) {
|
2022-09-09 06:46:46 +00:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
const token = getToken()
|
|
|
|
|
|
uni.uploadFile({
|
|
|
|
|
|
url: `${import.meta.env.VITE_APP_BASE_URL}/api/Upload/image`,
|
|
|
|
|
|
filePath: path,
|
|
|
|
|
|
name: 'file',
|
|
|
|
|
|
header: {
|
|
|
|
|
|
token
|
|
|
|
|
|
},
|
|
|
|
|
|
fileType: 'image',
|
|
|
|
|
|
success: (res) => {
|
|
|
|
|
|
console.log('uploadFile res ==> ', res)
|
|
|
|
|
|
const data = JSON.parse(res.data)
|
|
|
|
|
|
if (data.code == 1) {
|
|
|
|
|
|
resolve(data.data)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
reject()
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
fail: (err) => {
|
|
|
|
|
|
console.log(err)
|
|
|
|
|
|
reject()
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
})
|
2022-09-09 06:27:52 +00:00
|
|
|
|
}
|