张延森 3 years ago
parent
commit
6bf061aee2

+ 13
- 1
src/layouts/index.jsx View File

2
 import { useMemo } from 'react'
2
 import { useMemo } from 'react'
3
 import Taro from '@tarojs/taro'
3
 import Taro from '@tarojs/taro'
4
 import { useModel } from '@/store'
4
 import { useModel } from '@/store'
5
+import { getQueryString } from '@/utils'
5
 import useRouter from '@/utils/hooks/useRouter'
6
 import useRouter from '@/utils/hooks/useRouter'
7
+import useTrackPage from '@/utils/hooks/useTrackPage'
6
 import Loading from './Loading'
8
 import Loading from './Loading'
7
 import { getPageBy, getIndexPageOf } from '../routes'
9
 import { getPageBy, getIndexPageOf } from '../routes'
8
 
10
 
9
 export default (Child) => (props) => {
11
 export default (Child) => (props) => {
12
+  const location = Taro.getStorageSync('location')
13
+
10
   const { person } = useModel('person')
14
   const { person } = useModel('person')
11
   const router = useRouter()
15
   const router = useRouter()
12
 
16
 
17
+  // 埋点页面访问
18
+  const trackPageData = {
19
+    page: router.path,
20
+    params: getQueryString(router.params),
21
+    location,
22
+  }
23
+  useTrackPage(trackPageData)
24
+
13
   // 确保人员信息到位
25
   // 确保人员信息到位
14
   const loading = !person?.personRole;
26
   const loading = !person?.personRole;
15
 
27
 
48
     })
60
     })
49
   }
61
   }
50
 
62
 
51
-  return loading ? <Loading /> : <Child {...props} person={person} router={router} />
63
+  return loading ? <Loading /> : <Child {...props} person={person} router={router} location={location} />
52
 }
64
 }

+ 1
- 0
src/pages/index/index.config.js View File

4
   styleIsolation: 'shared',
4
   styleIsolation: 'shared',
5
   usingComponents: {
5
   usingComponents: {
6
     "mp-tabbar": "weui-miniprogram/tabbar/tabbar",
6
     "mp-tabbar": "weui-miniprogram/tabbar/tabbar",
7
+    "mp-tabs": "../../../components/tabs/index",
7
     "mp-checkbox-group": "weui-miniprogram/checkbox-group/checkbox-group",
8
     "mp-checkbox-group": "weui-miniprogram/checkbox-group/checkbox-group",
8
     "mp-checkbox": "weui-miniprogram/checkbox/checkbox",
9
     "mp-checkbox": "weui-miniprogram/checkbox/checkbox",
9
     "mp-cells": "weui-miniprogram/cells/cells",
10
     "mp-cells": "weui-miniprogram/cells/cells",

+ 15
- 0
src/services/track.js View File

1
+import request from '@/utils/request'
2
+
3
+/**
4
+ * 埋点页面访问
5
+ * @param {*} data 
6
+ * @returns 
7
+ */
8
+export const trackPage = (data) => request('/track/page', { data, method: 'post', skipError: true })
9
+
10
+/**
11
+ * 更新埋点访问
12
+ * @param {*} id 
13
+ * @returns 
14
+ */
15
+export const updateTrackPage = (id) => request(`/track/page/${id}`, { method: 'put', skipError: true })

+ 19
- 2
src/utils/hooks/useTrackPage.js View File

1
 
1
 
2
 import { useRef } from 'react'
2
 import { useRef } from 'react'
3
-import { useDidShow, useDidHide } from '@tarojs/taro'
3
+import { useDidShow, useDidHide, nextTick } from '@tarojs/taro'
4
+import { trackPage, updateTrackPage } from '@/services/track'
4
 
5
 
5
-export default (fun) => {
6
+export default (payload) => {
7
+  const payloadRef = useRef()
8
+  const resRef = useRef()
9
+  payloadRef.current = payload
6
 
10
 
11
+  useDidShow(() => {
12
+    trackPage(payloadRef.current).then((res) => {
13
+      resRef.current = res;
14
+    }).catch((err) => {
15
+      console.error(err);
16
+    })
17
+  })
7
 
18
 
19
+  useDidHide(() => {
20
+    if (resRef.current) {
21
+      updateTrackPage(resRef.current.trackId)
22
+      resRef.current = undefined
23
+    }
24
+  })
8
 }
25
 }

+ 22
- 0
src/utils/index.js View File

1
 
1
 
2
+/**
3
+ * queryString 转 object
4
+ * @param {*} queryStr 
5
+ * @returns 
6
+ */
2
 export function parseQueryString(queryStr) {
7
 export function parseQueryString(queryStr) {
3
   if (!queryStr) {
8
   if (!queryStr) {
4
     return;
9
     return;
20
   }, {});
25
   }, {});
21
   return params;
26
   return params;
22
 }
27
 }
28
+
29
+/**
30
+ * object 转 queryStr
31
+ * @param {*} params 
32
+ * @returns 
33
+ */
34
+export function getQueryString(params) {
35
+  if (!params) return ;
36
+
37
+  const keys = Object.keys(params)
38
+  const arr = keys.map((key) => {
39
+    const val = params[key]
40
+    return `${key}=${encodeURIComponent(val)}`
41
+  })
42
+
43
+  return arr.join('&')
44
+}

+ 17
- 1
src/utils/preFetchData.js View File

5
   const personId = Taro.getStorageSync('personId')
5
   const personId = Taro.getStorageSync('personId')
6
   const openid = Taro.getStorageSync('openid')
6
   const openid = Taro.getStorageSync('openid')
7
 
7
 
8
-  return Promise.resolve({ personId, openid })
8
+  return new Promise((resolve, reject) => {
9
+    Taro.getLocation({
10
+      success(res) {
11
+        const location = `${res.longitude},${res.latitude}`
12
+        Taro.setStorageSync('location', location)
13
+        resolve({
14
+          location,
15
+          personId,
16
+          openid
17
+        })
18
+      },
19
+      fail(err) {
20
+        console.error(err)
21
+        reject(err)
22
+      }
23
+    })
24
+  })
9
 
25
 
10
   // return new Promise((resolve, reject) => {
26
   // return new Promise((resolve, reject) => {
11
   //   Taro.getBackgroundFetchData({
27
   //   Taro.getBackgroundFetchData({

+ 2
- 13
src/utils/request.js View File

1
 
1
 
2
 import Taro from '@tarojs/taro'
2
 import Taro from '@tarojs/taro'
3
+import { getQueryString } from '.'
3
 
4
 
4
 const logger = Taro.getRealtimeLogManager()
5
 const logger = Taro.getRealtimeLogManager()
5
 
6
 
6
-const object2QueryString = (params) => {
7
-  if (!params) return ;
8
-
9
-  const keys = Object.keys(params)
10
-  const arr = keys.map((key) => {
11
-    const val = params[key]
12
-    return `${key}=${encodeURIComponent(val)}`
13
-  })
14
-
15
-  return arr.join('&')
16
-}
17
-
18
 export default (url, options) => {
7
 export default (url, options) => {
19
   const { params, skipError, header, ...leftOptions } = options || {}
8
   const { params, skipError, header, ...leftOptions } = options || {}
20
-  const queryStr = object2QueryString(params)
9
+  const queryStr = getQueryString(params)
21
 
10
 
22
   const urlWithParams = queryStr ? `${url}?${queryStr}` : url;
11
   const urlWithParams = queryStr ? `${url}?${queryStr}` : url;
23
   const nwUrl = `${HOST}/api/wx${urlWithParams}`
12
   const nwUrl = `${HOST}/api/wx${urlWithParams}`