prize-detail.js
6.62 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import {
getBindtapData,
pxToRpx,
getLocalDistance
} from '../../utils/util';
import Date from '../../utils/date';
import QR from '../../utils/qrcode'
let app = getApp();
Page({
data: {
userInfo: {},
used: false, //优惠券已使用,另外的样式
wishInfo: {},
couponInfo: {},
nearbyStoreVisible: false,
qrImagePath: "",
location: {},
},
onShareAppMessage() {},
onLoad(options) {
let giftData = app.globalData.giftData;
let wishInfo = giftData.order;
let couponInfo = wishInfo.coupon;
// 设置数据
this.setData({
wishInfo: wishInfo,
couponInfo: couponInfo
})
this.initData();
},
initData() {
let _this = this;
app.queryIndex().then((result) => {
this.setData({
userInfo: app.globalData.userInfo
})
let wishInfo = this.data.wishInfo;
let couponInfo = wishInfo.coupon;
// 设置二维码
let qrSize = this.setCanvasSize(400);
console.log("couponInfo:", couponInfo);
let codeContent = couponInfo.couponCode || '';
this.createQrCode(codeContent, 'qrcanvas', qrSize.w, qrSize.h);
// 判断是否使用
// 自提券 使用状态(0=未使用 1=已过期,2=已使用)
let used = couponInfo.state != 0;
// 设置起止时间
couponInfo.startDateStr = new Date(couponInfo.startDate).toString("yyyy.MM.dd");
couponInfo.endDateStr = new Date(couponInfo.endDate).toString("yyyy.MM.dd");
// 设置数据
this.setData({
couponInfo: couponInfo,
used: used
})
// 设置坐标
wx.getLocation({
type: 'gcj02',
success(res) {
let targetLocation = {
latitude: wishInfo.latitude,
longitude: wishInfo.longitude,
}
let distance = getLocalDistance(res, targetLocation).dis;
wishInfo.distance = distance;
console.log("distance:", distance);
_this.setData({
wishInfo: wishInfo
})
}
})
})
},
/**
* 请求门店地址
*/
queryShop() {
this.selectComponent("#nearbyStoreComp").queryShop().then((result) => {
this.setData({
nearbyStoreVisible: true
})
})
},
// 查看可用门店
onNearbyStoreHandler(evt) {
this.wxLocation();
},
// 使用微信位置
wxLocation() {
let _this = this;
wx.getLocation({
type: 'gcj02',
success(res) {
_this.setData({
location: res
})
_this.queryShop();
},
fail(err) {
wx.getSetting({
success: (res) => {
if (!res.authSetting['scope.userLocation']) {
// 未授权
wx.showModal({
title: '提示',
content: '小程序请求访问地理位置',
confirmText: '前往授权',
success(res) {
if (res.confirm) {
wx.openSetting({
complete() {
//设置完后重拉一遍位置,拉不到就算了
wx.getLocation({
type: 'wgs84', // 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
success: function (res) {
_this.setData({
location: res
})
_this.queryShop();
},
fail: function () {
_this.queryShop();
}
})
}
})
} else if (res.cancel) {
// 模态窗取消
_this.queryShop();
}
}
})
}
}
})
}
})
},
/**
* 显示位置
* @param {*} e
*/
onShowLocHandler(evt) {
let item = this.data.wishInfo;
console.log("item:", item);
wx.openLocation({
latitude: item.latitude, // 纬度,范围为-90~90,负数表示南纬
longitude: item.longitude, // 经度,范围为-180~180,负数表示西经
scale: 18, // 缩放比例
name: item.storeName, // 位置名
address: item.storeAddress, // 地址的详细说明
success: function (res) {
// success
}
})
},
createQrCode(content, canvasId, cavW, cavH) {
//调用插件中的draw方法,绘制二维码图片
QR.api.draw(content, canvasId, cavW, cavH);
this.canvasToTempImage(canvasId);
},
//获取临时缓存图片路径,存入data中
canvasToTempImage(canvasId) {
let that = this;
setTimeout(() => {
wx.canvasToTempFilePath({
canvasId, // 这里canvasId即之前创建的canvas-id
success: function (res) {
let tempFilePath = res.tempFilePath;
that.setData({ // 如果采用mpvue,即 this.imagePath = tempFilePath
qrImagePath: tempFilePath,
});
},
fail: function (res) {
console.log(res);
}
});
}, 50);
},
//适配不同屏幕大小的canvas
setCanvasSize(sz) {
var size = {};
try {
var res = wx.getSystemInfoSync();
var scale = 750 / sz; //不同屏幕下canvas的适配比例;设计稿是750宽
var width = res.windowWidth / scale;
var height = width; //canvas画布为正方形
size.w = width;
size.h = height;
} catch (e) {
// Do something when catch error
console.log("获取设备信息失败" + e);
}
return size;
},
hideMask() {
this.setData({
nearbyStoreVisible: false
})
},
// 子组件事件
evtcomp(evt) {
let {
name,
data
} = evt.detail;
switch (name) {
case "_evt_hide":
this.hideMask();
break;
// 提交完毕
case "_evt_submit_store_complete":
// 刷新状态
this.hideMask();
app.post({
url: app.api.wishbillGiftQuery,
data: {
instanceCode: this.data.wishInfo.instanceCode
}
}).then((result) => {
// 重新拉取数据
let giftData = result;
let wishInfo = giftData.order;
let couponInfo = wishInfo.coupon;
this.setData({
wishInfo: wishInfo,
couponInfo: couponInfo
})
this.initData();
wx.showModal({
content: '预约成功',
showCancel: false,
success(res) {}
});
})
break;
default:
break;
}
},
})