index.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <template>
  2. <div class="app-container">
  3. <div class="title">公共内容<span>显示在所有商品详情</span></div>
  4. <el-form ref="mainForm" :model="mainForm" :rules="mainFormRules" label-width="80px" label-position="top">
  5. <el-form-item label="" prop="content">
  6. <el-upload
  7. ref="imageListUpload"
  8. class="avatar-uploader2"
  9. :action="baseURL + 'common/upload'"
  10. :headers="myHeaders"
  11. multiple
  12. name="file"
  13. :show-file-list="false"
  14. :on-success="uploadSuccess4"
  15. :on-error="uploadError4"
  16. :before-upload="beforeUpload4">
  17. </el-upload>
  18. <!--富文本编辑器组件-->
  19. <el-row v-loading="quillImgLoading">
  20. <quill-editor
  21. v-model="mainForm.content"
  22. ref="myQuillEditor"
  23. :options="editorOption">
  24. </quill-editor>
  25. </el-row>
  26. </el-form-item>
  27. </el-form>
  28. <div class="page-footer">
  29. <div class="footer" :class="classObj">
  30. <el-button type="primary" @click="submitForm" :loading="formLoading">{{ formLoading ? '提交中 ...' : '提 交' }}</el-button>
  31. <el-popconfirm
  32. title="确定关闭吗?"
  33. @confirm="goBack"
  34. style="margin-left: 10px;"
  35. >
  36. <el-button slot="reference">关 闭</el-button>
  37. </el-popconfirm>
  38. </div>
  39. </div>
  40. </div>
  41. </template>
  42. <script>
  43. import { getToken } from '@/utils/auth'
  44. import { getCommonModuleDetail, editCommonModule } from '@/api/goods'
  45. import { quillEditor } from 'vue-quill-editor'
  46. import 'quill/dist/quill.core.css'
  47. import 'quill/dist/quill.snow.css'
  48. import 'quill/dist/quill.bubble.css'
  49. // 工具栏配置
  50. const toolbarOptions = [
  51. ['bold', 'italic', 'underline', 'strike'],
  52. ['blockquote', 'code-block'],
  53. [{'header': 1}, {'header': 2}],
  54. [{'list': 'ordered'}, {'list': 'bullet'}],
  55. [{'script': 'sub'}, {'script': 'super'}],
  56. [{'indent': '-1'}, {'indent': '+1'}],
  57. [{'direction': 'rtl'}],
  58. [{'size': ['small', false, 'large', 'huge']}],
  59. [{'header': [1, 2, 3, 4, 5, 6, false]}],
  60. [{'color': []}, {'background': []}],
  61. [{'font': []}],
  62. [{'align': []}],
  63. ['link', 'image', 'video'],
  64. // ['clean']
  65. ]
  66. export default {
  67. components: {
  68. quillEditor
  69. },
  70. data() {
  71. return {
  72. baseURL: process.env.VUE_APP_BASE_API,
  73. myHeaders: {'x-token': getToken()},
  74. mainForm: {
  75. content: '',
  76. },
  77. mainFormRules: {
  78. // content: [
  79. // { required: true, message: '请输入公共内容', trigger: 'blur' }
  80. // ],
  81. },
  82. quillImgLoading: false, // 富文本上传图片loading
  83. editorOption: { // 富文本配置
  84. placeholder: '请输入公共内容',
  85. theme: 'snow',
  86. modules: {
  87. toolbar: {
  88. container: toolbarOptions,
  89. handlers: {
  90. 'image': function (value) {
  91. if (value) {
  92. document.querySelector('.avatar-uploader2 input').click()
  93. } else {
  94. this.quill.format('image', false);
  95. }
  96. }
  97. }
  98. }
  99. }
  100. },
  101. editorImages: [],
  102. formLoading: false,
  103. }
  104. },
  105. computed: {
  106. sidebar() {
  107. return this.$store.state.app.sidebar
  108. },
  109. classObj() {
  110. return {
  111. hideSidebar: !this.sidebar.opened,
  112. openSidebar: this.sidebar.opened
  113. }
  114. },
  115. },
  116. created() {
  117. this.getCommonModuleDetail();
  118. },
  119. methods: {
  120. // 获取详情
  121. getCommonModuleDetail() {
  122. getCommonModuleDetail().then(res => {
  123. this.mainForm = {
  124. content: res.data.content,
  125. }
  126. })
  127. },
  128. submitForm() {
  129. this.$refs.mainForm.validate((valid) => {
  130. if (valid) {
  131. this.formLoading = true;
  132. editCommonModule({
  133. content: this.mainForm.content,
  134. }).then(res => {
  135. this.formLoading = false;
  136. this.$successMsg('编辑成功');
  137. setTimeout(()=>{
  138. this.goBack();
  139. }, 1000)
  140. }).catch(err => {
  141. this.formLoading = false;
  142. })
  143. }
  144. })
  145. },
  146. goBack() {
  147. this.$router.go(-1);
  148. },
  149. // 富文本图片上传前
  150. beforeUpload4() {
  151. this.quillImgLoading = true;
  152. },
  153. // 富文本图片上传成功
  154. uploadSuccess4(res, file, fileList) {
  155. console.log(fileList);
  156. fileList.forEach(item => {
  157. if(this.editorImages.indexOf(item.response.data.url) < 0) {
  158. this.editorImages.push(item.response.data.url);
  159. }
  160. })
  161. this.showImage()
  162. this.quillImgLoading = false;
  163. },
  164. showImage() {
  165. console.log(this.editorImages);
  166. let quill = this.$refs.myQuillEditor.quill;
  167. this.editorImages.forEach(item => {
  168. // 获取光标所在位置
  169. let length = quill.getSelection().index;
  170. // 插入图片 res.info为服务器返回的图片地址
  171. console.log(item);
  172. quill.insertEmbed(length, 'image', item);
  173. // this.$refs.imageListUpload.clearFiles()
  174. // 调整光标到最后
  175. quill.setSelection(length + 1);
  176. })
  177. this.editorImages = [];
  178. this.$refs.imageListUpload.clearFiles();
  179. },
  180. // 富文本图片上传失败
  181. uploadError4() {
  182. this.quillImgLoading = false;
  183. this.$errorMsg('图片插入失败');
  184. },
  185. }
  186. }
  187. </script>
  188. <style lang="scss" scoped>
  189. .title {
  190. font-size: 16px;
  191. color: #333333;
  192. margin-bottom: 20px;
  193. span {
  194. font-size: 14px;
  195. color: #999999;
  196. margin-left: 10px;
  197. }
  198. }
  199. </style>