wangfei 6 years ago
parent
commit
bc1d0c307a
3 changed files with 99 additions and 41 deletions
  1. 2
    0
      public/index.html
  2. 92
    40
      src/pages/user/mainPage/libraryIndex/index.vue
  3. 5
    1
      src/util/share.js

+ 2
- 0
public/index.html View File

@@ -8,6 +8,8 @@
8 8
     charset="utf-8" />
9 9
   <link rel="stylesheet" href="//at.alicdn.com/t/font_775069_clu263vfyod.css">
10 10
   <link rel="icon" href="<%= BASE_URL %>favicon.ico">
11
+  <script src="https://res.wx.qq.com/mmbizwap/zh_CN/htmledition/js/vconsole/3.0.0/vconsole.min.js"></script>
12
+  <script> new VConsole(); </script>
11 13
   <!-- <script src="https://jsconsole.com/js/remote.js?34f890f2-39c4-46f7-9d9e-7a8cb29a6007"></script> -->
12 14
   <!-- <script src="//cdn.bootcss.com/eruda/1.5.2/eruda.min.js"></script>
13 15
   <script>eruda.init();</script> -->

+ 92
- 40
src/pages/user/mainPage/libraryIndex/index.vue View File

@@ -123,7 +123,7 @@ export default {
123 123
     radio,
124 124
     loadMore
125 125
   },
126
-  mounted () {
126
+  created () {
127 127
     if (!this.CaseList.length || this.CaseList === null) {
128 128
       this.getCaseList().then((res) => {
129 129
         (res.cases || []).map((item) => {
@@ -132,7 +132,7 @@ export default {
132 132
             id: item.CaseId
133 133
           })
134 134
         })
135
-        this.Location()
135
+        this.detectLoc().then(() => this.init()).catch(() => this.init())
136 136
       })
137 137
     } else {
138 138
       (this.CaseList || []).map((item) => {
@@ -141,7 +141,7 @@ export default {
141 141
           id: item.CaseId
142 142
         })
143 143
       })
144
-      this.Location()
144
+      this.detectLoc().then(() => this.init()).catch(() => this.init())
145 145
     }
146 146
   },
147 147
   methods: {
@@ -165,46 +165,98 @@ export default {
165 165
       }
166 166
       this.$router.push({name: 'booksSearch', query: {caseid: this.currentCaseId}})
167 167
     },
168
-    Location () {
169
-      let that = this
170
-      wxsdk({ url: encodeURIComponent(window.location.href.split('#')[0]) }, {
171
-        title: '城的空间',
172
-        desc: '城的空间',
173
-        link: `${window.location.origin}${window.location.pathname}#/mainPage/indexPage`,
174
-        thu_image: `https://spaceofcheng.oss-cn-beijing.aliyuncs.com/indexlogo.jpg?x-oss-process=style/wxicon`
175
-      }).then(() => {
176
-      })
177
-      if(!this.bookcaseid) {
178
-        setTimeout(() => {
179
-          if(that.latitude == '' || that.longitude == '') {
180
-            that.currentCaseId = ((that.CaseList || [])[0] || {}).CaseId
168
+    detectLoc () {
169
+      return new Promise((resolve, reject) => {
170
+        let hasSol = false
171
+        const startDt = (new Date).getTime()
172
+
173
+        const t = window.setInterval(() => {
174
+          const nw = (new Date).getTime()
175
+
176
+          if (hasSol) {
177
+            window.clearInterval(t)
178
+            return
179
+          }
180
+
181
+          // 500 毫秒还未定位则直接取消定位
182
+          if ((nw - startDt) >= 500) {
183
+            hasSol = true
184
+            window.clearInterval(t)
185
+
186
+            this.setDefaultCase(((this.CaseList || [])[0] || {}).CaseId)
187
+            reject('定位超时 ...')
188
+          }
189
+        }, 100)
190
+
191
+        wxsdk({ url: encodeURIComponent(window.location.href.split('#')[0]) }, {
192
+          title: '城的空间',
193
+          desc: '城的空间',
194
+          link: `${window.location.origin}${window.location.pathname}#/mainPage/indexPage`,
195
+          thu_image: `https://spaceofcheng.oss-cn-beijing.aliyuncs.com/indexlogo.jpg?x-oss-process=style/wxicon`
196
+        }).then(() => {
197
+          if (hasSol) return
198
+          hasSol = true
199
+          const that = this
200
+
201
+          // 如果已经存在案场
202
+          if (!!that.bookcaseid) {
203
+            that.currentCaseId = that.bookcaseid
204
+            that.caseId = that.bookcaseid
205
+            that.currentCaseName = (that.CaseList.filter(x => x.CaseId === that.currentCaseId)[0] || {}).CaseName
206
+            resolve()
181 207
           } else {
182
-            for (let i = 0; i < that.CaseList.length; i++) {
183
-              let result = that.CaseList[i].Coordinate.split(",")
184
-              let latitude2 = parseFloat(result[0])
185
-              let longitude2 = parseFloat(result[1])
186
-              if (that.toolClass.getGreatCircleDistance(that.latitude, that.longitude, latitude2, longitude2) < 500) {
187
-                that.currentCaseId = that.CaseList[i].CaseId
188
-              }
189
-            }
208
+            // 如果没有案场
209
+            wx.getLocation({
210
+              type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
211
+              success: function (res) {
212
+                that.latitude = res.latitude // 纬度,浮点数,范围为90 ~ -90
213
+                that.longitude = res.longitude // 经度,浮点数,范围为180 ~ -180。
214
+                console.log('获取到位置 ->', that.latitude, that.longitude)
215
+              },
216
+              complete: function () {
217
+                console.log('获取位置后, 不管成功与否', that.latitude, that.longitude)
218
+
219
+                if(that.latitude && that.longitude ) {
220
+                  for (let i = 0; i < that.CaseList.length; i++) {
221
+                    let result = that.CaseList[i].Coordinate.split(",")
222
+                    let latitude2 = parseFloat(result[0])
223
+                    let longitude2 = parseFloat(result[1])
224
+                    if (that.toolClass.getGreatCircleDistance(that.latitude, that.longitude, latitude2, longitude2) < 500) {
225
+                      that.currentCaseId = that.CaseList[i].CaseId
226
+                    }
227
+                  }
228
+                }
229
+
230
+                if(!that.currentCaseId) {
231
+                  that.currentCaseId = ((that.CaseList || [])[0] || {}).CaseId
232
+                }
233
+
234
+                that.setDefaultCase(that.currentCaseId)
235
+                resolve()
236
+              },
237
+            })
238
+          }
239
+        }).catch((err) => {
240
+          console.log('微信初始化失败', err.message)
241
+          if (hasSol) return
242
+          hasSol = true
243
+
244
+          // 如果已经存在案场
245
+          if (!!this.bookcaseid) {
246
+            this.setDefaultCase(this.bookcaseid)
247
+          } else {
248
+            this.setDefaultCase(((that.CaseList || [])[0] || {}).CaseId)
190 249
           }
191
-          that.caseId = that.currentCaseId
192
-          that.setBookCase(that.caseId)
193
-          that.currentCaseName = ((that.CaseList || [])[0] || {}).CaseName
194
-          that.init()
195
-        }, 500)
196
-        wx.getLocation({
197
-          type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
198
-          success: function (res) {
199
-            that.latitude = res.latitude // 纬度,浮点数,范围为90 ~ -90
200
-            that.longitude = res.longitude // 经度,浮点数,范围为180 ~ -180。
201
-          },
250
+
251
+          reject()
202 252
         })
203
-      } else {
204
-        this.currentCaseId = this.bookcaseid
205
-        this.caseId = this.bookcaseid
206
-        this.currentCaseName = (this.CaseList.filter(x => x.CaseId === this.currentCaseId)[0] || {}).CaseName
207
-      }
253
+      })
254
+    },
255
+    setDefaultCase(caseID) {
256
+      this.setBookCase(caseID)
257
+      this.caseId = caseID
258
+      this.currentCaseId = caseID
259
+      this.currentCaseName = (this.CaseList.filter(x => x.CaseId === caseID)[0] || {}).CaseName
208 260
     },
209 261
     selectCaseId () { // 选择案场
210 262
       if (this.currentCaseId !== this.caseId) {

+ 5
- 1
src/util/share.js View File

@@ -39,7 +39,11 @@ function wxsdk (params, config) {
39 39
       wx.ready(function () {
40 40
         wx.onMenuShareTimeline(shareData)
41 41
         wx.onMenuShareAppMessage(shareData)
42
-        resolve('success')
42
+        resolve(wx)
43
+      })
44
+
45
+      wx.error(function(err) {
46
+        reject(err)
43 47
       })
44 48
     }).catch((err) => {
45 49
       reject(err)