Explorar el Código

feat: 添加元素时检查重名

zhangyuantao hace 1 año
padre
commit
d8939905b4

+ 11 - 2
src/main/java/com/tao/controller/AppElementController.java

@@ -9,9 +9,7 @@ import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
 
-import javax.servlet.http.HttpServletRequest;
 import java.util.List;
-import java.util.Map;
 
 /**
  * @ClassName AppElementController
@@ -87,4 +85,15 @@ public class AppElementController {
         return AjaxResult.success("添加成功");
     }
 
+    @GetMapping("/verifyName")
+    @ResponseBody
+    public AjaxResult findElementByName(String elementName) {
+        AppElement appElement = appElementService.selectAppElementByName(elementName);
+        if (appElement != null) {
+            return AjaxResult.error("已存在同名元素");
+        } else {
+            return AjaxResult.success("未重复");
+        }
+    }
+
 }

+ 7 - 0
src/main/java/com/tao/mapper/AppElementMapper.java

@@ -21,6 +21,13 @@ public interface AppElementMapper {
      */
     public AppElement selectAppElementById(Long id);
 
+    /**
+     * @param elementName
+     * @return com.tao.pojo.AppElement
+     * @Description 根据类型来查找元素集合
+     */
+    public AppElement selectAppElementByName(String elementName);
+
     /**
      * @param pageID
      * @return java.util.List<com.tao.pojo.AppElement>

+ 7 - 0
src/main/java/com/tao/service/AppElementService.java

@@ -21,6 +21,13 @@ public interface AppElementService {
      */
     public AppElement selectAppElementById(Long id);
 
+    /**
+     * @param elementName
+     * @return com.tao.pojo.AppElement
+     * @Description 根据类型来查找元素集合
+     */
+    public AppElement selectAppElementByName(String elementName);
+
     /**
      * @param pageID
      * @return java.util.List<com.tao.pojo.AppElement>

+ 10 - 0
src/main/java/com/tao/service/impl/AppElementServiceImpl.java

@@ -156,6 +156,16 @@ public class AppElementServiceImpl implements AppElementService {
         return appElementMapper.selectAppElementById(id);
     }
 
+    /**
+     * @param elementName
+     * @return com.tao.pojo.AppElement
+     * @Description 根据类型来查找元素集合
+     */
+    @Override
+    public AppElement selectAppElementByName(String elementName) {
+        return appElementMapper.selectAppElementByName(elementName);
+    }
+
     /**
      * @param pageID
      * @return java.util.List<com.tao.pojo.AppElement>

+ 5 - 0
src/main/resources/mybatis/mapper/AppElementMapper.xml

@@ -41,6 +41,11 @@
         where e.element_id = #{elementID}
     </select>
 
+    <select id="selectAppElementByName" parameterType="String" resultMap="AppElementResult">
+        <include refid="selectAppElementVo"/>
+        where e.element_name = #{elementName}
+    </select>
+
     <select id="selectAppElementListByPageID" parameterType="Long" resultMap="AppElementResult">
         <include refid="selectAppElementVo"/>
         where e.page_id = #{pageID}

+ 22 - 3
src/main/resources/templates/add.html

@@ -18,7 +18,7 @@
         <div class="layui-form-item">
             <label class="layui-form-label" style="width: 16%">元素名</label>
             <div class="layui-input-inline" style="width: 76%">
-                <input type="text" name="elementName" required lay-verify="required" placeholder="请输入元素名" autocomplete="off" class="layui-input">
+                <input type="text" name="elementName" required lay-verify="required|elementName" placeholder="请输入元素名" autocomplete="off" class="layui-input">
             </div>
         </div>
         <div class="layui-form-item">
@@ -37,7 +37,7 @@
         <div class="layui-form-item">
             <label class="layui-form-label" style="width: 20%">IOS端定位</label>
             <div class="layui-input-inline" style="width: 18%">
-                <select name="IOSType" >
+                <select name="IOSType">
                     <option value=""></option>
                     <option value="predicate">predicate</option>
                     <option value="xpath">xpath</option>
@@ -77,7 +77,7 @@
         //提交
         form.on('submit(preAddElement)', function (data) {
             data.field.elementAndroidLocate = "'" + data.field.androidType + "': '" + data.field.elementAndroidLocate + "'";
-            data.field.elementIOSLocate = "'" + data.field.IOSType + "': '"+ data.field.elementIOSLocate + "'";
+            data.field.elementIOSLocate = "'" + data.field.IOSType + "': '" + data.field.elementIOSLocate + "'";
             $.ajax({
                 type: 'post',
                 url: '/element/add',
@@ -100,6 +100,25 @@
             return false;
         });
 
+        form.verify({
+            elementName: function (value, item) { //value:表单的值、item:表单的DOM对象
+                let code = 500;
+                $.ajax({
+                    async: false,
+                    url: '/element/verifyName',
+                    type: 'GET',
+                    data: {elementName: value},
+                    dataType: 'json',
+                    timeout: 30000,
+                    success: function (response) {
+                        code = response.code;
+                    }
+                });
+                if (code === 500) {
+                    return '元素重名啦~';
+                }
+            }
+        });
     });
 
     function child(obj) {