Files
mew-h5/view/mew/common/js/common2.js
2023-11-24 11:33:37 +08:00

676 lines
21 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.

let productUrl = 'https://api.ymlive.fun'; // 正式环境
const testUrl = 'http://beta.api.ymlive.fun'; // 测试环境
function render (templateId, templateData, target) {
var html = template(templateId, templateData);
target.innerHTML += html;
}
// 访问页面域名
var tranUrl = 'https://h5.ymlive.fun';
function dateFormat (date, fmt) {
date = new Date(date);
var o = {
'M+': date.getMonth() + 1,
'd+': date.getDate(),
'h+': date.getHours(),
'm+': date.getMinutes(),
's+': date.getSeconds()
};
// 补全0
function padLeftZero (str) {
return ('00' + str).substr(str.length);
}
// 年份
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
}
// 月日时分秒
for (var k in o) {
if (new RegExp('(' + k + ')').test(fmt)) {
var str = o[k] + '';
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
}
}
date = o = padLeftZero = null;
return fmt;
}
function convert (_url) {
var patt = /\d+/;
var num = _url.match(patt);
var rs = {};
rs.uid = num[0];
return rs;
}
// 传递分享信息给客户端,showUrl为分享的页面链接,为空时表示不分享
// function shareInfo () {
// var _url = 'https://api.kawayisound.xyz/modules/bonus/fight.html';
// var res = EnvCheck();
// if (res == 'test'){
// _url = 'http://apibeta.kawayisound.xyz/modules/bonus/fight.html';
// }
// var info = {
// title: '轻寻与你一起红',
// imgUrl: 'https://img.erbanyy.com/qingxunlogo-256.png',
// desc: '登录即送20红包每天还有分享红包邀请红包分成红包四重红包大礼等你来拿',
// showUrl: _url
// };
// return JSON.stringify(info);
// }
// 根据域名适配环境
function EnvCheck () {
if (window.location.href) {
var _url = window.location.href;
var res = _url.match(/uat/);
var res1 = _url.match(/120.79.211.243/);
var res2 = _url.match(/192.168./)
var res3 = _url.match(/127.0/)
var res4 = _url.match(/beta/)
var res5 = _url.match(/h5dev/)
if (res || res1 || res2 || res3 || res4 || res5) {
tranUrl = 'http://beta.api.nnbc123.cn';
return 'test';
} else {
tranUrl = 'https://h5.nnbc123.cn';
return 'live';
}
}
}
// 根据域名判断 正式环境(含www)/测试环境(含beta), 并返回所需url前缀
// written by zxfxiong
function getUrlPrefix () {
if (!EnvCheck()) return undefined;
return EnvCheck() === 'live' ? productUrl : testUrl;
}
// 根据域名判断是本地打开还是服务器打开
function locateJudge () {
if (window.location.href) {
var _url = window.location.href;
var res = _url.match(/test|localhost/);
if (res) {
return '/api';
} else {
return '';
}
}
}
// 获取地址栏参数
function getQueryString () {
var _url = location.search;
var theRequest = new Object();
if (_url.indexOf('?') != -1) {
var str = _url.substr(1);
strs = str.split('&');
for (var i in strs) {
theRequest[strs[i].split('=')[0]] = decodeURI(strs[i].split('=')[1]);
}
}
return theRequest;
}
// 判断浏览器内核,手机类型
function checkVersion () {
var u = navigator.userAgent,
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
tutuApp: u.match('tutuApp'),
app: u.match('tutuApp') //是否在app内
};
}
// 图片预加载
function preloadImage (obj) {
console.log(obj)
var loadLength = 0,
newImages = [];
for (var i = 0; i < obj.imageArr.length; i++) {
newImages[i] = new Image();
newImages[i].src = obj.imageArr[i];
newImages[i].onload = newImages[i].onerror = function () {
loadLength++;
typeof obj.preloadPreFunc === 'function' && obj.preloadPreFunc(loadLength);
if (loadLength == obj.imageArr.length) {
typeof obj.doneFunc === 'function' && obj.doneFunc();
}
}
}
}
// 判断是否在App内
function isApp () {
var androidBol = false;
var osBol = false;
if (window.androidJsObj && typeof window.androidJsObj === 'object') {
androidBol = true;
}
if (window.webkit) {
console.log(window.webkit);
osBol = true;
}
return (androidBol || osBol);
}
function UrlSearch () {
var name, value;
var str = location.href;
var num = str.indexOf("?")
str = str.substr(num + 1);
var arr = str.split("&");
for (var i = 0; i < arr.length; i++) {
num = arr[i].indexOf("=");
if (num > 0) {
name = arr[i].substring(0, num);
value = arr[i].substr(num + 1);
this[name] = value;
}
}
return value;
}
function erbanMask (channel, tags, params) {
//此函数用于一般的轻寻底层面罩
var browser = checkVersion();
var env = EnvCheck();
// params = params? params:0;
var bol = $.isEmptyObject(params);
var keyId = "978cd79c98264f836450afda1228762e";
if (browser.ios) {
if (env == 'test') {
keyId = '33f560a83c9c40d465711c0038653ca0'
}
console.log('ios_linkedme_keyId:', keyId)
}
var jsonStr = '';
if (!bol) {
jsonStr = JSON.stringify(params);
}
if (!browser.app) {
$('#mask').css('display', 'flex');
var linkData = {
type: env,
channel: channel,
tags: tags,
// ios_custom_url: "https://itunes.apple.com/cn/app/id1252542069?mt=8",
params: jsonStr
};
linkedme.init(keyId, {
type: env
}, null);
linkedme.link(linkData, function (err, response) {
if (err) {
// 生成深度链接失败返回错误对象err
console.log('err:', err);
} else {
console.log(response);
$('#download a').attr("href", response.url);
$('.download a').attr('href', response.url);
}
}, false);
} else {
$('#mask').hide();
}
}
function wxConfig () {
var wxurl = encodeURIComponent(location.href.split('#')[0]);
var data = "url=" + wxurl;
console.log(data);
$.ajax({
type: 'GET',
url: '/wx/config',
data: data,
asyc: true,
success: function (data) {
if (data.code = 200) {
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来若要查看传入的参数可以在pc端打开参数信息会通过log打出仅在pc端时才会打印。
appId: 'wx009d793f92c24eec', // 必填,公众号的唯一标识
timestamp: data.data.timestamp, // 必填,生成签名的时间戳
nonceStr: data.data.nonceStr, // 必填,生成签名的随机串
signature: data.data.signature, // 必填签名见附录1
jsApiList: data.data.jsApiList // 必填需要使用的JS接口列表所有JS接口列表见附录2
});
wx.error(function (res) {
console.log('config error,msg:' + res);
});
}
},
error: function (res) {
console.log('config error,msg:' + res);
}
})
}
function refreshWeb () {
window.location.href = window.location.href;
}
function shareInfo (urlMsg) {
if (urlMsg) {
var env = EnvCheck();
if (env == 'test') {
return 'http://apibeta.kawayisound.xyz/' + urlMsg;
} else {
return 'https://api.kawayisound.xyz/' + urlMsg;
}
}
}
function initNav (obj) {
if ($.isEmptyObject(obj)) {
return;
}
var browser = checkVersion();
console.log(browser);
if (browser.app) {
if (browser.ios) {
window.webkit.messageHandlers.initNav.postMessage(obj);
} else if (browser.android) {
var json = JSON.stringify(obj);
window.androidJsObj.initNav(json);
}
}
}
var tools = {
cookieUtils: {
set: function (key, val, time) {
var date = new Date();
var expiresDays = time;
date.setTime(date.getTime() + expiresDays * 24 * 3600 * 1000);
document.cookie = key + '=' + val + ';expires=' + date.toGMTString();
},
get: function (key) {
var getCookie = document.cookie.replace(/[ ]/g, "");
var arrCookie = getCookie.split(';');
var val;
for (var i = 0; i < arrCookie.length; i++) {
var arr = arrCookie[i].split('=');
if (key === arr[0]) {
val = arr[1];
break;
}
}
return val;
},
delete: function (key) {
var date = new Date();
date.setTime(date.getTime() - 10000);
document.cookie = key + "+v; expires =" + date.toGMTString();
}
},
nativeUtils: {
jumpAppointPage: function (type, val) {
// routerType 跳转名称
// routerVal 跳转需要传的参数
var browser = checkVersion();
var jumpObj = {};
jumpObj.routerType = routeTypeConstant[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) {
if (androidJsObj && typeof androidJsObj === 'object') {
if (type.indexOf('_') > -1) {
window.androidJsObj.jumpAppointPage(JSON.stringify(jumpObj));
} else {
if (val) {
window.androidJsObj.type(val);
} else {
window.androidJsObj.type();
}
}
}
}
}
},
getUid: function () {
var browser = checkVersion();
console.log(browser);
var val;
if (browser.app) {
if (browser.ios) {
val = tools.cookieUtils.get('uid');
} else if (browser.android) {
if (androidJsObj && typeof androidJsObj === 'object') {
val = parseInt(window.androidJsObj.getUid());
}
}
} else {
var locate = getQueryString();
if (!locate.uid && !locate.shareUid) {
val = 901189;
} else {
if (locate.shareUid) {
val = locate.shareUid;
} else {
val = locate.uid;
}
}
}
return val;
},
getTicket: function () {
var browser = checkVersion();
var val;
if (browser.app) {
if (browser.ios) {
val = window.webkit.messageHandlers.getTicket.postMessage(null);
} else if (browser.android) {
if (androidJsObj && typeof androidJsObj === 'object') {
val = window.androidJsObj.getTicket();
}
}
} else {
val = 'app外'
}
return val;
}
}
}
// 透明loading层
var $Loading = {
count: 0,
isFadeOut: false,
show: function () {
this.count++;
if ($('#loadingToast').length >= 1) {
if (this.isFadeOut) {
this.isFadeOut = false;
$('#loadingToast').stop(true).fadeTo(0, 1);
}
} else {
$('body').append('<div id="loadingToast" style="display: none; position: fixed; left: 0; top: 0; width: 100%; height: 100%; z-index: 9999;"></div>')
$('#loadingToast').fadeIn(100);
}
},
hide: function () {
this.count--;
if (this.count === 0) {
this.isFadeOut = true;
$('#loadingToast').fadeOut(200, function () {
$Loading.isFadeOut = false;
$(this).remove();
});
}
}
};
var requestBaseUrl = locateJudge();
// ajax
function request (type, option) {
return $.ajax({
type: type,
url: requestBaseUrl + option.url,
data: option.data,
headers: option.headers,
beforeSend: function (xhr) {
!option.isHideLoading && $Loading.show();
},
success: function (res, status, xhr) {
!option.isHideLoading && $Loading.hide();
typeof option.success === 'function' && option.success(res, status, xhr);
},
error: function (xhr, status, error) {
!option.isHideLoading && $Loading.hide();
typeof option.error === 'function' && option.error(xhr, status, error);
}
})
}
// ajax get
function getJSON (option) {
request('get', option)
}
// ajax post
function postJSON (option) {
request('post', option);
}
// 封装 获取公共参数的方法(客户端提供)
// written by zxfxiong
const methodsFromClient = {
// 注意ios 提供的方法属异步操作
"uid": {
android: () => window.androidJsObj.getUid(),
ios: function () {
window.webkit.messageHandlers.getUid.postMessage(null);
// let allcookies = document.cookie;
// let $uid = allcookies.match(/\d+/);
// console.log($uid[0]);
}
},
"ticket": {
android: () => window.androidJsObj.getTicket(),
ios: function () {
window.webkit.messageHandlers.getTicket.postMessage(null);
}
},
"roomUid": {
android: () => window.androidJsObj.getRoomUid(),
ios: function () {
window.webkit.messageHandlers.getRoomUid.postMessage(null);
}
},
"deviceId": {
android: () => window.androidJsObj.getDeviceId(),
ios: function () {
window.webkit.messageHandlers.getDeviceId.postMessage(null);
}
},
"deviceInfo": {
android: () => window.androidJsObj.getDeviceInfo(),
ios: function () {
window.webkit.messageHandlers.getDeviceInfo.postMessage(null);
}
},
"encryptPwd": {
android: (data) => window.androidJsObj.encryptPwd(data),
ios: function (data) {
window.webkit.messageHandlers.encryptPwd.postMessage(data);
}
}
}
// 全局获取并配置公共参数
// written by zxfxiong
const pubInfo = {};
function getInfoFromClient () {
const browser = checkVersion();
if (browser.app) {
console.log('从客户端获取了用户信息设备信息此信息来源common2.js');
if (browser.android) {
pubInfo.uid = methodsFromClient.uid.android();
pubInfo.ticket = methodsFromClient.ticket.android();
pubInfo.deviceId = methodsFromClient.deviceId.android();
pubInfo.deviceInfo = methodsFromClient.deviceInfo.android();
} else {
methodsFromClient.uid.ios();
methodsFromClient.ticket.ios();
methodsFromClient.deviceId.ios();
methodsFromClient.deviceInfo.ios();
}
} else {
// 非app环境调试参数
pubInfo.uid = 0;
pubInfo.ticket = "";
// pubInfo.uid = "";
// pubInfo.ticket = "";
pubInfo.deviceId = "0";
pubInfo.deviceInfo = {
app: 'mew',
appVersion: '0.0.0',
os: '0.0.0',
osVersion: '0.0.0',
channel: 'browser',
client: 'h5'
};
}
setTimeout(function () {
if (browser.android) {
if (JSON.parse(pubInfo.deviceInfo).app == 'yinbao') {
productUrl = 'https://mew.api.shengxuanwangluo.com';
}
} else {
console.log(pubInfo.deviceInfo.app);
if (JSON.parse(pubInfo.deviceInfo.app == 'yinbao')) {
productUrl = 'https://mew.api.shengxuanwangluo.com';
}
}
}, 50);
};
// 控制body是否可以滑动
function bodyScroolFun (bool) {
if (bool) {
$('body').css('overflow', 'hidden');
} else {
$('body').css('overflow', 'auto');
}
}
// 封装数值超过最大数位处理单位
function unitProcessing (val, num, toFixeds, text) { //值 以什么为单位 保留几位小数 单位后最w
return val >= num ? (Math.floor(val / 1000) / 10).toFixed(toFixeds) + text : val;
}
// 封装 在ios环境中 配置公共参数的回调函数
// 配合 methodsFromClient[infoName].ios 方法
// written by zxfxiong
function getMessage (key, value) {
pubInfo[key] = value;
}
function objToParam (a) {
var s = [],
rbracket = /\[\]$/,
isArray = function (obj) {
return Object.prototype.toString.call(obj) === '[object Array]';
},
add = function (k, v) {
v = typeof v === 'function' ? v() : v === null ? '' : v === undefined ? '' : v;
s[s.length] = encodeURIComponent(k) + '=' + encodeURIComponent(v);
},
buildParams = function (prefix, obj) {
var i, len, key;
if (prefix) {
if (isArray(obj)) {
for (i = 0, len = obj.length; i < len; i++) {
if (rbracket.test(prefix)) {
add(prefix, obj[i]);
} else {
buildParams(prefix + '[' + (typeof obj[i] === 'object' ? i : '') + ']', obj[i]);
}
}
} else if (obj && String(obj) === '[object Object]') {
for (key in obj) {
buildParams(prefix + '[' + key + ']', obj[key]);
}
} else {
add(prefix, obj);
}
} else if (isArray(obj)) {
for (i = 0, len = obj.length; i < len; i++) {
add(obj[i].name, obj[i].value);
}
} else {
for (key in obj) {
buildParams(key, obj[key]);
}
}
return s;
};
return buildParams('', a).join('&').replace(/%20/g, '+');
}
// 封装 jquery 请求
// written by zxfxiong
function networkRequest (reqObj = {}, type) {
// 关于reqObj内部参数与 调用原生$.ajax()时传参一致
// 调用该函数前:须保证事先调用了 getInfoFromClient()
if (typeof pubInfo.deviceInfo === 'string') {
pubInfo.deviceInfo = JSON.parse(pubInfo.deviceInfo);
};
const pubHeader = {
app: type != 'yinbaos' ? pubInfo.deviceInfo.app : 'yinbao',
// app: 'mew',
// app: pubInfo.deviceInfo.app.indexOf('mew') != -1 ? 'mew' : 'PlanetStar',
appVersion: pubInfo.deviceInfo.appVersion || '',
os: pubInfo.deviceInfo.os || '',
os_version: pubInfo.deviceInfo.osVersion || '',
channel: pubInfo.deviceInfo.channel || '',
client: 'h5',
pub_uid: pubInfo.uid,
pub_ticket: pubInfo.ticket
};
const url = reqObj.url;
const commParams = objToParam(pubHeader);
if (url.indexOf('?') >= 0) {
reqObj.url = `${url}&${commParams}`
} else {
reqObj.url = `${url}?${commParams}`
}
if (!reqObj.headers || typeof reqObj.headers !== 'object') {
reqObj.headers = {};
};
Object.assign(reqObj.headers, pubHeader);
const response = $.ajax(reqObj);
return response;
}