From 9a8db9577af4b70a445a375ab0614b69ae721e71 Mon Sep 17 00:00:00 2001 From: ikechan8370 Date: Tue, 30 May 2023 16:35:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=94=BB=E5=9B=BE=E9=87=8D=E8=AF=955?= =?UTF-8?q?=E6=AC=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/BingDraw.js | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/utils/BingDraw.js b/utils/BingDraw.js index 39b07b0..6c81ffc 100644 --- a/utils/BingDraw.js +++ b/utils/BingDraw.js @@ -61,18 +61,28 @@ export default class BingDrawClient { if (Config.proxy) { fetchOptions.agent = proxy(Config.proxy) } - let response = await fetch(url, Object.assign(fetchOptions, { body, redirect: 'manual', method: 'POST' })) - let res = await response.text() - 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.') - } - if (response.status !== 302) { - 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' })) - if (response3.status !== 302) { - throw new Error('绘图失败,请检查Bing token和代理/反代配置') + 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() + 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.') } - response = response3 + if (response.status !== 302) { + url = `${this.opts.baseUrl}/images/create?q=${urlEncodedPrompt}&rt=3&FORM=GENCRE` + response = await fetch(url, Object.assign(fetchOptions, { body, redirect: 'manual', method: 'POST' })) + } + 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 requestId = redirectUrl.split('id=')[1]