// 随机长度字符串 const randomString = (len: number) => { len = len || 10; const chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; const maxPos = chars.length; let str = ""; for (let i = 0; i < len; i++) { str += chars.charAt(Math.floor(Math.random() * maxPos)); } return str; }; const checkVersion = function() { const u = navigator.userAgent; 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.indexOf("QQ") > -1, // 是否QQ tutuApp: u.match("toupaiApp"), app: u.match("toupaiApp") // 是否在app内 }; }; // 获取当前链接参数 const getQueryString = function() { const url = window.location.href; const paramArr = url.slice(url.indexOf("?") + 1).split("&"); const params: any = {}; paramArr.map(param => { const [key, val] = param.split("="); params[key] = decodeURIComponent(val); }); return params; }; const routeTypeConstant = { 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 (车库) // 1是跳到我的座驾 HEADWEAR_PAGE: 8, //跳转到头饰 传参(routerType) : 0 (装扮商城) 或者 1 (头饰库) // 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, //跳转到推荐卡仓库 TASK_CENTER_PAGE: 27, // 跳转到任务中心就是萝卜不足也是跳那里 TEACHER_PUPIL_PAGE: 28, //跳转到师徒页面入口 SQUARE_PUBLISH: 51, // 发布动态 FEEDPACK_PAGE: 52 //跳转反馈页面入口 }; // 客户端方法 const tools = { cookieUtils: { set: function(key:string|number, val:string|number, time:number) { const date = new Date(); const expiresDays = time; date.setTime(date.getTime() + expiresDays * 24 * 3600 * 1000); document.cookie = key + "=" + val + ";expires=" + date.toUTCString(); }, get: function(key:string) { const getCookie = document.cookie.replace(/[ ]/g, ""); const arrCookie = getCookie.split(";"); let val; for (let i = 0; i < arrCookie.length; i++) { const arr = arrCookie[i].split("="); if (key === arr[0]) { val = arr[1]; break; } } return val; }, delete: function(key:string) { const date = new Date(); date.setTime(date.getTime() - 10000); document.cookie = key + "+v; expires =" + date.toUTCString(); } }, nativeUtils: { jumpAppointPage: function(type: string, val: string) { // routerType 跳转名称 // routerVal 跳转需要传的参数 const browser = checkVersion(); const jumpObj:any = {}; jumpObj.routerType = routeTypeConstant[type as keyof typeof routeTypeConstant]; 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 (window.androidJsObj && typeof window.androidJsObj === "object") { if (type.indexOf("_") > -1) { window.androidJsObj.jumpAppointPage(JSON.stringify(jumpObj)); } else { window.androidJsObj.jumpAppointPage(type); } } } } }, getUid: function() { const browser = checkVersion(); let val; if (browser.app) { if (browser.ios) { val = tools.cookieUtils.get("uid"); } else if (browser.android) { // eslint-disable-next-line no-undef if (window.androidJsObj && typeof window.androidJsObj === "object") { val = parseInt(window.androidJsObj.getUid()); } } } else { const locate = getQueryString(); if (!locate.uid && !locate.shareUid) { val = 901189; } else { if (locate.shareUid) { val = locate.shareUid; } else { val = locate.uid; } } } return val; }, getTicket: function() { const browser = 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 (window.androidJsObj && typeof window.androidJsObj === "object") { val = window.androidJsObj.getTicket(); } } } else { val = "app外"; } return val; }, getAppVersion: function() { const browser = checkVersion(); let val; if (browser.app) { if (browser.ios) { val = window.webkit.messageHandlers.getAppVersion.postMessage(null); } else { val = window.androidJsObj.getAppVersion(); } } return val; } } }; const getMsgFromDevice = function() { const browser = checkVersion(); let info = {}; if (browser.app) { if (browser.ios) { window.webkit.messageHandlers.getDeviceId.postMessage(null); window.webkit.messageHandlers.getRoomUid.postMessage(null); } else if (browser.android) { // eslint-disable-next-line no-undef if (window.androidJsObj && typeof window.androidJsObj === "object") { window.info.deviceId = window.androidJsObj.getDeviceId(); window.info.roomUid = window.androidJsObj.getRoomUid(); } window.info.appVersion = tools.nativeUtils.getAppVersion(); } window.info.uid = tools.nativeUtils.getUid(); window.info.ticket = tools.nativeUtils.getTicket(); window.info.appVersion = tools.nativeUtils.getAppVersion(); } else { const params = getQueryString(); if ( params.debug === true || params.debug === "true" || location.hostname === "localhost" ) { window.info.roomUid = 904860; window.info.uid = 1000009; window.info.deviceId = "afhksafhksajhfs1kjahsfkjsf0000aaaaaaaacaa"; window.info.ticket = "eyJhbGciOiJIUzI1NiJ9.eyJ0aWNrZXRfdHlwZSI6bnVsbCwidWlkIjo5MTMzOSwidGlja2V0X2lkIjoiOGJmMTdlYmQtMTY4ZC00NTliLWI0NzUtMDU3M2Y1ZmM4MDQzIiwiZXhwIjozNjAwLCJjbGllbnRfaWQiOiJlcmJhbi1jbGllbnQifQ.MnKGEc6bSkyOkCbx26lib6J0koFDA2uiKnbwkhdk3MU"; } } info = window.info; return info; }; export { randomString, checkVersion, tools, getQueryString, getMsgFromDevice, routeTypeConstant };