wx-starlock/pages/p2p/authorizeWechat.vue

170 lines
4.8 KiB
Vue
Raw Normal View History

2025-04-06 18:18:01 +08:00
<template>
<view>
2025-04-07 17:01:22 +08:00
<view class="mt-10 text-center text-base font-bold text-[#999]">授权后设备可呼叫该微信</view>
<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 mt-10"
@click="handleAuthorize"
>
授权
</view>
<view v-else>
<view v-if="!reject" class="text-center text-lg font-bold mt-4">您已授权</view>
<view v-else>
<view class="text-center text-lg font-bold mt-4"> 您已拒绝授权请去设置中 </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="openSetting"
>
打开设置
</view>
</view>
</view>
</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)
onShow(() => {
uni.showLoading({
title: '加载中...'
})
wx.getDeviceVoIPList({
async success(res) {
list.value = res.list
if (res.list.length > 0) {
const result = await getInfo()
if (result.code === 0) {
const data = list.value.find(item => item.sn === result.data.WXIoTDeviceInfo.SN)
2025-04-07 17:01:22 +08:00
if (data) {
if (data.status === 1) {
if (reject.value) {
passthrough({
request_method: 'POST',
request_uri: '/api/v1/tencentYun/reportWechatAuthSuccess',
post_args: {
lockId: $bluetooth.currentLockInfo.lockId,
wxOpenid: getStorage('openid'),
wxDeviceSn: result.data.WXIoTDeviceInfo.SNTicket
}
})
}
2025-04-07 17:01:22 +08:00
reject.value = false
} else if (data.status === 0) {
reject.value = true
}
isAuthorized.value = true
requestFinish.value = true
uni.hideLoading()
} else {
requestFinish.value = true
uni.hideLoading()
}
} else {
uni.showToast({
title: result.message,
icon: 'none'
})
requestFinish.value = true
uni.hideLoading()
}
} else {
requestFinish.value = true
uni.hideLoading()
}
}
})
2025-04-06 18:18:01 +08:00
})
2025-04-07 17:01:22 +08:00
const openSetting = () => {
uni.openSetting({
success: res => {
console.log(res)
}
})
}
const getInfo = async () => {
const result = await passthrough({
request_method: 'GET',
request_uri: '/api/v1/tencentYun/getWechatDeviceTicket',
post_args: {
lockId: $bluetooth.currentLockInfo.lockId
}
})
return result
}
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-04-07 17:01:22 +08:00
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.SNTicket
}
})
2025-04-07 17:01:22 +08:00
uni.showToast({
title: '授权成功',
icon: 'none'
})
},
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>