form-frame.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <div id="form-frame">
  3. <el-row>
  4. <el-col :span="24" class="formMain">
  5. <el-col :span="24" class="one">
  6. <el-button
  7. type="primary"
  8. plain
  9. icon="el-icon-back"
  10. size="mini"
  11. @click="toBack"
  12. >返回</el-button
  13. ></el-col
  14. >
  15. <el-col :span="24" class="two">
  16. <el-form :model="form" :rules="rules" ref="form" label-width="100px">
  17. <el-form-item label="申请年度" prop="apply_year">
  18. <el-input
  19. v-model="form.apply_year"
  20. placeholder="请输入申请年度"
  21. ></el-input>
  22. </el-form-item>
  23. <el-col :span="24" class="btn">
  24. <el-button type="primary" @click="onSubmit('form')"
  25. >提交保存</el-button
  26. >
  27. <el-button type="warning" @click="onDraft()">保存草稿</el-button>
  28. <el-button @click="onReset('form')">重置信息</el-button>
  29. </el-col>
  30. </el-form>
  31. </el-col>
  32. </el-col>
  33. </el-row>
  34. </div>
  35. </template>
  36. <script>
  37. import { mapState, mapGetters, createNamespacedHelpers } from "vuex";
  38. const { mapActions: merits_apply } = createNamespacedHelpers("merits_apply");
  39. export default {
  40. name: "form-frame",
  41. props: {
  42. id: { type: String },
  43. },
  44. components: {},
  45. data: function () {
  46. return {
  47. form: {},
  48. rules: {
  49. apply_year: [
  50. { required: true, message: "请输入申请年度", trigger: "blur" },
  51. ],
  52. },
  53. };
  54. },
  55. created() {
  56. this.$set(this, `form`, { user_id: this.user_id, user_name: this.name });
  57. },
  58. methods: {
  59. ...merits_apply(["fetch", "create", "update"]),
  60. // 返回
  61. toBack() {
  62. this.$emit("toBack");
  63. },
  64. // 提交保存
  65. onSubmit(formName) {
  66. this.$refs[formName].validate(async (valid) => {
  67. if (valid) {
  68. let data = this.form;
  69. data.status = "1";
  70. let res;
  71. if (data.id) res = await this.update(data);
  72. else res = await this.create(data);
  73. if (res.code === 200) {
  74. this.$message({ type: `success`, message: res.msg });
  75. this.toBack();
  76. } else {
  77. this.$message({ type: `error`, message: res.msg });
  78. }
  79. } else {
  80. console.log("error submit!!");
  81. return false;
  82. }
  83. });
  84. },
  85. // 保存草稿
  86. async onDraft() {
  87. let data = this.form;
  88. let res;
  89. if (data.id) res = await this.update(data);
  90. else res = await this.create(data);
  91. if (res.code === 200) {
  92. this.$message({ type: `success`, message: res.msg });
  93. this.toBack();
  94. } else {
  95. this.$message({ type: `error`, message: res.msg });
  96. }
  97. },
  98. // 查看详情
  99. async searchInfo(id) {
  100. let res = await this.fetch(id);
  101. if (res.code === 200) {
  102. this.$set(this, `form`, res.data);
  103. } else {
  104. this.$message({ type: `error`, message: res.msg });
  105. }
  106. },
  107. // 重置信息
  108. onReset(formName) {
  109. this.$refs[formName].resetFields();
  110. },
  111. },
  112. computed: {
  113. ...mapGetters(["user_id", "name"]),
  114. },
  115. metaInfo() {
  116. return { title: this.$route.meta.title };
  117. },
  118. watch: {
  119. id: {
  120. deep: true,
  121. immediate: true,
  122. handler(val) {
  123. if (val) this.searchInfo(val);
  124. },
  125. },
  126. },
  127. };
  128. </script>
  129. <style lang="less" scoped>
  130. .formMain {
  131. .one {
  132. margin: 0 0 10px 0;
  133. }
  134. .two {
  135. border: 1px solid #ccc;
  136. padding: 10px;
  137. .btn {
  138. text-align: center;
  139. margin: 10px 0 0 0;
  140. }
  141. }
  142. }
  143. </style>