Skip to content

Commit

Permalink
feat: 增加api2d的余额查询
Browse files Browse the repository at this point in the history
  • Loading branch information
chenweiyi committed Aug 2, 2024
1 parent 12d96a5 commit bbfe88a
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 26 deletions.
4 changes: 2 additions & 2 deletions packages/backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
},
"scripts": {
"dev": "nodemon ./src/bin/www.mts",
"debug": "cross-env DEBUG=\"server,app,,service:*,controller:*\" nodemon ./src/bin/www.mts",
"start": "cross-env DEBUG=\"server,app,,service:*,controller:*\" ts-node ./src/bin/www.mts",
"debug": "cross-env DEBUG=\"server,app,vendor,service:*,controller:*\" nodemon ./src/bin/www.mts",
"start": "cross-env DEBUG=\"server,app,vendor,service:*,controller:*\" ts-node ./src/bin/www.mts",
"test": "echo \"Error: no test specified\" && exit 1",
"clean": "rm -rf ./build",
"build-ts": "tsc",
Expand Down
2 changes: 2 additions & 0 deletions packages/backend/src/app.mts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import serve from 'koa-static'
import path from 'path'

import { router as message } from './routes/message.mjs'
import { router as vendor } from './routes/vendor.mjs'
import users from './routes/users.mjs'
import conditional from './utils/koa-conditional-get.mjs'

Expand Down Expand Up @@ -78,6 +79,7 @@ app.use(
// app.use(users.routes(), users.allowedMethods())
app.use(users.routes()).use(users.allowedMethods())
app.use(message.routes()).use(message.allowedMethods())
app.use(vendor.routes()).use(vendor.allowedMethods())

// error-handling
app.on('error', (err, ctx) => {
Expand Down
12 changes: 12 additions & 0 deletions packages/backend/src/controller/vendor.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Koa from 'koa'
import { api2DProfile } from '../service/vendor.mjs'

export default class VendorController {
/**
* 获取vendor用户信息
* @param ctx
*/
public static async postApi2DProfile(ctx: Koa.Context) {
await api2DProfile(ctx)
}
}
6 changes: 6 additions & 0 deletions packages/backend/src/routes/vendor.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { router } from './index.mjs'
import VendorController from '../controller/vendor.mjs'

router.post('/userProfile/api2d', VendorController.postApi2DProfile)

export { router }
44 changes: 44 additions & 0 deletions packages/backend/src/service/vendor.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Context } from 'koa'
import fetch from 'node-fetch'
import proxy from 'https-proxy-agent'
import debugLibrary from 'debug'

const debug = debugLibrary('vendor')

export async function api2DProfile(ctx: Context) {
try {
const res = await fetch(`https://api.api2d.com/user/profile`, {
method: 'POST',
headers: {
authorization: 'Bearer 100037|WOdHl3svgUPJdKIMkqlbSZbl7YjOXxHqTEVVtyFa',
Accept: 'application/json'
},
agent: proxy(process.env.CUSTOM_PROXY)
})
const data: any = await res.json()
debug('api2DProfile result: ', data)
if (data.code === 0) {
ctx.body = {
code: 200,
msg: 'ok',
data: data.data.profile,
success: true
}
} else {
ctx.body = {
code: 500,
msg: data.msg || 'error',
data: data,
success: false
}
}
} catch (e) {
debug('api2DProfile error: ', e)
ctx.body = {
code: 500,
msg: e.message || 'error',
data: null,
success: false
}
}
}
2 changes: 1 addition & 1 deletion packages/frontend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const request: RequestConfig = {
// 拦截响应数据,进行个性化处理
const { data } = response as unknown as Common.Response<any>
if (data.code === 200) {
return data.data
return data
} else {
return Promise.reject(data)
}
Expand Down
7 changes: 6 additions & 1 deletion packages/frontend/src/pages/ai/chatgpt/LayoutSider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ export default function LayoutSider() {
</ul>
</div>
<div className={styles.bottomDiv}>
<Dropdown menu={operateMenu} placement='top' arrow>
<Dropdown
menu={operateMenu}
placement='top'
arrow
trigger={['click']}
>
<Button
type='default'
shape='default'
Expand Down
79 changes: 58 additions & 21 deletions packages/frontend/src/pages/ai/chatgpt/components/ModelConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ interface IModelConfigProps {
export default function ModelConfig(props: IModelConfigProps) {
const formRef = useRef<FormInstance<IModelInfo>>(null)
const [checkLoading, setCheckLoading] = useState(false)
const [billLoading, setBillLoading] = useState(false)
const [bill, setBill] = useState('')
const [checkError, setCheckError] = useState('')
const [form] = Form.useForm()
const [modelType, setModelType] = useState(
Expand Down Expand Up @@ -143,6 +145,22 @@ export default function ModelConfig(props: IModelConfigProps) {
})
}

const queryBill = () => {
setBillLoading(true)
CHATGPT.queryApi2DBill()
.then((res) => {
console.log('queryBill', res)
setBill(res.point)
})
.catch((e) => {
console.log('queryBill error', e)
setBill('-')
})
.finally(() => {
setBillLoading(false)
})
}

return (
<Form
className='w-full'
Expand Down Expand Up @@ -205,28 +223,47 @@ export default function ModelConfig(props: IModelConfigProps) {
<Slider min={0} max={1} step={0.1} onChange={saveHandler}></Slider>
</Form.Item>
{props.data ? (
<Form.Item label='联通性检查'>
<div className='flex items-center'>
<Button
type='primary'
onClick={check}
loading={checkLoading}
disabled={checkLoading}
>
检查
</Button>
{checkError !== 'ok' ? (
<span
title={checkError}
className='text-red-500 custom-ellipsis inline-block w-250px ml-12px'
<>
<Form.Item label='联通性检查'>
<div className='flex items-center'>
<Button
type='primary'
onClick={check}
loading={checkLoading}
disabled={checkLoading}
>
检查
</Button>
{checkError !== 'ok' ? (
<span
title={checkError}
className='text-red-500 custom-ellipsis inline-block w-250px ml-12px'
>
{checkError}
</span>
) : checkError === 'ok' ? (
<span className='text-green-500 ml-12px'>ok!</span>
) : null}
</div>
</Form.Item>
<Form.Item label='查余额'>
<div>
<Button
type='primary'
onClick={queryBill}
loading={billLoading}
disabled={billLoading}
>
{checkError}
</span>
) : checkError === 'ok' ? (
<span className='text-green-500 ml-12px'>ok!</span>
) : null}
</div>
</Form.Item>
查询
</Button>
{bill === '-' ? (
<span className='text-red-500 inline-block ml-12px'> - </span>
) : (
<span className='text-green-500 ml-12px'>{bill}</span>
)}
</div>
</Form.Item>
</>
) : null}
</Form>
)
Expand Down
5 changes: 5 additions & 0 deletions packages/frontend/src/services/chatgpt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@ export const CHATGPT = {
const res = await get<ChatMessage, ICheckApiParams>('/q/checkApi', params)
console.log('chatgpt - /q/checkApi:', res)
return res
},
queryApi2DBill: async (): Promise<any> => {
const res = await post<any>('/q/userProfile/api2d')
console.log('chatgpt - /q/userProfile/api2d:', res)
return res
}
}
3 changes: 2 additions & 1 deletion packages/frontend/src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ async function http<ResponseData, RequestData>(
params?: RequestData,
config?: any
) {
console.log('http')
const data = await request<ResponseData>(url, {
method,
headers: {
Expand All @@ -30,7 +31,7 @@ function get<ResponseData, RequestData = undefined>(
return http<ResponseData, RequestData>(url, 'GET', params, config)
}

function post<ResponseData, RequestData = undefined>(
function post<ResponseData, RequestData = any>(
url: string,
data?: RequestData,
config?: any
Expand Down

0 comments on commit bbfe88a

Please sign in to comment.