| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320 |
- import { Linking, Platform } from "react-native";
- import { showLocation } from 'react-native-map-link'
- import { host } from '../api/http';
- import apiUser from "../api/apiUser";
- import { getStorageSync, setStorage } from "./storage";
- import { PERMISSIONS } from "react-native-permissions";
- import app from '../../app.json';
- import { PageList } from "../pages/Router";
- /**
- * 工具集
- * @邠心vbe on 2021/04/20
- */
- const formatNumber = (n) => {
- n = n.toString()
- return n[1] ? n : '0' + n
- }
- export default {
- getDistance(dis) {
- if (dis) {
- if (dis > 1000) {
- return Number((dis / 1000).toFixed(1)) + 'km';
- } else {
- return dis + 'm';
- }
- } else {
- return 'NA';
- }
- },
- directMaps(lat, lng, address) {
- if (isIOS) {
- showLocation({
- latitude: lat,
- longitude: lng,
- title: address
- }).catch(erros => {
- console.warn("directMaps", erros);
- });
- } else {
- var uri = "geo:" + lat + "," + lng + "?q=" + address;
- Linking.openURL(uri).catch(erros => {
- console.warn("directMaps", erros);
- });
- }
- },
- getSiteInfo(obj) {
- if (obj) {
- const acRates = [], dcRates = [];
- obj?.rates.forEach((item) => {
- if (item.type?.indexOf('AC') >= 0) {
- acRates.push(item)
- } else {
- dcRates.push(item)
- }
- })
-
- return {
- id: obj.sitePk,
- name: obj.siteName,
- address: obj.siteAddress,
- latitude: obj.locationLatitude,
- longitude: obj.locationLongitude,
- acConnector: obj.acConnector,
- allConnector: obj.allConnector,
- dcConnector: obj.dcConnector,
- distance: this.getDistance(obj.distance),
- favorite: obj.favorite,
- acRates: acRates,
- dcRates: dcRates,
- idleFee: obj.idleDetail,
- labels: obj.siteLabels,
- upcoming: obj.upcoming,
- rateList: obj.rates,
- siteType: obj.siteType,
- parkingFee: obj.parkingFee,
- hasDiscount: obj.hasDiscount,
- parkingFeeFree: obj.free,
- operatingHours: obj.operatingHours,
- additionalNotes: obj.additionalNotes,
- endlessService: obj.endlessService,
- serviceProvider: obj.serviceProvider,
- enableReservation: obj.enableReservation
- }
- } else {
- return {id: 0}
- }
- },
- getNowHHmm() {
- const now = new Date();
- var month = now.getHours();
- var minute = now.getMinutes();
- return [month, minute].map(formatNumber).join(':');
- },
- getYYMMdd(date) {
- var year = date.getFullYear()
- var month = date.getMonth() + 1
- var day = date.getDate()
-
- //var hour = date.getHours()
- //var minute = date.getMinutes()
- //var second = date.getSeconds()
-
- return [year, month, day].map(formatNumber).join('/')
- },
- formatYYMM(date) {
- var year = date.getFullYear()
- var month = date.getMonth() + 1
- return [year, month].map(formatNumber).join('-')
- },
- hour2HHmm(hour) {
- if (hour) {
- if (hour > 0) {
- const h = parseInt(hour);
- const m = (hour - h) * 60;
- return h + ' hr ' + parseInt(m) + 'min';
- } else {
- const m = hour * 60;
- return parseInt(m) + 'min';
- }
- } else {
- return '0 min';
- }
- },
- minutes2HHmm(minutes) {
- if (minutes) {
- const m = Number(minutes);
- if (m > 60) {
- const h = m / 60;
- const mm = m % 60;
- return parseInt(h) + ' hr ' + parseInt(mm) + ' min';
- } else {
- return parseInt(minutes) + ' min';
- }
- } else {
- return '0 min';
- }
- },
- minutes2HHMM(minutes) {
- if (minutes) {
- const m = Number(minutes);
- if (m > 60) {
- const h = m / 60;
- const mm = m % 60;
- if (h >= 24) {
- const hh = h % 24;
- const d = h / 24;
- return parseInt(d) + "d " + formatNumber(parseInt(hh)) + ':' + formatNumber(parseInt(mm));
- } else {
- return formatNumber(parseInt(h)) + ':' + formatNumber(parseInt(mm));
- }
- } else {
- return '00:' + formatNumber(parseInt(minutes));
- }
- } else {
- return '00:00';
- }
- },
- isEmpty(str, encNo=false) {
- if (typeof str == 'number') {
- if (encNo) {
- return str === 0;
- } else {
- return false;
- }
- } else if (str) {
- if (typeof str == 'object') {
- return Object.keys(str).length == 0;
- } else if (Array.isArray(str)) {
- return str.length == 0;
- } else {
- if (str == undefined || str == null || str == "")
- return true;
- return false;
- }
- } else {
- return true;
- }
- },
- isNotEmpty(str, encNo) {
- return !this.isEmpty(str, encNo);
- },
- getParamsFromUrl(url) {
- var params = {}
- const list = url.split('&');
- for (let item of list) {
- if (item.indexOf('=') > 0) {
- const param = item.split('=');
- if (param.length == 2)
- params[param[0]] = param[1];
- }
- }
- return params;
- },
- join(arrays=[], spect="") {
- let str = ''
- if (arrays) {
- arrays.forEach((item, index) => {
- if (index == 0) {
- str += item;
- } else {
- str += spect + item;
- }
- });
- }
- return str;
- },
- /**
- * 将给定数字保留任意位小数
- * @param {*} text
- * @param {number} scape
- * @returns 小数
- */
- toFixed(text, scape) {
- if (text) {
- return Number(Number(text).toFixed(scape));
- } else {
- return text;
- }
- },
- /**
- * 16进制颜色转为RGB
- * @param {*} value 16进制颜色
- * @returns RGB数据
- */
- hexColorToRgb(value) {
- let sColor = value.toLowerCase()
- if (sColor && sColor.indexOf("#") >= 0) {
- if (sColor.length === 4) {
- let sColorNew = '#'
- for (let i = 1; i < 4; i += 1) {
- sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
- }
- sColor = sColorNew
- }
- // 处理1+6位的颜色值
- let sColorChange = []
- for (let i = 1; i < 7; i += 2) {
- sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2)))
- }
- return sColorChange;//'rgb(' + sColorChange.join(',') + ')'
- } else if (sColor && sColor.indexOf("0x") >= 0) {
- if (sColor.length === 4) {
- let sColorNew = '0x'
- for (let i = 1; i < 4; i += 1) {
- sColorNew += sColor.slice(i, i + 1).concat(sColor.slice(i, i + 1))
- }
- sColor = sColorNew
- }
- // 处理2+6位的颜色值
- let sColorChange = []
- for (let i = 2; i < 8; i += 2) {
- sColorChange.push(parseInt('0x' + sColor.slice(i, i + 2)))
- }
- return sColorChange;//'rgb(' + sColorChange.join(',') + ')'
- }
- return sColor
- },
- getRgbaColor(colors, alpha=1) {
- if (colors.length == 3) {
- return "rgba(" + this.join(colors, ",") + "," +alpha + ")";
- }
- },
- /**
- * 注册FCM通知token
- * @param {String} token 原始保存的Firebase令牌
- * @param {Function} back 执行结果(boolean)
- */
- async registerFirebaseToken(token, back) {
- const thisDate = this.formatYYMM(new Date()) + "-" + getUserId();
- const lastDate = await getStorageSync('RegisterTokenDate');
- console.log('>>>RegisterToken<<<', thisDate, lastDate + "●");
- if (thisDate != lastDate || token != notifyToken.token) {
- const params = {
- os: isIOS ? "ios": "android",
- googleToken: notifyToken.token
- }
- apiUser.setNotifyToken(params).then(res => {
- console.log('>>>RegisterToken-Suc<<<', res);
- setStorage('RegisterTokenDate', thisDate);
- if (back) back(true);
- }).catch(err => {
- console.log('>>>RegisterToken-Err<<<', err);
- if (back) back(false);
- });
- } else {
- if (back) back(false);
- }
- },
- setBackClick(routeNames, func) {
- global.pageBackFallback = {
- names: routeNames,
- callback: func
- }
- },
- getImageUrl(path) {
- if (path && path.indexOf("http") >= 0) {
- return path;
- } else {
- return host + path;
- }
- },
- getFilePermissionString() {
- if (Platform.Version >= 33) {
- return PERMISSIONS.ANDROID.READ_MEDIA_IMAGES;
- } else {
- return PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE;
- }
- },
- toChargeDetailPage(sitePk, action, form) {
- if (app.charge.version == 1) {
- startPage(PageList.chargeDetail, { stationInfo: {id: sitePk}, action: action, from: form});
- } else {
- startPage(PageList.chargeDetailPage, {stationInfo: {id: sitePk}, action: action, from: form});
- }
- },
- toChargingPage(connectorInfo) {
- startPage(app.charge.version == 4 ? PageList.chargingPageV4 : PageList.chargingPage, connectorInfo);
- }
- }
|