feat:first commit
This commit is contained in:
166
src/mixins/BaseMixin.ts
Normal file
166
src/mixins/BaseMixin.ts
Normal file
@@ -0,0 +1,166 @@
|
||||
import { JumpType } from '@/utils/common'
|
||||
import { dateFormat, formatRichText, PUBLIC_PATH_URL, showToast } from '@/utils/util'
|
||||
import store from '@/store'
|
||||
|
||||
export default {
|
||||
install(Vue: any) {
|
||||
Vue.mixin({
|
||||
data() {
|
||||
return {}
|
||||
},
|
||||
computed: {},
|
||||
methods: {
|
||||
getStatusHeightCss() {
|
||||
const systemInfo = uni.getSystemInfoSync()
|
||||
return { '--status-height': systemInfo.statusBarHeight + 'px' }
|
||||
},
|
||||
getNavHeightCss() {
|
||||
return store.getters.getNavHeight
|
||||
},
|
||||
/**
|
||||
* 跳转页面
|
||||
* @param url
|
||||
* @param jumpType
|
||||
* @param delay
|
||||
*/
|
||||
goto(url: string, jumpType: JumpType = JumpType.navigator, delay: number = 0) {
|
||||
// console.info('goto', url, jumpType, delay)
|
||||
let checkIsTab = (url: string): boolean => {
|
||||
// TODO tab标签
|
||||
let tabPage = ['/pages/index/index', '/pages/mine/index']
|
||||
let isTab = false
|
||||
if (url) {
|
||||
for (let i = 0; i < tabPage.length; i++) {
|
||||
if (url.indexOf(tabPage[i]) == 0) {
|
||||
isTab = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return isTab
|
||||
}
|
||||
|
||||
let goto = (url: string, jumpType: JumpType = JumpType.navigator) => {
|
||||
let isTab = checkIsTab(url)
|
||||
if (isTab) {
|
||||
jumpType = JumpType.switchTab
|
||||
}
|
||||
if (jumpType == JumpType.navigator) {
|
||||
// @ts-ignore
|
||||
this.$Router.push(url)
|
||||
// uni.navigateTo({url: url});
|
||||
} else if (jumpType == JumpType.switchTab) {
|
||||
// @ts-ignore
|
||||
this.$Router.pushTab(url)
|
||||
// let baseUrl = url.split('?')
|
||||
// if (baseUrl[1]) {
|
||||
// this.$Router.pushTab(baseUrl[0])
|
||||
// } else {
|
||||
// this.$Router.pushTab(url)
|
||||
// }
|
||||
// uni.switchTab({url: baseUrl})
|
||||
} else if (jumpType == JumpType.redirect) {
|
||||
// @ts-ignore
|
||||
this.$Router.replace(url)
|
||||
// uni.redirectTo({url: url})
|
||||
}
|
||||
}
|
||||
|
||||
if (url) {
|
||||
if (delay > 0) {
|
||||
let timer = setTimeout(() => {
|
||||
goto(url, jumpType)
|
||||
}, delay)
|
||||
} else {
|
||||
goto(url, jumpType)
|
||||
}
|
||||
} else {
|
||||
goto('/pages/index/index', JumpType.redirect)
|
||||
}
|
||||
},
|
||||
goBack() {
|
||||
let pages = getCurrentPages()
|
||||
if (pages.length > 1) {
|
||||
uni.navigateBack({})
|
||||
} else {
|
||||
this.goto('')
|
||||
}
|
||||
},
|
||||
/**
|
||||
* @description 保存图片至手机相册
|
||||
* @param url
|
||||
*/
|
||||
saveInPhoto(url: string) {
|
||||
// #ifdef APP-PLUS
|
||||
uni.showLoading({
|
||||
// @ts-ignore
|
||||
title: '加载中'
|
||||
})
|
||||
uni.downloadFile({
|
||||
url,
|
||||
success: (res) => {
|
||||
uni.hideLoading()
|
||||
if (res.statusCode === 200) {
|
||||
uni.saveImageToPhotosAlbum({
|
||||
filePath: res.tempFilePath,
|
||||
success: () => {
|
||||
showToast('保存成功')
|
||||
},
|
||||
fail: () => {
|
||||
showToast('保存失败')
|
||||
}
|
||||
})
|
||||
} else {
|
||||
showToast('下载失败')
|
||||
}
|
||||
}
|
||||
})
|
||||
// #endif
|
||||
},
|
||||
/**
|
||||
* 格式化图片地址
|
||||
* @param url 图片地址
|
||||
* @param defaultUrl 图片默认地址
|
||||
*/
|
||||
getFullImageUrl(url: string, defaultUrl: string = ''): string {
|
||||
if (!url) {
|
||||
return defaultUrl
|
||||
}
|
||||
if (url.startsWith('http://') || url.startsWith('https://')) {
|
||||
return url
|
||||
}
|
||||
return `${PUBLIC_PATH_URL}${url}`
|
||||
},
|
||||
/**
|
||||
* 格式化时间
|
||||
* @param value
|
||||
* @param fmt
|
||||
*/
|
||||
formatDateTime(value: string, fmt: string = 'YYYY-MM-DD HH:mm:ss'): string {
|
||||
if (!value) return ''
|
||||
return dateFormat(value, fmt)
|
||||
},
|
||||
/**
|
||||
* 格式化富文本
|
||||
* @param value 富文本内容
|
||||
*/
|
||||
formatHtmlContent(value: string): string {
|
||||
if (!value) return ''
|
||||
return formatRichText(value)
|
||||
},
|
||||
// 解析url
|
||||
parseUrl(url: string) {
|
||||
const params = url.split('?')[1]
|
||||
if (!params) return null
|
||||
const paramsArr = params.split('&')
|
||||
const paramsObj: any = {}
|
||||
paramsArr.forEach((item: string) => {
|
||||
const key = item.split('=')[0]
|
||||
paramsObj[key] = item.split('=')[1]
|
||||
})
|
||||
return paramsObj
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user