caiyunyi/utils/index.js
ylj20011123 5d8cf4605f update
2025-07-07 20:02:40 +08:00

233 lines
7.5 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里面要调用
export function addUserBehaviorNew(obj, haveQuery) {
// haveQuery 是否把携带的参数也拼上
var pages = getCurrentPages() // 获取加载的页面
console.log('pages', pages);
let len = pages.length
if (len) {
var currentPage = pages[len - 1] // 获取当前页面的对象
var nowRoute = '' // 当前页面url
// if (haveQuery) {
// nowRoute = currentPage.$page.fullPath // 当前页面url
// } else {
// nowRoute = '/' + currentPage.route // 当前页面url
// }
nowRoute = '/' + currentPage.route // 当前页面url
if (obj) {
obj.outtoRoute = (obj.outtoRoute === '' || obj.outtoRoute) ? obj.outtoRoute : nowRoute
obj.intoRoute = obj.intoRoute ? obj.intoRoute : ''
// obj.intoRoute.split('?')[0]
} else {
obj = {}
obj.outtoRoute = nowRoute
obj.intoRoute = len > 1 ? '/' + pages[len - 2].route : '/pages/index/index'
}
}
let baseInfo = uni.getStorageSync('vuex')
let userDate = {}
if (baseInfo) {
let obj = JSON.parse(baseInfo)
userDate = obj.user
}
console.log('userDate', userDate);
// obj.visitChannels = store.state.visitChannels
// let userDate = store.state.userData
let userInfo = uni.getStorageSync('userInfo')
console.log('userInfo', userInfo);
const app = getApp();
// let req = {
// userName: userDate.MEMBERSHIP_NAME,
// phoneNumber: userDate.MEMBERSHIP_MOBILEPHONE,
// userId: userDate.MEMBERSHIP_ID ? userDate.MEMBERSHIP_ID : '',
// wechatAppId: 'wxee018fb96955552a',
// intoRoute: obj.intoRoute,
// outtoRoute: obj.outtoRoute,
// visitChannels: obj.visitChannels,
// behaviorRecordDesc: app.globalData.recommendId && app.globalData.recommendCode ? `recommendId=${app.globalData.recommendId}&recommendCode=${app.globalData.recommendCode}` : '',
// LoginIP: baseInfo.ip || '',
// LoginPlace: (baseInfo.prov ? baseInfo.prov : '') + (baseInfo.prov && baseInfo.city ? '-' : '') + (baseInfo.city ? baseInfo.city : ''),
// SOURCE_PLATFORM: '彩云驿出行'
// }
let req = {
userName: userDate.MEMBERSHIP_NAME,
phoneNumber: userDate.MEMBERSHIP_MOBILEPHONE,
userId: userDate.MEMBERSHIP_ID ? userDate.MEMBERSHIP_ID : '',
wechatAppId: 'wxee018fb96955552a',
intoRoute: obj.intoRoute || '/',
outtoRoute: obj.outtoRoute || '/',
visitChannels: obj.visitChannels || '',
// behaviorRecordDesc: app.globalData.recommendId && app.globalData.recommendCode ? `recommendId=${app.globalData.recommendId}&recommendCode=${app.globalData.recommendCode}` : '',
behaviorRecordDesc: obj.behaviorRecordDesc ? obj.behaviorRecordDesc : app.globalData.recommendId && app.globalData.recommendCode ? `recommendId=${app.globalData.recommendId}&recommendCode=${app.globalData.recommendCode}` : `${obj.intoRoute}跳转到${obj.outtoRoute}`,
LoginIP: userInfo.ip || '',
LoginPlace: (userInfo.prov ? userInfo.prov : '') + (userInfo.prov && userInfo.city ? '-' : '') + (userInfo.city ? userInfo.city : ''),
OperatingSystem: '彩云驿出行',
// type: 'encryption'
}
console.log('obj', obj);
console.log('req', req);
api.$get('/CommercialApi/UserBehavior/AddUserBehavior', req).then(() => {
})
// api.$get(
// "/CommercialApi/UserBehavior/AddUserBehavior",
// req
// ).then(() => {
// })
// api.$zzyLocalGet(
// "/Log/SynchroBEHAVIORRECORD",
// req
// );
}
export default {
formatNumber,
formatTime,
getLocalTime,
ArrayGroup,
barc,
qrc,
cutDate,
wrapTreeNode,
formatDate,
handleFormatNumber,
addUserBehaviorNew
}