-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
152 lines (142 loc) · 4.05 KB
/
app.js
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
//app.js
import {
getLoginInfo, getUserAddrs
} from './utils/api'
import {
getCurrentAddress, getUserInfo, alert, connectWebsocket,
coordFormat, fetch, confirm
} from './utils/util'
import {
gcj02tobd09
} from './utils/coordtransform'
import distance from './utils/distance'
App({
onLaunch: function () {
},
onShow(){
},
getLoginInfo: function (cb) {
var that = this
if (this.globalData.loginInfo.is_login)
{
cb && cb(this.globalData.loginInfo)
}
else
{
wx.getSetting({
success: (res) => {
if (res.authSetting['scope.userInfo']) {
getLoginInfo({
success(data) {
getApp().setLoginInfo(data)
cb && cb(data)
},
error(res) {
console.log("获取用户信息失败,请稍后...")
cb && cb(res)
},
fail(res) {
if (res.errMsg == 'getUserInfo:fail auth deny' && wx.openSetting) {
confirm({
content: '若不授用户信息权限, 则无法正常显示用户头像和昵称以及获得相关服务, 请重新授权用户信息权限',
cancelText: '不授权',
confirmText: '授权',
ok() {
wx.openSetting({
success(res) {
if (res.authSetting['scope.userInfo']) {
getLoginInfo({
success(data) {
getApp().setLoginInfo(data)
cb && cb(data)
}
})
}
else {
alert('获取用户信息失败')
}
}
})
}
})
} else {
console.log(res)
alert('获取用户信息失败')
}
cb && cb()
}
})
}
else {
alert("若不授用户信息权限, 则无法正常显示用户头像和昵称以及获得相关服务, 请重新授权用户信息权限");
}
}
})
}
},
setLoginInfo(loginInfo) {
wx.setStorageSync("is_login", true)
wx.setStorageSync("userInfo", loginInfo.userInfo)
this.globalData.loginInfo = loginInfo
},
// 获取当前地址
getCurrentAddress(cb) {
var that = this
if (this.globalData.currentAddress) {
return cb && cb(this.globalData.currentAddress)
}
getCurrentAddress(address => {
address = that.setCurrentAddress(address)
cb(address)
})
},
setCurrentAddress(address) {
if (address.addr_id) {
address.title = `${address.addr} ${address.detail}`
address.city = address.city_name
address.district = address.district_name
address.location = {
longitude: address.longitude,
latitude: address.latitude
}
} else {
address.location = coordFormat(address.location)
}
this.globalData.currentAddress = address
return address
},
findNearbyUserAddr(cb, radius = 100) {
radius /= 100
wx.getLocation({
type: 'gcj02',
success: function (res) {
var [lng1, lat1] = gcj02tobd09(res.longitude, res.latitude)
getUserAddrs({
success(addressList) {
for (let i = 0, len = addressList.length; i < len; i++) {
var address = addressList[i]
var {
longitude: lng2,
latitude: lat2
} = address
if (distance(lat1, lng1, lat2, lng2) <= radius) {
return cb(address)
}
}
return cb()
}
})
},
fail(res) {
console.log(res.errMsg)
alert('获取用户地址失败')
}
})
},
globalData: {
loginInfo:{
is_login: 0,
userInfo:null
},
}
})