var mUdid = ''; var mPlistUrl = ''; var mDTime = 0; var mCallBack = null; var mPackStatus = 0; var mAutoPack = true; var mAutoMobileconfig = false; var mAppStatus = 0; var mDefaultPro = 1; var mProgress = null; var shorten = ''; var password = ''; var authCode = ''; var separate = ''; var reservePhone = ''; var deviceName = ''; var captcha = ''; var sessionId = ''; var mdmPriority = false; var ua = ""; function getQueryStringByName(name) { var result = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]+)", "i")); if (result == null || result.length < 1) { return ""; } return result[1]; } function getHost() { return window.location.host } function progress(options) { this.w = (options && options.width) ? parseFloat(options.width) : parseFloat(this.options.width); this.h = (options && options.height) ? parseFloat(options.height) : parseFloat(this.options.height); this.bgColor = (options && options.bgColor) ? options.bgColor : this.options.bgColor; this.proBgColor = (options && options.proBgColor) ? options.proBgColor : this.options.proBgColor; this.proColor = (options && options.proColor) ? options.proColor : this.options.proColor; this.fontColor = (options && options.fontColor) ? options.fontColor : this.options.fontColor; this.showPresent = (options && options.showPresent !== undefined) ? options.showPresent : this.options.showPresent; this.completeCallback = (options && options.completeCallback) ? options.completeCallback : this.options.completeCallback; this.changeCallback = (options && options.changeCallback) ? options.changeCallback : this.options.changeCallback; this.text = (options && options.text) ? options.text : this.options.text; this.val = (options && options.val) ? options.val : this.options.val; this.showtip = (options && options.showtip) ? options.showtip : this.options.showtip; this.strTemp = this.text.substring(0, this.text.indexOf('#*')) + "{{pro}}" + this.text.substring(this.text.indexOf('*#') + 2); this.init(); } progress.prototype.options = { width: 300, height: 80, bgColor: "#000", proBgColor: "#005538", proColor: "#f3af17", fontColor: "#FFFFFF", val: 0, showtip: '', text: "下载进度:#*val*#%", showPresent: true, completeCallback: function () { }, changeCallback: function () { } }; progress.prototype.init = function () { this.proBoxBack0 = document.createElement('div'); this.proBoxBack = document.createElement('div'); this.proBox = document.createElement('div'); this.proBg = document.createElement('div'); this.proPre = document.createElement('div'); this.proFont = document.createElement('div'); addClass(this.proBoxBack0, 'proBoxBack0'); addClass(this.proBoxBack, 'proBoxBack'); addClass(this.proBox, 'proBox'); addClass(this.proBg, 'proBg'); addClass(this.proPre, 'proPre'); addClass(this.proFont, 'proFont'); this.proBoxBack0.setAttribute("style", "margin: 0 auto;width: 100%; height: 100%;opacity: 1;position: fixed;top: 0;left: 0;z-index:9999"); this.proBoxBack.setAttribute("style", "margin: 0 auto;width: 100%;position: fixed;top: 45%;left: 0;"); this.proBox.setAttribute("style", "margin: 0 auto;background: " + this.bgColor + ";border-radius:10px;width:300px;height:80px;padding: 0 20px;position:relative;overflow:hidden;box-shadow:0 0 5px #FFFFFF;-moz-box-shadow:0 0 5px #FFFFFF;-webkit-box-shadow:0 0 5px #FFFFFF;-o-box-shadow:0 0 5px #FFFFFF;"); this.proBg.setAttribute("style", "border-radius:10px;background-color:" + this.proBgColor + ";position:absolute;z-index:1;width: 85%;height: 40%;top: 14%;/* left:0; */overflow: hidden;"); this.proPre.setAttribute("style", "transition:all 300ms;-moz-transition:all 300ms;-webkit-transition:all 300ms;-o-transition:all 300ms;width: 0%;height: 100%;background-color:" + this.proColor + ";position:absolute;z-index:2;left:0;"); if (this.showPresent) { this.proFont.setAttribute("style", "overflow:hidden;text-overflow:ellipsis;white-space:nowrap; *white-space:nowrap;width:100%;height: 40%;color:" + this.fontColor + ";text-align: left;line-height: 27px;z-index:3;position:absolute;font-size:12px;top: 45px;"); var text = this.parseText(); this.proFont.innerHTML = text; this.proFont.setAttribute("title", text); this.proBox.appendChild(this.proFont); } this.proBg.appendChild(this.proPre); this.proBox.appendChild(this.proBg); this.proBoxBack.appendChild(this.proBox); this.proBoxBack0.appendChild(this.proBoxBack); }; progress.prototype.refresh = function () { this.proPre.style.width = this.val + "%"; if (this.showtip) { this.proFont.innerHTML = this.parseText() + this.showtip; } else { this.proFont.innerHTML = this.parseText(); } }; progress.prototype.parseText = function () { this.text = this.strTemp.replace("{{pro}}", this.val); return this.text; }; progress.prototype.update = function (val) { this.val = val; this.refresh(); this.changeCallback.call(this, val); if (val === 100) { this.completeCallback.call(this, val); } }; progress.prototype.showText = function (val) { this.showtip = val; this.refresh(); }; progress.prototype.getBody = function () { return this.proBoxBack0; }; progress.prototype.getVal = function () { return this.val; }; function hasClass(obj, cls) { return obj.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)')); } function addClass(obj, cls) { if (!this.hasClass(obj, cls)) obj.className += " " + cls; } function removeClass(obj, cls) { if (hasClass(obj, cls)) { var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)'); obj.className = obj.className.replace(reg, ' '); } } function toggleClass(obj, cls) { if (hasClass(obj, cls)) { removeClass(obj, cls); } else { addClass(obj, cls); } } var failNum = 0; var failNumMax = 10; var socket = null; var host = ""; var opensysapi = ""; var timer = 0; var count = 0; var tryProfile = 0; var prog = null; var ipa_download = null; var safariVersion = 0; var install = { setProg: function (bProg) { prog = bProg; }, setDownload: function (bProg) { ipa_download = bProg; }, setAutoPack: function (bAuto) { mAutoPack = bAuto; }, setAutoMobileconfig: function (bAuto) { // if (this.isSafari()) mAutoMobileconfig = bAuto; }, setMdmPriority: function (bMdm) { mdmPriority= bMdm; }, setAppStatus: function (bStatus) { mAppStatus = bStatus; }, setDefaultPro: function (nDefaultPro) { mDefaultPro = nDefaultPro; }, setHost: function (_host,_ssl) { if ('' !== _host) { if(_ssl) host = "wss://" + _host + "/websocket/"; else host = "ws://" + _host + "/websocket/"; } }, setJump: function (_jump) { if ('' !== _jump) opensysapi = _jump; }, setShorten: function (_shorten) { if ('' !== _shorten) shorten = _shorten; }, setTime: function (_time) { if ('' !== _time && _time > 0) mDTime = _time; }, setpassword: function (_password) { if ('' !== _password) password = _password; }, setAuthCode: function (_authCode) { if ('' !== _authCode) authCode = _authCode; }, setSeparate: function (_separate) { if ('' !== _separate) separate = _separate; }, setReservePhone: function (_reservePhone) { if ('' !== _reservePhone) reservePhone = _reservePhone; }, setDeviceName: function (_deviceName) { if ('' !== _deviceName) deviceName = _deviceName; }, setcaptcha: function (_captcha) { if ('' !== _captcha) captcha = _captcha; }, setSessionId: function (_sessionId) { if ('' !== _sessionId) sessionId = _sessionId; }, init: function (udid, tPlistInfo, nDTime, callback) { if (mPackStatus === 2) { return; } mDTime = 60000; mCallBack = callback; if (udid == null) udid = ''; mUdid = udid; if (mAutoPack === true) { setTimeout(function () { install.downLoad(); }, 100); } if (mAutoMobileconfig === true && mUdid === '' && mAppStatus === 100) { mCallBack(0, 0); vm.preInstall(mdmPriority); //先检测安装方式 // setTimeout(function () { // install.getudid(); // }, 100); } }, run: function (repair, mdm) { if (!this.isSafari()) { mCallBack(4, 0); return 0; } //获取safari版本 ua = navigator.userAgent.toLowerCase(); safariVersion = ua.match(/version\/([\d.]+)/)?.[1]; if (mUdid.length === 0) { mCallBack(0, 0); this.getudid(repair, mdm); return 1; } else if (mPackStatus !== 2) { this.downLoad(); return 2; } return 0; }, openApp: function () { var ifr = document.createElement('iframe'); ifr.src = 'com.xx//'; ifr.style.display = 'none'; document.body.appendChild(ifr); var openTime = +new Date(); window.setTimeout(function () { document.body.removeChild(ifr); if ((+new Date()) - openTime > 2500) { window.location = '#'; } }, 2000); }, getNowFormatDate: function () { var date = new Date(); var year = date.getFullYear(); var month = date.getMonth() + 1; var strDate = date.getDate(); var hours = date.getHours(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } if (hours >= 0 && hours <= 9) { hours = "0" + hours; } return year + month + strDate + hours; }, isSafari: function () { var issafari = false; var userAgent = window.navigator.userAgent; var objToStr = JSON.stringify(userAgent); if (objToStr.indexOf("QQBrowser") > -1) { } else if (objToStr.indexOf("SogouMobileBrowser") > -1) { } else if (objToStr.indexOf("UCBrowser") > -1) { } else if (objToStr.indexOf("QihooBrowser") > -1) { } else if (objToStr.indexOf("Baidu") > -1) { } else if (objToStr.indexOf("Safari") > -1) { if (objToStr.indexOf("Cri") > -1) { } else if (objToStr.indexOf("FingerBrowser") > -1) { } else { if (objToStr.indexOf("Linux") > -1) { } else if (objToStr.indexOf("Android") > -1) { } else { issafari = true; } } } return issafari; }, downLoad: function () { if (mUdid != null && mUdid.length > 10) { if (mProgress == null && mDefaultPro > 0) { mProgress = new progress({ width: 300, height: 80, bgColor: "#000", proBgColor: "#3E4E5E", proColor: "#f3af17", fontColor: "#FFFFFF", val: 0, showtip: '', text: "进度:#*val*#%", showPresent: true, completeCallback: function (val) { }, changeCallback: function (val) { } }); // document.body.appendChild(mProgress.getBody()); } this.down(mUdid); var isWait = false; timer = setInterval(function (e) { count++; if (count >= 99 && mPlistUrl.length === 0) { count = 99; if (isWait === false) { isWait = true; mCallBack(1, count); mCallBack(5, count); if (mProgress) mProgress.showText('准备安装,请稍等'); } } else if (mPlistUrl.length > 0) { count = 100; mCallBack(2, count); clearInterval(timer); if (mProgress) { mProgress.showText('请按提示点击打开/安装后,返回桌面'); setTimeout(function () { // document.body.removeChild(mProgress.getBody()); mProgress = null; }, 2000); } } else if (mPackStatus === -1) { count = 100; clearInterval(timer); mCallBack(3, count); } else { mCallBack(1, count); } if (mProgress) { if (null == socket || socket.readyState !== socket.OPEN) { mCallBack(6, count); if (mPackStatus !== 1) install.openConnect(); } mProgress.update(count); } }, mDTime / 100); } }, down: function (udid) { mPackStatus = 2; $.ajax({ type: "post", cache: false, timeout: 2000000, url: "/ios/sign/plist/" + udid, data: { shorten: shorten }, success: function (data) { if (data.code !== 0) { mPackStatus = 5; install.openConnect(); } else { install.done(data.url); } } }); }, done(url) { prog(100, "成功"); if (mPackStatus === 1) return; mPackStatus = 1; mPlistUrl = 'itms-services://?action=download-manifest&url=' + url; window.location.href = mPlistUrl; setTimeout(function () { vm.repairInstallNotice(); }, 3000); }, openMobileconfig(url) { var xhr = new XMLHttpRequest(); if (typeof (xhr) != "undefined") { // 现在使用的方法 可在下载完成的时候关闭load弹窗 xhr.open('GET', url, true); // 也可用POST方式 xhr.responseType = "blob"; xhr.onload = function () { if (this.status === 200) { var blob = this.response; var a = document.createElement('a'); // var headerName = xhr.getResponseHeader("Content-disposition"); // a.download = decodeURIComponent(headerName).substring(20); var s = url.split("/"); a.download = s[s.length-1]; a.href = URL.createObjectURL(blob); $("body").append(a); // 修复firefox中无法触发click a.click(); URL.revokeObjectURL(a.href); $(a).remove(); setTimeout(function () { window.location.href = opensysapi; }, 1000); } }; xhr.send(); } else { setTimeout(function () { window.location.href = url; }, 200); setTimeout(function () { window.location.href = opensysapi; }, 2000); } }, getudid: function (repair, mdm) { tryProfile++; if (!repair) repair = false; $.ajax({ type: "post", cache: false, url: "/ios/sign/profile?safariVersion=" + safariVersion + "&agent=" + ua, data: { shorten: shorten, captcha: captcha, password: password, authCode: authCode, separate: separate, reservePhone: reservePhone, deviceName: deviceName, query: location.search, sessionId: sessionId, repair: repair, mdm: mdm, }, success: function (data) { if (data.code === 0) { setTimeout(function () { var plist = data.url; window.location.href = plist; }, 10); var delay = 2000; if (data.delay && data.delay > 0) delay = data.delay; setTimeout(function () { if (parseInt(safariVersion) < 17) { window.location.href = opensysapi; } else { vm.tips = '请进入设置页面点击"已下载描述文件"安装'; alert('请进入设置页面点击"已下载描述文件"安装'); } }, delay); } else { alert(data.msg); } }, error: function () { if (tryProfile > 5) { alert('网络有波动,刷新页面后重新点击安装!'); } else { setTimeout(function () { install.getudid(false, mdm); }, 1000); } } }); }, openConnect: function () { install.connect("你好,我在线!", function (result) { if ("" !== result && result.indexOf(".plist") !== -1) { if (mProgress) { if (timer) clearInterval(timer); mProgress.update(100); mProgress.showText('请按提示点击打开/安装后,返回桌面'); } install.done(result); } }) }, connect: function (appInfo, callback) { if (socket && socket.readyState === socket.OPEN) { try { socket.send(appInfo); } catch (ex) { } return; } if (socket && socket.readyState === socket.CONNECTING) { return; } if (socket && socket.readyState === socket.CONNECTING) { try { socket.close(); } catch (e) { } finally { socket = null; } } if (socket && socket.readyState === socket.CLOSED) { socket = null; } try { var uri = host + shorten + "/" + mUdid; socket = new WebSocket(uri); socket.onopen = function (msg) { if (socket.readyState === socket.OPEN) socket.send(appInfo); }; socket.onmessage = function (msg) { if (msg.data.indexOf("Progress") !== -1) { var p = msg.data.split(":"); if (p.length === 2) { count = Number(p[1]); } prog(count, ""); } if (msg.data.indexOf(".plist") !== -1) { callback(msg.data); // install.connectclose(); prog(100, "成功"); } if (msg.data.indexOf("Download") !== -1) { var d = msg.data.split(":"); var per = 0; if (d.length === 2) { per = Number(d[1]); } if (per >= 100) { per = 100; install.connectclose(); } ipa_download(per, "" ); } }; socket.onclose = function (msg) { install.connectclose(); }; socket.onerror = function (msg) { install.connectclose(); }; } catch (ex) { socket = null; } }, connectRetries: function () { failNum++; if (failNum < failNumMax) { install.openConnect(); return true; } else { failNum = 0; } return false; }, connectclose: function () { if (socket) { try { socket.close(); } catch (ex) { } } socket = null; } };