textForm.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <template>
  2. <div class="app-container">
  3. <el-page-header :content="isEdit?'编辑文本素材':'添加文本素材'" @back="goBack" />
  4. <el-card class="form-container" shadow="never">
  5. <el-form ref="weChatTextFrom" :model="text" :rules="rules" label-width="150px">
  6. <el-form-item label="标题:" prop="title">
  7. <el-input v-model="text.title" />
  8. </el-form-item>
  9. <el-form-item label="内容:" prop="content">
  10. <el-input v-model="text.content" type="textarea" />
  11. </el-form-item>
  12. <el-form-item>
  13. <el-button type="primary" @click="onSubmit('weChatTextFrom')">提交</el-button>
  14. <el-button v-if="!isEdit" type="info" @click="resetForm('weChatTextFrom')">重置</el-button>
  15. <el-button @click="goBack">返回</el-button>
  16. </el-form-item>
  17. </el-form>
  18. </el-card>
  19. </div>
  20. </template>
  21. <script>
  22. import { getText, updateText, createText } from '@/api/wechat/text'
  23. const defaultText = {
  24. id: '',
  25. title: '',
  26. content: '',
  27. type: ''
  28. }
  29. export default {
  30. name: 'WeChatTextForm',
  31. props: {
  32. // type 类型: 1采美,2呵呵商城
  33. type: {
  34. type: Number,
  35. default: 0
  36. }
  37. },
  38. data() {
  39. return {
  40. rules: {
  41. title: [{ required: true, message: '标题不能为空', trigger: 'blur' }]
  42. },
  43. text: Object.assign({}, defaultText),
  44. isEdit: false
  45. }
  46. },
  47. watch: {
  48. $route(route) {
  49. this.getFormData()
  50. }
  51. },
  52. created() {
  53. this.getFormData()
  54. },
  55. methods: {
  56. goBack() {
  57. // 调用全局挂载的方法,关闭当前标签页
  58. this.$store.dispatch('tagsView/delView', this.$route)
  59. // 返回上一步路由,返回上一个标签页
  60. this.$router.go(-1)
  61. },
  62. getFormData() {
  63. if (this.$route.query.id) {
  64. this.text.id = this.$route.query.id
  65. this.isEdit = true
  66. getText(this.text.id).then(response => {
  67. this.text.title = response.data.title
  68. this.text.content = response.data.content
  69. })
  70. } else {
  71. this.text.id = ''
  72. this.isEdit = false
  73. this.text = Object.assign({}, defaultText)
  74. }
  75. this.text.type = this.type
  76. },
  77. resetForm(formName) {
  78. this.$refs[formName].resetFields()
  79. this.text = Object.assign({}, defaultText)
  80. this.getFormData()
  81. },
  82. onSubmit(formName) {
  83. this.$refs[formName].validate(valid => {
  84. console.log(this.text)
  85. if (valid) {
  86. this.$confirm('是否提交数据', '提示', {
  87. confirmButtonText: '确定',
  88. cancelButtonText: '取消',
  89. type: 'warning'
  90. }).then(() => {
  91. const self = this
  92. if (this.isEdit) {
  93. updateText(this.$route.query.id, this.text).then(response => {
  94. this.$message({
  95. message: '修改成功',
  96. type: 'success',
  97. duration: 1000
  98. })
  99. self.goBack()
  100. })
  101. } else {
  102. createText(this.text).then(response => {
  103. this.$refs[formName].resetFields()
  104. this.resetForm(formName)
  105. this.$message({
  106. message: '提交成功',
  107. type: 'success',
  108. duration: 1000
  109. })
  110. self.goBack()
  111. })
  112. }
  113. })
  114. } else {
  115. this.$message({
  116. message: '验证失败',
  117. type: 'error',
  118. duration: 1000
  119. })
  120. return false
  121. }
  122. })
  123. }
  124. }
  125. }
  126. </script>
  127. <style scoped>
  128. </style>