EditNew.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <el-form ref="form" :model="form" label-width="120px">
  3. <el-form-item label="新闻名称">
  4. <el-input v-model="form.name" size="mini"></el-input>
  5. </el-form-item>
  6. <el-form-item label="新闻标题">
  7. <el-input v-model="form.title" size="mini"></el-input>
  8. </el-form-item>
  9. <el-form-item label="发布时间">
  10. <el-input v-model="form.time" size="mini"></el-input>
  11. </el-form-item>
  12. <el-form-item label="新闻描述">
  13. <el-input
  14. v-model="form.describe"
  15. size="mini"
  16. type="textarea"
  17. :autosize="{ minRows: 2, maxRows: 4}"
  18. ></el-input>
  19. </el-form-item>
  20. <el-form-item label="顺序号">
  21. <el-input v-model="form.sort" type="number" size="mini"></el-input>
  22. </el-form-item>
  23. <el-form-item label="阅读数">
  24. <el-input v-model="form.wacth" type="number" size="mini"></el-input>
  25. </el-form-item>
  26. <el-form-item label="新闻图片">
  27. <el-upload
  28. class="avatar-uploader"
  29. :action="uploadAction"
  30. :show-file-list="false"
  31. :on-success="onUploadSuccess"
  32. :before-upload="onBeforeUpload"
  33. >
  34. <img v-if="form.icon" :src="form.icon" class="avatar" />
  35. <i v-else class="el-icon-plus avatar-uploader-icon"></i>
  36. </el-upload>
  37. <p class="zy">注意:图标比例一定要1:1</p>
  38. </el-form-item>
  39. <el-form-item label="新闻内容">
  40. <div id="bar" class="bar"></div>
  41. <div id="editor" class="editor"></div>
  42. </el-form-item>
  43. <el-form-item>
  44. <el-button type="primary" @click="save" size="mini">保存</el-button>
  45. <el-button @click="goBack" size="mini">取消</el-button>
  46. </el-form-item>
  47. </el-form>
  48. </template>
  49. <script>
  50. var E = require("wangeditor");
  51. export default {
  52. data() {
  53. return {
  54. uploadAction: process.env.SERVER_PATH + "upload",
  55. form: {
  56. id: 0,
  57. name: "",
  58. title: "",
  59. time: "",
  60. describe: "",
  61. sort: 0,
  62. wacth: 0,
  63. icon: "",
  64. status: false,
  65. recommend: false
  66. },
  67. html: "",
  68. editor: null
  69. };
  70. },
  71. watch: {
  72. html(newVal) {
  73. if (this.editor) {
  74. this.editor.txt.html(newVal);
  75. }
  76. }
  77. },
  78. methods: {
  79. onBeforeUpload: function(file) {
  80. const isJPG = file.type === "image/jpeg";
  81. const isPNG = file.type === "image/png";
  82. if (!isJPG && !isPNG) {
  83. this.$message.error("上传新闻图片只能是 JPG/PNG 格式!");
  84. return false;
  85. } else {
  86. return true;
  87. }
  88. },
  89. onUploadSuccess: function(res, file) {
  90. if (res.code != 0) {
  91. this.$message.error(
  92. "上传图片失败 原因:" + res.msg + "(" + res.code + ")"
  93. );
  94. return;
  95. } else {
  96. this.form.icon = res.obj;
  97. }
  98. },
  99. save: function() {
  100. let base = {
  101. id: this.form.id,
  102. name: this.form.name,
  103. title: this.form.title,
  104. icon: this.form.icon,
  105. time: this.form.time,
  106. describe: this.form.describe,
  107. sort: this.form.sort,
  108. wacth: this.form.wacth,
  109. html: this.html
  110. };
  111. let that = this;
  112. this.$http.editNewBase(base, this).then(data => {
  113. if (data.code === 0) {
  114. this.$message({
  115. message: "操作成功",
  116. type: "success",
  117. duration: 1000,
  118. onClose: function() {
  119. that.$router.push("/newsManager");
  120. }
  121. });
  122. }
  123. });
  124. },
  125. goBack: function() {
  126. this.$router.back();
  127. }
  128. },
  129. mounted: function() {
  130. var id = this.$route.query.id;
  131. var that = this;
  132. this.form.id = id;
  133. var editor = new E("#bar", "#editor");
  134. editor.customConfig.uploadImgShowBase64 = true;
  135. //editor.customConfig.uploadImgServer = process.env.SERVER_PATH + 'editupload'
  136. //editor.customConfig.uploadFileName = 'file'
  137. editor.customConfig.onchange = function(html) {
  138. that.html = html;
  139. };
  140. editor.customConfig.colors = [
  141. '#00000000',
  142. '#000000',
  143. '#eeece0',
  144. '#1c487f',
  145. '#4d80bf',
  146. '#c24f4a',
  147. '#8baa4a',
  148. '#7b5ba1',
  149. '#46acc8',
  150. '#f9963b',
  151. '#ffffff'
  152. ];
  153. editor.create();
  154. this.editor = editor;
  155. console.log(id);
  156. if (id === 0) {
  157. return;
  158. }
  159. this.$http.getOneNew({ id: id }, this).then(res => {
  160. if (res.code === 0) {
  161. var base = res.obj;
  162. this.form.name = base.name;
  163. this.form.title = base.title;
  164. this.form.icon = base.icon;
  165. this.form.time = base.time;
  166. this.form.describe = base.describe;
  167. this.form.sort = base.sort;
  168. this.form.wacth = base.wacth;
  169. this.html = base.html;
  170. }
  171. });
  172. }
  173. };
  174. </script>
  175. <style scoped>
  176. .w-e-text-container,
  177. .w-e-toolbar {
  178. border: 1px solid #dcdfe6;
  179. }
  180. .w-e-toolbar {
  181. border-bottom: none;
  182. }
  183. .avatar-uploader .el-upload {
  184. border: 1px dashed #d9d9d9;
  185. border-radius: 6px;
  186. cursor: pointer;
  187. position: relative;
  188. overflow: hidden;
  189. width: 178px;
  190. }
  191. .avatar-uploader .el-upload:hover {
  192. border-color: #409eff;
  193. }
  194. .avatar-uploader img {
  195. height: 100%;
  196. width: 100%;
  197. }
  198. .el-form-item {
  199. margin-bottom: 0px;
  200. }
  201. .avatar {
  202. width: 178px;
  203. height: 178px;
  204. display: block;
  205. }
  206. .zy {
  207. color: red;
  208. }
  209. </style>