Suggest.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <template>
  2. <el-dialog title="添加公告" :visible.sync="visible_" :close-on-click-modal="false" :before-close="close_before" append-to-body width="640px" top="5%">
  3. <el-form ref="dialogData" label-width="100px" class="form">
  4. <el-form-item label="公告标题" :rules="[{ required: true, message: '请输入公告标题', trigger: 'blur' }]">
  5. <el-input type="text" v-model="params.name" placeholder="请输入公告标题" maxlength="30" show-word-limit></el-input>
  6. </el-form-item>
  7. <div style="border: 1px solid #ccc;margin-bottom: 20px;" v-if="visible_">
  8. <!-- 工具栏 -->
  9. <Toolbar style="border-bottom: 1px solid #ccc" :editorId="editorId" :defaultConfig="toolbarConfig"/>
  10. <!-- 编辑器 -->
  11. <Editor style="height: 300px" :editorId="editorId" :defaultConfig="editorConfig" :defaultContent="getDefaultContent" @onChange="onChange" />
  12. </div>
  13. <el-form-item label="图片">
  14. <uploadOss
  15. :headers="Xtoken"
  16. :action="'https://' + 'integralsys.oss-cn-shenzhen.aliyuncs.com'"
  17. :show-file-list="true"
  18. :file-list="img_fileList"
  19. :on-success="handleFilesSuccess"
  20. :on-preview="onFilePreView"
  21. :before-upload="beforeUpload"
  22. accept="image/jpeg,image/png"
  23. :on-remove="onFileRemove"
  24. :limit="1"
  25. :multiple="true"
  26. >
  27. <el-button type="primary" size="small">点击上传</el-button>
  28. (最多选择1张)
  29. </uploadOss>
  30. </el-form-item>
  31. </el-form>
  32. <span slot="footer" class="dialog-footer">
  33. <el-button @click="close">取 消</el-button>
  34. <el-button type="primary" @click="confirm">确 定</el-button>
  35. </span>
  36. </el-dialog>
  37. </template>
  38. <script>
  39. import uploadOss from '@/components/upload';
  40. import { Editor, Toolbar, getEditor, removeEditor, createEditor } from '@wangeditor/editor-for-vue';
  41. import cloneDeep from 'lodash.clonedeep';
  42. import { getToken } from '@/api/auth';
  43. export default {
  44. name: 'Suggest',
  45. components: { uploadOss, Editor, Toolbar },
  46. props: {
  47. isAdd: {
  48. type: Boolean,
  49. default: true
  50. },
  51. visible: {
  52. // 是否显示组件
  53. type: Boolean,
  54. default: false
  55. },
  56. detaliData:{
  57. type: Object,
  58. default:()=>{
  59. return {}
  60. }
  61. }
  62. },
  63. data() {
  64. return {
  65. Xtoken: { 'X-Token': getToken() },
  66. visible_: false,
  67. // 建议
  68. img_fileList: [], // 图片附件
  69. save_loading: false,
  70. params: {
  71. name: '',
  72. html: '',
  73. content: '',
  74. file_list: [],
  75. // tag: ['福利']
  76. },
  77. // 富文本
  78. editorId: 'wangEditor-1', // 定义一个编辑器 id ,要求:全局唯一且不变。重要!!!
  79. defaultContent: [], // 编辑器的默认内容,只在初始化时使用
  80. latestContent: [], // 用于存储编辑器最新的内容,onChange 时修改
  81. toolbarConfig: {
  82. mode: 'simple',
  83. toolbarKeys: [
  84. 'headerSelect', // 分割线
  85. '|',
  86. 'bold',
  87. 'italic',
  88. 'underline',
  89. 'through',
  90. 'color',
  91. 'bgColor',
  92. 'indent',
  93. 'justifyLeft',
  94. 'justifyRight',
  95. 'justifyCenter',
  96. 'justifyJustify',
  97. 'bulletedList',
  98. 'numberedList',
  99. 'clearStyle'
  100. ]
  101. },
  102. editorConfig: {
  103. placeholder: '请输入内容...'
  104. }
  105. };
  106. },
  107. computed: {
  108. // <!-- 注意,这里使用 computed 的结果 -->
  109. getDefaultContent() {
  110. return cloneDeep(this.defaultContent); // 深拷贝,重要!!!
  111. }
  112. },
  113. watch: {
  114. visible(val) {
  115. if(!this.isAdd&&val){
  116. let detaliData=this.detaliData;
  117. if(detaliData.content.content){
  118. this.defaultContent=JSON.parse(detaliData.content.content);
  119. if(detaliData.file_list){
  120. this.img_fileList=[{name:'图片',url:detaliData.file_list}]
  121. this.params.file_list=[detaliData.file_list]
  122. }
  123. }
  124. this.params.name=detaliData.name
  125. }
  126. this.visible_ = JSON.parse(JSON.stringify(val));
  127. }
  128. },
  129. methods: {
  130. onChange(editor) {
  131. this.params.content = editor.children;
  132. this.params.html = editor.getHtml();
  133. },
  134. // 图片上传
  135. beforeUpload(file) {
  136. const isJPG = /^image\/(jpeg|png|jpg)$/.test(file.type);
  137. const isLt2M = file.size / 1024 / 1024 < 5;
  138. if (!isJPG) {
  139. this.$message.error('上传图片只能是 jpeg|png|jpg 格式!');
  140. }
  141. if (!isLt2M) {
  142. this.$message.error('上传图片大小不能超过 5MB!');
  143. }
  144. return isJPG && isLt2M;
  145. },
  146. onFilePreView(file) {
  147. window.open(file.url, '_blank');
  148. },
  149. onFileRemove(file, fileList) {
  150. this.img_fileList = fileList;
  151. let imgs = [];
  152. fileList.forEach((element, index) => {
  153. imgs.push(element.url);
  154. });
  155. this.params.file_list = imgs;
  156. },
  157. handleFilesSuccess(response, file, fileList) {
  158. let fileListData=fileList.filter(e=>{
  159. return e.url
  160. })
  161. this.img_fileList = fileListData;
  162. let imgs = [];
  163. fileListData.forEach((element, index) => {
  164. imgs.push(element.url);
  165. });
  166. this.params.file_list = imgs;
  167. },
  168. // 确定
  169. confirm() {
  170. if (!this.params.name) {
  171. this.$message.error('请填写标题');
  172. return false;
  173. }
  174. this.params.content=JSON.stringify(this.params.content)
  175. if(!this.isAdd){
  176. this.params.id=this.detaliData.id;
  177. this.$axios('post','/api/information/update',this.params).then(res => {
  178. this.$message.success('发布成功');
  179. this.$parent.noticeList();
  180. this.close();
  181. })
  182. }else{
  183. this.$axios('post','/api/information/create',this.params).then(res => {
  184. this.$message.success('发布成功');
  185. this.$parent.noticeList();
  186. this.close();
  187. })
  188. }
  189. },
  190. close_before(done) {
  191. this.close();
  192. done();
  193. },
  194. close() {
  195. this.img_fileList=[];
  196. this.defaultContent=[];
  197. this.params={
  198. name: '',
  199. html: '',
  200. content: '',
  201. file_list: [],
  202. };
  203. this.beforeDestroys();
  204. this.$emit('update:visible', false);
  205. },
  206. beforeDestroys() {
  207. const editor = getEditor(this.editorId);
  208. if (editor == null) return;
  209. editor.destroy(); // 组件销毁时,及时销毁编辑器 ,重要!!!
  210. removeEditor(this.editorId);
  211. }
  212. },
  213. };
  214. </script>
  215. <style src="@wangeditor/editor/dist/css/style.css"></style>
  216. <style scoped="scoped" lang="scss">
  217. /* 表格 */
  218. table {
  219. border-collapse: collapse;
  220. }
  221. table th,
  222. table td {
  223. border: 1px solid #ccc;
  224. min-width: 50px;
  225. height: 20px;
  226. text-align: left;
  227. }
  228. table th {
  229. background-color: #f1f1f1;
  230. text-align: center;
  231. }
  232. /* 代码块 */
  233. pre > code {
  234. display: block;
  235. border: 1px solid hsl(0, 0%, 91%);
  236. border-radius: 4px 4px;
  237. text-indent: 0;
  238. background-color: #fafafa;
  239. padding: 10px;
  240. font-size: 14px;
  241. }
  242. /* 引用 */
  243. blockquote {
  244. display: block;
  245. border-left: 8px solid #d0e5f2;
  246. padding: 10px 10px;
  247. margin: 10px 0;
  248. background-color: #f1f1f1;
  249. }
  250. /* 列表 */
  251. ul,
  252. ol {
  253. margin: 10px 0 10px 20px;
  254. }
  255. /* 分割线 */
  256. hr {
  257. display: block;
  258. width: 90%;
  259. margin: 20px auto;
  260. border: 0;
  261. height: 1px;
  262. background-color: #ccc;
  263. }
  264. </style>