Commit 3f287cbebfe71ecb63855b4ed5c1794f5051cde5

Authored by ww
1 parent 3549c919

perf: add validate before send instruction

@@ -14063,11 +14063,16 @@ class HandleDataInteraction { @@ -14063,11 +14063,16 @@ class HandleDataInteraction {
14063 const { value } = form.val(enumActionEl.ISSUED_WAY_FILTER) 14063 const { value } = form.val(enumActionEl.ISSUED_WAY_FILTER)
14064 let { deviceId, attr } = contentData.dataSources.find(item => item.nodeId === nodeId) || {} 14064 let { deviceId, attr } = contentData.dataSources.find(item => item.nodeId === nodeId) || {}
14065 let { command, way } = content 14065 let { command, way } = content
14066 - const validate = [value, deviceId, way, command, attr]  
14067 - if (!validate.every(Boolean)) return 14066 + const validate = new Validate([
  14067 + { value, required: true, message: '下发值是必填项' },
  14068 + { value: deviceId, required: true, message: '未绑定设备' },
  14069 + { value: way, required: true, message: '未绑定指令下发方式(单向/双向)' },
  14070 + { value: command, required: true, message: '未设置下发命令' },
  14071 + { value: attr, required: true, message: '未绑定设备属性' },
  14072 + ])
  14073 + if (!validate.begin()) return
14068 if (typeof command === 'string') command = jsonParse(command) 14074 if (typeof command === 'string') command = jsonParse(command)
14069 const data = replaceAttrPlaceholder(command, attr, value) 14075 const data = replaceAttrPlaceholder(command, attr, value)
14070 - console.log(data)  
14071 const instructionData = { 14076 const instructionData = {
14072 method: "methodThingskit", 14077 method: "methodThingskit",
14073 params: data, 14078 params: data,
@@ -14858,3 +14863,37 @@ function RAFSetInterval(callback, time) { @@ -14858,3 +14863,37 @@ function RAFSetInterval(callback, time) {
14858 } 14863 }
14859 14864
14860 14865
  14866 +class Validate {
  14867 + /**
  14868 + * @description
  14869 + * @type {{value: any, message: string, required?: boolean, validator?: any}[]} list
  14870 + */
  14871 + list = []
  14872 +
  14873 + /**
  14874 + * @description
  14875 + * @param {{value: any, message: string, required?: boolean, validator?: any}[]} ruleList
  14876 + */
  14877 + constructor(ruleList = []) {
  14878 + this.list = ruleList
  14879 + }
  14880 +
  14881 + /**
  14882 + * @description 设置规则
  14883 + * @param {{value: any, message: string, required?: boolean, validator?: any}} rule
  14884 + */
  14885 + set(rule) {
  14886 + this.list.push(rule)
  14887 + }
  14888 +
  14889 + begin() {
  14890 + for (const rule of this.list) {
  14891 + const { required, value, message, validator } = rule
  14892 + if (required && !value) {
  14893 + UseLayUi.topErrorMsg(message)
  14894 + return false
  14895 + }
  14896 + }
  14897 + return true
  14898 + }
  14899 +}
1 -<!DOCTYPE html>  
2 -<html lang="en">  
3 -  
4 -<head>  
5 - <meta charset="UTF-8">  
6 - <meta http-equiv="X-UA-Compatible" content="IE=edge">  
7 - <meta name="viewport" content="width=device-width, initial-scale=1.0">  
8 - <title>Document</title>  
9 -</head>  
10 -  
11 -<body>  
12 - <script>  
13 -  
14 - function replaceAttrPlaceholder(oldValue = {}, replaceAttr = '', replaceValue = '', newValue = {},) {  
15 - if (typeof oldValue !== 'object') return newValue  
16 -  
17 - for (const key of Object.keys(oldValue)) {  
18 - if (key === 'attrPlaceholder') {  
19 - newValue[replaceAttr] = replaceValue  
20 - continue  
21 - }  
22 - if (typeof oldValue[key] === 'object') {  
23 - newValue[key] = replaceAttrPlaceholder(oldValue[key], replaceAttr, replaceValue)  
24 - }  
25 - }  
26 -  
27 - return newValue  
28 - }  
29 -  
30 - const value = {  
31 - attrPlaceholder: {  
32 - a: 123  
33 - },  
34 - name: {  
35 - attrPlaceholder: 123,  
36 - b: {  
37 - attrPlaceholder: 123  
38 - }  
39 - }  
40 - }  
41 -  
42 - console.log(replaceAttrPlaceholder(value, 'co', 2))  
43 - </script>  
44 -</body>  
45 -  
46 -</html>