[baozhangchao] 3 yıl önce
ebeveyn
işleme
38086371f0
2 değiştirilmiş dosya ile 43 ekleme ve 7 silme
  1. 9
    1
      src/main.js
  2. 34
    6
      src/util/initial.js

+ 9
- 1
src/main.js Dosyayı Görüntüle

@@ -16,7 +16,7 @@ import {
16 16
   Image, SwipeItem, Tag, Swipe, Divider, Picker, Popup, Dialog, List, ImagePreview
17 17
 } from 'vant';
18 18
 import App from './App.vue'
19
-import { Login, redirect } from './util/initial'
19
+import { Login, redirect, getQueryPath } from './util/initial'
20 20
 
21 21
 // import Questionnaire from './views/components/Questionnaire'
22 22
 // import Loading from './views/components/Questionnaire/Loading.vue';
@@ -25,6 +25,14 @@ import store from './store';//状态管理
25 25
 
26 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 36
 Vue.config.productionTip = false
29 37
 
30 38
 Vue.use(Popup);

+ 34
- 6
src/util/initial.js Dosyayı Görüntüle

@@ -13,7 +13,7 @@ const jsApiList = [
13 13
 ]
14 14
 
15 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 17
     window.wx.config({
18 18
       debug: process.env.NODE_ENV === 'development', // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
19 19
       appId: res.appId, // 必填,公众号的唯一标识
@@ -31,11 +31,11 @@ function initSDK (url) {
31 31
  * @param {*} opt
32 32
  */
33 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 39
   const imgUrl = opt.imgUrl || defaultImg
40 40
 
41 41
   initSDK(link)
@@ -54,7 +54,36 @@ export function share (opt) {
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 89
 // Vue.use(Vuex)
@@ -102,7 +131,6 @@ export function hideLoading () {
102 131
   document.getElementsByClassName('page-loading-wrapper')[0].style = "display: none"
103 132
 }
104 133
 
105
-
106 134
 /**
107 135
  * 获取 code
108 136
  * @returns