You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
flyapps/fir_client/src/utils/index.js

262 lines
8.2 KiB

//滚动条在Y轴上的滚动距离
5 years ago
export function getScrollTop() {
5 years ago
let scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
5 years ago
if (document.body) {
bodyScrollTop = document.body.scrollTop;
}
5 years ago
if (document.documentElement) {
documentScrollTop = document.documentElement.scrollTop;
}
scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
5 years ago
// return Math.ceil(scrollTop);
return scrollTop;
}
//文档的总高度
5 years ago
export function getScrollHeight() {
5 years ago
let scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
5 years ago
if (document.body) {
bodyScrollHeight = document.body.scrollHeight;
}
5 years ago
if (document.documentElement) {
documentScrollHeight = document.documentElement.scrollHeight;
}
scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
return scrollHeight;
}
//浏览器视口的高度
5 years ago
export function getWindowHeight() {
5 years ago
let windowHeight = 0;
5 years ago
if (document.compatMode === "CSS1Compat") {
windowHeight = document.documentElement.clientHeight;
5 years ago
} else {
windowHeight = document.body.clientHeight;
}
return windowHeight;
}
5 years ago
export function getappinfo(file, successcallback, errcallback) {
const AppInfoParser = require('app-info-parser');
const parser = new AppInfoParser(file);
5 years ago
let analyseappinfo = {};
parser.parse().then(result => {
5 years ago
analyseappinfo.icon = result.icon;
analyseappinfo.filename = file.name;
analyseappinfo.filesize = file.size;
if (result.CFBundleDisplayName) {
analyseappinfo.appname = result.CFBundleDisplayName;
analyseappinfo.bundleid = result.CFBundleIdentifier;
analyseappinfo.version = result.CFBundleShortVersionString;
analyseappinfo.buildversion = result.CFBundleVersion;
analyseappinfo.miniosversion = result.MinimumOSVersion;
if (result.mobileProvision.ProvisionedDevices) {
analyseappinfo.release_type = 'Adhoc';
analyseappinfo.release_type_id = 1;
analyseappinfo.udid = result.mobileProvision.ProvisionedDevices;
} else {
analyseappinfo.release_type = 'Inhouse';
analyseappinfo.release_type_id = 2;
analyseappinfo.udid = [];
}
5 years ago
analyseappinfo.type = 'iOS';
} else {
analyseappinfo.appname = result.application.label[0];
analyseappinfo.bundleid = result.package;
analyseappinfo.version = result.versionName;
analyseappinfo.buildversion = result.versionCode;
analyseappinfo.miniosversion = result.usesSdk.minSdkVersion;
analyseappinfo.type = 'Android';
analyseappinfo.release_type = 'Android';
analyseappinfo.release_type_id = 0;
}
successcallback(analyseappinfo)
}).catch(err => {
errcallback(err);
});
}
5 years ago
export function uploadqiniuoss(file, certinfo, app, successcallback, processcallback) {
// let app = this;
// let file=this.currentfile;
let observer = {
5 years ago
next(res) {
processcallback(Math.round(res.total.percent));
},
5 years ago
error(err) {
// eslint-disable-next-line no-console
console.log(err);
app.$message({
5 years ago
message: file.name + '上传失败,请刷新页面重试',
type: 'error',
5 years ago
duration: 0
});
},
5 years ago
complete(res) {
// eslint-disable-next-line no-console
// console.log(res);
successcallback(res);
}
};
let putExtra = {
fname: file.name,
params: {},
// mimeType: ["image/png"]
};
5 years ago
let config = {};
let observable = app.qiniu.upload(file, certinfo.upload_key, certinfo.upload_token, putExtra, config);
// eslint-disable-next-line no-unused-vars
let subscription = observable.subscribe(observer) // 上传开始
// subscription.unsubscribe() // 上传取消
}
export function dataURLtoFile(dataurl, filename) {//将base64转换为文件
let arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
5 years ago
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
5 years ago
return new File([u8arr], filename, {type: mime});
}
5 years ago
export function uploadaliyunoss(file, certinfo, app, successcallback, processcallback) {
let token = certinfo.upload_token;
let client = new app.oss({
endpoint: token.endpoint,
accessKeyId: token.access_key_id,
accessKeySecret: token.access_key_secret,
stsToken: token.security_token,
bucket: token.bucket
});
// eslint-disable-next-line no-unused-vars
let currentCheckpoint;
const progress = async function progress(p, checkpoint) {
currentCheckpoint = checkpoint;
// eslint-disable-next-line no-console
5 years ago
// console.log(Math.floor(p * 100));
// eslint-disable-next-line no-console
5 years ago
// console.log(checkpoint);
processcallback(Math.floor(p * 100));
};
const options = {
progress,
partSize: 1024 * 1024 / 4,
// meta: {
// year: 2017,
// people: 'test',
// },
};
5 years ago
client.multipartUpload(certinfo.upload_key, file, options).then((res) => {
// eslint-disable-next-line no-console
5 years ago
// console.log('upload success: %j', res);
successcallback(res);
currentCheckpoint = null;
}).catch((err) => {
5 years ago
// eslint-disable-next-line no-console
console.error(err);
app.$message({
5 years ago
message: file.name + '上传失败,请刷新页面重试',
type: 'error',
5 years ago
duration: 0
});
});
}
import {uploadstorage} from '../restful'
5 years ago
export function uploadlocalstorage(file, certinfo, app, successcallback, processcallback) {
uploadstorage(certinfo, file, successcallback, processcallback)
}
export function removeAaary(_arr, _obj) {
var length = _arr.length;
for (var i = 0; i < length; i++) {
if (_arr[i] == _obj) {
if (i == 0) {
_arr.shift(); //删除并返回数组的第一个元素
return _arr;
5 years ago
} else if (i == length - 1) {
_arr.pop(); //删除并返回数组的最后一个元素
return _arr;
5 years ago
} else {
_arr.splice(i, 1); //删除下标为i的元素
return _arr;
}
}
}
5 years ago
}
4 years ago
//深拷贝-字段不能为null
// export function deepCopy(source) {
// let result = {};
// for (let key in source) {
// result[key] = typeof source[key] === 'object' ? deepCopy(source[key]) : source[key];
// }
// return result;
// }
//
// //深拷贝-只有json
// export function deepCopyJson(source) {
// return JSON.parse(JSON.stringify(source))
// }
5 years ago
5 years ago
export function IsNum(s) {
if (s != null) {
var r, re;
5 years ago
re = /\d*/i; //\d表示数字,*表示匹配多个数字
r = s.match(re);
5 years ago
return (r == s) ? true : false;
5 years ago
}
return false;
4 years ago
}
function getType(x) {
if (x) {
if (x.constructor.toString().indexOf("Array") > -1) {
return 'array'
} else if (x.constructor.toString().indexOf("Number") > -1) {
return 'number'
} else if (x.constructor.toString().indexOf("String") > -1) {
return 'string'
} else if (x.constructor.toString().indexOf("Object") > -1) {
return 'object'
} else {
return x.constructor.toString()
}
} else {
return x
}
}
//深拷贝
export function deepCopy(data) {
let type = getType(data);
let obj;
if (type === 'array') {
obj = [];
} else if (type === 'object') {
obj = {};
} else {
//不再具有下一层次
return data;
}
if (type === 'array') {
for (let i = 0, len = data.length; i < len; i++) {
obj.push(deepCopy(data[i]));
}
} else if (type === 'object') {
for (let key in data) {
obj[key] = deepCopy(data[key]);
}
}
return obj;
}