knowledgeForm.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <uni-section title="新增知识库" type="line">
  3. <view class="example">
  4. <!-- 自定义表单校验 -->
  5. <uni-forms ref="customForm" :rules="rules" :modelValue="customFormData" label-align="center">
  6. <uni-forms-item label="标题" required name="titleName">
  7. <uni-easyinput v-model="customFormData.titleName" placeholder="请输入标题" />
  8. </uni-forms-item>
  9. <uni-forms-item label="正文" required name="textDetails">
  10. <!-- <uni-easyinput v-model="customFormData.textDetails" placeholder="请输入正文" /> -->
  11. <mp-html :content="html" />
  12. </uni-forms-item>
  13. <uni-forms-item label="图片" required name="url">
  14. <upload :imgArr="imgUrlList" :fileSize="1" :limit="3" @updateImg="updateImg" />
  15. </uni-forms-item>
  16. <uni-forms-item label="是否热门" required name="integral">
  17. <switch v-model="customFormData.popular" />
  18. </uni-forms-item>
  19. <uni-forms-item label="付费积分" required name="integral">
  20. <uni-number-box v-model="customFormData.integral" />
  21. </uni-forms-item>
  22. </uni-forms>
  23. <button type="primary" @click="submit('customForm')">提交</button>
  24. </view>
  25. </uni-section>
  26. </template>
  27. <script>
  28. import {
  29. addKnowledge
  30. } from "@/api/knowledge/knowledge";
  31. import upload from '../../components/upload/index.vue'
  32. import mpHtml from '@/uni_modules/mp-html/components/mp-html/mp-html.vue'
  33. export default {
  34. components: {
  35. upload,
  36. mpHtml
  37. },
  38. data() {
  39. return {
  40. html: '<div>Hello World!</div>',
  41. // 基础表单数据
  42. baseFormData: {
  43. titleName: '',
  44. textDetails: '',
  45. integral: '',
  46. popular: '',
  47. url: []
  48. },
  49. // 校验规则
  50. rules: {
  51. titleName: {
  52. rules: [{
  53. required: true,
  54. errorMessage: '标题不能为空'
  55. }]
  56. },
  57. textDetails: {
  58. rules: [{
  59. required: true,
  60. errorMessage: '正文不能为空'
  61. }]
  62. }
  63. },
  64. imgUrlList: [],
  65. customFormData: {},
  66. }
  67. },
  68. onLoad() {},
  69. onReady() {
  70. // 设置自定义表单校验规则,必须在节点渲染完毕后执行
  71. this.$refs.customForm.setRules(this.rules)
  72. },
  73. created() {},
  74. methods: {
  75. submit(ref) {
  76. this.$refs[ref].validate().then(res => {
  77. if (res) {
  78. if (this.imgUrlList != null && this.imgUrlList != [] && this.imgUrlList.length > 1) {
  79. uni.showToast({
  80. title: '图片只能上传一张'
  81. })
  82. } else {
  83. this.customFormData.imgUrlList = this.imgUrlList.join(',')
  84. addKnowledge(this.customFormData).then(response => {
  85. this.$modal.msgSuccess('提交成功');
  86. this.customFormData = {
  87. ...this.baseFormData
  88. };
  89. this.imgUrlList = [];
  90. uni.navigateTo({
  91. url: '../knowledge/knowledge'
  92. });
  93. }).catch(err => {
  94. console.log('err', err);
  95. })
  96. }
  97. }
  98. })
  99. },
  100. updateImg(imgUrlList) {
  101. this.imgUrlList = imgUrlList;
  102. }
  103. }
  104. }
  105. </script>
  106. <style lang="scss">
  107. .example {
  108. padding: 15px;
  109. background-color: #fff;
  110. }
  111. .segmented-control {
  112. margin-bottom: 15px;
  113. }
  114. .button-group {
  115. margin-top: 15px;
  116. display: flex;
  117. justify-content: space-around;
  118. }
  119. .form-item {
  120. display: flex;
  121. align-items: center;
  122. }
  123. .button {
  124. display: flex;
  125. align-items: center;
  126. height: 35px;
  127. margin-left: 10px;
  128. }
  129. </style>