caiyunyi/utils/index.js
2025-11-11 09:42:39 +08:00

208 lines
6.6 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.

import api from './api.js'
function formatNumber(n) {
const str = n.toString()
return str[1] ? str : `0${str}`
}
export function formatTime(date) {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
const t1 = [year, month, day].map(formatNumber).join('/')
const t2 = [hour, minute, second].map(formatNumber).join(':')
return `${t1} ${t2}`
}
export function getLocalTime(now, type) {
type = type || 'YYYY-MM-DD hh:mm:ss'
now = new Date(now)
var year = now.getFullYear()
var month = (now.getMonth() + 1) < 10 ? '0' + (now.getMonth() + 1) : (now.getMonth() + 1) // 获取当前月份的日期不足10补0
var date = now.getDate() < 10 ? '0' + now.getDate() : now.getDate() // 获取当前几号不足10补0
var hour = now.getHours() < 10 ? '0' + now.getHours() : now.getHours()
var minute = now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes()
var second = now.getSeconds() < 10 ? '0' + now.getSeconds() : now.getSeconds()
return type.replace('YYYY', year).replace('MM', month).replace('DD', date).replace('hh', hour).replace('mm', minute).replace('ss', second)
// return year + '-' + month + '-' + date + ' ' + hour + ':' + minute // + ":" + second;
}
// 转义日期格式
function cutDate(dd, type, daynum) {
dd = new Date(dd) || new Date()
type = type || 'YYYY/MM/DD' // hh:mm:ss
daynum = daynum || 0
dd.setDate(dd.getDate() + daynum) // 获取AddDayCount天后的日期
var y = dd.getFullYear()
var m = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1) // 获取当前月份的日期不足10补0
var d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate() // 获取当前几号不足10补0
var h = dd.getHours() < 10 ? '0' + dd.getHours() : dd.getHours()
var mi = dd.getMinutes() < 10 ? '0' + dd.getMinutes() : dd.getMinutes()
var s = dd.getSeconds() < 10 ? '0' + dd.getSeconds() : dd.getSeconds()
return type.replace('YYYY', y).replace('MM', m).replace('DD', d).replace('hh', h).replace('mm', mi).replace('ss', s)
}
// 生成二维码和条形码
let barcode = require('../common/JsBarcode.all.js')
let qrcode = require('../common/qarcode.js')
export function barc(id, code, width, height, rolate) {
barcode.code128(uni.createCanvasContext(id), code, width, height, rolate)
}
function qrc(id, code, width, height, ecc, src) {
ecc = ecc || null
src = src || null
qrcode.qrApi.draw(code, id, width, height, ecc, src)
}
// 一维数组按照c成二维数组
/*
array 需要分割的数组
subGroupLen 按照多少长度分割
*/
const ArrayGroup = (array, subGroupLen) => {
let index = 0
let newArray = []
while (index < array.length) {
newArray.push(array.slice(index, index += subGroupLen))
}
return newArray
}
export function wrapTreeNode(data) {
const wrapData = data.map((item) => {
const node = {
...item.node
};
if (item.children && item.children.length > 0) {
node.children = wrapTreeNode(item.children);
}
return node
});
return wrapData;
}
export function formatDate(dateStr) {
const date = new Date(dateStr);
// 获取年份、月份和日期,确保是两位数
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
// 返回格式化后的结果
return `${year}-${month}-${day}`;
}
// 格式化数字
export function handleFormatNumber(num) {
// 将数字转为字符串,避免浮点数精度问题
let str = num.toString();
// 如果有小数点
if (str.indexOf('.') !== -1) {
// 截取小数点后两位,不做四舍五入
let decimalPart = str.split('.')[1];
if (decimalPart.length > 2) {
return str.split('.')[0] + '.' + decimalPart.slice(0, 2);
}
return str;
} else {
// 没有小数点,补齐后两位
return str + '.00';
}
}
// 记录用户行为的方法 每个页面的onUnload里面要调用
// 保存上一个页面路径的全局变量
let lastPageRoute = '/pages/index/index';
// 记录用户行为的函数
export function addUserBehaviorNew(obj) {
var pages = getCurrentPages() // 获取加载的页面
let len = pages.length
if (len) {
var currentPage = pages[len - 1] // 获取当前页面的对象
var nowRoute = '/' + currentPage.route // 当前页面url
// 如果没有传入obj则初始化一个空对象
if (!obj) {
obj = {}
}
// 设置页面切换信息
// outtoRoute 是离开的页面intoRoute 是进入的页面
// 如果明确传入了这些值,则使用传入的值
if (obj.outtoRoute === undefined || obj.outtoRoute === null) {
obj.outtoRoute = lastPageRoute // 使用上一个记录的页面作为离开页面
}
if (obj.intoRoute === undefined || obj.intoRoute === null) {
obj.intoRoute = nowRoute // 当前页面作为进入页面
}
// 确保路由不包含查询参数
if (obj.outtoRoute && obj.outtoRoute.includes('?')) {
obj.outtoRoute = obj.outtoRoute.split('?')[0]
}
if (obj.intoRoute && obj.intoRoute.includes('?')) {
obj.intoRoute = obj.intoRoute.split('?')[0]
}
// 更新上一个页面的记录,为下次页面切换做准备
lastPageRoute = nowRoute;
}
// 获取用户信息
const baseInfo = uni.getStorageSync('vuex');
const userDate = baseInfo ? JSON.parse(baseInfo).user : {};
const userInfo = uni.getStorageSync('userInfo');
const app = getApp();
// 构造埋点请求数据
const req = {
userName: userDate.MEMBERSHIP_NAME || '未授权',
phoneNumber: userDate.MEMBERSHIP_MOBILEPHONE || '未授权手机号',
userId: userDate.MEMBERSHIP_ID || '',
wechatAppId: 'wxee018fb96955552a',
intoRoute: obj.intoRoute || '/',
outtoRoute: obj.outtoRoute || '/',
visitChannels: obj.visitChannels || '',
behaviorRecordDesc: obj.behaviorRecordDesc ||
(app.globalData.recommendId && app.globalData.recommendCode
? `recommendId=${app.globalData.recommendId}&recommendCode=${app.globalData.recommendCode}`
: ''),
LoginIP: userInfo?.ip || '',
LoginPlace: (userInfo?.prov || '') + (userInfo?.prov && userInfo?.city ? '-' : '') + (userInfo?.city || ''),
OperatingSystem: '彩云驿出行',
};
api.$get('/CommercialApi/UserBehavior/AddUserBehavior', req).then(() => { });
}
export default {
formatNumber,
formatTime,
getLocalTime,
ArrayGroup,
barc,
qrc,
cutDate,
wrapTreeNode,
formatDate,
handleFormatNumber,
addUserBehaviorNew
}