wx-starlock/pages/p2p/authorizeWechat.vue

311 lines
8.5 KiB
Vue
Raw Normal View History

2025-04-06 18:18:01 +08:00
<template>
<view class="flex flex-col justify-center items-center pt-[30vh]">
2025-04-07 17:01:22 +08:00
<view v-if="requestFinish">
<view
v-if="!isAuthorized"
class="bg-[#63b8af] text-white rounded-full text-center leading-88rpx h-88rpx w-686 mx-4"
2025-04-07 17:01:22 +08:00
@click="handleAuthorize"
>
授权
</view>
<view v-else>
<view v-if="!reject" class="text-center text-2xl font-bold">您已授权</view>
2025-04-07 17:01:22 +08:00
<view v-else>
2025-06-06 09:05:03 +08:00
<view v-if="weChatTicketsFlag">
<view class="text-center text-lg font-bold mt-6"> 授权失败 </view>
<view class="text-center text-lg font-bold mt-4"> 请保证设备一直处于在线状态 </view>
<view
class="bg-[#63b8af] text-white rounded-full text-center leading-88rpx h-88rpx w-686 mx-4 mt-10"
@click="retry"
>
重试
</view>
</view>
2025-06-06 09:05:03 +08:00
<view v-else>
<view class="text-center text-lg font-bold mt-6"> 您已拒绝授权请去设置中 </view>
<view class="text-center text-lg font-bold mt-4">
打开<text class="text-red">语音视频通话提醒</text>开关
</view>
<view
class="bg-[#63b8af] text-white rounded-full text-center leading-88rpx h-88rpx w-686 mx-4 mt-10"
@click="openSetting"
>
打开设置
</view>
2025-04-07 17:01:22 +08:00
</view>
</view>
</view>
</view>
<view v-if="requestFinish" class="mt-10 text-center text-base font-bold text-[#999]">
授权后在设备上按下门铃按钮
</view>
<view v-if="requestFinish" class="mt-4 text-center text-base font-bold text-[#999]">
微信会收到视频通话请求
</view>
2025-06-16 16:23:42 +08:00
<view
class="fixed bottom-120rpx w-400rpx h-88rpx rounded-md leading-88rpx left-1/2 -translate-x-1/2 bg-[#63b8af] text-center text-base font-bold text-[#ffffff]"
@click="getTicket"
>
获取ticket
</view>
2025-04-06 18:18:01 +08:00
</view>
</template>
<script setup>
2025-04-07 17:01:22 +08:00
import { onShow } from '@dcloudio/uni-app'
import { ref } from 'vue'
import { passthrough } from '@/api/sdk'
2025-04-07 17:01:22 +08:00
import { useBluetoothStore } from '@/stores/bluetooth'
import env from '@/config/env'
import { getStorage } from '@/utils/storage'
2025-04-06 18:18:01 +08:00
2025-04-07 17:01:22 +08:00
const $bluetooth = useBluetoothStore()
const requestFinish = ref(false)
const isAuthorized = ref(false)
const list = ref([])
const reject = ref(false)
const pending = ref(false)
2025-06-06 09:05:03 +08:00
const weChatTicketsFlag = ref(false)
onShow(async () => {
2025-04-07 17:01:22 +08:00
uni.showLoading({
title: '加载中...'
})
2025-06-06 09:05:03 +08:00
const resultList = await Promise.all([getDeviceVoIPList(), getAuthResult()])
list.value = resultList[0]
const authResult = resultList[1]
const data = list.value.find(item => item.sn === authResult.data.wxDeviceSn)
if (authResult?.code === 0 && data) {
if (authResult?.data?.wxOpenid === getStorage('openid')) {
if (data.status === 1) {
reject.value = false
weChatTicketsFlag.value = false
} else if (data.status === 0) {
reject.value = true
weChatTicketsFlag.value = false
}
} else {
2025-06-06 09:05:03 +08:00
reject.value = true
weChatTicketsFlag.value = true
2025-04-07 17:01:22 +08:00
}
isAuthorized.value = true
requestFinish.value = true
2025-06-06 09:05:03 +08:00
uni.hideLoading()
} else {
requestFinish.value = true
uni.hideLoading()
}
2025-04-06 18:18:01 +08:00
})
2025-04-07 17:01:22 +08:00
2025-06-16 16:23:42 +08:00
const getTicket = async () => {
uni.showLoading({
title: '加载中...'
})
const result = await getInfo()
uni.hideLoading()
if (result.code === 0) {
const ticket = result.data.WXIoTDeviceInfo.SNTicket
uni.showModal({
title: '票据',
content: ticket,
showCancel: false,
confirmText: '复制',
success: ({ confirm }) => {
if (confirm) {
uni.setClipboardData({
data: ticket,
success: () => {
uni.showToast({
title: '复制成功',
icon: 'none'
})
}
})
}
}
})
} else {
uni.showToast({
title: result.message,
icon: 'none'
})
}
}
2025-06-06 09:05:03 +08:00
const getDeviceVoIPList = async () => {
return new Promise(resolve => {
wx.getDeviceVoIPList({
success: res => {
resolve(res.list)
}
})
})
}
const retry = async () => {
if (pending.value) return
uni.showLoading({
title: '授权中...'
})
pending.value = true
const ticketsResult = await weChatTickets()
pending.value = false
if (ticketsResult?.code === 0) {
passthrough({
request_method: 'POST',
request_uri: '/api/v1/tencentYun/reportWechatAuthSuccess',
post_args: {
lockId: $bluetooth.currentLockInfo.lockId,
wxOpenid: getStorage('openid'),
wxDeviceSn: ticketsResult.data.wxDeviceSn
}
})
uni.hideLoading()
isAuthorized.value = true
reject.value = false
weChatTicketsFlag.value = false
2025-06-06 09:05:03 +08:00
} else {
uni.hideLoading()
uni.showToast({
title: '授权失败',
icon: 'none'
})
}
}
2025-04-07 17:01:22 +08:00
const openSetting = () => {
uni.openSetting({
success: res => {
console.log(res)
}
})
}
2025-06-06 09:05:03 +08:00
const getAuthResult = async () => {
const result = await passthrough({
request_method: 'GET',
request_uri: '/api/v1/tencentYun/checkWechatAuth',
post_args: {
lockId: $bluetooth.currentLockInfo.lockId,
wxOpenid: getStorage('openid')
}
})
return result
}
2025-04-07 17:01:22 +08:00
const getInfo = async () => {
const result = await passthrough({
request_method: 'GET',
request_uri: '/api/v1/tencentYun/getWechatDeviceTicket',
post_args: {
lockId: $bluetooth.currentLockInfo.lockId
}
})
return result
}
2025-06-10 14:23:50 +08:00
const weChatTickets = async () => {
let ticket = null
let snResult = null
if (!ticket) {
2025-06-06 09:05:03 +08:00
const result = await getInfo()
ticket = result.data.WXIoTDeviceInfo.SNTicket
snResult = result.data.WXIoTDeviceInfo.SN
}
const result = await $bluetooth.weChatTickets({
keyId: $bluetooth.currentLockInfo.keyId.toString(),
uid: $bluetooth.currentLockInfo.uid.toString(),
openId: getStorage('openid'),
snTicket: ticket
})
return {
code: result.code,
data: {
wxDeviceSn: snResult
}
}
}
2025-04-07 17:01:22 +08:00
const handleAuthorize = async () => {
if (pending.value) return
pending.value = true
uni.showLoading({
title: '授权中...'
})
const result = await getInfo()
if (result.code === 0) {
wx.requestDeviceVoIP({
sn: result.data.WXIoTDeviceInfo.SN,
snTicket: result.data.WXIoTDeviceInfo.SNTicket,
modelId: result.data.WXIoTDeviceInfo.ModelId,
deviceName: await env[await getApp().globalData.getEnvConfig()].appName,
async success() {
2025-06-06 09:05:03 +08:00
const ticketsResult = await weChatTickets(
result.data.WXIoTDeviceInfo.SNTicket,
result.data.WXIoTDeviceInfo.SN
)
if (ticketsResult?.code === 0) {
isAuthorized.value = true
uni.hideLoading()
pending.value = false
passthrough({
request_method: 'POST',
request_uri: '/api/v1/tencentYun/reportWechatAuthSuccess',
post_args: {
lockId: $bluetooth.currentLockInfo.lockId,
wxOpenid: getStorage('openid'),
wxDeviceSn: result.data.WXIoTDeviceInfo.SN
}
})
reject.value = false
weChatTicketsFlag.value = false
2025-06-06 09:05:03 +08:00
uni.showToast({
title: '授权成功',
icon: 'none'
})
} else {
isAuthorized.value = true
uni.hideLoading()
pending.value = false
reject.value = true
weChatTicketsFlag.value = true
uni.showToast({
title: '授权失败',
icon: 'none'
})
}
2025-04-07 17:01:22 +08:00
},
fail() {
isAuthorized.value = true
reject.value = true
uni.hideLoading()
pending.value = false
uni.showToast({
title: '授权失败',
icon: 'none'
})
}
})
} else {
uni.hideLoading()
pending.value = false
uni.showToast({
title: result.message,
icon: 'none'
})
}
}
</script>