fix: 画图重试5次

This commit is contained in:
ikechan8370 2023-05-30 16:35:36 +08:00
parent f2f77235b1
commit 9a8db9577a

View file

@ -61,18 +61,28 @@ export default class BingDrawClient {
if (Config.proxy) { if (Config.proxy) {
fetchOptions.agent = proxy(Config.proxy) fetchOptions.agent = proxy(Config.proxy)
} }
let response = await fetch(url, Object.assign(fetchOptions, { body, redirect: 'manual', method: 'POST' })) let success = false
let retry = 5
let response
while (!success && retry >= 0) {
response = await fetch(url, Object.assign(fetchOptions, { body, redirect: 'manual', method: 'POST' }))
let res = await response.text() let res = await response.text()
if (res.toLowerCase().indexOf('this prompt has been blocked') > -1) { if (res.toLowerCase().indexOf('this prompt has been blocked') > -1) {
throw new Error('Your prompt has been blocked by Bing. Try to change any bad words and try again.') throw new Error('Your prompt has been blocked by Bing. Try to change any bad words and try again.')
} }
if (response.status !== 302) { if (response.status !== 302) {
url = `${this.opts.baseUrl}/images/create?q=${urlEncodedPrompt}&rt=3&FORM=GENCRE` url = `${this.opts.baseUrl}/images/create?q=${urlEncodedPrompt}&rt=3&FORM=GENCRE`
let response3 = await fetch(url, Object.assign(fetchOptions, { body, redirect: 'manual', method: 'POST' })) response = await fetch(url, Object.assign(fetchOptions, { body, redirect: 'manual', method: 'POST' }))
if (response3.status !== 302) {
throw new Error('绘图失败请检查Bing token和代理/反代配置')
} }
response = response3 if (response.status === 302) {
success = true
break
} else {
retry--
}
}
if (!success) {
throw new Error('绘图失败请检查Bing token和代理/反代配置')
} }
let redirectUrl = response.headers.get('Location').replace('&nfy=1', '') let redirectUrl = response.headers.get('Location').replace('&nfy=1', '')
let requestId = redirectUrl.split('id=')[1] let requestId = redirectUrl.split('id=')[1]