浏览代码

获取地理位置封装

seimin 3 年之前
父节点
当前提交
6a59443032
共有 2 个文件被更改,包括 138 次插入97 次删除
  1. 31 2
      src/http/http.js
  2. 107 95
      src/views/my.vue

+ 31 - 2
src/http/http.js

@@ -16,12 +16,12 @@ export function SM(Vue) {
16 16
           signature: res.signature, // 必填,签名,见附录1
17 17
           jsApiList: res.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
18 18
         });
19
-        wx.ready(function() {
19
+        wx.ready(function () {
20 20
           wx.scanQRCode({
21 21
             desc: "scanQRCode desc",
22 22
             needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果,
23 23
             scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码,默认二者都有
24
-            success: function(res) {
24
+            success: function (res) {
25 25
               // 当needResult 为 1 时,扫码返回的结果
26 26
               let str = res.resultStr.replace(/[\s\/]/g, '') || 'none';
27 27
               resolve(str);
@@ -32,3 +32,32 @@ export function SM(Vue) {
32 32
     })
33 33
   });
34 34
 }
35
+// 获取地理位置
36
+export function GL(Vue) {
37
+  return new Promise((resolve, reject) => {
38
+    let param = {
39
+      requestUrl: location.href.split('#')[0]
40
+    };
41
+    Vue.$http.post("service/wechat/getJsConfig", param).then(res => {
42
+      res = res.data;
43
+      if (res) {
44
+        wx.config({
45
+          debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
46
+          appId: res.appId, // 必填,企业号的唯一标识,此处填写企业号corpid
47
+          timestamp: res.timestamp, // 必填,生成签名的时间戳
48
+          nonceStr: res.nonceStr, // 必填,生成签名的随机串
49
+          signature: res.signature, // 必填,签名,见附录1
50
+          jsApiList: res.jsApiList // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
51
+        });
52
+        wx.ready(function () {
53
+          wx.getLocation({
54
+            type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
55
+            success: function (res) {
56
+              resolve(res);
57
+            }
58
+          })
59
+        });
60
+      }
61
+    })
62
+  });
63
+}

+ 107 - 95
src/views/my.vue

@@ -118,8 +118,11 @@
118 118
   </div>
119 119
 </template>
120 120
 <script>
121
-let sdk = new jssdk(3, "https://wx2.zuel.edu.cn"); // 域名更换为对应环境域名, 204 更换为对应环境wid
122
-sdk.config(0); //1表示调试模式,生产环境传0
121
+if (this.isZncd) {
122
+  let sdk = new jssdk(3, "https://wx2.zuel.edu.cn"); // 域名更换为对应环境域名, 204 更换为对应环境wid
123
+  sdk.config(0); //1表示调试模式,生产环境传0
124
+}
125
+import { GL } from "./../http/http";
123 126
 import "./../../static/css/iconfont.js";
124 127
 export default {
125 128
   data() {
@@ -132,126 +135,134 @@ export default {
132 135
       groups: "", //工作组
133 136
       candidateGroups: "",
134 137
       toast: null,
135
-      isWorkOnline:false,//当前用户是否可以上下班
136
-      isWorkLoading:false,//当前用户是否可以上下班loading
137
-      online:0,//1是上班,0是下班
138
+      isWorkOnline: false, //当前用户是否可以上下班
139
+      isWorkLoading: false, //当前用户是否可以上下班loading
140
+      online: 0, //1是上班,0是下班
138 141
     };
139 142
   },
140 143
   methods: {
141 144
     //获取位置,经纬度
142 145
     getLocation(online) {
143 146
       // 判断是否处于微信浏览器环境
144
-      if(!(/MicroMessenger/i.test(window.navigator.userAgent))){
147
+      if (!/MicroMessenger/i.test(window.navigator.userAgent)) {
145 148
         this.$createToast({
146
-          txt: '请前往微信中上下班',
147
-          type: 'warn',
148
-          mask: true
149
-        }).show()
149
+          txt: "请前往微信中上下班",
150
+          type: "warn",
151
+          mask: true,
152
+        }).show();
150 153
         return;
151 154
       }
152
-      let _this = this;
153 155
       this.toast = this.$createToast({
154 156
         time: 0,
155 157
         mask: true,
156
-        txt: "正在加载中"
158
+        txt: "正在加载中",
157 159
       });
158 160
       this.toast.show();
159
-      // 本地测试
160
-      if(!this.isZncd){
161
-        this.goToWork(online, '');
162
-        return;
161
+      // 中南财大
162
+      if (this.isZncd) {
163
+        //第二个参数 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
164
+        sdk.getLocation((res) => {
165
+          // alert(JSON.stringify(res));
166
+          // latitude
167
+          // longitude
168
+          this.gl(res, online);
169
+        });
170
+      } else {
171
+        GL(this).then((res) => {
172
+          this.gl(res, online);
173
+        });
163 174
       }
164
-      //第二个参数 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
165
-      sdk.getLocation(res => {
166
-        // alert(JSON.stringify(res));
167
-        // latitude
168
-        // longitude
169
-        $.ajax({
170
-          url: "https://apis.map.qq.com/ws/geocoder/v1",
171
-          data: {
172
-            location: res.latitude + "," + res.longitude,
173
-            key: "TADBZ-IRBWU-QAXVE-2IWED-UW2F5-YVF66",
174
-            output: "jsonp"
175
-          },
176
-          dataType: "jsonp",
177
-          success: function(result) {
178
-            if (result.status === 0) {
179
-              let address = result.result.formatted_addresses.recommend;
180
-              _this.goToWork(online, address);
181
-            }
175
+    },
176
+    //获取地理位置方法
177
+    gl(res, online) {
178
+      let _this = this;
179
+      $.ajax({
180
+        url: "https://apis.map.qq.com/ws/geocoder/v1",
181
+        data: {
182
+          location: res.latitude + "," + res.longitude,
183
+          key: "TADBZ-IRBWU-QAXVE-2IWED-UW2F5-YVF66",
184
+          output: "jsonp",
185
+        },
186
+        dataType: "jsonp",
187
+        success(result) {
188
+          if (result.status === 0) {
189
+            let address = result.result.formatted_addresses.recommend;
190
+            _this.goToWork(online, address);
182 191
           }
183
-        });
192
+        },
184 193
       });
185 194
     },
186 195
     // 上下班
187 196
     goToWork(online, address) {
188 197
       this.$http
189 198
         .post("service/auth/online", { online, address })
190
-        .then(res => {
199
+        .then((res) => {
191 200
           if (res.data.status == 200) {
192
-            this.$http
193
-              .post("service/auth/showOnlineBtn", {})
194
-              .then((res) => {
195
-                this.toast.hide();
196
-                if(res.data.status == 200){
197
-                  this.online = res.data.online;
198
-                  this.showToast(online, '成功');
199
-                }
200
-              });
201
+            this.$http.post("service/auth/showOnlineBtn", {}).then((res) => {
202
+              this.toast.hide();
203
+              if (res.data.status == 200) {
204
+                this.online = res.data.online;
205
+                this.showToast(online, "成功");
206
+              }
207
+            });
201 208
           } else {
202 209
             this.toast.hide();
203
-             this.$createDialog({
204
-              type: 'confirm',
205
-              icon: 'cubeic-alert',
206
-              title: '提示',
207
-              content: '您现在为非下班时间,您确认要进行下班操作吗?',
210
+            this.$createDialog({
211
+              type: "confirm",
212
+              icon: "cubeic-alert",
213
+              title: "提示",
214
+              content: "您现在为非下班时间,您确认要进行下班操作吗?",
208 215
               confirmBtn: {
209
-                text: '确定按钮',
216
+                text: "确定按钮",
210 217
                 active: true,
211 218
                 disabled: false,
212
-                href: 'javascript:;'
219
+                href: "javascript:;",
213 220
               },
214 221
               cancelBtn: {
215
-                text: '取消按钮',
222
+                text: "取消按钮",
216 223
                 active: false,
217 224
                 disabled: false,
218
-                href: 'javascript:;'
225
+                href: "javascript:;",
219 226
               },
220 227
               onConfirm: () => {
221 228
                 this.toast = this.$createToast({
222 229
                   time: 0,
223 230
                   mask: true,
224
-                  txt: "正在加载中"
231
+                  txt: "正在加载中",
225 232
                 });
226 233
                 this.toast.show();
227 234
                 this.$http
228
-                .post("service/auth/online", { online, address, confirm:'ok' })
229
-                .then(res => {
230
-                  if (res.data.status == 200) {
231
-                    this.$http
232
-                    .post("service/auth/showOnlineBtn", {})
233
-                    .then((res) => {
235
+                  .post("service/auth/online", {
236
+                    online,
237
+                    address,
238
+                    confirm: "ok",
239
+                  })
240
+                  .then((res) => {
241
+                    if (res.data.status == 200) {
242
+                      this.$http
243
+                        .post("service/auth/showOnlineBtn", {})
244
+                        .then((res) => {
245
+                          this.toast.hide();
246
+                          if (res.data.status == 200) {
247
+                            this.online = res.data.online;
248
+                            this.showToast(online, "成功");
249
+                          }
250
+                        });
251
+                    } else {
234 252
                       this.toast.hide();
235
-                      if(res.data.status == 200){
236
-                        this.online = res.data.online;
237
-                        this.showToast(online, '成功')
238
-                      }
239
-                    });
240
-                  }else{
241
-                    this.toast.hide();
242
-                    this.showToast(online, '失败')
243
-                  }
244
-                })
253
+                      this.showToast(online, "失败");
254
+                    }
255
+                  });
245 256
               },
246
-            }).show()
257
+            }).show();
247 258
           }
248 259
         });
249 260
     },
250 261
     // 展示信息
251
-    showToast(online, msg){
262
+    showToast(online, msg) {
252 263
       const t = this.$createToast({
253 264
         type: "txt",
254
-        txt: online == 1 ? "上班"+msg : "下班"+msg
265
+        txt: online == 1 ? "上班" + msg : "下班" + msg,
255 266
       });
256 267
       t.show();
257 268
       setTimeout(() => {
@@ -262,7 +273,7 @@ export default {
262 273
     getGroups() {
263 274
       var that = this;
264 275
       if (that.loginUser.group) {
265
-        that.loginUser.group.forEach(element => {
276
+        that.loginUser.group.forEach((element) => {
266 277
           that.groups += element.groupName + "/";
267 278
           that.candidateGroups += element.id + ",";
268 279
         });
@@ -281,8 +292,8 @@ export default {
281 292
       this.$router.push({
282 293
         name: "Inspection",
283 294
         params: {
284
-          state: state
285
-        }
295
+          state: state,
296
+        },
286 297
       });
287 298
     },
288 299
     // 事件数量
@@ -291,42 +302,43 @@ export default {
291 302
       this.$http
292 303
         .post("service/bpm/data/getIncidentCount", {
293 304
           assignee: that.loginUser.id,
294
-          candidateGroups: that.candidateGroups
305
+          candidateGroups: that.candidateGroups,
295 306
         })
296
-        .then(function(res) {
307
+        .then(function (res) {
297 308
           that.incidentCount = res.data;
298 309
         });
299 310
     },
300 311
     //获取展示可以上下班人员列表
301
-    getWorkOnline(){
312
+    getWorkOnline() {
302 313
       this.isWorkLoading = true;
303 314
       this.$http
304
-        .post("service/bpm/data/fetchDataList/workOnlineConfig", {"idx":0,"sum":1000})
315
+        .post("service/bpm/data/fetchDataList/workOnlineConfig", {
316
+          idx: 0,
317
+          sum: 1000,
318
+        })
305 319
         .then((res) => {
306
-          if(res.status == 200){
307
-            let userList = res.data.list[0].userList.map(v=>v.id);//用户id列表
320
+          if (res.status == 200) {
321
+            let userList = res.data.list[0].userList.map((v) => v.id); //用户id列表
308 322
             this.isWorkOnline = userList.includes(this.loginUser.id);
309
-            if(this.isWorkOnline){
310
-              this.$http
311
-                .post("service/auth/showOnlineBtn", {})
312
-                .then((res) => {
313
-                  this.isWorkLoading = false;
314
-                  if(res.data.status == 200){
315
-                    this.online = res.data.online;
316
-                  }
317
-                });
318
-            }else{
323
+            if (this.isWorkOnline) {
324
+              this.$http.post("service/auth/showOnlineBtn", {}).then((res) => {
325
+                this.isWorkLoading = false;
326
+                if (res.data.status == 200) {
327
+                  this.online = res.data.online;
328
+                }
329
+              });
330
+            } else {
319 331
               this.isWorkLoading = false;
320 332
             }
321 333
           }
322 334
         });
323
-    }
335
+    },
324 336
   },
325 337
   created() {
326 338
     this.getGroups();
327 339
     this.getIncidentData();
328 340
     this.getWorkOnline();
329
-  }
341
+  },
330 342
 };
331 343
 </script>
332 344
 <style scoped>