import {
getThirdWebAddress,
getThirdWebAddressByMessage,
facResultHandle
} from '@/api/government.js';
import $CONFIG from "@/config/global.config.js"
import keys from "./keys.js";
import config from '@/config/global.config.js'
import Vue from 'vue'
const pathHint = () => {
uni.showToast({
icon: 'none',
title: '敬请期待'
})
}
// const skipApps = (path) => {
// if (path === "#") {
// pathHint()
// return false
// }
// uni.navigateTo({
// url: `/pages/web-view/Apps?path=${encodeURIComponent(path)}`
// });
// }
const skipAggregation = (themeId) => {
if (themeId) {
try {
uni.navigateTo({
url: `/pages/government/Aggregate-page?themeId=${themeId}`,
fail: err => {
console.log('000000000', err)
}
});
} catch (err) {
console.log('未配置页面')
}
} else {
uni.showToast({
icon: 'none',
title: '聚合页面配置错误'
})
}
}
const skipWeb = (path, title) => {
if (path === "#") {
pathHint()
return false
}
uni.navigateTo({
url: `/pages/web-view/Apps?path=${encodeURIComponent(path)}&title=${title || ''}`,
complete: () => {
oldApps = null;
}
});
}
const InternalPage = {
home: '/pages/home',
zhengwujihe: '/pages/government/more-app',
moreApps: '/pages/government/more-app',
shenghuojihe: '/pages/government/more-app',
call_center: '/pages/government/customer-center',
shop: '/pages/index/lidaPage',
myorder: '/pages/government/myOrder'
}
const tabbarPage = [
'pages/home', 'pages/government/map', 'pages/government/discovery', 'pages/government/mine'
]
const antiShake = true; // 是否开启防抖
let oldApps = null;
let openApp = null;
const getAppLocation = (id, name) => {
openApp = id
getThirdWebAddress(id).then(res => {
try {
// 只打开最新点击的应用
if (antiShake && openApp !== id) {
throw new Error('')
}
// type 应用类型(内部1/第三方h5应用2/第三方微信小程序3)
if (res.data.address === "#") {
pathHint()
throw new Error('')
}
if (res.data.type == 1) {
if (tabbarPage.includes(res.data.address)) {
uni.switchTab({
url: res.data.address,
complete: () => {
oldApps = null;
}
})
} else {
uni.navigateTo({
url: res.data.address,
complete: () => {
oldApps = null;
}
})
}
} else if (res.data.type == 2) {
if (res.data.address) {
skipWeb(res.data.address, name)
}
} else {
Vue.prototype.$openWXminiprogram(res.data);
oldApps = null;
return false
// plus.share.getServices(result => {
// let sweixin = '';
// for (var i = 0; i < result.length; i++) {
// let t = result[i];
// if (t.id == 'weixin') {
// sweixin = t;
// }
// }
// if (sweixin) {
// sweixin.launchMiniProgram({
// id: res.data.appletOriginalId, // 要跳转小程序的原始ID
// path: res.data.address
// },
// // 目标小程序点击返回App后执行的回调,在此接收微信小程序传递的参数
// );
// } else {
// // 没有获取到微信分享服务
// uni.showToast({
// icon: 'none',
// title: '当前环境不支持微信操作!'
// })
// }
// }, error => {
// // 获取分享服务列表失败
// console.log(error)
// })
}
} catch (e) {
//TODO handle the exception
oldApps = null;
}
}).catch(err => {
console.log('请求直接偶遇 catch', err, openApp, id)
oldApps = null;
})
}
// 人脸识别回跳方法
const facRedirectHandle = (data, resData) => {
facResultHandle({
...data,
verifyResult: resData.data
}).then(res => {
let url = ''
if (res.code == 200) {
let result = res.data;
console.log(result)
url = `${data.redirect}?isFac=${result.isFac}&result=${result.result}`
} else {
url = `${data.redirect}?isFac=false`
}
skipWeb(url)
})
}
const getMessageAppLocation = (appId, messageAddress) => {
getThirdWebAddressByMessage(appId, messageAddress).then(res => {
if (res.data.address) {
skipWeb(res.data.address)
}
}).catch(err => {})
}
const filePathHandle = (path, str) => {
if (path.startsWith(str)) {
return path
}
return config.imgURL + path
}
export default {
install(Vue) {
Vue.prototype.$keys = keys;
Vue.prototype.$skeletonTime = 500; // 骨架屏幕过度事件
Object.defineProperty(Vue.prototype, '$skipWeb', {
value: (path, title) => {
skipWeb(path, title)
}
});
/**
* 图片连接封装, 地址拼接
*
*/
Object.defineProperty(Vue.prototype, '$getImgPath', {
value: (path = '') => {
// let img = ""
// if (path.indexOf('http://') !== 0 && path.indexOf('https://') !== 0) {
// if (path.indexOf('/') !== 0) {
// img = $CONFIG.imgURL + '/' + path
// } else {
// img = $CONFIG.imgURL + path
// }
// }
// return img || path
return path
}
// value: (path = '', str = 'http') => {
// return filePathHandle(path, str)
// }
})
Object.defineProperty(Vue.prototype, '$onDesensitization', {
value: (type, text) => {
let val = '';
if (text) {
switch (type) {
case 'name':
let arr = text.split("")
arr[0] = "*"
val = `${arr.join("")}`;
// val = `**${text.substring(text.length - 1)}`;
break;
case 'tel':
const tel = '' + text;
val = tel.replace(tel.substring(3, 7), "****")
break;
}
}
return val
}
})
Object.defineProperty(Vue.prototype, '$editorFile', {
value: (content = '') => {
// const regex = /src="([^"]*)"/g;
const regex = /
]*src="([^"]*)"/g;
const updatedText = content.replace(regex, `
{
if (antiShake && oldApps && oldApps.id === dataVal.id) {
// 防止频繁点击
console.log('开启防抖')
return false
}
oldApps = dataVal;
if (type) {
switch (type) {
case 1:
// 聚合页面
skipAggregation(dataVal);
break;
}
oldApps = null;
} else if (dataVal.hasOwnProperty('jumpType')) {
// 金刚区
const {
jumpType
} = dataVal
try {
if (jumpType === undefined || jumpType === null) {
if (dataVal.resourceModel == 3) {
uni.navigateTo({
url: `/pages/news/news_detail?informationId=${dataVal.resourceModelId}`
})
}
throw new Error(1);
}
switch (Number(jumpType)) {
case 0:
// 内部页面
const {
innerPageCode
} = dataVal;
let path = ``;
switch (innerPageCode) {
case "zhengwujihe":
path =
`${InternalPage.moreApps}?key=${keys.ZW_MOER_APPS}&title=${dataVal.name || '更多服务'}`;
break;
case "shenghuojihe":
path =
`${InternalPage.moreApps}?key=${keys.SH_MOER_APPS}&title=${dataVal.name || '更多服务'}`;
break;
default:
path = InternalPage[innerPageCode];
break;
}
// console.log(path)
try {
uni.navigateTo({
url: path,
fail: err => {
// console.log('跳转 = ' , err)
uni.switchTab({
url: path
});
}
});
} catch (err) {
console.log('未配置页面')
}
break;
case 1:
// 外部链接
const {
outsideAddress,
name
} = dataVal;
skipWeb(outsideAddress, name)
break;
case 2:
// 内部资源模型
if (dataVal.resourceModel === 1) {
// 获取应用地址
getAppLocation(dataVal.resourceModelId, dataVal.name)
return
} else if (dataVal.resourceModel === 2) {
// 聚合页面
skipAggregation(dataVal.resourceModelId)
} else if (dataVal.resourceModel === 3) {
// 新闻页面 dataVal.resourceModelId
uni.navigateTo({
url: `/pages/news/news_detail?informationId=${dataVal.resourceModelId}`
})
} else if (dataVal.resourceModel === 4) {
// 游戏大转盘
uni.navigateTo({
url: `/pages/game/Lottery?activityId=${dataVal.resourceModelId}`
})
}
break;
}
} catch (e) {
const TYPE = e.message
let message = ''
switch (Number(TYPE)) {
case 1:
message = '系统设置错误,请联系管理员';
break;
}
if (message) uni.showToast({
title: message,
icon: 'none'
})
}
oldApps = null;
} else {
getAppLocation(dataVal.thirdApplicationId, dataVal.name);
return
}
}
});
// Object.defineProperty(Vue.prototype, '$ApiCallback', {
// value: async (Api, params) => {
// const {
// path,
// method
// } = Api;
// let data = null
// switch (method) {
// case 'get':
// data = await http.get(path, params);
// break;
// case 'post':
// data = await http.post(path, params);
// break;
// }
// if (data && data.code === 200) {
// return Promise.resolve(data)
// } else {
// return Promise.reject(2)
// }
// }
// });
/**
* 校验是否已登录
* redirect : 是否需要重定向,true时直接跳转登录页面,false 则不跳转,通过Promise.reject返回错误,页面自行处理,
* 默认为true
*/
Object.defineProperty(Vue.prototype, '$VerifyLogin', {
value: (redirect = true) => {
return Promise.resolve()
// return Promise.reject(345)
}
});
// export const ApiCallback = async (Api, params) => {
// const {
// path,
// method
// } = Api;
// let data = null
// switch (method) {
// case 'get':
// data = await http.get(path, params);
// break;
// case 'post':
// data = await http.post(path, params);
// break;
// }
// if (data && data.code === 200) {
// return Promise.resolve(data)
// } else {
// return Promise.reject(2)
// }
// }
/**
* 消息跳转
* type
* applicationId 第三方应用 appid
* linkAddress 第三方应用地址
*/
Object.defineProperty(Vue.prototype, '$openMessage', {
value: (messageInfo) => {
if (messageInfo.type == 1) {
getMessageAppLocation(messageInfo.applicationId, messageInfo.linkAddress)
}
}
});
/**
* 唤醒小程序
* 人脸识别
* typeValue 0 纯唤醒小程序 1 授权登录 2 人脸识别
*/
Object.defineProperty(Vue.prototype, '$openWXminiprogram', {
value: (data = {}, typeValue = 0, auth = false) => {
return new Promise((resolve, reject) => {
plus.share.getServices(result => {
let sweixin = '';
for (var i = 0; i < result.length; i++) {
let t = result[i];
if (t.id == 'weixin') {
sweixin = t;
}
}
if (sweixin) {
let id = ''
let path = ''
let type = 0
switch (typeValue) {
case 0:
id = data.appletOriginalId
path = data.address
break;
case 1:
id = 'gh_9b86ebba08b0'
type = 0
path = `/pages/authLogin/authLogin?appName=宜格服务`
break;
case 2:
id = 'gh_9b86ebba08b0'
type = 0
path =
`/pages/identify/face_indentify?name=${data.realName}&idCardNumber=${data.identityCard}&channel=app`
break;
}
sweixin.launchMiniProgram({
// id:"gh_9b86ebba08b0", // 要跳转小程序的原始ID
// type: 2, // 微信小程序版本类型可取值: 0-正式版; 1-测试版; 2-体验版。 默认值为0。
// path: `pages/identify/face_indentify?name=${data.realName}&idCardNumber=${data.identityCard}&channel=app`
id: id,
type: type,
path: path
},
// 目标小程序点击返回App后执行的回调,在此接收微信小程序传递的参数
res => {
let resData = JSON.parse(res)
if (typeValue == 1) {
resolve(resData)
} else if (typeValue == 2) {
if (auth) {
resolve(resData)
} else {
facRedirectHandle(data, resData)
}
}
},
err => {
console.log(err)
uni.showToast({
icon: 'none',
title: '当前环境不支持微信操作!'
})
}
);
} else {
// 没有获取到微信分享服务
uni.showToast({
icon: 'none',
title: '当前环境不支持微信操作!'
})
}
}, error => {
// 获取分享服务列表失败
console.log(error)
})
})
}
});
/**
* 任务状态说明
* checkStatus 审状态(0未提交,1待审核,2驳回,3审核通过)
*/
Object.defineProperty(Vue.prototype, '$TaskStatusText', {
value: (status) => {
let text = "";
if (status >= 0) {
switch (status) {
case 0:
text = "未提交";
break;
case 1:
text = "待审核";
break;
case 2:
text = "驳回";
break;
case 3:
text = "审核通过";
break;
}
}
return text
}
})
}
}