index.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. import { ArrayItems, FormItem, FormLayout, Input, NumberPicker, Select } from '@formily/antd';
  2. import { createSchemaField, observer } from '@formily/react';
  3. import type { ISchema } from '@formily/json-schema';
  4. import { DataTypeList, DateTypeList, FileTypeList } from '@/pages/device/data';
  5. import { Store } from 'jetlinks-store';
  6. import { useAsyncDataSource } from '@/utils/util';
  7. import { service } from '@/pages/device/components/Metadata';
  8. import BooleanEnum from '@/components/Metadata/BooleanParam';
  9. import EnumParam from '@/components/Metadata/EnumParam';
  10. import ArrayParam from '@/components/Metadata/ArrayParam';
  11. import { useIntl } from '@/.umi/plugin-locale/localeExports';
  12. import Editable from '../EditTable';
  13. // 不算是自定义组件。只是抽离了JSONSchema
  14. interface Props {
  15. keys?: string;
  16. isFunction?: boolean;
  17. }
  18. const JsonParam = observer((props: Props) => {
  19. const intl = useIntl();
  20. const SchemaField = createSchemaField({
  21. components: {
  22. FormItem,
  23. Input,
  24. Select,
  25. JsonParam,
  26. ArrayItems,
  27. Editable,
  28. FormLayout,
  29. NumberPicker,
  30. BooleanEnum,
  31. EnumParam,
  32. ArrayParam,
  33. },
  34. });
  35. const getUnit = () =>
  36. service.getUnit().then((resp) => {
  37. const _data = resp.result.map((item: any) => ({
  38. label: item.description,
  39. value: item.id,
  40. }));
  41. // 缓存单位数据
  42. Store.set('units', _data);
  43. return _data;
  44. });
  45. const checkArray: any = (arr: any) => {
  46. if (Array.isArray(arr) && arr.length) {
  47. return arr.every((item: any) => {
  48. if (item.valueType?.type === 'object') {
  49. return item.id && item.name && checkArray(item.json?.properties);
  50. }
  51. return item.id && item.name && item.valueType;
  52. });
  53. }
  54. return false;
  55. };
  56. const schema: ISchema = {
  57. type: 'object',
  58. properties: {
  59. [props?.keys || 'properties']: {
  60. type: 'array',
  61. 'x-component': 'ArrayItems',
  62. 'x-decorator': 'FormItem',
  63. 'x-reactions': {
  64. dependencies: ['.type'],
  65. fulfill: {
  66. state: {
  67. value: [{}],
  68. },
  69. },
  70. },
  71. 'x-validator': [
  72. {
  73. triggerType: 'onBlur',
  74. validator: (value: any[]) => {
  75. return new Promise((resolve) => {
  76. if (props.keys === 'inputs' && value.length === 0) {
  77. resolve('');
  78. }
  79. const flag = checkArray(value);
  80. if (!!flag) {
  81. resolve('');
  82. } else {
  83. resolve('请配置参数');
  84. }
  85. });
  86. },
  87. },
  88. ],
  89. items: {
  90. type: 'object',
  91. 'x-decorator': 'ArrayItems.Item',
  92. properties: {
  93. sort: {
  94. type: 'void',
  95. 'x-decorator': 'FormItem',
  96. 'x-component': 'ArrayItems.SortHandle',
  97. },
  98. config: {
  99. type: 'void',
  100. title: '配置参数',
  101. 'x-decorator': 'Editable.Popover',
  102. 'x-component': 'FormLayout',
  103. 'x-component-props': {
  104. layout: 'vertical',
  105. },
  106. 'x-decorator-props': {
  107. placement: 'left',
  108. },
  109. 'x-reactions':
  110. '{{(field)=>field.title = field.query(".config.name").get("value") || field.title}}',
  111. properties: {
  112. id: {
  113. title: '标识',
  114. required: true,
  115. 'x-decorator': 'FormItem',
  116. 'x-component': 'Input',
  117. 'x-validator': [
  118. {
  119. max: 64,
  120. message: '最多可输入64个字符',
  121. },
  122. {
  123. required: true,
  124. message: '请输入标识',
  125. },
  126. {
  127. validateId: true,
  128. message: 'ID只能由数字、26个英文字母或者下划线组成',
  129. },
  130. ],
  131. },
  132. name: {
  133. title: '名称',
  134. required: true,
  135. 'x-decorator': 'FormItem',
  136. 'x-component': 'Input',
  137. 'x-validator': [
  138. {
  139. max: 64,
  140. message: '最多可输入64个字符',
  141. },
  142. {
  143. required: true,
  144. message: '请输入名称',
  145. },
  146. ],
  147. },
  148. valueType: {
  149. type: 'object',
  150. properties: {
  151. type: {
  152. title: '数据类型',
  153. required: true,
  154. 'x-decorator': 'FormItem',
  155. 'x-component': 'Select',
  156. enum: DataTypeList.filter((item) =>
  157. ['int', 'long', 'float', 'double', 'string', 'boolean', 'date'].includes(
  158. item.value,
  159. ),
  160. ),
  161. },
  162. booleanConfig: {
  163. title: '布尔值',
  164. type: 'void',
  165. 'x-decorator': 'FormItem',
  166. 'x-component': 'BooleanEnum',
  167. 'x-reactions': {
  168. dependencies: ['..valueType.type'],
  169. fulfill: {
  170. state: {
  171. visible: "{{['boolean'].includes($deps[0])}}",
  172. },
  173. },
  174. },
  175. },
  176. enumConfig: {
  177. title: intl.formatMessage({
  178. id: 'pages.device.productDetail.metadata.enum',
  179. defaultMessage: '枚举项',
  180. }),
  181. type: 'void',
  182. 'x-decorator': 'FormItem',
  183. 'x-component': 'EnumParam',
  184. 'x-reactions': {
  185. dependencies: ['..valueType.type'],
  186. fulfill: {
  187. state: {
  188. visible: "{{['enum'].includes($deps[0])}}",
  189. },
  190. },
  191. },
  192. },
  193. elementType: {
  194. title: intl.formatMessage({
  195. id: 'pages.device.productDetail.metadata.elementConfiguration',
  196. defaultMessage: '元素配置',
  197. }),
  198. 'x-decorator': 'FormItem',
  199. 'x-component': 'ArrayParam',
  200. 'x-component-props': {
  201. isFunction: props.isFunction,
  202. },
  203. 'x-reactions': {
  204. dependencies: ['..valueType.type'],
  205. fulfill: {
  206. state: {
  207. visible: "{{['array'].includes($deps[0])}}",
  208. },
  209. },
  210. },
  211. },
  212. fileType: {
  213. title: intl.formatMessage({
  214. id: 'pages.device.productDetail.metadata.fileType',
  215. defaultMessage: '文件类型',
  216. }),
  217. 'x-decorator': 'FormItem',
  218. 'x-component': 'Select',
  219. 'x-visible': false,
  220. enum: FileTypeList,
  221. 'x-reactions': {
  222. dependencies: ['..valueType.type'],
  223. fulfill: {
  224. state: {
  225. visible: "{{['file'].includes($deps[0])}}",
  226. },
  227. },
  228. },
  229. },
  230. unit: {
  231. title: '单位',
  232. 'x-decorator': 'FormItem',
  233. 'x-component': 'Select',
  234. 'x-visible': false,
  235. 'x-component-props': {
  236. showSearch: true,
  237. allowClear: true,
  238. showArrow: true,
  239. filterOption: (input: string, option: any) =>
  240. option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0,
  241. },
  242. enum: Store.get('units'),
  243. 'x-reactions': [
  244. {
  245. dependencies: ['..valueType.type'],
  246. fulfill: {
  247. state: {
  248. visible:
  249. !props.isFunction &&
  250. "{{['int','float','long','double'].includes($deps[0])}}",
  251. },
  252. },
  253. },
  254. '{{useAsyncDataSource(getUnit)}}',
  255. ],
  256. },
  257. format: {
  258. title: '时间格式',
  259. 'x-decorator': 'FormItem',
  260. 'x-component': 'Select',
  261. enum: DateTypeList,
  262. 'x-visible': false,
  263. default: 'string',
  264. 'x-validator': [
  265. {
  266. required: true,
  267. message: '请选择时间格式',
  268. },
  269. ],
  270. 'x-reactions': {
  271. dependencies: ['..valueType.type'],
  272. fulfill: {
  273. state: {
  274. visible: "{{['date'].includes($deps[0])}}",
  275. },
  276. },
  277. },
  278. },
  279. expands: {
  280. type: 'object',
  281. properties: {
  282. maxLength: {
  283. title: '最大长度',
  284. 'x-decorator': 'FormItem',
  285. 'x-component': 'NumberPicker',
  286. 'x-decorator-props': {
  287. tooltip: intl.formatMessage({
  288. id: 'pages.device.productDetail.metadata.maxLength.byte',
  289. defaultMessage: '字节',
  290. }),
  291. },
  292. 'x-component-props': {
  293. min: 1,
  294. },
  295. 'x-validator': [
  296. {
  297. format: 'integer',
  298. message: '请输入1-2147483647之间的正整数',
  299. },
  300. {
  301. max: 2147483647,
  302. message: '请输入1-2147483647之间的正整数',
  303. },
  304. {
  305. min: 1,
  306. message: '请输入1-2147483647之间的正整数',
  307. },
  308. ],
  309. 'x-reactions': {
  310. dependencies: ['..type'],
  311. fulfill: {
  312. state: {
  313. visible:
  314. !props.isFunction &&
  315. "{{['string','password'].includes($deps[0])}}",
  316. },
  317. },
  318. },
  319. },
  320. },
  321. },
  322. },
  323. },
  324. 'valueType.scale': {
  325. title: '精度',
  326. 'x-decorator': 'FormItem',
  327. 'x-component': 'NumberPicker',
  328. 'x-visible': false,
  329. default: 2,
  330. 'x-validator': [
  331. {
  332. format: 'integer',
  333. message: '请输入0-2147483647之间的正整数',
  334. },
  335. {
  336. max: 2147483647,
  337. message: '请输入0-2147483647之间的正整数',
  338. },
  339. {
  340. min: 0,
  341. message: '请输入0-2147483647之间的正整数',
  342. },
  343. ],
  344. 'x-component-props': {
  345. min: 1,
  346. },
  347. 'x-reactions': {
  348. dependencies: ['..valueType.type'],
  349. fulfill: {
  350. state: {
  351. visible: !props.isFunction && "{{['float','double'].includes($deps[0])}}",
  352. },
  353. },
  354. },
  355. },
  356. json: {
  357. type: 'string',
  358. title: 'JSON对象',
  359. 'x-visible': false,
  360. 'x-decorator': 'FormItem',
  361. 'x-component': 'JsonParam',
  362. 'x-component-props': {
  363. isFunction: props.isFunction,
  364. },
  365. 'x-reactions': {
  366. dependencies: ['.valueType.type'],
  367. fulfill: {
  368. state: {
  369. visible: "{{['object'].includes($deps[0])}}",
  370. },
  371. },
  372. },
  373. },
  374. },
  375. },
  376. remove: {
  377. type: 'void',
  378. 'x-decorator': 'FormItem',
  379. 'x-component': 'ArrayItems.Remove',
  380. },
  381. },
  382. },
  383. properties: {
  384. add: {
  385. type: 'void',
  386. title: '添加参数',
  387. 'x-component': 'ArrayItems.Addition',
  388. },
  389. },
  390. },
  391. },
  392. };
  393. return <SchemaField schema={schema} scope={{ useAsyncDataSource, getUnit }} />;
  394. });
  395. export default JsonParam;