Yizhan-app/common/common.js

140 lines
3.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const getImage = function(imgUrl) {
var img = event.srcElement;
img.src = imgUrl;
}
// 时间转换 年-月-日
const formDateChange = function(date) {
let month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1
let day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate()
return `${date.getFullYear()}-${month}-${day}`
}
function getDictionaryValue(list, key) {
for (var i = 0; i < list.length; i++) {
if (list[i].dataType == key)
return list[i].dataValue;
}
// for (var element of list) {
// if(element.dataType == key)
// return element.dataValue;
// }
// list.forEach(function(element) {
// if(element.dataType == key)
// return element.dataValue;
// });
return '';
}
// 获取url参数 支持中文/明文/密文 输出对象
function getUrlRequest() {
// var url = location.search; //获取url中"?"符后的字串
var url = decodeURI(decodeURI(location.search)); //获取url中"?"符后的字串
// var theRequest = new Object();
// if (url.indexOf("?") != -1) {
// var str = url.substr(1);
// strs = str.split("&");
// for(var i = 0; i < strs.length; i ++) {
// theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
// }
// }
// debugger;
// return theRequest;
// 通过 ? 分割获取后面的参数字符串
let urlStr = url.split('?')[1]
// 创建空对象存储参数
let obj = {};
// 再通过 & 将每一个参数单独分割出来
if (urlStr == undefined) return obj
let paramsArr = urlStr.split('&')
for (let i = 0, len = paramsArr.length; i < len; i++) {
// 再通过 = 将每一个参数分割为 key:value 的形式
paramsArr[i] = paramsArr[i].replace(/=/, "######");
// 问题有的字段val会带有==’,会自动过滤掉
// 解决:在截取之前把第一个= 换成一堆自己定义的符号,例如 ####### 、@@@@@ 保证不会重复 ,然后再截取
let arr = paramsArr[i].split('######');
obj[arr[0]] = arr[1];
}
return obj
}
//需传入key值(上面的方法[getUrlRequest]会过滤掉加密后的==号) 【保留/使用中】
function getQueryString(key) {
var t = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
var a = window.location.href.match(t);
if (a != null) return a[2];
return ""
}
// 跳转兼容【保留】
function toPageX(page) {
if (navigator.userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
location.href = page; //此处判断是否为ios后,使用location.href跳转
}
window.location.href = page; //安卓跳转
}
const parseTime = function(time, cFormat) {
if (arguments.length === 0) {
return ''
}
const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
let date
if (typeof time === 'object') {
date = time
} else {
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
time = parseTime(time)
}
if ((typeof time === 'number') && (time.toString().length === 10)) {
time = time * 1000
}
date = new Date(time)
}
const formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
let value = formatObj[key]
if (key === 'a') {
return ['日', '一', '二', '三', '四', '五', '六'][value]
}
if (result.length > 0 && value < 10) {
value = '0' + value
}
return value || 0
})
return time_str
}
const myn = function() {
let time = new Date()
var a = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
let gday = 24 * 60 * 60 * 1000
let arr = []
for (let i = 0; i < 30; i++) {
let tiems = new Date((time.getTime() + gday * i)).format('MM-dd')
// arr.push(tiems)
var day = new Date((time.getTime() + gday * i)).getDay();
var week = a[day]
arr.push({
tiems: tiems,
week: week
})
}
return arr
}
module.exports = {
formDateChange: formDateChange,
getImage: getImage,
parseTime: parseTime,
myn: myn,
}