WeRunAuth.vue
7.14 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
<template>
<view class="werun-auth-container">
<!-- 未授权状态 -->
<view v-if="isAuthorized === false" class="auth-prompt">
<view class="px-5 py-6 bg-white rounded-xl shadow-md mx-4 mt-4">
<view class="flex flex-col items-center justify-center">
<!-- <view class="mb-4">
<image
src="https://placehold.co/100x100/e2f3ff/0369a1?text=步数&font=roboto"
class="w-20 h-20 rounded-full"
/>
</view> -->
<text class="text-lg font-medium mb-2">获取微信运动数据</text>
<text class="text-gray-500 text-center mb-4 px-4">
授权后可查看您的步数信息
</text>
<view class="bg-blue-50 border border-blue-200 rounded-lg p-4 mb-4 mx-4">
<text class="text-blue-800 text-sm font-medium block mb-2">数据用途说明:</text>
<text class="text-blue-700 text-xs leading-relaxed block mb-1">• 获取您的每日步数数据,用于家庭步数统计和积分计算</text>
<text class="text-blue-700 text-xs leading-relaxed block mb-1">• 与家庭成员共享步数信息,促进健康运动</text>
<text class="text-blue-700 text-xs leading-relaxed block mb-1">• 参与活动打卡和积分兑换功能</text>
<text class="text-blue-700 text-xs leading-relaxed block">• 我们承诺不会将您的数据用于其他用途,严格保护您的隐私</text>
</view>
<view
class="bg-blue-500 text-white px-8 py-3 rounded-full text-sm"
@click="requestAuth"
>
立即授权
</view>
</view>
</view>
</view>
<!-- 已授权状态 - 显示步数相关内容 -->
<view v-else-if="isAuthorized === true">
<slot :isLoading="isLoading" />
</view>
<!-- 初始状态或检查中 - 不显示任何内容,避免闪烁 -->
<view v-else>
<slot :isLoading="true" />
</view>
</view>
</template>
<script setup>
import { ref, onMounted, defineEmits, defineProps } from 'vue'
import Taro from '@tarojs/taro'
// import { syncWxStepAPI } from '@/api/points'
import { fetch } from '@/api/fn'
// 定义props
const props = defineProps({
// 是否自动检查授权状态,默认为false,避免页面加载时立即显示授权提示
autoCheck: {
type: Boolean,
default: false
}
})
// 定义事件
const emit = defineEmits(['auth-change', 'steps-update', 'steps-synced', 'sync-failed'])
// 响应式数据
const isAuthorized = ref(null) // 初始值为null,避免未检查前显示未授权状态
const isLoading = ref(false)
const showManualUpdate = ref(false)
/**
* @description 检查微信运动授权状态
* @param {boolean} shouldGetData - 是否在授权后获取步数数据
*/
const checkAuthStatus = async (shouldGetData = false) => {
try {
const authSetting = await Taro.getSetting()
const hasWeRunAuth = authSetting.authSetting['scope.werun']
isAuthorized.value = hasWeRunAuth === true
// 通知父组件授权状态变化
emit('auth-change', isAuthorized.value)
// 根据参数决定是否获取步数数据
if (isAuthorized.value && shouldGetData) {
await getWeRunData()
}
} catch (error) {
console.error('检查授权状态失败:', error)
// 检查失败时设置为false,表示未授权
isAuthorized.value = false
emit('auth-change', false)
}
}
/**
* @description 请求微信运动授权
*/
const requestAuth = async () => {
try {
isLoading.value = true
// 请求微信运动授权
await Taro.authorize({
scope: 'scope.werun'
})
isAuthorized.value = true
emit('auth-change', true)
// 授权成功后获取步数数据
await getWeRunData()
Taro.showToast({
title: '授权成功',
icon: 'success'
})
} catch (error) {
console.error('授权失败:', error)
// 用户拒绝授权,引导到设置页面
Taro.showModal({
title: '授权提示',
content: '需要获取微信运动数据权限才能使用步数功能,请在设置中开启',
confirmText: '去设置',
success: (res) => {
if (res.confirm) {
Taro.openSetting({
success: (settingRes) => {
if (settingRes.authSetting['scope.werun']) {
checkAuthStatus()
}
}
})
}
}
})
} finally {
isLoading.value = false
}
}
/**
* @description 获取微信运动数据
*/
const getWeRunData = async () => {
try {
isLoading.value = true
// 检查是否为测试环境下的测试用户,如果是则跳过步数同步
if (process.env.NODE_ENV === 'development') {
console.log('开发环境下跳过步数同步')
Taro.showToast({
title: '开发环境无需同步步数',
icon: 'none',
duration: 2000
})
isLoading.value = false
return
}
// 先调用 wx.login 获取 code
const loginRes = await Taro.login()
// 获取微信运动数据
const weRunRes = await Taro.getWeRunData()
console.log('微信运动数据获取成功:', {
encryptedData: weRunRes.encryptedData,
iv: weRunRes.iv,
code: loginRes.code
})
// 同步微信步数 - 直接调用原始API以获取完整响应
try {
const response = await fetch.post('/srv/?a=point&t=sync_wx_step', { encryptedData: weRunRes.encryptedData, iv: weRunRes.iv });
const { code, data, msg } = response.data;
if (code == 1) {
// 提示获取步数成功
console.warn('同步微信步数成功', data);
// 同步成功,隐藏手动更新按钮
showManualUpdate.value = false;
// 触发步数同步完成事件,通知父组件
emit('steps-synced', data);
isLoading.value = false
} else {
// 检查是否是401错误且包含同步微信步数失败的消息
if (msg && msg.includes('同步微信步数失败')) {
console.warn('同步微信步数失败,显示手动更新按钮');
showManualUpdate.value = true;
emit('sync-failed', { showManualUpdate: true });
}
Taro.showToast({
title: msg || '同步步数失败',
icon: 'none',
duration: 2000
});
}
} catch (apiError) {
console.error('同步微信步数API调用失败:', apiError);
Taro.showToast({
title: '网络请求失败',
icon: 'none'
});
}
} catch (error) {
console.error('获取微信运动数据失败:', error)
Taro.showToast({
title: '获取步数失败',
icon: 'none'
})
} finally {
isLoading.value = false
}
}
/**
* @description 刷新步数数据
*/
const refreshSteps = async () => {
if (isAuthorized.value) {
await getWeRunData()
}
}
// 暴露方法给父组件
defineExpose({
refreshSteps,
checkAuthStatus,
showManualUpdate
})
// 组件挂载时根据props决定是否检查授权状态
onMounted(() => {
if (props.autoCheck) {
checkAuthStatus(false)
}
})
</script>
<style scoped>
.werun-auth-container {
width: 100%;
}
.auth-prompt {
text-align: center;
}
</style>