一颗努力的小土豆 2024-08-22 11:27 采纳率: 88.9%
浏览 9
已结题

vue+element-ui,如何实现当新增时,弹出表单中应用类型为企业微信小程序时,appid 和secret为必填字段,为app时,是非必填?

vue+element-ui,如何实现当新增时,弹出表单中应用类型为企业微信小程序时,appid 和secret为必填字段,为app时,是非必填?
页面展示

img

目前代码展示

<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" class="container-top">
      <bread-crumb />
      <el-form-item label="应用名称" prop="applicationName">
        <el-input
          v-model="queryParams.applicationName"
          placeholder="请输入应用名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="展示名称" prop="displayName">
        <el-input
          v-model="queryParams.displayName"
          placeholder="请输入展示名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应用类型" prop="applicationType">
        <el-input
          v-model="queryParams.applicationType"
          placeholder="请输入展示名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button class="button-search" type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button class="button-refresh" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="container-bottom">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:post:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:post:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:post:remove']"
          >删除</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange" :height="tableHeight"  style="background-color: transparent;" header-row-class-name="header-row-style">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="应用名称" align="center" prop="applicationName" />
        <el-table-column label="展示名称" align="center" prop="displayName" />
        <el-table-column label="应用类型" align="center" prop="applicationType">
          <!-- <template slot-scope="scope">
            {{ scope.row.applicationType === '0'  ? "企业微信"  : "小程序" }}
          </template> -->
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:post:edit']"
            >修改</el-button>
            <el-button
              class="del-btn"
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:post:remove']"
            >删除</el-button>
            <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 应用管理详细 -->
    <el-dialog title="应用管理详细" :visible.sync="detailOpen" width="700px" append-to-body>
      <el-form ref="form" :model="detailForm" label-width="100px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="应用名称:">{{ detailForm.applicationName }}</el-form-item>
            <el-form-item label="应用类型:">{{ detailForm.applicationType }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="展示名称:">{{ detailForm.displayName }}</el-form-item>
            <el-form-item label="appid:">{{ detailForm.appid }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="secret:">{{ detailForm.secret }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailOpen = false">关 闭</el-button>
      </div>
    </el-dialog>
    

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="应用名称" prop="applicationName">
          <el-input v-model="form.applicationName" placeholder="请输入应用名称" :disabled="isEdit"/>
        </el-form-item>
        <el-form-item label="展示名称" prop="displayName">
          <el-input v-model="form.displayName" placeholder="请输入展示名称" />
        </el-form-item>
        <el-form-item label="应用类型" prop="applicationType">
          <el-select v-model="form.applicationType" placeholder="请选择应用类型">
            <el-option key="app" label="app" value="app"></el-option>
            <el-option key="企业微信小程序" label="企业微信小程序" value="企业微信小程序"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="appid" prop="appid">
          <el-input v-model="form.appid" placeholder="请输入appid" :type="hidePassword ? 'password' : 'text'"/>
        </el-form-item>
        <el-form-item label="secret" prop="secret">
          <el-input v-model="form.secret" placeholder="请输入secret" :type="hidePassword ? 'password' : 'text'"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPost, getPost, delPost, addPost, updatePost } from "@/api/system/post";
import { listApplied,getApplied,addApplied,updateApplied,delApplied } from "@/api/confManager/appliedManager";
import breadCrumb from "@/components/Breadcrumb/index.vue"
import autoComputedH from '@/utils/mixins/autoComputedH'
export default {
  name: "Post",
  mixins: [autoComputedH],
  dicts: ['sys_normal_disable'],
  components: {
    breadCrumb,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      detailOpen:false,
      isEdit:false,
      hidePassword:true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applicationName: undefined,
        displayName: undefined,
        applicationType: undefined
      },
      // 表单参数
      form: {},
      detailForm:{},
      // 表单校验
      rules: {
        applicationName: [
          { required: true, message: "应用名称不能为空", trigger: "blur" },
          { validator: this.validateAppName, trigger: "blur" }
        ],
        displayName: [
          { required: true, message: "展示名称不能为空", trigger: "blur" }
        ],
        applicationType: [
          { required: true, message: "应用类型不能为空", trigger: "blur" }
        ],
       appid: [
          { validator: this.checkAppid, message: "appid不能为空", trigger: 'blur' }
        ],
        secret: [
          { validator: this.checkSecret, message: "secret不能为空",trigger: 'blur' }
        ],
      },
      containerTop: null,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 自定义校验函数
    validateAppName(rule, value, callback) {
      if (!value) {
        callback(new Error("应用名称不能为空"));
      } else if (!/^[a-zA-Z_]+$/.test(value)) {
        callback(new Error("应用名称只能包含英文字母和下划线"));
      } else {
        callback();
      }
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listApplied(this.queryParams).then(response => {
        console.log(response,'response')
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        applicationName: undefined,
        displayName: undefined,
        applicationType: undefined,
        appid: undefined,
        secret: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
 
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isEdit = false;
      this.hidePassword=false;
      this.open = true;
      this.title = "添加应用管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isEdit = true;
      const id = row.id || this.ids
      getApplied(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改应用管理";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateApplied(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApplied(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除所选数据项?').then(function() {
        return delApplied(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleView(row) {
        const entrustId = row.entrustId;
        getEntrust(entrustId).then(response => {
          this.form = response.data;
          this.testUserArry = this.form.testUser && this.form.testUser.split(",").map(Number);
          this.methodArry = this.form.method && this.form.method.split(",").map(Number);
          this.testitemsNumberArry = this.form.testitemsName && this.form.testitemsName.split(",");
          this.open = true;
          this.title = "委托单信息";
        });
      },
     /** 查看详情按钮操作 */
     handleView(row) {
      const appliedId = row.id
      getApplied(appliedId).then(response => {
          this.detailForm = response.data;
          this.detailOpen = true;
          this.title = "应用详情";
        });
      
      /* this.detailForm = row; */
    },
  }
};
</script>
<style scoped>
</style>



  • 写回答

1条回答 默认 最新

  • Nymph_Zhu 2024-08-22 12:11
    关注

    参考这个案例试试看:

    <template>
      <el-form :model="form" :rules="rules" ref="myForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
        </el-form-item>
      </el-form>
    </template>
     
    <script>
      export default {
        data() {
          return {
            form: {
              username: ''
            },
            rules: {
              username: [
                {
                  required: false, // 默认不必填
                  validator: (rule, value, callback) => {
                    if (/* 某个条件 */) {
                      // 条件满足,必填
                      rule.required = true;
                    } else {
                      // 条件不满足,不必填
                      rule.required = false;
                    }
                    this.$refs.myForm.validateField('username');
                    callback();
                  },
                  trigger: 'blur'
                }
              ]
            }
          };
        },
        methods: {
          submitForm() {
            this.$refs.myForm.validate(valid => {
              if (valid) {
                alert('验证通过');
              } else {
                console.log('验证失败');
                return false;
              }
            });
          }
        }
      };
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 8月30日
  • 已采纳回答 8月22日
  • 创建了问题 8月22日

悬赏问题

  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 在虚拟机环境下完成以下,要求截图!
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见