-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import axios from 'axios'; | ||
import { Obj2Query } from '../tools'; | ||
|
||
export const getClientToken = (googleClientVerKey?: string) => { | ||
if (!googleClientVerKey || typeof window.grecaptcha === 'undefined' || !window.grecaptcha?.ready) | ||
return ''; | ||
return new Promise<string>((resolve, reject) => { | ||
window.grecaptcha.ready(async () => { | ||
try { | ||
const token = await window.grecaptcha.execute(googleClientVerKey, { | ||
action: 'submit' | ||
}); | ||
resolve(token); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
// service run | ||
export const authGoogleToken = async (data: { | ||
secret: string; | ||
response: string; | ||
remoteip?: string; | ||
}) => { | ||
const res = await axios.post<{ | ||
score?: number; | ||
success: boolean; | ||
'error-codes': string[]; | ||
}>(`https://www.recaptcha.net/recaptcha/api/siteverify?${Obj2Query(data)}`); | ||
|
||
if (res.data.success) { | ||
return Promise.resolve(''); | ||
} | ||
return Promise.reject(res?.data?.['error-codes']?.[0] || '非法环境'); | ||
}; |