import { includeList } from './router.config.js' // 配置信息 import ajaxService from '@/services/ajax.service.js' import UserService from '@/services/user.service' const UserApi = new UserService(ajaxService) // 校验是否为配置的路径 const isInclude = (url) => { if (!url) return false return includeList.some(item => url.indexOf(item.url) > -1) } // 校验返回页面类型 const isIncludeType = (url) => { if (!url) return false return includeList.find(item => url === item.url) } // 参数 const defaultParams = { accessClient: 1, // 来源 0 网站 1 小程序 pagePath: '', //页面路径 accessDuration: 0, //浏览时长初始值为 0 pageType: '', //页面类型 pageLabel: '', //页面标签 userId: 0, //用户Id productId: 0, //商品Id behaviorType: 1 // 统计类型 } // 页面进入 const enter = async (current) => { await userBehavior(current) current.meta.enterTime = Date.now() } // 页面离开 const leave = (prev) => { prev.meta.leaveTime = Date.now() } // 页面切换 const routting = async (current, prev) => { // await userBehavior(current, prev) } /* 用户停留时间 */ async function userBehavior(current) { try { // if (process.env.NODE_ENV !== 'production') { return } if (!current) return if (!isInclude(current.path)) return console.log('\n') console.log('------------------------') console.log('当前页面:', current.path) // console.log('离开页面:', prev.path) // 接口参数设置 setTimeout(()=>{ setingSysParams(current) },1000) // 清除设置缓存 clearsSysParams(current) console.log('\n') console.log('------------------------') console.log('\n') } catch (e) { console.log(e) console.log('---用户行为轨迹记录异常---') } } // 接口参数设置 const setingSysParams = async (current) => { const sysParams = Object.assign({}, defaultParams) const userSync = uni.getStorageSync('userInfo') const pageData = isIncludeType(current.path) // 筛除掉协销不做统计 if (userSync.userIdentity === 1) return // 筛除掉以下页面路径的重复统计 if (current.path === '/pages/goods/product' || current.path === '/pages/search/search' || current.path === '/pages/supplier/user/my-shop' || current.path === '/pages/search/search-supplier' ) { return } // 参数设置 sysParams.userId = userSync.userId ? userSync.userId : 0 sysParams.pagePath = current.fullPath sysParams.pageType = pageData ? pageData.pageType : '' sysParams.behaviorType = uni.getStorageSync('behaviorType') ? uni.getStorageSync('behaviorType') : 1 // 根据path获取不同的参数 if (current.path === '/pages/supplier/user/my-shop') { sysParams.shopId = current.query.shopId ? current.query.shopId : 0 } if (current.path === '/pages/goods/product' || current.path === '/pages/second/product/product-details') { sysParams.productId = current.query.id ? current.query.id : 0 sysParams.pageLabel = uni.getStorageSync('productLabel') } else { sysParams.pageLabel = uni.getStorageSync('pageLabel') ? uni.getStorageSync('pageLabel') : pageData.pageLabel } console.log('记录路径:', sysParams.pagePath, '标签:', sysParams.pageLabel) // 统计接口调用 await UserApi.userRecordStatistics(sysParams) console.log('---用户行为轨迹记录成功---') } // 清除设置缓存 const clearsSysParams = async (current) => { if (current.path === '/pages/goods/product' || current.path === '/pages/second/product/product-details') { uni.removeStorageSync('productLabel') } else { uni.removeStorageSync('pageLabel') } uni.removeStorageSync('behaviorType') } export default { enter, leave, routting }