[baozhangchao] 3 years ago
parent
commit
38086371f0
2 changed files with 43 additions and 7 deletions
  1. 9
    1
      src/main.js
  2. 34
    6
      src/util/initial.js

+ 9
- 1
src/main.js View File

16
   Image, SwipeItem, Tag, Swipe, Divider, Picker, Popup, Dialog, List, ImagePreview
16
   Image, SwipeItem, Tag, Swipe, Divider, Picker, Popup, Dialog, List, ImagePreview
17
 } from 'vant';
17
 } from 'vant';
18
 import App from './App.vue'
18
 import App from './App.vue'
19
-import { Login, redirect } from './util/initial'
19
+import { Login, redirect, getQueryPath } from './util/initial'
20
 
20
 
21
 // import Questionnaire from './views/components/Questionnaire'
21
 // import Questionnaire from './views/components/Questionnaire'
22
 // import Loading from './views/components/Questionnaire/Loading.vue';
22
 // import Loading from './views/components/Questionnaire/Loading.vue';
25
 
25
 
26
 import './global_style.less'
26
 import './global_style.less'
27
 
27
 
28
+// 如果有 query 分享路径
29
+const queryPath = getQueryPath()
30
+if (queryPath) {
31
+  window.location.href = queryPath
32
+  return
33
+}
34
+
35
+
28
 Vue.config.productionTip = false
36
 Vue.config.productionTip = false
29
 
37
 
30
 Vue.use(Popup);
38
 Vue.use(Popup);

+ 34
- 6
src/util/initial.js View File

13
 ]
13
 ]
14
 
14
 
15
 function initSDK (url) {
15
 function initSDK (url) {
16
-  request(`https://api.h5.njyunzhi.com/mp/jssdk?url=${encodeURIComponent(url)}`).then((res) => {
16
+  request(`http://api.h5.njyunzhi.com/mp/jssdk?url=${encodeURIComponent(url)}`).then((res) => {
17
     window.wx.config({
17
     window.wx.config({
18
       debug: process.env.NODE_ENV === 'development', // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
18
       debug: process.env.NODE_ENV === 'development', // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
19
       appId: res.appId, // 必填,公众号的唯一标识
19
       appId: res.appId, // 必填,公众号的唯一标识
31
  * @param {*} opt
31
  * @param {*} opt
32
  */
32
  */
33
 export function share (opt) {
33
 export function share (opt) {
34
-  const { origin, pathname, search } = window.location
35
-  // const defaultLink = origin + pathname
36
-  const defaultImg = `${origin}${pathname}images/share.png`
34
+  const { origin, pathname, search, hash } = window.location
35
+  const defaultImg = `${origin}${pathname}images/share.jpg`
37
 
36
 
38
-  const link = origin + pathname + search // opt.link || defaultLink
37
+  // const link = origin + pathname + search
38
+  const link = encodeHashLocation()
39
   const imgUrl = opt.imgUrl || defaultImg
39
   const imgUrl = opt.imgUrl || defaultImg
40
 
40
 
41
   initSDK(link)
41
   initSDK(link)
54
   })
54
   })
55
 }
55
 }
56
 
56
 
57
+/**
58
+ * hash 路由处理为可分享路由
59
+ */
60
+export function encodeHashLocation () {
61
+  const { origin, pathname, search, hash } = window.location
62
+
63
+  const hashRoute = hash.substring(1)
64
+  const routeParam = btoa(hashRoute)
65
+
66
+  const query = search ? `${search}&p=${routeParam}` : `?p=${routeParam}`
67
+
68
+  return origin + pathname + query
69
+}
70
+
71
+/**
72
+ * getQueryPath 获取 query 中的 path 参数
73
+ * @returns 
74
+ */
75
+export function getQueryPath () {
76
+  const { origin, pathname, search, hash } = window.location
77
+  if (!search) return undefined
57
 
78
 
79
+  const queryStr = search.substring(1) // 去掉 ?
80
+  const pathParam = queryStr.split('&').filter(q => /^p=/.test(q))[0]
81
+  if (!pathParam) return undefined
82
+
83
+  const path = atob(pathParam.substring(2))
84
+
85
+  return origin + pathname + search + '#' + path
86
+}
58
 
87
 
59
 
88
 
60
 // Vue.use(Vuex)
89
 // Vue.use(Vuex)
102
   document.getElementsByClassName('page-loading-wrapper')[0].style = "display: none"
131
   document.getElementsByClassName('page-loading-wrapper')[0].style = "display: none"
103
 }
132
 }
104
 
133
 
105
-
106
 /**
134
 /**
107
  * 获取 code
135
  * 获取 code
108
  * @returns 
136
  * @returns