351 lines
12 KiB
JavaScript
351 lines
12 KiB
JavaScript
|
||
function init(data) {
|
||
//console.log(data.apk_version)
|
||
// console.log(localStorage.getItem('jsUrl'))
|
||
localStorage.setItem('jsUrl', "../js/");
|
||
localStorage.setItem('cssUrl', "../css/");
|
||
localStorage.setItem('version', "96");
|
||
|
||
apk_version = data.apk_version;
|
||
apkUrl = data.apk_down_url;
|
||
iosUrl = data.ios_down_url;
|
||
var must_update = data.must_update;
|
||
var ios_must_update = data.ios_must_update;
|
||
mui.plusReady(function() {
|
||
plus.navigator.setStatusBarStyle('dark');
|
||
plus.runtime.getProperty(plus.runtime.appid, function(inf) {
|
||
ver = inf.version;
|
||
var client;
|
||
var ua = navigator.userAgent.toLowerCase();
|
||
if(/iphone|ipad|ipod/.test(ua)) { //苹果手机
|
||
$.ajax({
|
||
type: "get",
|
||
dataType: 'json',
|
||
url: data.update_url, //获取当前上架APPStore版本信息
|
||
data: {
|
||
id: data.ios_appid //APP唯一标识ID
|
||
},
|
||
contentType: 'application/x-www-form-urlencoded;charset=UTF-8',
|
||
success: function(data) {
|
||
|
||
if(data.results[0].version > ver) {
|
||
if(confirm("发现新版本:V" + data.results[0].version + "是否更新")) {
|
||
document.location.href = iosUrl; //上新APPStore下载地址
|
||
}
|
||
} else {
|
||
return;
|
||
}
|
||
}
|
||
});
|
||
} else if(/android/.test(ua)) {
|
||
if(apk_version > ver) {
|
||
if(confirm("发现新版本:V" + apk_version + "是否更新")) {
|
||
downloadUpdate(apkUrl)
|
||
} else {
|
||
plus.runtime.quit();
|
||
}
|
||
} else {
|
||
// console.log('当前版本号已是最新');
|
||
return;
|
||
}
|
||
}
|
||
|
||
})
|
||
})
|
||
};
|
||
function downloadUpdate(apkUrl){
|
||
let waiting = plus.nativeUI.showWaiting("正在下载更新包");
|
||
var dtask = plus.downloader.createDownload(apkUrl, {}, function(d, status) {
|
||
if(status == 200) {
|
||
waiting.close()
|
||
plus.nativeUI.toast("正在准备环境,请稍后!");
|
||
var path = d.filename; //下载apk
|
||
plus.runtime.install(path,{},function(){
|
||
plus.nativeUI.alert("应用资源更新完成!",function(){
|
||
plus.runtime.restart();
|
||
});
|
||
},function(e){
|
||
plus.nativeUI.alert("更新失败["+e.code+"]:"+e.message);
|
||
}); // 自动安装apk文件
|
||
} else {
|
||
waiting.setTitle("下载失败")
|
||
setTimeout(function(){waiting.close()}, 2000)
|
||
mui.alert('版本更新失败:' + status);
|
||
}
|
||
});
|
||
dtask.start();
|
||
dtask.addEventListener("statechanged",function(download, status) {
|
||
if(download.state != 4){
|
||
var text = "("+(download.downloadedSize*100/download.totalSize).toFixed(0)+"%)"
|
||
waiting.setTitle('正在下载'+text)
|
||
}
|
||
if(download.state == 4){
|
||
// 下载完成
|
||
if(status == 200){
|
||
waiting.close()
|
||
}else{
|
||
waiting.setTitle("下载失败")
|
||
setTimeout(function(){waiting.close()}, 2000)
|
||
}
|
||
console.log("Download success: " + download.getFileName());
|
||
}
|
||
},false)
|
||
}
|
||
function jumpPage(ipxSizeBottom) {
|
||
//跳转页面
|
||
|
||
var bSize = 50 + (+ipxSizeBottom) + 'px';
|
||
var subpages = ['templete/home.html', 'templete/shops.html', 'templete/zhugouba.html', 'templete/zhuweiba.html',
|
||
'templete/my.html'
|
||
];
|
||
var subpage_style = {
|
||
top: '0px',
|
||
bottom: bSize,
|
||
scrollIndicator: 'none'
|
||
};
|
||
var aniShow = {}; //动画显示
|
||
//首次启动切滑效果
|
||
mui.plusReady(function() {
|
||
//刷新
|
||
window.addEventListener('refresh', function(e) {
|
||
var my = plus.webview.getWebviewById('templete/my.html'); //触发父页面的自定义事件(refresh),从而进行刷新
|
||
var zhuweiba = plus.webview.getWebviewById('templete/zhuweiba.html'); //触发父页面的自定义事件(refresh),从而进行刷新
|
||
var zhugouba = plus.webview.getWebviewById('templete/zhugouba.html'); //触发父页面的自定义事件(refresh),从而进行刷新
|
||
mui.fire(my, 'refresh');
|
||
mui.fire(zhuweiba, 'refresh');
|
||
mui.fire(zhugouba, 'refresh');
|
||
})
|
||
// launchScreen();
|
||
plus.screen.lockOrientation("portrait-primary");
|
||
// plus.navigator.setStatusBarStyle('dark');
|
||
// console.log(plus.navigator.getStatusBarStyle())
|
||
var self = plus.webview.currentWebview();
|
||
for (var i = 0; i < subpages.length; i++) {
|
||
var temp = {};
|
||
//http://www.html5plus.org/doc/zh_cn/webview.html#plus.webview.create
|
||
var sub = plus.webview.create(subpages[i], subpages[i], subpage_style);
|
||
if (i > 0) {
|
||
sub.hide();
|
||
} else {
|
||
temp[subpages[i]] = "true";
|
||
mui.extend(aniShow, temp); //合并对象
|
||
}
|
||
self.append(sub);
|
||
}
|
||
});
|
||
//当前激活选项
|
||
var activeTab = subpages[0];
|
||
document.addEventListener("NAV_TO", function(e){
|
||
var id = parseInt(e.detail);
|
||
var $all = $(".mui-bar-tab a");
|
||
if (id > $all.length || id < 0) return;
|
||
var href = $all.eq(id).attr('href');
|
||
plus.webview.show(href, "fade-in", 300);
|
||
activeTab = href;
|
||
$all.eq(id).trigger('tap').click();
|
||
})
|
||
|
||
//选项卡点击事件
|
||
mui('.mui-bar-tab').on('tap', 'a', function(e) {
|
||
var targetTab = this.getAttribute('href');
|
||
console.log(targetTab);
|
||
if (targetTab == activeTab) {
|
||
// var targetTab = plus.webview.getWebviewById(targetTab);
|
||
// mui.fire(targetTab, 'refresh');
|
||
return;
|
||
}
|
||
//更换标题
|
||
// title.innerHTML = this.querySelector('.mui-tab-label').innerHTML;
|
||
//显示目标选项卡
|
||
//若为iOS平台或非首次显示,则直接显示
|
||
if (mui.os.ios || aniShow[targetTab]) {
|
||
plus.webview.show(targetTab);
|
||
|
||
} else {
|
||
//否则,使用fade-in动画,且保存变量
|
||
var temp = {};
|
||
temp[targetTab] = "true";
|
||
mui.extend(aniShow, temp);
|
||
plus.webview.show(targetTab, "fade-in", 300);
|
||
}
|
||
//隐藏当前;
|
||
plus.webview.hide(activeTab);
|
||
//更改当前活跃的选项卡
|
||
activeTab = targetTab;
|
||
if (targetTab == 'templete/home.html') {}
|
||
if (targetTab == 'templete/shops.html') {
|
||
var targetTab = plus.webview.getWebviewById(targetTab);
|
||
mui.fire(targetTab, 'refresh');
|
||
}
|
||
|
||
if (targetTab == 'templete/zhuweiba.html') {
|
||
var targetTab = plus.webview.getWebviewById(targetTab);
|
||
mui.fire(targetTab, 'refresh');
|
||
var token = localStorage.getItem("token");
|
||
if (!token) {
|
||
mui.openWindow({
|
||
url: 'templete/login.html',
|
||
id: 'templete/login.html',
|
||
styles: {
|
||
top: '0px', //新页面顶部位置
|
||
bottom: '0px', //新页面底部位置
|
||
width: '100%', //新页面宽度,默认为100%
|
||
height: '100%' //新页面高度,默认为100%
|
||
},
|
||
extras: {
|
||
// data_href: data_href
|
||
// ..... //自定义扩展参数,可以用来处理页面间传值
|
||
},
|
||
createNew: false, //是否重复创建同样id的webview,默认为false:不重复创建,直接显示
|
||
show: {
|
||
// autoShow: true, //页面loaded事件发生后自动显示,默认为true
|
||
// aniShow: animationType, //页面显示动画,默认为”slide-in-right“;
|
||
// duration: animationTime, //页面动画持续时间,Android平台默认100毫秒,iOS平台默认200毫秒;
|
||
// event: 'titleUpdate', //页面显示时机,默认为titleUpdate事件时显示
|
||
// extras: {} //窗口动画是否使用图片加速
|
||
},
|
||
waiting: {
|
||
autoShow: true, //自动显示等待框,默认为true
|
||
title: '正在加载...', //等待对话框上显示的提示内容
|
||
options: {
|
||
// width: waiting - dialog - widht, //等待框背景区域宽度,默认根据内容自动计算合适宽度
|
||
// height: waiting - dialog - height, //等待框背景区域高度,默认根据内容自动计算合适高度
|
||
// ......
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
if (targetTab == 'templete/my.html') {
|
||
var targetTab = plus.webview.getWebviewById(targetTab);
|
||
mui.fire(targetTab, 'refresh');
|
||
var token = localStorage.getItem("token");
|
||
if (!token) {
|
||
mui.openWindow({
|
||
url: 'templete/login.html',
|
||
id: 'templete/login.html',
|
||
styles: {
|
||
top: '0px', //新页面顶部位置
|
||
bottom: '0px', //新页面底部位置
|
||
width: '100%', //新页面宽度,默认为100%
|
||
height: '100%' //新页面高度,默认为100%
|
||
},
|
||
extras: {
|
||
// data_href: data_href
|
||
// ..... //自定义扩展参数,可以用来处理页面间传值
|
||
},
|
||
createNew: false, //是否重复创建同样id的webview,默认为false:不重复创建,直接显示
|
||
show: {
|
||
// autoShow: true, //页面loaded事件发生后自动显示,默认为true
|
||
// aniShow: animationType, //页面显示动画,默认为”slide-in-right“;
|
||
// duration: animationTime, //页面动画持续时间,Android平台默认100毫秒,iOS平台默认200毫秒;
|
||
// event: 'titleUpdate', //页面显示时机,默认为titleUpdate事件时显示
|
||
// extras: {} //窗口动画是否使用图片加速
|
||
},
|
||
waiting: {
|
||
autoShow: true, //自动显示等待框,默认为true
|
||
title: '正在加载...', //等待对话框上显示的提示内容
|
||
options: {
|
||
// width: waiting - dialog - widht, //等待框背景区域宽度,默认根据内容自动计算合适宽度
|
||
// height: waiting - dialog - height, //等待框背景区域高度,默认根据内容自动计算合适高度
|
||
// ......
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
function nav(num) {
|
||
$('.mui-tab-item span .img').each(function(a) {
|
||
if (num == a) {
|
||
$(this).attr('src', './img/nav_' + a + '_1.png?version='+localStorage.getItem('version'));
|
||
} else {
|
||
$(this).attr('src', './img/nav_' + a + '_0.png?version='+localStorage.getItem('version'));
|
||
}
|
||
})
|
||
|
||
}
|
||
|
||
$('.mui-bar').on('tap', '.mui-tab-item', function(e) {
|
||
var num = $(this).attr('data-num');
|
||
if (num == 2) {
|
||
$('.nav_in').addClass('shun');
|
||
$('.nav_out').addClass('ni');
|
||
} else {
|
||
$('.nav_in').removeClass('shun');
|
||
$('.nav_out').removeClass('ni');
|
||
}
|
||
nav(num);
|
||
})
|
||
|
||
|
||
document.addEventListener('plusready', function() {
|
||
checkArguments();
|
||
}, false);
|
||
// 判断启动方式
|
||
function checkArguments() {
|
||
// console.log("plus.runtime.launcher: " + plus.runtime.launcher);
|
||
var args = plus.runtime.arguments;
|
||
if (args) {
|
||
// 处理args参数,如打开新页面等
|
||
var url;
|
||
var id;
|
||
var datago = {};
|
||
var arrgo = args.slice(args.indexOf('//') + 2).split(': ');
|
||
// console.log(args.slice(args.indexOf('//')+2).split(':'))
|
||
if (arrgo[0] == 'goods_id') {
|
||
url = 'details.html';
|
||
id = arrgo[1];
|
||
datago = {
|
||
data_id: arrgo[1]
|
||
}
|
||
} else if (arrgo[0] == 'shop_id') {
|
||
url = 'storeout.html';
|
||
id = arrgo[1];
|
||
datago = {
|
||
shopId: arrgo[1]
|
||
}
|
||
} else {
|
||
return;
|
||
}
|
||
|
||
mui.openWindow({
|
||
url: 'templete/' + url,
|
||
id: url + id,
|
||
styles: {
|
||
top: '0px', //新页面顶部位置
|
||
bottom: '0px', //新页面底部位置
|
||
width: '100%', //新页面宽度,默认为100%
|
||
height: '100%' //新页面高度,默认为100%
|
||
},
|
||
extras: datago,
|
||
createNew: false, //是否重复创建同样id的webview,默认为false:不重复创建,直接显示
|
||
show: {
|
||
// autoShow: true, //页面loaded事件发生后自动显示,默认为true
|
||
// aniShow: animationType, //页面显示动画,默认为”slide-in-right“;
|
||
// duration: animationTime, //页面动画持续时间,Android平台默认100毫秒,iOS平台默认200毫秒;
|
||
// event: 'titleUpdate', //页面显示时机,默认为titleUpdate事件时显示
|
||
// extras: {} //窗口动画是否使用图片加速
|
||
},
|
||
waiting: {
|
||
autoShow: true, //自动显示等待框,默认为true
|
||
title: '正在加载...', //等待对话框上显示的提示内容
|
||
options: {
|
||
// width: waiting - dialog - widht, //等待框背景区域宽度,默认根据内容自动计算合适宽度
|
||
// height: waiting - dialog - height, //等待框背景区域高度,默认根据内容自动计算合适高度
|
||
// ......
|
||
}
|
||
}
|
||
})
|
||
}
|
||
}
|
||
|
||
|
||
// 处理从后台恢复
|
||
document.addEventListener('newintent', function() {
|
||
// console.log("addEventListener: newintent");
|
||
checkArguments();
|
||
}, false);
|