|
@@ -0,0 +1,114 @@
|
|
|
+import Vue from 'vue'
|
|
|
+import UniRouteGuards from '../js_sdk/pocky-route-gurads/lib/index.js' // 路由导航守卫
|
|
|
+import { includeList } from './router.config.js' // 配置信息
|
|
|
+import ajaxService from '@/services/ajax.service.js'
|
|
|
+import UserService from '@/services/user.service'
|
|
|
+const UserApi = new UserService(ajaxService)
|
|
|
+// 路由导航守卫
|
|
|
+Vue.use(UniRouteGuards)
|
|
|
+
|
|
|
+const map = new Map()
|
|
|
+
|
|
|
+const bucketStack = []
|
|
|
+
|
|
|
+const guard = new UniRouteGuards()
|
|
|
+// 截取路径参数
|
|
|
+const getUrlParams = (appPath) => {
|
|
|
+ const queryArr = appPath.split('?')
|
|
|
+ const queryStr = queryArr[1]
|
|
|
+ let query = queryStr.split('&')
|
|
|
+ let params = {}
|
|
|
+ for (let i = 0; i < query.length; i++) {
|
|
|
+ let q = query[i].split('=')
|
|
|
+ console.log(q, 'q')
|
|
|
+ if (q.length === 2) {
|
|
|
+ params[q[0]] = q[1]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return params
|
|
|
+}
|
|
|
+// 校验是否为配置的路径
|
|
|
+const isInclude = (url) => {
|
|
|
+ if (!url) return false
|
|
|
+ return includeList.some(item => url.indexOf(item.url) > -1)
|
|
|
+}
|
|
|
+
|
|
|
+// 参数
|
|
|
+const userSync = uni.getStorageSync('userInfo')
|
|
|
+const defaultParams = {
|
|
|
+ pagePath: '', //页面路径
|
|
|
+ accessDuration: 0, //浏览时长初始值为 0
|
|
|
+ pageType: '', //页面类型
|
|
|
+ pageLabel: '', //页面标签
|
|
|
+ userId: userSync ? userSync.userId : 0, //用户Id
|
|
|
+ productId: 0 //商品Id
|
|
|
+}
|
|
|
+// 上送接口Api
|
|
|
+const userRecordStatistics = (params) => {
|
|
|
+ UserApi.userRecordStatistics(params)
|
|
|
+ .then(response => {
|
|
|
+ console.log('<=上送用户行为记录成功=>',response.msg)
|
|
|
+ })
|
|
|
+ .catch(error => {
|
|
|
+ console.log('<=上送用户行为记录异常=>',error.msg)
|
|
|
+ return
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+// 跳过路由白名单拦截
|
|
|
+guard.beforeEach((to, from, next) => {
|
|
|
+ console.log('\n')
|
|
|
+ console.log('============')
|
|
|
+ console.log('guard.beforeEach')
|
|
|
+ console.log('to:', to)
|
|
|
+ console.log('from:', from)
|
|
|
+ console.log('页面拦截状态:', isInclude(to.url))
|
|
|
+ // if (to.action !== 'navigateBack') {
|
|
|
+ // bucketStack.push(to.url)
|
|
|
+ // console.log('入栈:', to.url)
|
|
|
+ // }
|
|
|
+
|
|
|
+ if (isInclude(to.url)) {
|
|
|
+ console.log('stay time started')
|
|
|
+ map.set(to.url.split('?')[0], Date.now())
|
|
|
+ defaultParams.pagePath = to.url
|
|
|
+ console.log('pagePath', defaultParams.pagePath)
|
|
|
+ const urlParams = getUrlParams(to.url)
|
|
|
+ if(urlParams){
|
|
|
+ defaultParams.productId = urlParams ? urlParams.id : 0
|
|
|
+ console.log('defaultParams', defaultParams)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // map.set(to.url.split('?')[0], Date.now())
|
|
|
+ console.log('============')
|
|
|
+ console.log('\n')
|
|
|
+ next()
|
|
|
+})
|
|
|
+
|
|
|
+guard.afterEach((to, from) => {
|
|
|
+ console.log('\n')
|
|
|
+ console.log('============')
|
|
|
+ console.log('guard.afterEach')
|
|
|
+ console.log('to:', to)
|
|
|
+ console.log('from:', from)
|
|
|
+ // if (to.action === 'navigateBack') {
|
|
|
+ // const lastUrl = bucketStack.pop()
|
|
|
+ // map.set(lastUrl.split('?')[0], Date.now())
|
|
|
+ // const current = bucketStack[bucketStack.length - 1]
|
|
|
+ // if(current){
|
|
|
+ // map.set(current.split('?')[0], Date.now())
|
|
|
+ // }
|
|
|
+ // console.log('未关闭页面栈列表:', bucketStack)
|
|
|
+ // }
|
|
|
+
|
|
|
+ if (map.has(from.url)) {
|
|
|
+ const beginTime = map.get(from.url)
|
|
|
+ defaultParams.accessDuration = Date.now() - beginTime
|
|
|
+ console.log('页面停留时间:', (Date.now() - beginTime), '秒')
|
|
|
+ // api
|
|
|
+ console.log('api is action ...')
|
|
|
+ userRecordStatistics(defaultParams)
|
|
|
+ }
|
|
|
+ console.log('============')
|
|
|
+ console.log('\n')
|
|
|
+})
|