Fork me on GitHub

vue-cli项目中获取微信授权

1、引入微信js-sdk

npm install weixin-js-sdk --save

2、项目中使用

import wx from 'weixin-js-sdk',如下图:
“图片描述”

3、具体开始使用

具体代码如下:

1、template模板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<template>
<div class="wraper">
<loadmore :height="height" @loadBottom="loadBottom" @loadTop="loadTop" ref="loadmores">
<div>
<div class="box">
<mt-swipe :auto="2000">
<mt-swipe-item v-for="(item, index) in items" :key="item.id">
<img :src="item.url" class="img" @click="banner(index)"/>
</mt-swipe-item>
</mt-swipe>
</div>
<!--店铺列表-->
<div class="listWarper" v-for="(item, index) in list" :key="index">
<div class="listBox clearfix" @click="shopInfo(item.shopNum, item.customerNum)">
<div class="boxLeft">
<img :src="item.customerLogo">
</div>
<div class="boxRight">
<div class="boxRightTop">
<div class="clearfix">
<div class="boxRightTopName">
{{item.shopName}}
</div>
<div class="boxRightTopDistant">
{{item.distance}}
</div>
</div>
<div class="boxIntroduce">
<div class="boxVarieties" v-if="item.shopAddress">{{item.shopAddress}}</div>
<div class="boxVarieties" v-if="item.industryName">{{item.industryName}}</div>
</div>
</div>
</div>
</div>
</div>
</div>
</loadmore>
</div>
</template>

2、script部分(核心)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<script>
import wx from 'weixin-js-sdk'

export default {
data() {
return {
height: 'calc(100vh - 1rem)',
name: '111',
items: [{
title: '图片1',
url: require('../../assets/imgs/home_banner.jpg')
}, {
title: '图片2',
url: require('../../assets/imgs/home_banner.jpg')
}],
lat: '',
lng: '',
pageNum: 1,//页码
loader: true,
list: [],//数据
allLoaded: false,
bottomStatus: '',
wrapperHeight: 0,
topStatus: '',
translate: 0,
moveTranslate: 0,
}
},
created() {
//授权获取数据
this.getNearInfo();
},
methods: {
banner(index) {
console.log(`banner${index}`);
},
shopInfo(shopNum, customerNum) {
this.$router.push({path: '/shopList/shopInfo',query: {shopNum,customerNum}})
},
//上拉加载
loadBottom() {
this.$refs.loadmores.onBottomLoaded();
if (!this.loader) return;
let params = {
pageSize: 10,
currentPage: this.pageNum,
mapLat: this.lat,
mapLng: this.lng
}
this.getNearList(params);
},
//下拉刷新
loadTop() {
this.loader = true;
this.pageNum = 1;
this.$refs.loadmores.onTopLoaded();
let params = {
currentPage: this.pageNum,
pageSize: 10,
mapLat: this.lat,
mapLng: this.lng
}
this.getNearList(params)
},
getNearInfo() {
let _this = this,
infoParameter = {
requestUrl: encodeURIComponent(window.location.href.split("#")[0])
}
//先从后台获取后面需要配置的微信信息
this.$http.getNearInfo(infoParameter).then(res => {
if(res.result == 'success') {
let data = res.data,
appId = data.appId,
nonceStr = data.nonceStr,
timestamp = data.timestamp,
signature = data.signature;
//根据后台返回的配置信息进行配置
wx.config({
debug: false, //必填,true为调试模式,已alert形式弹出
appId: appId, // 必填,公众号的唯一标识
timestamp: timestamp, // 必填,生成签名的时间戳
nonceStr: nonceStr, // 必填,生成签名的随机串
signature: signature, // 必填,签名
jsApiList: ['showOptionMenu',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'getLocation'] // 必填,需要使用的JS接口列表
});
//开始获取用户的位置信息
wx.ready(() => {
wx.getLocation({
type: 'gcj02', //默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
success: function (res) {
//用户点击同意授权的回调函数
_this.lat = res.latitude; // 纬度
_this.lng = res.longitude; // 经度
//list请求
let params = {
currentPage: _this.pageNum,
pageSize: 10,
mapLat: _this.lat,
mapLng: _this.lng
}
//获取列表信息
_this.getNearList(params)

},
cancel: function(){
//用户点击拒绝授权的回调函数,设置默认位置(天安门)
_this.lat = 39.9219;
_this.lng = 116.44355;
let params = {
currentPage: _this.pageNum,
pageSize: 10,
mapLat: _this.lat,
mapLng: _this.lng
}
//获取列表信息
_this.getNearList(params)
},
error: function(err){
console.log('err', err);
}
});
})
}
}).catch(err => {
console.log(err);
})
},
getNearList(params) {
let pageNum = this.pageNum;
if (!this.loader) return;
this.$http.getNearList(params).then(res => {
if (res.result == 'success') {
let data = res.data,
lister = data.list,
list = this.list,
l = lister.length;
if (pageNum > 1) {
list = [...list, ...lister];
} else {
list = lister;
}
this.list = list;
if (l < 10) {
this.loader = false;
} else {
this.pageNum = (pageNum + 1);
this.loader = true;
}
}
}).catch(err => {

})
}
}
}
</script>

3、css部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<style lang="scss" scoped>
.wraper {
height: calc(100vh - 1rem);
overflow-y: auto;
overflow-x: hidden;
.box {
overflow: hidden;
width: 100%;
height: 3rem;
img {
width: 100%;
height: 3rem;
}
}
.listBox {
width: 93%;
margin: 0 auto;
background: #fff;
position: relative;
border-bottom: solid #e6e6e6 1px;
padding: .26rem 0;
.boxLeft {
width: 20%;
height: 1.26rem;
float: left;
img {
width: 1.26rem;
height: 1.26rem;
border: solid #e8e8e8 1px;
}
}
.boxRight {
width: 80%;
float: left;
font-size: .3rem;
.boxRightTop {
width: 100%;
height: 1.28rem;
.boxRightTopName {
float: left;
line-height: .3rem;
padding: .1rem 0;
font-weight: bold;
font-size: .3rem;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.boxRightTopDistant {
float: right;
height: .3rem;
line-height: .3rem;
font-size: .26rem;
font-weight: bolder;
color: #aeaeae;
}
.boxIntroduce {
width: 100%;
height: .78rem;
.boxVarieties {
display: inline-block;
max-width: 1.2rem;
line-height: .78rem;
font-size: .24rem;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
color: #aeaeae;
padding-right: .2rem;
}
}
}
}
}
}
</style>