/** 本文件由工具自动生成,请勿手动修改! **/ // @ts-nocheck <% if(!isFunction) {%>import { useState, useEffect } from 'react';<% } %> import * as R from 'ramda'; import Request, { YWRequestConfig } from '@ywfe/request'; import { getHost, <% if(!isV2){ %>reduceParams, <% } %>parseParams, querystring } from '@ywfe/utils'; <%if (projectId){%> <%if (isMock){%> const { basePath = '', loginPath = '' } = getHost("<%= projectId%>", "<%= project_id%>", <%= isMock%>) <% } else { %> const { basePath = '', loginPath = '' } = getHost("<%= projectId%>", "<%= project_id%>") <% } %> <% } else { %> const basePath = ''; const loginPath = ''; <% } %> // 接口文档 <% if(interfaces && interfaces.length) {%> <% for(let i = 0; i < interfaces.length; i++) {%> <% if (interfaces[i].data && interfaces[i].data.length > 0) {%> export interface <%= interfaces[i].name%> { <% for(let j = 0; j < interfaces[i].data.length; j++) {%> /** <%= interfaces[i].data[j].description ? interfaces[i].data[j].description : ''%> */ <%= interfaces[i].data[j].name%><% if(!interfaces[i].data[j].required) {%>?<%}%>: <%= interfaces[i].data[j].type%>; <% } %> }; <% } else {%> export type <%= interfaces[i].name%> = {}; <% } %> <% } %> <%}%> <% if (inputs.length) {%> export interface <% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Input<% } else { %>Req<% } %> { <% for(let i = 0; i < inputs.length; i++) {%><%= inputs[i]%><% } %> }; <% } else {%> export type <% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Input<% } else { %>Req<% } %> = <%= requestType%>; <% } %> <% if (responses && responses.length) {%> export interface <% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Data<% } else { %>Res<% } %> { <% for(let i = 0; i < responses.length; i++) {%>/** <%= responses[i].description ? responses[i].description : ''%> */ <%= responses[i].name%><% if(!responses[i].required) {%>?<%}%>: <%= responses[i].type%>; <% } %> }; <% } else {%> export type <% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Data<% } else { %>Res<% } %> = <%= responsesType%>; <% } %> // 接口函数 <%= id%> <%= title%> <% if(isFunction) { %> export const do<%= classify(dasherize(method))%><%= name%> = async (<%if (inputs && inputs.length) {%>inputs: <% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Input<% } else { %>Req<% } %>,<% } %> config?: YWRequestConfig): Promise<<% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Data<% } else { %>Res<% } %>> => { <%if (inputs && inputs.length) {%> <% if(isV2){ %> const params = ((inputs as unknown as R.Dictionary<<% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Input<% } else { %>Req<% } %>>) || {}) as <% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Input<% } else { %>Req<% } %>; <%} else {%> const params = (reduceParams(inputs as unknown as R.Dictionary<<% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Input<% } else { %>Req<% } %>>) || {}) as <% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Input<% } else { %>Req<% } %>; <% } %> <% } %> try { <% if ( isRestful ) { %> const res: any = await Request.<%= dasherize(method)%><<%= name%><% if(old) {%>Data<% } else { %>Res<% } %>>(`${basePath}<%= path%>/<% if (inputs && inputs.length) { %>${parseParams<<% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Input<% } else { %>Req<% } %>>(params)}<% }%>`, { loginHost: loginPath, ...config }); <% } else { %> <% if (method === 'GET') { %> const res: any = await Request.get<<% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Data<% } else { %>Res<% } %>>(`${basePath}<%= path%><% if (inputs && inputs.length) { %>?${querystring.stringify(params as any)}<% }%>`, { loginHost: loginPath, ...config }); <% } %> <% if (method === 'POST') { %> const res: any = await Request.post<<% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Data<% } else { %>Res<% } %>>(`${basePath}<%= path%>`, <% if (inputs && inputs.length) { %>params<% } else {%>{}<% }%>, { loginHost: loginPath, ...config }); <% } %> <% } %> return res; } catch(error){ console.error('API error: ',error); return false as unknown as <% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Data<% } else { %>Res<% } %>; } } <% } else { %> export interface IUse<%= classify(dasherize(method))%><%= name%>Return { /** <%= title%>: 接口请求状态 */ is<%= name%>Loading: boolean; /** <%= title%>: 接口请求返回数据 */ data<%= name%>: <% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Data<% } else { %>Res<% } %>; /** <%= title%>: 接口数据请求方法 */ fetch<%= name%>Data: (<%if (inputs && inputs.length) {%>inputs: <% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Input<% } else { %>Req<% } %>,<% } %> config?: YWRequestConfig) => Promise<<% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Data<% } else { %>Res<% } %>>; /** <%= title%>: 接口请求URL */ requestUrl: string; } export const use<%= classify(dasherize(method))%><%= name%> = ():IUse<%= classify(dasherize(method))%><%= name%>Return => { let isMount = true; const [is<%= name%>Loading, setIs<%= name%>Loading] = useState(false); const [data<%= name%>, setData<%= name%>] = useState<<% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Data<% } else { %>Res<% } %>>({} as <% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Data<% } else { %>Res<% } %>); const requestUrl = `${basePath}<%= path%>`; const fetch<%= name%>Data = async (<%if (inputs && inputs.length) {%>inputs: <% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Input<% } else { %>Req<% } %>,<% } %> config?: YWRequestConfig): Promise<<% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Data<% } else { %>Res<% } %>> => { <%if (inputs && inputs.length) {%> <% if(isV2){ %> const params = ((inputs as unknown as R.Dictionary<<% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Input<% } else { %>Req<% } %>>) || {}) as <% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Input<% } else { %>Req<% } %>; <%} else {%> const params = (reduceParams(inputs as unknown as R.Dictionary<<% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Input<% } else { %>Req<% } %>>) || {}) as <% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Input<% } else { %>Req<% } %>; <% } %> <% } %> if ( isMount ) { setIs<%= name%>Loading(true); } try { <% if ( isRestful ) { %> const res: any = await Request.<%= dasherize(method)%><<%= name%><% if(old) {%>Data<% } else { %>Res<% } %>>(`${basePath}<%= path%>/<% if (inputs && inputs.length) { %>${parseParams<<% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Input<% } else { %>Req<% } %>>(params)}<% }%>`, { loginHost: loginPath, ...config }); <% } else { %> <% if (method === 'GET') { %> const res: any = await Request.get<<% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Data<% } else { %>Res<% } %>>(`${basePath}<%= path%><% if (inputs && inputs.length) { %>?${querystring.stringify(params as any)}<% }%>`, { loginHost: loginPath, ...config }); <% } %> <% if (method === 'POST') { %> const res: any = await Request.post<<% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Data<% } else { %>Res<% } %>>(`${basePath}<%= path%>`, <% if (inputs && inputs.length) { %>params<% } else {%>{}<% }%>, { loginHost: loginPath, ...config }); <% } %> <% } %> if (isMount) { setData<%= name%>(res || {} as <% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Data<% } else { %>Res<% } %>); setIs<%= name%>Loading(false); } return res; } catch (error) { console.error('[ERROR] API Request Error: ', error); if ( isMount ) { setIs<%= name%>Loading(false); } return false as unknown as <% if(isMethod) { %><%= method%><% }%><%= name%><% if(old) {%>Data<% } else { %>Res<% } %>; } } useEffect(() => { return () => { isMount = false; }; }, []); return { is<%= name%>Loading, data<%= name%>, fetch<%= name%>Data, requestUrl, } } <% } %>