Skip to content

Commit

Permalink
fix(core): 修复题库配置BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
enncy committed May 20, 2022
1 parent 03fa9be commit 2b8c939
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/core/src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ function parseAnswererWrappers (value: string): AnswererWrapper[] {
return [];
}
} catch (e) {
console.log(e);
message('error', '题库配置格式错误!');
return [];
}
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/core/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,14 @@ export function request(url: string, opts: {
method?: 'get' | 'post';
headers?: Record<string, string>;
contentType?: 'json' | 'text',
body?: string
body?: string | URLSearchParams
}): Promise<string | object> {
return new Promise((resolve, reject) => {
/** 默认参数 */
const { contentType = 'json', body, method = 'get', headers = {}, type = 'fetch' } = opts || {};
/** 环境变量 */
const env = isInBrowser() ? 'browser' : 'node';
// console.log('request', { url, opts });

/** 如果是跨域模式并且是浏览器环境 */
if (type === 'GM_xmlhttpRequest' && env === 'browser') {
Expand All @@ -131,7 +132,7 @@ export function request(url: string, opts: {
GM_xmlhttpRequest({
method: opts.method?.toLocaleUpperCase() as any || 'GET',
url,
data: JSON.stringify(body || {}),
data: body?.toString(),
headers: opts.headers || {},
responseType: 'json',
onload: (response) => {
Expand Down
19 changes: 11 additions & 8 deletions packages/core/src/core/worker/answer.wrapper.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ export async function defaultAnswerWrapperHandler(
const responseData = await request(url, {
method: wrapper.method,
contentType: wrapper.contentType,
body: wrapper.method === 'post' ? JSON.stringify(data) : undefined,
body: wrapper.method === 'post' ? new URLSearchParams(data) : undefined,
type: wrapper.type,
headers: wrapper.headers
});
Expand Down Expand Up @@ -180,13 +180,16 @@ export async function defaultAnswerWrapperHandler(
}

function resolvePlaceHolder(str: string) {
const matches = str.match(/\${(.*?)}/g) || [];
matches.forEach((placeHolder) => {
const value: any =
/** 获取元素属性 */
get(env, placeHolder.replace(/\${(.*)}/, '$1'));
str = str.replace(placeHolder, value);
});
if (typeof str === 'string') {
const matches = str.match(/\${(.*?)}/g) || [];
matches.forEach((placeHolder) => {
const value: any =
/** 获取元素属性 */
get(env, placeHolder.replace(/\${(.*)}/, '$1'));
str = str.replace(placeHolder, value);
});
}

return str;
}

Expand Down

0 comments on commit 2b8c939

Please sign in to comment.