From acdb0999dacd18f993cb20bb4f69debb6037fdbc Mon Sep 17 00:00:00 2001 From: tx <2622874537@qq.com> Date: Fri, 27 Dec 2024 17:47:44 +0800 Subject: [PATCH] 11 --- src/utils/api.ts | 2 + src/views/bind/bind_index.tsx | 153 ++++++++++++++++++++++++++++------ 2 files changed, 130 insertions(+), 25 deletions(-) diff --git a/src/utils/api.ts b/src/utils/api.ts index 4cf17f0..9e3aa82 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -111,5 +111,7 @@ export const apiService = { updateKey: (data: any) => api.put('/appVerify/editKey', data), deleteKey: (key: any) => api.delete('/appVerify/del/' + key), getExpiredKeys: () => api.get('/appVerify/getExpiredKeyListByOwnerId'), + getKeyInfo: (keyId: string) => api.get('/appVerify/key/'+keyId), + bindKey: (keyId: string) => api.post('/appVerify/claimKey?keyId='+keyId), // updateKeyExpiration: (keyId: string, expirationTime: string) => api.put('/appVerify/updateKeyExpiration', { keyId, expirationTime }), }; \ No newline at end of file diff --git a/src/views/bind/bind_index.tsx b/src/views/bind/bind_index.tsx index 9ea0aa3..53e16e1 100644 --- a/src/views/bind/bind_index.tsx +++ b/src/views/bind/bind_index.tsx @@ -1,11 +1,11 @@ import React, { useState } from 'react'; -import { - View, - StyleSheet, - TextInput, - TouchableWithoutFeedback, - Keyboard, - Text, +import { + View, + StyleSheet, + TextInput, + TouchableWithoutFeedback, + Keyboard, + Text, TouchableOpacity, Image, Modal @@ -34,7 +34,7 @@ type RootStackParamList = { type NavigationProp = StackNavigationProp; const BackIcon = (props: any) => ( - + ); const SnBind = () => { @@ -48,7 +48,7 @@ const SnBind = () => { }; const BackAction = () => ( - + ); const togglePopover = () => { @@ -57,15 +57,116 @@ const SnBind = () => { // 处理 keyId 的方法 const handleKeyId = async (keyId: string) => { - const res = await apiService.getUserInfo(); - - console.log('处理 keyId:', keyId); - // TODO: 在这里添加处理 keyId 的逻辑 + try { + // 先获取用户信息 + const userInfoRes = await new Promise((resolve, reject) => { + apiService.getUserInfo() + .then(res => { + if (!res || !res.user) { + Toast.show({ + type: 'error', + text1: '获取用户信息失败', + }); + reject(new Error('获取用户信息失败')); + return; + } + resolve(res); + }) + .catch(error => { + console.error('获取用户信息出错:', error); + Toast.show({ + type: 'error', + text1: '获取用户信息失败', + }); + reject(error); + }); + }); + + // 用户信息获取成功后,再获取密钥信息 + const keyInfoRes = await new Promise((resolve, reject) => { + apiService.getKeyInfo(keyId) + .then(res => { + if (!res || !res.data) { + Toast.show({ + type: 'error', + text1: '二维码已过期', + }); + reject(new Error('二维码已过期')); + return; + } + resolve(res); + }) + .catch(error => { + console.error('获取密钥信息出错:', error); + Toast.show({ + type: 'error', + text1: '获取密钥信息失败', + }); + reject(error); + }); + }); + + // 验证手机号匹配 + const userPhone = userInfoRes.user.phonenumber; + const sharePhone = keyInfoRes.data.sharePhone; + + if (!userPhone || !sharePhone) { + Toast.show({ + type: 'error', + text1: '手机号信息不完整', + }); + return; + } + + if (userPhone !== sharePhone) { + Toast.show({ + type: 'error', + text1: '您不是分享码的拥有者', + }); + return; + } + + // 执行绑定操作 + const bindRes = await new Promise((resolve, reject) => { + apiService.bindKey(keyId) + .then(res => { + if (res.code === 200) { + Toast.show({ + type: 'success', + text1: '绑定成功', + }); + navigateBack(); + resolve(res); + } else { + Toast.show({ + type: 'error', + text1: res.msg || '绑定失败', + }); + reject(new Error(res.msg || '绑定失败')); + } + }) + .catch(error => { + console.error('绑定密钥出错:', error); + Toast.show({ + type: 'error', + text1: '绑定失败', + }); + reject(error); + }); + }); + + } catch (error) { + console.error('处理密钥绑定时出错:', error); + Toast.show({ + type: 'error', + text1: '操作失败,请稍后重试', + }); + } }; // 处理扫描结果的方法 const handleScanResult = (data: string) => { - console.log('扫描结果:', data); + // console.log('扫描结果:', data); try { // 检查是否包含 sn const snMatch = data.match(/[?&]sn=([^&]+)/); @@ -83,7 +184,9 @@ const SnBind = () => { // 检查是否包含 keyId const keyIdMatch = data.match(/[?&]keyId=([^&]+)/); + if (keyIdMatch && keyIdMatch[1]) { + setIsScanning(false); handleKeyId(keyIdMatch[1]); return; @@ -114,7 +217,7 @@ const SnBind = () => { const handleImagePicker = () => { setPopoverVisible(false); - launchImageLibrary({ + launchImageLibrary({ mediaType: 'photo', quality: 1, }, async (response) => { @@ -125,7 +228,7 @@ const SnBind = () => { } else if (response.assets && response.assets.length > 0) { try { const { uri } = response.assets[0]; - + // 使用 QRReader 识别图片中的二维码 const result = await QRReader.detect({ uri: uri @@ -164,14 +267,14 @@ const SnBind = () => { accessoryLeft={BackAction} style={styles.topNavigation} /> - + {/* 顶部图片 */} - - + {/* 输入框和扫码按钮 */} { ( - @@ -228,8 +331,8 @@ const SnBind = () => { title='扫描二维码' alignment='center' accessoryLeft={() => ( - setIsScanning(false)} /> )} @@ -247,7 +350,7 @@ const SnBind = () => { } bottomContent={ - setIsScanning(false)} >