react-app-shell
Version:
react打包脚本和example, 这里的版本请忽略
278 lines (256 loc) • 6.47 kB
JavaScript
import { localStore } from './index';
import store from 'store';
import appConfig from '../config/app-config';
// 日期格式化
Date.prototype.format = function(format) {
const o = {
'M+': this.getMonth() + 1,
'd+': this.getDate(),
'h+': this.getHours(),
'H+': this.getHours(),
'm+': this.getMinutes(),
's+': this.getSeconds(),
'q+': Math.floor((this.getMonth() + 3) / 3),
S: this.getMilliseconds()
};
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, String(this.getFullYear()).substr(4 - RegExp.$1.length));
}
for (const k in o) {
if (new RegExp('(' + k + ')').test(format)) {
format = format.replace(
RegExp.$1,
RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(String(o[k]).length)
);
}
}
return format;
};
/**
* 获取参数值
* @param paramKey
*/
export const getQueryString = (paramKey) => {
const args = {};
let argsStr = window.location.search; // 获取URL参数字符串
if (argsStr.length > 0) {
argsStr = argsStr.substring(1);
const nameValueArr = argsStr.split('&'); // 多参数
for (let i = 0; i < nameValueArr.length; i++) {
const pos = nameValueArr[i].indexOf('=');
if (pos === -1) continue; // 如果没有找到就跳过
const argName = nameValueArr[i].substring(0, pos); // 提取name
const argVal = nameValueArr[i].substring(pos + 1); // 提取value
args[argName] = unescape(argVal);
}
return args[paramKey];
}
};
/**
* 更新查询参数
* @param uri
* @param key
* @param value
* @returns {*}
*/
export const updateParamValue = (uri, key, value) => {
const re = new RegExp('([?&])' + key + '=.*?(&|$)', 'i');
const separator = uri.indexOf('?') !== -1 ? '&' : '?';
if (uri.match(re)) {
return uri.replace(re, '$1' + key + '=' + value + '$2');
} else {
return uri + separator + key + '=' + value;
}
};
/**
* 删除参数
* @param url
* @param key
* @returns {*}
*/
export const deleteParam = (url, key) => {
// 如果不包括此参数
if (url.indexOf(key) === -1) return url;
const arr_url = url.split('?');
const base = arr_url[0];
const arr_param = arr_url[1].split('&');
let index = -1;
for (let i = 0; i < arr_param.length; i++) {
const paired = arr_param[i].split('=');
if (paired[0] === key) {
index = i;
break;
}
}
if (index === -1) {
return url;
} else {
arr_param.splice(index, 1);
return base + '?' + arr_param.join('&');
}
};
/**
* 判断是否是微信浏览器
* @returns {boolean}
*/
export const isWeiXin = () => {
// window.navigator.userAgent属性包含了浏览器类型、版本、操作系统类型、浏览器引擎类型等信息,这个属性可以用来判断浏览器类型
return /micromessenger/i.test(navigator.userAgent);
};
/**
* 是否是wap
* @returns {boolean}
*/
export const isWap = () => {
return /iphone|android|mobile|windows ce|miui|ipad|ipod/i.test(navigator.userAgent);
};
/**
* 检查是否支持MpPay
* MpPay 是什么鬼
* @returns {boolean}
*/
export const supportMpPay = () => {
if (/micromessenger\/(\S+)/i.test(navigator.userAgent)) {
if (parseFloat(RegExp.$1) >= 5.0) {
return true;
}
}
return false;
};
// 设置标题
export const setTitle = (title) => {
document.title = `魔力耳朵-${title}`;
};
/**
* 判断是否是IOS系统
* @returns {boolean}
*/
export const isIOS = () => {
const agent = window.navigator.userAgent;
return !!agent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/);
};
/**
* 判断是否是安卓
* @returns {boolean}
*/
export const isAndroid = () => {
const agent = window.navigator.userAgent;
return agent.indexOf('Android') > -1 || agent.indexOf('Adr') > -1;
};
/**
* 判断是否是微信浏览器
* @returns {boolean}
*/
export const isWeChat = () => {
return /micromessenger/i.test(navigator.userAgent);
};
/**
* 简单函数节流
* @param action
* @param delay
* @returns {Function}
*/
export const throttle = function(action, delay) {
let last = 0;
return function() {
const current = Number(new Date());
if (current - last > delay) {
action.apply(this, arguments);
last = current;
}
};
};
/**
* 简单函数防抖
* @param action
* @param delay
* @returns {Function}
*/
export const debounce = function(action, delay = 300) {
let timer = null;
return function() {
clearTimeout(timer);
timer = setTimeout(() => {
action.apply(this, arguments);
}, delay);
};
};
/**
* @description 是否登录
* @return {boolean} true/登录 false/未登录
*/
export const isLogin = () => {
return !!(localStore.getToken() && localStore.getUserId());
};
/**
* 获取当前用户在 主公众号 授权的openId
* @returns {*}
*/
export const getMainOpenId = () => {
const { mainOpenIdKey } = appConfig.cookie;
const mainOpenId = store.get(mainOpenIdKey);
return mainOpenId;
};
/**
* 获取当前用户在 活动公众号 授权的openId
*/
export const getActivityOpenId = () => {
const { activityOpenIdKey } = appConfig.cookie;
const activityOpenId = store.get(activityOpenIdKey);
return activityOpenId;
};
/**
* 获取当前用户在 活动公众号 授权的openId
*/
export const getBonnyOpenId = () => {
const { bonnyOpenIdKey } = appConfig.cookie;
const bonnyOpenId = store.get(bonnyOpenIdKey);
return bonnyOpenId;
};
/**
* @time delay 时间
* 延期执行函数
*/
export const delayEvent = (time = 0) => {
return new Promise((event) => {
setTimeout(() => {
event();
}, time);
});
};
/**
* 加密用户手机号
* @param account
* @return {*}
*/
export const encryptAccount = (account = '') => {
account = account ? String(account) : '';
if (account.match(/^1[0-9]{10}$/g)) {
return account.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2');
}
return account;
};
/**
* 检查是否是简单对象
* @param val
* @returns {boolean}
*/
export const isPlainObject = (value) => {
return Object.prototype.toString.call(value) === '[object Object]';
};
/**
* 转换分为元
* @param value 分
*/
export const convertPenny = (value) => {
const money = value * 0.01;
return money.toFixed(2);
};
/**
* @description 根据时间精准获取oss上的视频封面
* @param url
* @return {string}
*/
export function getOssVideoPoster(url) {
return url + '?x-oss-process=video/snapshot,t_5000,f_jpg,w_0,h_0';
}