about.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <div class="container add_edit">
  3. <el-form :model="form" ref="ruleForm" label-width="50px">
  4. <el-form-item label="类型">
  5. <el-radio-group v-model="form.type">
  6. <el-radio label="网页">网页</el-radio>
  7. <el-radio label="内容">内容</el-radio>
  8. </el-radio-group>
  9. </el-form-item>
  10. <el-form-item label="网页" v-show="form.type == '网页'"><el-input v-model="form.url"></el-input></el-form-item>
  11. <el-form-item label="内容" v-show="form.type == '内容'">
  12. <div id="wangeditor" class="editor">
  13. <div ref="editorElem" style="text-align:left;"></div>
  14. </div>
  15. </el-form-item>
  16. <el-form-item><el-button class="confirm_btn" @click="submitForm('ruleForm')">提 交</el-button></el-form-item>
  17. </el-form>
  18. </div>
  19. </template>
  20. <script>
  21. import E from 'wangeditor';
  22. const __name = 'usemall-app-about';
  23. export default {
  24. data() {
  25. return {
  26. dataId: '',
  27. form: {
  28. type: '网页',
  29. url: '',
  30. content: '',
  31. state: '启用'
  32. },
  33. editor: null
  34. };
  35. },
  36. props: ['catchData'],
  37. methods: {
  38. async loadData() {
  39. await this.$db[__name].tofirst().then(res => {
  40. if (res.code == 200) {
  41. for (let item in this.form) {
  42. this.form[item] = res.datas[item];
  43. }
  44. this.dataId = res.datas._id;
  45. if (res.datas && res.datas.content) {
  46. this.editor.txt.html(res.datas.content);
  47. }
  48. }
  49. });
  50. },
  51. submitForm(formName) {
  52. this.$refs[formName].validate((valid, obj) => {
  53. // 默认获取第一个未验证 form 属性名
  54. this.$api.set_unvalidated_form_focus(this, obj);
  55. if (valid) {
  56. if (!this.dataId) {
  57. this.$db[__name].add(this.form).then(res => {
  58. if (res.code == 200) {
  59. this.$message({
  60. message: '操作成功',
  61. type: 'success'
  62. });
  63. this.loadData();
  64. }
  65. });
  66. } else {
  67. this.$db[__name].update(this.dataId, this.form).then(res => {
  68. if (res.code == 200) {
  69. this.$message({
  70. message: '操作成功',
  71. type: 'success'
  72. });
  73. this.loadData();
  74. }
  75. });
  76. }
  77. }
  78. });
  79. }
  80. },
  81. created() {
  82. this.loadData();
  83. },
  84. mounted() {
  85. let $this = this;
  86. this.editor = new E(this.$refs.editorElem);
  87. // 编辑器的事件,每次改变会获取其html内容
  88. this.editor.customConfig.onchange = html => {
  89. this.form.content = html;
  90. };
  91. // 将图片大小限制为 2M
  92. this.editor.customConfig.uploadImgMaxSize = 2 * 1024 * 1024;
  93. // 限制一次最多上传 10张图片
  94. this.editor.customConfig.uploadImgMaxLength = 10;
  95. this.editor.customConfig.customAlert = function(info) {
  96. // info 是需要提示的内容
  97. alert('自定义提示:' + info);
  98. };
  99. this.editor.customConfig.customUploadImg = function(files, insert) {
  100. // files 是 input 中选中的文件列表
  101. // insert 是获取图片 url 后,插入到编辑器的方法
  102. for (var i = 0; i < files.length; i++) {
  103. uniCloud.uploadFile({
  104. filePath: window.URL.createObjectURL(files[i]),
  105. cloudPath: files[i].name,
  106. fail(err) {},
  107. success(res) {
  108. // 上传代码返回结果之后,将图片插入到编辑器中
  109. insert(res.fileID);
  110. }
  111. });
  112. }
  113. };
  114. this.editor.customConfig.menus = [
  115. // 菜单配置
  116. 'head', // 标题
  117. 'bold', // 粗体
  118. 'fontSize', // 字号
  119. 'fontName', // 字体
  120. 'italic', // 斜体
  121. 'underline', // 下划线
  122. 'strikeThrough', // 删除线
  123. 'foreColor', // 文字颜色
  124. 'backColor', // 背景颜色
  125. 'link', // 插入链接
  126. 'list', // 列表
  127. 'justify', // 对齐方式
  128. 'quote', // 引用
  129. 'emoticon', // 表情
  130. 'image', // 插入图片
  131. 'table', // 表格
  132. 'code', // 插入代码
  133. 'undo', // 撤销
  134. 'redo' // 重复
  135. ];
  136. this.editor.create(); // 创建富文本实例
  137. }
  138. };
  139. </script>
  140. <style>
  141. </style>