You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.tsx 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * @Author: 赵伟
  3. * @Date: 2024-04-16 13:58:08
  4. * @Description: 创建推理服务
  5. */
  6. import PageTitle from '@/components/PageTitle';
  7. import SubAreaTitle from '@/components/SubAreaTitle';
  8. import { CommonTabKeys, serviceTypeOptions } from '@/enums';
  9. import { createServiceReq, updateServiceReq } from '@/services/modelDeployment';
  10. import { to } from '@/utils/promise';
  11. import {
  12. getSessionStorageItem,
  13. removeSessionStorageItem,
  14. serviceInfoKey,
  15. } from '@/utils/sessionStorage';
  16. import { useNavigate } from '@umijs/max';
  17. import { App, Button, Col, Form, Input, Row, Select } from 'antd';
  18. import { pick } from 'lodash';
  19. import { useEffect, useState } from 'react';
  20. import { ServiceData, ServiceOperationType } from '../types';
  21. import styles from './index.less';
  22. // 表单数据
  23. export type FormData = {
  24. service_name: string; // 服务名称
  25. service_type: string; // 服务类型
  26. description: string; // 描述
  27. };
  28. function CreateService() {
  29. const navigate = useNavigate();
  30. const [form] = Form.useForm();
  31. const [operationType, setOperationType] = useState(ServiceOperationType.Create);
  32. const [serviceInfo, setServiceInfo] = useState<ServiceData | undefined>(undefined);
  33. const { message } = App.useApp();
  34. useEffect(() => {
  35. const res = getSessionStorageItem(serviceInfoKey, true);
  36. if (res) {
  37. setOperationType(res.operationType);
  38. setServiceInfo(res);
  39. form.setFieldsValue(pick(res, ['service_name', 'service_type', 'description']));
  40. }
  41. return () => {
  42. removeSessionStorageItem(serviceInfoKey);
  43. };
  44. }, []);
  45. // 创建、更新服务
  46. const createService = async (formData: FormData) => {
  47. const request =
  48. operationType === ServiceOperationType.Create ? createServiceReq : updateServiceReq;
  49. const params =
  50. operationType === ServiceOperationType.Create
  51. ? formData
  52. : {
  53. id: serviceInfo?.id,
  54. ...formData,
  55. };
  56. const [res] = await to(request(params));
  57. if (res) {
  58. message.success('操作成功');
  59. navigate(-1);
  60. }
  61. };
  62. // 提交
  63. const handleSubmit = (values: FormData) => {
  64. createService(values);
  65. };
  66. // 取消
  67. const cancel = () => {
  68. navigate(-1);
  69. };
  70. const disabled = operationType !== ServiceOperationType.Create;
  71. const title = operationType === ServiceOperationType.Create ? '创建推理服务' : '更新推理服务';
  72. return (
  73. <div className={styles['model-deployment-create']}>
  74. <PageTitle title={title}></PageTitle>
  75. <div className={styles['model-deployment-create__content']}>
  76. <div>
  77. <Form
  78. name="model-deployment-create"
  79. labelCol={{ flex: '100px' }}
  80. labelAlign="left"
  81. form={form}
  82. initialValues={{ upload_type: CommonTabKeys.Public }}
  83. onFinish={handleSubmit}
  84. size="large"
  85. autoComplete="off"
  86. >
  87. <SubAreaTitle
  88. title="基本信息"
  89. image={require('@/assets/img/mirror-basic.png')}
  90. style={{ marginBottom: '26px' }}
  91. ></SubAreaTitle>
  92. <Row gutter={8}>
  93. <Col span={10}>
  94. <Form.Item
  95. label="服务名称"
  96. name="service_name"
  97. rules={[
  98. {
  99. required: true,
  100. message: '请输入服务名称',
  101. },
  102. ]}
  103. >
  104. <Input
  105. placeholder="请输入服务名称"
  106. disabled={disabled}
  107. maxLength={30}
  108. showCount
  109. allowClear
  110. />
  111. </Form.Item>
  112. </Col>
  113. </Row>
  114. <Row gutter={8}>
  115. <Col span={10}>
  116. <Form.Item
  117. label="服务类型"
  118. name="service_type"
  119. rules={[
  120. {
  121. required: true,
  122. message: '请选择服务类型',
  123. },
  124. ]}
  125. >
  126. <Select placeholder="请选择服务类型" options={serviceTypeOptions} allowClear />
  127. </Form.Item>
  128. </Col>
  129. </Row>
  130. <Row gutter={8}>
  131. <Col span={20}>
  132. <Form.Item
  133. label="描  述"
  134. name="description"
  135. rules={[
  136. {
  137. required: true,
  138. message: '请输入描述',
  139. },
  140. ]}
  141. >
  142. <Input.TextArea
  143. autoSize={{ minRows: 2, maxRows: 6 }}
  144. placeholder="请输入描述,最长128字符"
  145. maxLength={128}
  146. showCount
  147. allowClear
  148. />
  149. </Form.Item>
  150. </Col>
  151. </Row>
  152. <Form.Item wrapperCol={{ offset: 0, span: 16 }}>
  153. <Button type="primary" htmlType="submit">
  154. 确定
  155. </Button>
  156. <Button
  157. type="default"
  158. htmlType="button"
  159. onClick={cancel}
  160. style={{ marginLeft: '20px' }}
  161. >
  162. 取消
  163. </Button>
  164. </Form.Item>
  165. </Form>
  166. </div>
  167. </div>
  168. </div>
  169. );
  170. }
  171. export default CreateService;