Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
tower1229 committed Sep 12, 2018
1 parent fe91eb0 commit 23c317f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 48 deletions.
80 changes: 34 additions & 46 deletions view/demo/ota/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,43 @@ define(function(require) {
var download = require('download');
var zip = api.require('zip');

//配置
var otaPath = api.systemType === "android" ? "/HybridStartOTA/" : "fs://Download/";
var pluginCommonParam = {
root: api.wgtRootDir
};
//下载配置
var otaPath = api.systemType === "android" ? "/Download/" : "fs://Download/";

//本地数据
var localPluginsData = app.storage.val('ota-plugins') || [];
var localPluginsData = app.storage.val('plugins-data') || [];

//渲染列表
var myRender = render({
el: '#view',
data: {},
callback: function() {
callback: function(ele, data) {
app.storage.val('plugins-data', data.data)
app.loading.hide();
}
});

//获取插件数据
var getData = function() {
app.loading.show();
app.ajax({
url: 'http://static-zt.oss-cn-qingdao.aliyuncs.com/mock/plugins.json',
type: 'get',
data: {},
success: function(res) {
if (Array.isArray(res.data)) {
//合并本地数据
var moreArray = [];
res.data.forEach(function(remote){
var isIn = false;
localPluginsData.forEach(function(local){
if(local.name===remote.name){
isIn = true;
$.extend(local,remote);
res.data.forEach(function(e){
localPluginsData.forEach(function(l){
if(e.name===l.name){
$.extend(e,l)
}
});
if(!isIn){
moreArray.push(remote);
}
});
localPluginsData = localPluginsData.concat(moreArray);
app.storage.val('ota-plugins', localPluginsData);
})
})
localPluginsData = res.data;
myRender.data({
data: localPluginsData
});
} else if (res.msg) {
} else {
app.loading.hide();
app.toast(res.msg);
app.toast("插件数据异常");
}
}
});
Expand All @@ -62,58 +53,52 @@ define(function(require) {
getData();

var openPlugin = function(item) {
var remote = $(item).data('remote');
var path = $(item).data('path');
var index = $(item).data('index');
var name = $(item).data('name');
var pagePath = path + name + index;
var pagePath = item.path + item.name + item.index;
//检测文件存在
api.readFile({
path: pagePath
}, function(ret, err) {
if (ret.status) {
app.window.open({
url: pagePath,
param: pluginCommonParam
url: pagePath
});
} else {
localPluginsData.forEach(function(local){
if(local.name===name){
if(local.name===item.name){
delete local.path;
}
});
app.storage.val('ota-plugins', localPluginsData);
myRender.data({
data: localPluginsData
});
app.toast('开始下载');
downloadPlugin(item, true);
}
});
};
var downloadPlugin = function(item, open){
var remote = $(item).data('remote');
var name = $(item).data('name');
if(!remote || !name){
if(!item.remote || !item.name){
return app.toast("该插件暂不可用");
}
app.loading.show();
download(remote,{
download(item.remote,{
path: otaPath,
name: name + ".zip",
name: item.name + ".zip",
cache: false,
success: function(path){
app.loading.hide();
var downPath = path.replace(name + ".zip","");
var downPath = path.replace(item.name + ".zip","");
zip.unarchive({
file: path,
toPath: downPath
}, function(ret, err) {
app.loading.hide();
if (ret.status) {
localPluginsData.forEach(function(local){
if(local.name===name){
if(local.name===item.name){
local.path = downPath;
}
});
app.storage.val('ota-plugins', localPluginsData);
myRender.data({
data: localPluginsData
});
Expand All @@ -126,9 +111,12 @@ define(function(require) {
});
};
//事件绑定
$('#view').on('touchend', '.item', function(e) {
var item = e.target;
openPlugin(item);
$('#view').tap('.item', function(e) {
var itemData = $(e.target).data('str');
if(itemData){
openPlugin(JSON.parse(itemData))
}

});


Expand Down
10 changes: 8 additions & 2 deletions view/demo/ota/temp.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@
<div class="btn" id="goBack">
<i class="ion">&#xe651;</i>
</div>
<div class="title" id="headTitle">插件管理</div>
<div class="title" id="headTitle">扩展插件</div>
</div>
<div class="flex-1 scroll_cont" id="view">
<textarea class="hide" template>
<div class="list">
<!-- for: ${data} as ${plugin} -->
<div class="item" data-remote="${plugin.remote}" data-path="${plugin.path}" data-index="${plugin.index}" data-name="${plugin.name}">
<!-- var: str = JSON.stringify(${plugin}) -->
<div class="item item-icon-right" data-str="${str}">
${plugin.showName}
<!-- if: ${plugin.path} -->
<i class="ion">&#xe610;</i>
<!-- else -->
<i class="ion">&#xe60b;</i>
<!-- /if -->
</div>
<!-- /for -->
</div>
Expand Down

0 comments on commit 23c317f

Please sign in to comment.