Skip to content

Commit

Permalink
Merge pull request #304 from pifire/master
Browse files Browse the repository at this point in the history
muzi高速下载和普通下载切换组件
  • Loading branch information
Pearl Wang authored Jan 3, 2017
2 parents 7deeeba + a1168f3 commit 0157109
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 0 deletions.
30 changes: 30 additions & 0 deletions mip-mz-addonapp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# mip-mz-addonapp

mip-mz-addonapp 推荐app下载!

标题|内容
----|----
类型|通用
所需脚本|http://mipcache.bdstatic.com/static/v1/mip-mz-addonapp/mip-mz-addonapp.js

## 示例

### 推荐app下载!
```html
<mip-mz-addonapp type="comm"></mip-mz-addonapp>
```

## 属性

### type

说明:app类型
必选项:是
类型:字符串
取值范围:comm,full
单位:无
默认值:无




40 changes: 40 additions & 0 deletions mip-mz-addonapp/mip-mz-addonapp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @file mip-mz-addonapp 推荐app下载
* @author pifire
*/

define(function (require) {
var $ = require('zepto');
var util = require('util');
var platform = util.platform;
var customElement = require('customElement').create();
function initAD(type, obj) {
var sys = (platform.isIos()) ? 'ios' : 'android';
var apps = obj[type][sys];
var html = '<ul class="sixbox"><li>';
for (var i = 0; i < apps.length; ++i) {
html += '<a href="' + apps[i].url + '"><img src="' + apps[i].img + '" /><span>'
+ apps[i].name + '</span></a>';
}
html += '<li></ul>';
return html;
}

/**
* createdCallback
*/
customElement.prototype.createdCallback = function () {
var element = this.element;
var $element = $(element);
var type = $element.attr('type');
var myRequest = new Request('https://m.muzisoft.com/mipaddonapp.json');
fetch(myRequest).then(function (response) {
return response.json().then(function (json) {
var innerHTML = initAD(type, json);
var obj = ($('.vother').length > 0) ? $('.vother') : $('.info');
obj.after(innerHTML);
});
});
};
return customElement;
});
14 changes: 14 additions & 0 deletions mip-mz-addonapp/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "mip-mz-addonapp",
"version": "1.0.0",
"description": "推荐app下载",
"contributors": [
{
"name": "pifire",
"email": "[email protected]"
}
],
"engines": {
"mip": ">=1.1.0"
}
}
49 changes: 49 additions & 0 deletions mip-mz-appdownload/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# mip-mz-appdownload

mip-mz-appdownload app下载组件,在高速下载和直接下载之间切换!

标题|内容
----|----
类型|通用
所需脚本|http://mipcache.bdstatic.com/static/v1/mip-mz-appdownload/mip-mz-appdownload.js

## 示例

### app下载组件,在高速下载和直接下载之间切换!
```html
<mip-mz-appdownload ad="1" aid="143301addcd" addr=""></mip-mz-appdownload>
```

## 属性

### ad

说明:是否启用高速下载
必选项:是
类型:数字
取值范围:0,1
单位:无
默认值:无

### aid

说明:对应的应用id
必选项:是
类型:字符串
取值范围:数字或者其他
单位:无
默认值:无

### addr

说明:应用的下载地址
必选项:是
类型:字符串
取值范围:空或者字符串
单位:无
默认值:无





139 changes: 139 additions & 0 deletions mip-mz-appdownload/mip-mz-appdownload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/**
* @file mip-mz-appdownload 木子的app下载切换效果
* @author pifire
*/

define(function (require) {
var $ = require('zepto');
var util = require('util');
var platform = util.platform;
var customElement = require('customElement').create();
var localhref = 'http://m.muzisoft.com/mz/';
function initAD(ad, aid, addr, obj) {
if (platform.isIos()) {
var softid = aid.substring(0, aid.length - 5);
if (inarray(obj.vnpids, softid)) {
return '<a href="' + obj.ios[1].url + '" class="gsdbtn">' + obj.ios[0].btnvalue + '</a>';
}
return '<a href="' + checkurl(aid, addr) + '" class="gsdbtn confirmios">'
+ obj.ios[0].btnvalue + '</a>';
}
if (ad > 0) {
$('.down').css('height', '90px');
var presenti = 0;
for (var i = 0; i < obj.android.length; ++i) {
if (!getCookie(obj.android[i].id)) {
presenti = i;
break;
}
}
return '<input type="checkbox" id="ckb" class="ckb" checked="checked">'
+ '<span>' + obj.android[presenti].name + '</span>'
+ '<a href="' + obj.android[presenti].url + '" id="gsdbtn" presentid="'
+ obj.android[presenti].id + '" class="gsdbtn">' + obj.android[presenti].btnvalue + '</a>'
+ '<p id="yybtext" class="yybtext">' + obj.android[presenti].info + '</p>';
}
return '<a href="' + checkurl(aid, addr) + '" class="gsdbtn">' + obj.android[0].ubtnvalue + '</a>';
}
// 判断url下载还是id下载
function checkurl(aid, addr) {
return (addr.length === 0) ? localhref + aid : addr;
}
// 点击按钮切换下载
function changeDown(aid, addr, androidAD) {
var chk = document.getElementById('ckb');
var yybtext = $('.yybtext');
var gsdbtn = document.getElementById('gsdbtn');
var presenti = 0;
for (var i = 0; i < androidAD.length; ++i) {
if (!getCookie(androidAD[i].id)) {
presenti = i;
break;
}
}
if (chk.checked) {
yybtext.css('color', 'black');
document.querySelector('#down span').innerText = androidAD[presenti].name;
gsdbtn.setAttribute('href', androidAD[presenti].url);
gsdbtn.innerText = androidAD[presenti].btnvalue;
yybtext.innerText = androidAD[presenti].info;
}
else {
yybtext.css('color', 'red');
document.querySelector('#down span').innerText = androidAD[presenti].name;
gsdbtn.setAttribute('href', checkurl(aid, addr));
gsdbtn.innerText = androidAD[presenti].ubtnvalue;
yybtext.innerText = androidAD[presenti].uinfo;
}
}
// 设置cookie
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = 'expires=' + d.toGMTString();
document.cookie = cname + '=' + cvalue + ';expires=' + expires;
}
// 获取cookie
function getCookie(cname) {
var name = cname + '=';
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i].trim();
if (c.indexOf(name) === 0) {
return c.substring(name.length);
}
}
}
// 数组中是否包含
function inarray(arr, obj) {
var i = arr.length;
while (i--) {
if (arr[i] === obj) {
return true;
}
}
return false;
}

/**
* createdCallback
*/
customElement.prototype.createdCallback = function () {
var element = this.element;
var $element = $(element);
var ad = $element.attr('ad');
var aid = $element.attr('aid');
var addr = $element.attr('addr');
function callback(json) {
var innerHTML = initAD(ad, aid, addr, json);
$('.down ul li').append(innerHTML);
$('.ckb').click(function () {
changeDown(aid, addr, json.android);
});
// 安卓点击了高速按钮,写cookie
$('.gsdbtn').click(function () {
for (var i = 0; i < json.android.length; ++i) {
var j = (i === json.android.length - 1) ? 0 : i + 1;
if ($('.gsdbtn').attr('presentid') === json.android[i].id) {
setCookie(json.android[j].id, '', 1);
}
else {
setCookie(json.android[j].id, 1, 1);
}
}
});
// 苹果点击了下载
$('.confirmios').click(function () {
if (confirm(json.ios[0].name)) {
window.location.href = json.ios[0].url;
return false;
}
});
}
var myRequest = new Request('https://m.muzisoft.com/mipappdown.json');
fetch(myRequest).then(function (response) {
return response.json().then(callback);
});
};
return customElement;
});
14 changes: 14 additions & 0 deletions mip-mz-appdownload/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "mip-mz-appdownload",
"version": "1.0.0",
"description": "app下载组件,在高速下载和直接下载之间切换!",
"contributors": [
{
"name": "pifire",
"email": "[email protected]"
}
],
"engines": {
"mip": ">=1.1.0"
}
}

0 comments on commit 0157109

Please sign in to comment.