/
App.tsx
170 lines (146 loc) · 4.25 KB
/
App.tsx
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
import * as React from "react"
import {
Button,
EventSubscription,
NativeModules,
NativeEventEmitter,
StyleSheet,
Text,
View,
} from "react-native"
import Toast from "react-native-root-toast"
import axios from "axios"
import GeetestModule from "react-native-geetest-module"
type GeetestValidationData = {
geetestChallenge: string
geetestSeccode: string
geetestValidate: string
}
const toastShow = (message: string): void => {
Toast.show(message, {
duration: Toast.durations.LONG,
shadow: false,
animation: true,
hideOnPress: true,
delay: 0,
position: 50,
opacity: 1,
backgroundColor: "#FF2301",
})
}
const registerURL = "" // Your register endpoint
const validateURL = "" // Your validate endpoint
const getRegisterCaptcha = async () => {
const { data } = await axios.get(registerURL)
const params = {
success: data.success,
challenge: data.challenge,
gt: data.gt,
new_captcha: data.new_captcha,
}
GeetestModule.handleRegisteredGeeTestCaptcha(JSON.stringify(params))
}
export default function App() {
const [geetestValidationData, setGeetestValidationData] =
React.useState<GeetestValidationData | null>(null)
const [isVerified, setIsVerified] = React.useState<boolean | null>(null)
const onGeetestDialogResultListener = React.useRef<EventSubscription>()
const onGeetestFailedListener = React.useRef<EventSubscription>()
const postValidateCaptcha = async () => {
if (geetestValidationData === null) return
const postData = {
geetest_challenge: geetestValidationData.geetestChallenge,
geetest_validate: geetestValidationData.geetestValidate,
geetest_seccode: geetestValidationData.geetestSeccode,
}
const { data } = await axios.post(validateURL, postData)
if (data.result === "success") {
setIsVerified(true)
} else if (data.result === "fail") {
setIsVerified(false)
}
}
React.useEffect(() => {
GeetestModule.setUp()
const eventEmitter = new NativeEventEmitter(NativeModules.GeetestModule)
onGeetestDialogResultListener.current = eventEmitter.addListener(
"GT3-->onDialogResult-->",
(event) => {
const parsedDialogResult = JSON.parse(event.result)
setGeetestValidationData({
geetestChallenge: parsedDialogResult.geetest_challenge,
geetestSeccode: parsedDialogResult.geetest_seccode,
geetestValidate: parsedDialogResult.geetest_validate,
})
},
)
onGeetestFailedListener.current = eventEmitter.addListener(
"GT3-->onFailed-->",
(event) => {
console.log("GT3-->onFailed->", event.error)
toastShow(event.error)
},
)
return () => {
GeetestModule.tearDown()
onGeetestDialogResultListener.current?.remove()
onGeetestFailedListener.current?.remove()
}
}, [])
const reset = () => {
setGeetestValidationData(null)
setIsVerified(null)
}
const getValidationContent = () => {
if (geetestValidationData === null) {
return <Button title={"Verify"} onPress={getRegisterCaptcha} />
} else if (isVerified === null) {
return <Button title="Validate" onPress={postValidateCaptcha} />
}
const validationMessage = isVerified
? "Validation succeeded!"
: "Validation failed"
return (
<>
<Text>{validationMessage}</Text>
<Button title={"Reset"} onPress={reset} />
</>
)
}
return (
<>
<View style={styles.container}>{getValidationContent()}</View>
<View style={styles.container}>
<View style={styles.container}>
<Text>
GeetestChallenge:{" "}
{geetestValidationData?.geetestChallenge ?? "none"}
</Text>
<Text>
GeetestSeccode: {geetestValidationData?.geetestSeccode ?? "none"}
</Text>
<Text>
GeetestValidate: {geetestValidationData?.geetestValidate ?? "none"}
</Text>
</View>
</View>
</>
)
}
const styles = StyleSheet.create({
button: {
backgroundColor: "#3050C4",
marginHorizontal: "50rem",
marginTop: "30rem",
},
container: {
flex: 1,
alignItems: "center",
justifyContent: "center",
},
box: {
width: 60,
height: 60,
marginVertical: 20,
},
})