Files
eparty-h5/view/eparty/vue-project/rechargeAgent/src/utils/common.js

277 lines
8.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 指定全部变量
/* global linkedme wx */
// import axios from 'axios'
// const axios = require('axios')
const commonJs = {
// 获取设备信息
checkVersion: function () {
let u = navigator.userAgent
// eslint-disable-next-line no-unused-vars
let app = navigator.appVersion
return {
trident: u.indexOf('Trident') > -1, // IE内核
presto: u.indexOf('Presto') > -1, // opera内核
webKit: u.indexOf('AppleWebKit') > -1, // 苹果、谷歌内核
gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') === -1, // 火狐内核
mobile: !!u.match(/AppleWebKit.*Mobile.*/), // 是否为移动终端
ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), // ios终端
android: u.indexOf('Android') > -1 || u.indexOf('Adr') > -1, // android终端
iPhone: u.indexOf('iPhone') > -1, // 是否为iPhone或者QQHD浏览器
iPad: u.indexOf('iPad') > -1, // 是否iPad
webApp: u.indexOf('Safari') > -1, // 是否web应该程序没有头部与底部
weixin: u.indexOf('MicroMessenger') > -1, // 是否微信
qq: u.match(/\sQQ/i) === ' qq', // 是否QQ
EParty: u.match('EParty'),
app: u.match('EParty') // 是否在app内
}
},
// 适配服务器环境(正式/测试)
EnvCheck: function () {
if (window.location.href) {
let _url = window.location.href
let res = _url.match(/beta/)
return (res) ? 'test' : 'live'
}
},
// 获取当前链接需要配置的代理字符串
locateJudge: function () {
if (window.location.href) {
let _url = window.location.href
let res = _url.match(/test|localhost/)
return (res) ? '/api' : ''
// if (res) {
// return '/api';
// } else {
// return '';
// }
}
},
// 获取当前链接参数
getQueryString: function () {
let _url = location.search
// eslint-disable-next-line no-new-object
let theRequest = new Object()
if (_url.indexOf('?') !== -1) {
let str = _url.substr(1)
let strs = str.split('&')
for (let i in strs) {
theRequest[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1])
}
}
return theRequest
},
// 客户端方法
tools: {
cookieUtils: {
set: function (key, val, time) {
let date = new Date()
let expiresDays = time
date.setTime(date.getTime() + expiresDays * 24 * 3600 * 1000)
document.cookie = key + '=' + val + ';expires=' + date.toGMTString()
},
get: function (key) {
let getCookie = document.cookie.replace(/[ ]/g, '')
let arrCookie = getCookie.split(';')
let val
for (let i = 0; i < arrCookie.length; i++) {
let arr = arrCookie[i].split('=')
if (key === arr[0]) {
val = arr[1]
break
}
}
return val
},
delete: function (key) {
let date = new Date()
date.setTime(date.getTime() - 10000)
document.cookie = key + '+v; expires =' + date.toGMTString()
}
},
routerTypeContent: {
ROOM_PAGE: 1, // 跳转房间页 传参routerType: uid
H5_PAGE: 2, // 跳转h5
PURSE_PAGE: 3, // 跳转钱包页
RED_PAGE: 4, // xcRedColor
RECHARGE_PAGE: 5, // 跳转充值页
PERSON_PAGE: 6, // 跳转个人页 传参(routerType) : uid
CAR_PAGE: 7, // 跳转座驾 传参(routerType) : 0 (装扮商城) 或者 1 (车库)
HEADWEAR_PAGE: 8, // 跳转到头饰 传参(routerType) : 0 (装扮商城) 或者 1 (头饰库)
SYSTEMMESSAGE_PAGE: 9, // 系统消息
FAMILY_PAGE: 10, // 跳转到家族页面
GROUP_PAGE: 11, // 跳转到群组
BACKGROUND_PAGE: 12, // 跳转到背景设置 传参(routerType): 0 (装扮商城) 或者 1 (背景库)
NEW_USER_PAGE: 13, // 新秀玩友
INVITE_FRIEND_PAGE: 14, // 邀请好友
PUBLICCHAT_PAGE: 15, // 公聊大厅
XCZ_ACCOUNT_PAGE: 16, // 绑定 xcz 账号
PHONE_NUM_PAGE: 17, // 绑定手机号
PAY_PWD_PAGE: 18, // 设置支付密码
WITHDRAW_RECORD_PAGE: 19, // 提现记录页面
RECOMMEND_CARD_PAGE: 20, // 跳转到推荐卡仓库
TEACHER_PUPIL_PAGE: 28 // 跳转到师徒页面入口
},
nativeUtils: {
jumpAppointPage: function (type, val) {
// routerType 跳转名称
// routerVal 跳转需要传的参数
let browser = commonJs.checkVersion()
let jumpObj = {}
jumpObj.routerType = commonJs.tools.routerTypeContent[type]
if (val) jumpObj.routerVal = val
if (browser.app) {
if (browser.ios) {
if (type.indexOf('_') > -1) {
window.webkit.messageHandlers.jumpAppointPage.postMessage(jumpObj)
} else {
if (val) {
window.webkit.messageHandlers.type.postMessage(val)
} else {
window.webkit.messageHandlers.type.postMessage(null)
}
}
} else if (browser.android) {
// eslint-disable-next-line no-undef
if (androidJsObj && typeof androidJsObj === 'object') {
if (type.indexOf('_') > -1) {
window.androidJsObj.jumpAppointPage(JSON.stringify(jumpObj))
} else {
window.androidJsObj.jumpAppointPage(type)
}
}
}
}
},
getUid: function () {
let browser = commonJs.checkVersion()
let val
if (browser.app) {
if (browser.ios) {
val = commonJs.tools.cookieUtils.get('uid')
} else if (browser.android) {
// eslint-disable-next-line no-undef
if (androidJsObj && typeof androidJsObj === 'object') {
val = parseInt(window.androidJsObj.getUid())
}
}
} else {
let locate = commonJs.getQueryString()
if (!locate.uid && !locate.shareUid) {
val = 935006
} else {
if (locate.shareUid) {
val = locate.shareUid
} else {
val = locate.uid
}
}
}
return val
},
getTicket: function () {
let browser = commonJs.checkVersion()
let val
if (browser.app) {
if (browser.ios) {
val = window.webkit.messageHandlers.getTicket.postMessage(null)
} else if (browser.android) {
// eslint-disable-next-line no-undef
if (androidJsObj && typeof androidJsObj === 'object') {
val = window.androidJsObj.getTicket()
}
}
} else {
val = ''
}
return val
},
getRoomUid: function () {
let browser = commonJs.checkVersion()
let val
if (browser.app) {
if (browser.ios) {
val = window.webkit.messageHandlers.GetRoomUid.postMessage(null)
} else if (browser.android) {
// eslint-disable-next-line no-undef
if (androidJsObj && typeof androidJsObj === 'object') {
val = window.androidJsObj.getRoomUid()
}
}
} else {
val = 'app外'
}
return val
},
getDeviceId: function () {
let browser = commonJs.checkVersion()
let val
if (browser.app) {
if (browser.ios) {
val = window.webkit.messageHandlers.getDeviceId.postMessage(null)
} else if (browser.android) {
// eslint-disable-next-line no-undef
if (androidJsObj && typeof androidJsObj === 'object') {
val = window.androidJsObj.getDeviceId()
}
}
} else {
val = 'app外'
}
return val
}
}
},
// 图片预加载
preloadImage: function (obj) {
let loadLength = 0
let newImages = []
for (let i = 0; i < obj.imageArr.length; i++) {
newImages[i] = new Image()
newImages[i].src = obj.imageArr[i]
newImages[i].onload = newImages[i].onerror = () => {
loadLength++
typeof obj.preloadPreFunc === 'function' && obj.preloadPreFunc(loadLength)
// eslint-disable-next-line eqeqeq
if (loadLength == obj.imageArr.length) {
typeof obj.doneFunc === 'function' && obj.doneFunc()
}
}
}
},
// 调用客户端分享方法
initNav: function (obj) {
// console.log('查看是否调用initNav', Object.keys(obj).length)
if (Object.keys(obj).length === 0) {
// eslint-disable-next-line no-useless-return
return
}
let browser = commonJs.checkVersion()
if (browser.app) {
if (browser.ios) {
window.webkit.messageHandlers.initNav.postMessage(obj)
} else if (browser.android) {
let json = JSON.stringify(obj)
window.androidJsObj.initNav(json)
}
}
}
}
// module.exports = commonJs
export default commonJs