437 lines
13 KiB
TypeScript
437 lines
13 KiB
TypeScript
import pages from '@/pages.json'
|
||
import {ComponentInternalInstance} from 'vue';
|
||
import {isObject, isString, isUndefined, map} from "lodash";
|
||
import {qs} from "@/utils/index";
|
||
|
||
//获取某个元素的宽高
|
||
export function getDom(id: string, instance: ComponentInternalInstance | null): Promise<UniApp.NodeInfo | UniApp.NodeInfo[]> {
|
||
// const instance = getCurrentInstance();
|
||
return new Promise((resolve) => {
|
||
const query = uni.createSelectorQuery().in(instance);
|
||
query.select(id).boundingClientRect(data => {
|
||
resolve(data)
|
||
}).exec();
|
||
})
|
||
}
|
||
|
||
//获取当前路由
|
||
export function getRouter() {
|
||
const routes: any = getCurrentPages();
|
||
let options
|
||
//#ifdef H5
|
||
options = routes[routes.length - 1].$page.options
|
||
//#endif
|
||
//#ifdef MP-WEIXIN
|
||
options = routes[routes.length - 1]['options']
|
||
//#endif
|
||
return routes[routes.length - 1].route + qs(options)
|
||
}
|
||
|
||
//获取tabBar高度
|
||
export function getTabBarHeight() {
|
||
return uni.getSystemInfoSync().windowBottom
|
||
}
|
||
|
||
//获取顶部高度
|
||
export function getTopHeight() {
|
||
return uni.getSystemInfoSync().statusBarHeight
|
||
}
|
||
|
||
//获取底部安全区域高度
|
||
export function getSafeAreaHeight() {
|
||
return uni.getSystemInfoSync().safeAreaInsets?.bottom || 0
|
||
}
|
||
|
||
//保留当前页面,跳转到应用内的某个页面,使用uni.navigateBack可以返回到原页面。
|
||
export function navigateTo(OBJECT: UniNamespace.NavigateToOptions | string) {
|
||
if (isObject(OBJECT)) {
|
||
uni.navigateTo(OBJECT)
|
||
}
|
||
if (isString(OBJECT)) {
|
||
uni.navigateTo({url: OBJECT})
|
||
}
|
||
}
|
||
|
||
//关闭当前页面,跳转到应用内的某个页面。
|
||
export function redirectTo(OBJECT: UniNamespace.RedirectToOptions | string) {
|
||
if (isObject(OBJECT)) {
|
||
uni.redirectTo(OBJECT)
|
||
}
|
||
if (isString(OBJECT)) {
|
||
uni.redirectTo({url: OBJECT})
|
||
}
|
||
}
|
||
|
||
//关闭所有页面,打开到应用内的某个页面。
|
||
export function reLaunch(OBJECT: UniNamespace.ReLaunchOptions | string) {
|
||
if (isObject(OBJECT)) {
|
||
uni.reLaunch(OBJECT)
|
||
}
|
||
if (isString(OBJECT)) {
|
||
uni.redirectTo({url: OBJECT})
|
||
}
|
||
}
|
||
|
||
//跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。
|
||
export function switchTab(OBJECT: UniNamespace.SwitchTabOptions | string) {
|
||
if (isObject(OBJECT)) {
|
||
uni.switchTab(OBJECT)
|
||
}
|
||
if (isString(OBJECT)) {
|
||
uni.switchTab({url: OBJECT})
|
||
}
|
||
}
|
||
|
||
//关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈,决定需要返回几层。
|
||
export function navigateBack(OBJECT?: UniNamespace.NavigateBackOptions) {
|
||
if (isObject(OBJECT)) {
|
||
uni.navigateBack(OBJECT)
|
||
}
|
||
if (isString(OBJECT) || isUndefined(OBJECT)) {
|
||
uni.navigateBack({delta: 1})
|
||
}
|
||
}
|
||
|
||
//判断是ios、android
|
||
export function getPlatform(): 'ios' | 'android' {
|
||
return uni.getSystemInfoSync().platform as 'ios' | 'android'
|
||
}
|
||
|
||
//获取屏幕宽
|
||
export function getWindowWidth() {
|
||
return uni.getSystemInfoSync().windowWidth
|
||
}
|
||
|
||
//获取屏幕高
|
||
export function getWindowHeight() {
|
||
return uni.getSystemInfoSync().windowHeight
|
||
}
|
||
|
||
//是否页面带有TabBar
|
||
export function isTabBar() {
|
||
return !!(pages as any).tabBar && (pages as any).tabBar.list.length > 0;
|
||
}
|
||
|
||
//显示消息提示框。
|
||
export function showToast(OBJECT: UniNamespace.ShowToastOptions | string) {
|
||
if (isObject(OBJECT)) {
|
||
uni.showToast(OBJECT)
|
||
}
|
||
if (isString(OBJECT)) {
|
||
uni.showToast({title: OBJECT, icon: 'none'})
|
||
}
|
||
}
|
||
|
||
//隐藏消息提示框
|
||
export function hideToast() {
|
||
uni.hideToast();
|
||
}
|
||
|
||
//显示 loading 提示框, 需主动调用 uni.hideLoading 才能关闭提示框。
|
||
export function showLoading(OBJECT: UniNamespace.ShowLoadingOptions | string) {
|
||
if (isObject(OBJECT)) {
|
||
uni.showLoading(OBJECT)
|
||
}
|
||
if (isString(OBJECT)) {
|
||
uni.showLoading({title: OBJECT})
|
||
}
|
||
}
|
||
|
||
//隐藏 loading 提示框。
|
||
export function hideLoading() {
|
||
uni.hideLoading()
|
||
}
|
||
|
||
//显示模态弹窗,可以只有一个确定按钮,也可以同时有确定和取消按钮。
|
||
export function showModal(OBJECT: UniNamespace.ShowModalOptions): Promise<UniNamespace.ShowModalRes> {
|
||
return new Promise((resolve, reject) => {
|
||
uni.showModal({
|
||
...OBJECT,
|
||
success: (result) => {
|
||
if (result.confirm) {
|
||
resolve(result)
|
||
}
|
||
if (result.cancel) {
|
||
reject(result)
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
reject(err)
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
//从底部向上弹出操作菜单
|
||
export function showActionSheet(itemList: any[], key?: string) {
|
||
return new Promise((resolve, reject) => {
|
||
if (itemList.length > 0) {
|
||
let itemLists = itemList
|
||
if (isObject(itemList[0])) {
|
||
if (key) {
|
||
itemLists = map(itemList, key)
|
||
} else {
|
||
resolve('请传入对象展示字段')
|
||
return
|
||
}
|
||
}
|
||
uni.showActionSheet({
|
||
itemList: itemLists,
|
||
success: (result) => {
|
||
resolve({value: itemList[result.tapIndex], index: result.tapIndex, itemList})
|
||
},
|
||
fail: (err) => {
|
||
resolve(err)
|
||
}
|
||
})
|
||
} else {
|
||
resolve('数组长度为0')
|
||
}
|
||
})
|
||
}
|
||
|
||
export function getLocation(): Promise<UniNamespace.GetLocationSuccess> {
|
||
return new Promise(async (resolve, reject) => {
|
||
uni.getLocation({
|
||
type: 'gcj02',
|
||
success: (res) => {
|
||
resolve(res)
|
||
},
|
||
fail: (err) => {
|
||
reject(err)
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
//申请权限
|
||
export function requestPermissions(permissions: string, message: string) {
|
||
return new Promise((resolve, reject) => {
|
||
plus.android.requestPermissions([permissions], (resultObj) => {
|
||
let result = 0
|
||
for (let i = 0; i < resultObj.granted.length; i++) {
|
||
let grantedPermission = resultObj.granted[i];
|
||
if (permissions == grantedPermission) {
|
||
result = 1
|
||
}
|
||
}
|
||
for (let i = 0; i < resultObj.deniedPresent.length; i++) {
|
||
let deniedPresentPermission = resultObj.deniedPresent[i];
|
||
if (permissions == deniedPresentPermission) {
|
||
result = 0
|
||
}
|
||
}
|
||
for (let i = 0; i < resultObj.deniedAlways.length; i++) {
|
||
let deniedAlwaysPermission = resultObj.deniedAlways[i];
|
||
if (permissions == deniedAlwaysPermission) {
|
||
result = -1
|
||
}
|
||
}
|
||
if (result === 1) {
|
||
// @ts-ignore
|
||
resolve()
|
||
} else {
|
||
reject()
|
||
showToast({title: message, icon: 'none'})
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
//拨打电话。
|
||
export function makePhoneCall(phoneNumber: string) {
|
||
if (getPlatform() === 'android') {
|
||
requestPermissions('android.permission.CALL_PHONE', '请开启拨号权限').then(() => {
|
||
uni.makePhoneCall({
|
||
phoneNumber: phoneNumber,
|
||
success: (success) => {
|
||
console.log('success', success)
|
||
},
|
||
fail: (err) => {
|
||
console.log('err', err)
|
||
}
|
||
});
|
||
})
|
||
} else {
|
||
uni.makePhoneCall({
|
||
phoneNumber: phoneNumber,
|
||
success: (success) => {
|
||
console.log('success', success)
|
||
},
|
||
fail: (err) => {
|
||
console.log('err', err)
|
||
}
|
||
});
|
||
}
|
||
}
|
||
|
||
|
||
export function usePlayBackAudio(title: string, coverImgUrl: string) {
|
||
let bgAudioManager: UniNamespace.BackgroundAudioManager | null = null
|
||
|
||
function stop() {
|
||
if (bgAudioManager) {
|
||
bgAudioManager.stop()
|
||
bgAudioManager = null
|
||
}
|
||
}
|
||
|
||
stop()
|
||
|
||
function play(src: string, text: string, isVibrateLong = false) {
|
||
stop()
|
||
bgAudioManager = uni.getBackgroundAudioManager();
|
||
bgAudioManager.title = title;
|
||
bgAudioManager.singer = text;
|
||
bgAudioManager.coverImgUrl = coverImgUrl;
|
||
bgAudioManager.src = src;
|
||
bgAudioManager.play()
|
||
if (isVibrateLong) {
|
||
uni.vibrateLong({});
|
||
}
|
||
}
|
||
|
||
return {play, stop}
|
||
}
|
||
|
||
//获取设备唯一
|
||
export function getIdfa() {
|
||
let idfa = '';
|
||
try {
|
||
if ('ios' == getPlatform()) {
|
||
// @ts-ignore
|
||
idfa = plus.device.uuid
|
||
}
|
||
if ('android' == getPlatform()) {
|
||
let mainActivity: any = plus.android.runtimeMainActivity();
|
||
let Settings: any = plus.android.importClass("android.provider.Settings");
|
||
idfa = Settings.Secure.getString(mainActivity.getContentResolver(), Settings.Secure.ANDROID_ID)
|
||
}
|
||
} catch (e) {
|
||
console.error('exception in getIdfa@dc-idfa!!', e);
|
||
}
|
||
return idfa;
|
||
}
|
||
|
||
|
||
//退出app
|
||
export function exitApp() {
|
||
if (uni.getSystemInfoSync().osName == 'ios') {
|
||
//@ts-ignore
|
||
plus.ios.import("UIApplication").sharedApplication().performSelector("exit")//退出
|
||
}
|
||
if (uni.getSystemInfoSync().osName == 'android') {
|
||
plus.runtime.quit(); //退出
|
||
}
|
||
}
|
||
|
||
//跳转到外部打开
|
||
export function openURL(url: string) {
|
||
plus.runtime.openURL(url)
|
||
}
|
||
|
||
//重启app
|
||
export function restartApp() {
|
||
plus.runtime.restart()
|
||
}
|
||
|
||
//跳转到应用设置权限位置
|
||
export function goSettings() {
|
||
if (getPlatform() === 'android') {
|
||
let Intent = plus.android.importClass("android.content.Intent");
|
||
let Settings = plus.android.importClass("android.provider.Settings");
|
||
let Uri = plus.android.importClass("android.net.Uri");
|
||
let mainActivity = plus.android.runtimeMainActivity();
|
||
// @ts-ignore
|
||
let intent = new Intent();
|
||
// @ts-ignore
|
||
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
||
// @ts-ignore
|
||
let uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
|
||
intent.setData(uri);
|
||
// @ts-ignore
|
||
mainActivity.startActivity(intent);
|
||
}
|
||
if (getPlatform() === 'ios') {
|
||
// @ts-ignore
|
||
let UIApplication = plus.ios.import("UIApplication");
|
||
let application2 = UIApplication.sharedApplication();
|
||
// @ts-ignore
|
||
let NSURL2 = plus.ios.import("NSURL");
|
||
let setting2 = NSURL2.URLWithString("app-settings:");
|
||
application2.openURL(setting2);
|
||
plus.ios.deleteObject(setting2);
|
||
plus.ios.deleteObject(NSURL2);
|
||
plus.ios.deleteObject(application2);
|
||
}
|
||
}
|
||
|
||
|
||
//打开第三方导航
|
||
export function openLocation(OBJECT: { latitude: number, longitude: number, name: string, address: string }) {
|
||
uni.openLocation(OBJECT)
|
||
}
|
||
|
||
//设置标题
|
||
export function setTitle(title: string) {
|
||
uni.setNavigationBarTitle({title});
|
||
}
|
||
|
||
//发起支付宝支付
|
||
export function alipayPay(orderInfo: any) {
|
||
return new Promise((resolve, reject) => {
|
||
uni.requestPayment({
|
||
provider: 'alipay',
|
||
orderInfo,
|
||
success: (res) => {
|
||
resolve(res)
|
||
},
|
||
fail: (err) => {
|
||
showModal({
|
||
content: '支付失败',
|
||
showCancel: false
|
||
})
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
|
||
// 发起微信支付
|
||
export function wxPay(orderInfo: any) {
|
||
return new Promise((resolve, reject) => {
|
||
uni.requestPayment({
|
||
provider: "wxpay",
|
||
timeStamp: orderInfo.timestamp,
|
||
nonceStr: orderInfo.noncestr,
|
||
package: orderInfo.package,
|
||
signType: "MD5",
|
||
paySign: orderInfo.sign,
|
||
orderInfo: {
|
||
appid: orderInfo.appid,
|
||
noncestr: orderInfo.noncestr,
|
||
package: orderInfo.package,
|
||
partnerid: orderInfo.partnerid,
|
||
prepayid: orderInfo.prepayid,
|
||
timestamp: orderInfo.timestamp,
|
||
sign: orderInfo.sign,
|
||
},
|
||
success: (res) => {
|
||
resolve(res)
|
||
},
|
||
fail: async (err) => {
|
||
await showModal({
|
||
content: '支付失败',
|
||
showCancel: false
|
||
})
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
|
||
//将程序移动到后台
|
||
export function moveAppToBackground() {
|
||
let main: any = plus.android.runtimeMainActivity();
|
||
main.moveTaskToBack(false);
|
||
} |