UNPKG

cjd-parkball

Version:

> 中后台业务组件库,中后台就像公园,进入需要买门票(登录),所以以 Parkball(公园球) 命名,公园内必定捕获!作为一个组件库,提供使用方法文档,方便开发者的调用

183 lines (167 loc) 4.79 kB
import fetch from 'isomorphic-fetch' /** * 获取query * @param {String} name 名称 {可选} */ export function getQuerys (name) { let querys = {} const search = window.location.search.replace('?', '') if (search) { search.split('&').map((v) => { const [key, value] = v.split('=') querys[key] = value return v }) return name ? querys[name] : querys } return name ? '' : {} } /** * 判断目标值是否是null或者undefined * @param {String} value 目标值 */ export function isNullOrUndefined (value) { return value === null || value === undefined } /** * 对象转params * @param {Object} obj 对象 */ export function formatParams (obj = {}) { let params = [] Object.keys(obj).forEach((key) => { const value = obj[key] if (!isNullOrUndefined(value) && value !== '') { params.push(`${key}=${value}`) } }) return params.join('&') } /** * 价格格式化为元 * @param {Number|String} price 价格 * @param {Number} bit 需要保留的小数位 */ export function formatPrice (price, bit = 2) { if (typeof (price) === 'string' && price !== '') { return (parseFloat(price) / 100).toFixed(bit) } else if (typeof (price) === 'number' && !Number.isNaN(price)) { return parseFloat(price / 100).toFixed(bit) } return price } /** * 价格格式化为分 * @param {Number|String} price 价格 */ export function formatCent (price) { if (typeof (price) === 'string' && price !== '') { return parseFloat((parseFloat(price) * 100).toFixed()) } else if (typeof (price) === 'number' && !Number.isNaN(price)) { return (parseFloat((price * 100).toFixed())) } return price } /** * 时间格式化 * @param {Int} date 时间戳 * @param {String|Boolean} _type 格式化方式,默认是`Y-D-M`,如果为`true`则是`Y-M-D h:m:s` */ export function formatDate (date, _type = 'Y-M-D') { let myDate let hour let time if (date) { let timestamp = parseInt(date, 10) if (timestamp < 10000) { timestamp = date } if (!Number.isNaN(+timestamp)) { myDate = new Date(timestamp) } else { myDate = new Date(timestamp.replace(/-/g, '/')) } hour = myDate.getHours() } else { return '' // myDate = new Date() // hour = myDate.getHours() } const type = _type === true ? 'Y-M-D h:m:s' : _type const Y = myDate.getFullYear() const M = myDate.getMonth() + 1 < 10 ? `0${myDate.getMonth() + 1}` : myDate.getMonth() + 1 const D = myDate.getDate() < 10 ? `0${myDate.getDate()}` : myDate.getDate() const h = hour < 10 ? `0${hour}` : hour const m = myDate.getMinutes() < 10 ? `0${myDate.getMinutes()}` : myDate.getMinutes() const s = myDate.getSeconds() < 10 ? `0${myDate.getSeconds()}` : myDate.getSeconds() time = type.replace('Y', Y) time = time.replace('M', M) time = time.replace('D', D) time = time.replace('h', h) time = time.replace('m', m) time = time.replace('s', s) return time } /** * Requests a URL, returning a promise. * * @param {string} url The URL we want to request * @param {object} [options] The options we want to pass to "fetch" * @return {object} An object containing either "data" or "err" */ export function request (url, options = {}) { const header = { 'x-requested-with': 'XMLHttpRequest' } const { method = 'GET', headers = {}, ...rest } = options const newHeader = Object.assign({}, header, headers) function parseText (response) { return response.text() } function checkStatus (response) { if ((response.status >= 200 && response.status < 300) || response.status === 304) { return response } return response.text().then((data) => { let error = { status: response.status, } try { error.message = JSON.parse(data).message } catch (e) { error.message = data } throw error }) } return fetch(url, { credentials: 'same-origin', method, headers: newHeader, ...rest, }) .then(checkStatus) .then(parseText) .then((data) => { try { return JSON.parse(data) } catch (e) { return data } }) .catch((err) => { throw err }) } /** * 组件错误提示 * @param {*} name 组件名称 * @param {*} message 错误信息 */ export function errorTip (name, message) { console.warn(`Parkball ${name}: ${message}`) } export function customStoreKey (props) { const { scope = 'parkball', compKey = 'pk-local-key' } = props const url = window.location.host + window.location.pathname return `${scope}:${url}:${compKey}` } // 如果是空值,返回默认值 export function emptyValue (value, emptyText) { return (value !== null && value !== undefined) ? value : emptyText }