// import config from "./config" import store from "@/store/index.js" import { Decimal } from 'decimal.js' import config from "./config.js" export const setToken = (tokenVal) => { try { uni.setStorageSync(config.tokenKey, tokenVal); } catch (e) { // error } } export const getToken = () => { try { const value = uni.getStorageSync(config.tokenKey); return value || '' } catch (e) { // error } } export const setStorageSync = (key, val) => { try { uni.setStorageSync(key, val); } catch (e) { // error } } export const getStorageSync = (key) => { try { const value = uni.getStorageSync(key); return value || '' } catch (e) { // error } } export const removeStorageSync = (key) => { try { uni.removeStorageSync(key); } catch (e) { // error } } export const refreshAccount = () => { store.commit('app/SET_TOKEN', '') uni.reLaunch({ url: '/pages/index/index' }); } // 判断是否登录 export const ifLogin_ = () => { return new Promise((resolve, reject) => { if (getToken()) { resolve() } else { uni.navigateTo({ url: '/pages/login/index' }); reject() }; }) }; export const reverseBack = (path = undefined) => { const pages = getCurrentPages(); if (pages.length <= 1) { uni.reLaunch({ url: '/pages/index/index' }); } else { let deltaNum = 1; if (path) { if (path.indexOf('/') === 0) { path = path.substring(1); }; const pageLength = pages.length let num = pageLength; for (let i = 1; i <= pageLength; i++) { num -= 1; if (pages[num].route === path) { deltaNum = i; break; } } }; uni.navigateBack({ delta: deltaNum, fail: err => {} }) } } export const decimalNum = { // 加法 add: (a, b) => { return new Decimal(a).add(new Decimal(b)) }, // 减法 sub: (a, b) => { return new Decimal(a).sub(new Decimal(b)) }, // 乘法 mul: (a, b) => { return new Decimal(a).mul(new Decimal(b)) }, // 除法 div: (a, b) => { return new Decimal(a).div(new Decimal(b)) }, // // 加法 // let c = new Decimal(a).add(new Decimal(b)) // // 减法 // let d = new Decimal(a).sub(new Decimal(b)) // // 乘法 // let e = new Decimal(a).mul(new Decimal(b)) // // 除法 // let f = new Decimal(a).div(new Decimal(b)) } // 涨跌颜色 export const setColor = (nums, type = false) => { if (type) { return nums >= 0 ? true : false } else { return nums >= 0 ? 'zhang' : 'die' } } const doubleDigit = (t) => { if (t <= 9) { return `0${t}` } return t } export const getData_ = (time = '', type = true) => { if (time || type) { let now = time ? new Date(time) : new Date(); const year = now.getFullYear(); const month = doubleDigit(now.getMonth() + 1); const day = doubleDigit(now.getDate()); const hour = doubleDigit(now.getHours()); const min = doubleDigit(now.getMinutes()); const seon = doubleDigit(now.getSeconds()); return `${year}-${month}-${day} ${hour}:${min}:${seon}` } return '' } export const scanCode = () => { // 允许从相机和相册扫码 return new Promise((resolve, reject) => { uni.scanCode({ success: res => { console.log('条码类型:' + res.scanType); console.log('条码内容:' + res.result); resolve(res.result) }, fail: err => { reject() }, }); }) } // 阅读文章详情 export const readArticleInfo = (id) => { uni.navigateTo({ url: this.$path.articleDetails + `${id}` }) } // 数字截取 export const numIntercepting = (num = 0, decimals = 0) => { if (num) { try { num = Number(num).toFixed(decimals) } catch(err) { // console.log(err , num , typeof num , Number(num)) } } return num } export const getChange = (item, type) => { // 计算 let num = '' switch (type) { case 1: // 浮动盈亏 = ((开仓价格 - 标记价格) × 数量 - 手续费)*100/ 标记价格 num = ((item.update_price - item.origin_price) * item.number - item.trade_fee) * 100 / item .origin_price if (num) { num = num.toFixed(2) }; break; case 2: // 风险率 保证金*100/可用余额 // num = item.caution_money * 100 / item.user.lever break; case 3: // 强平价 = 标记价格*100 num = item.origin_price * 100 break; case 4: // 平仓盈亏 = 盈亏在百分比计算方式 = 盈亏*100/保证金 num = item.profits * 100 / item.caution_money if (num) { num = num.toFixed(2) }; break; } return (num || num === 0) ? num : '--' } export const showAccount = (account = '') => { // showAccount(userInfo.account_number) if (account) { if (account.length === 1) { return `${account}****` } else if (account.length > 1 && account.length <= 6) { return `${account[0]}****${account[account.length - 1]}` } else { const num = (account.length - 4) / 2 // ceil floor let l = `${account}`.slice(0 , Math.ceil(num)) , r = `${account}`.slice(account.length - Math.floor(num)) return `${l}****${r}` } } }