MrShi
10 小时以前 00a7a61df86db969f2ba61c508d02ba4709ce3d4
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
/**
 * 截取日期
 */
export const runcation = (date) => {
    return date.substring(0, 16)
}
 
// 返回距离
export function distance(lat1, lng1, lat2, lng2) {
    var that = this;
    let rad1 = lat1 * Math.PI / 180.0;
    let rad2 = lat2 * Math.PI / 180.0;
    let a = rad1 - rad2;
    let b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
    let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(rad1) *
        Math.cos(
            rad2) * Math.pow(
            Math.sin(b / 2), 2)));
    s = s * 6378.137 * 1000;
    
    s = (Math.round(s * 10000) / 10000).toFixed(2);
    console.log(s)
    // s = s.toString();
    // s = s.substring(0, s.indexOf('.') + 2);
    
    return s
}
 
// 登录
export function userLogin() {
    let that = this;
    let promise = new Promise((resolve, reject) => {
        uni.login({
            provider: 'weixin',
            success: async function(loginRes) {
                let { code } = loginRes;
                let res = await that.$u.api.wxLogin({ code })
                if (res.code === 200) {
                    resolve(res);
                    // that.$store.commit('setToken', res.data.token)
                    // that.$store.commit('setUserInfo', res.data.userResponse)
                } else {
                    reject(err)
                }
            }
        });
    })
    return promise;
}
 
const LOCATION_NOTICE_STORAGE_KEY = 'location_permission_notice_shown'
const IMAGE_NOTICE_STORAGE_KEY = 'image_permission_notice_shown'
 
const DEFAULT_LOCATION_NOTICE = {
    title: '获取位置信息',
    content: '为了给您分配附近的订单,并计算接送货的精准距离与导航路线,本应用需要获取您的位置信息。',
    confirmText: '已知晓'
}
 
const DEFAULT_IMAGE_NOTICE = {
    title: '获取相机权限',
    content: '为了支持您进行司机身份认证,以及方便您在取送件时拍照留存凭证,本应用需要获取您的相机与相册权限。',
    confirmText: '已知晓'
}
 
export function ensureLocationPermissionNotice(modalOptions = {}) {
    const hasShown = uni.getStorageSync(LOCATION_NOTICE_STORAGE_KEY)
    if (hasShown) {
        return Promise.resolve(true)
    }
 
    const options = Object.assign({}, DEFAULT_LOCATION_NOTICE, modalOptions)
 
    return new Promise((resolve) => {
        uni.showModal({
            title: options.title,
            content: options.content,
            confirmText: options.confirmText,
            showCancel: false,
            success: (res) => {
                if (res.confirm) {
                    uni.setStorageSync(LOCATION_NOTICE_STORAGE_KEY, true)
                    resolve(true)
                    return
                }
                resolve(false)
            },
            fail: () => {
                resolve(false)
            }
        })
    })
}
 
export function getLocationWithNotice(locationOptions = {}, modalOptions = {}) {
    return ensureLocationPermissionNotice(modalOptions).then((confirmed) => {
        if (!confirmed) {
            return Promise.reject({ errMsg: 'location permission notice canceled' })
        }
 
        return new Promise((resolve, reject) => {
            uni.getLocation(Object.assign({}, locationOptions, {
                success: (res) => {
                    uni.$emit('locationPermissionGranted', res)
                    if (typeof locationOptions.success === 'function') {
                        locationOptions.success(res)
                    }
                    resolve(res)
                },
                fail: (err) => {
                    if (typeof locationOptions.fail === 'function') {
                        locationOptions.fail(err)
                    }
                    reject(err)
                }
            }))
        })
    })
}
 
export function ensureImagePermissionNotice(modalOptions = {}) {
    const hasShown = uni.getStorageSync(IMAGE_NOTICE_STORAGE_KEY)
    if (hasShown) {
        return Promise.resolve(true)
    }
 
    const options = Object.assign({}, DEFAULT_IMAGE_NOTICE, modalOptions)
 
    return new Promise((resolve) => {
        uni.showModal({
            title: options.title,
            content: options.content,
            confirmText: options.confirmText,
            showCancel: false,
            success: (res) => {
                if (res.confirm) {
                    uni.setStorageSync(IMAGE_NOTICE_STORAGE_KEY, true)
                    resolve(true)
                    return
                }
                resolve(false)
            },
            fail: () => {
                resolve(false)
            }
        })
    })
}
 
export function chooseImageWithNotice(imageOptions = {}, modalOptions = {}) {
    return ensureImagePermissionNotice(modalOptions).then((confirmed) => {
        if (!confirmed) {
            return Promise.reject({ errMsg: 'image permission notice canceled' })
        }
 
        return new Promise((resolve, reject) => {
            uni.chooseImage(Object.assign({}, imageOptions, {
                success: (res) => {
                    if (typeof imageOptions.success === 'function') {
                        imageOptions.success(res)
                    }
                    resolve(res)
                },
                fail: (err) => {
                    const errMsg = (err && err.errMsg) || ''
                    if (errMsg && !/cancel/i.test(errMsg)) {
                        uni.showToast({
                            title: '需要授权相机权限',
                            icon: 'none'
                        })
                    }
                    if (typeof imageOptions.fail === 'function') {
                        imageOptions.fail(err)
                    }
                    reject(err)
                }
            }))
        })
    })
}
 
export function checkLocationPermission() {
    return new Promise((resolve) => {
        if (typeof uni.getAppAuthorizeSetting === 'function') {
            try {
                const setting = uni.getAppAuthorizeSetting()
                const locationAuthorized = setting.locationAuthorized
                resolve(locationAuthorized === 'authorized' || locationAuthorized === 'authorizedAlways')
                return
            } catch (e) {
                console.log('getAppAuthorizeSetting fail', e)
            }
        }
 
        uni.getSetting({
            success: (res) => {
                const authSetting = res.authSetting || {}
                resolve(!!authSetting['scope.userLocation'])
            },
            fail: () => {
                resolve(false)
            }
        })
    })
}