-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
143 lines (143 loc) · 5.29 KB
/
app.js
File metadata and controls
143 lines (143 loc) · 5.29 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
const log = console.log.bind(console, '>>>')
const amapFile = require('ku/js/amap-wx.js')
const config = require('ku/js/config.js')
App({
// onLaunch 全局登陆触发一次
data: {},
onLaunch() {
// 开启罗盘
// wx.startCompass()
},
onHide() {
wx.setStorageSync('onShow', true)
},
fail() {
log('网络状态异常!')
},
getUserInfo(callback) {
let that = this
wx.login({
success: function (res) {
let code = res.code
wx.getUserInfo({
withCredentials: false,
success: function (res) {
// 获取 ucloud session
let userInfo = res.userInfo
userInfo.code = code
userInfo.rawData = res.rawData
wx.request({
url: config.url + '/login',
data: {
// 出发点
wxcode: userInfo.code,
rawdata: userInfo.rawData
},
method: "POST",
header: {
"Content-Type": "application/x-www-form-urlencoded",
},
success: function(res) {
let cards = res.data.info.cards
userInfo.session_key = res.data.info.session_key
wx.setStorageSync('userInfo', userInfo)
if (typeof callback === 'function') { callback(cards) }
},
fail: (err) => {that.fail()}
})
},
fail: (err) => {that.fail()}
})
},
fail: (err) => {that.fail()}
})
},
getLocation(callback) {
let that = this
wx.getLocation({
type: "gcj02",
success: function(res) {
if (res.accuracy > 40) {
// showToast
log('当前GPS信号弱,请行驶到开阔地带')
}
let location = res
location.now = [res.latitude, res.longitude].join(',')
location.origin = [res.longitude, res.latitude].join(',')
wx.request({
url: 'https://restapi.amap.com/v3/geocode/regeo?parameters',
data: {
key: config.web,
location: location.origin,
},
method: "GET",
header: {
"Content-Type": "application/json",
},
success: function(res) {
location.data = res.data
let address = res.data.regeocode.addressComponent.streetNumber
location.street_number = address.street + address.number
wx.setStorageSync('userLocation', location)
if (typeof callback === 'function') { callback() }
},
fial: function(err) {
wx.setStorageSync('userLocation', location)
if (typeof callback === 'function') { callback() }
}
})
},
fail: (err) => {that.fail()}
})
},
login(callback) {
let that = this
let User = {
info: wx.getStorageSync('userInfo'),
location: wx.getStorageSync('userLocation'),
cards: wx.getStorageSync('userCards')
}
if (User.info && User.location) {
if (typeof callback === 'function') { callback(User) }
} else {
that.getLocation(function() {
User.location = wx.getStorageSync('userLocation')
that.getUserInfo(function(cards) {
// 获取 cards
let now = [User.location.longitude, User.location.latitude].join(',')
for (let i of cards) {
i.origin = now
i.myorigin = now
}
wx.setStorageSync('userCards', cards)
// 读取
User.info = wx.getStorageSync('userInfo')
User.cards = wx.getStorageSync('userCards')
if (typeof callback === 'function') { callback(User) }
})
})
}
},
direction(du) {
// 360 / 8 = 45
if (du >= 338 || du < 23) {
return '北'
} else if (du >= 23 && du < 68) {
return '东北'
} else if (du >= 68 && du < 113) {
return '东'
} else if (du >= 113 && du < 158) {
return '东南'
} else if (du >= 158 && du < 203) {
return '南'
} else if (du >= 203 && du < 248) {
return '西南'
} else if (du >= 248 && du < 293) {
return '西'
} else if (du >= 293 && du < 338) {
return '西北'
} else {
return null
}
}
})