knowledgeForm.vue 3.6 KB

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