Vue中使用wangEditor富文本编辑器|图片上传(含后端代码)

embedded/2024/9/23 10:24:45/

一、效果

二、安装依赖

npm install wangeditor --save
npm install @wangeditor/editor-for-vue@next --save

三、使用

在src下common文件夹下创建wangEditor文件夹,并在其文件夹下创建index.vue文件

javascript"><template><div style="border: 1px solid #ccc; width: 100%"><Toolbarstyle="border-bottom: 1px solid #ccc":editor="editor":defaultConfig="toolbarConfig":mode="mode"/><Editorstyle="height: 500px; overflow-y: hidden"v-model="html":defaultConfig="editorConfig":mode="mode"@onCreated="handleCreated"@onChange="handleChange"/></div>
</template>
<script>
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import { DomEditor } from "@wangeditor/editor";
export default {components: {Editor,Toolbar,},props: {value: {type: String,default: "",},},watch: {value(val) {setTimeout(() => {this.html = val;}, 1000);},},data() {return {editor: null,html: "",mode: "default",editorConfig: {placeholder: "请输入产品信息...",backColor: "red", // 背景颜色MENU_CONF: {uploadImage: {customUpload: this.uploaadImg,},uploadVideo: {customUpload: this.uploaadVideo,},},},toolbarConfig: {},};},methods: {handleCreated(editor) {this.editor = Object.seal(editor); // 一定要用 Object.seal() ,否则会报错//   设置工具栏详情this.toolbarConfig = {excludeKeys: ["insertVideo","uploadVideo","group-video","fullScreen",],};},handleChange(content) {const toolbar = DomEditor.getToolbar(content);//   查看工具栏列表toolbar.getConfig().toolbarKeysthis.$emit("change", this.html);},uploaadImg(file, insertFn) {this.$emit("uploadImg", file, insertFn);},uploaadVideo(file, insertFn) {this.$emit("uploadVideo", file, insertFn);},},beforeDestroy() {const editor = this.editor;if (editor == null) return;editor.destroy(); // 销毁编辑器},
};
</script>
<style src="@wangeditor/editor/dist/css/style.css"></style>

在需要使用wangEditor的组件中编写如下信息:

javascript"><template><WangEditorv-model="ruleForm.reproduceStep"@change="richTextChangeData"@uploadImg="richTextUploadImg"></WangEditor>
</template><script>
import {uploadImg,
} from "@/apis/uploadImg";
import wangEditor from "@/components/wangEditor";export default {name: "product",components: { wangEditor },data() {return {ruleForm: {reproduceStep: "",},};},methods: {richTextChangeData(val) {// 获取最新的html数据this.form.productIntroduction = val;},setFormData() {this.ruleForm.reproduceStep = "<h1>h1</h1>";},async richTextUploadImg(file, insertFn) {// 处理入参const formData = new FormData();formData.append("file", file);await uploadImg(formData).then((res) => {insertFn(res.data.data.imgUrl); // 页面插入图片});},},
};
</script>

创建文件上传API

  • uploadImg.js文件
javascript">// 图片上传
export const uploadImg = (formData) => {return request.post("/upload/img", formData, {headers: {"Content-Type": "multipart/form-data"},});
};

需要自己编写后端代码,参考:

  • 控制层:
javascript">//    上传图片@PostMapping("/uploadimg")public Result uploadImg(@RequestParam("file") MultipartFile file) throws IOException {String originalFilename = file.getOriginalFilename();//获取图片原始文件名int index = originalFilename.lastIndexOf(".");String extention = originalFilename.substring(index);//获得图片后缀名  .jpgString fileName =  UUID.randomUUID().toString() + extention; //进行拼接fileName = fileName.replace("-",""); //将文件路径中的-替换掉String uploadQiniu = QiniuUtils.uploadQiniu(file.getBytes(), fileName, "imgUpload/");return Result.success("上传图片成功",uploadQiniu);}

七牛云存储方法

  • QiniuUtils.java
javascript">package xxx.xxx.xxx.utils;import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;public class QiniuUtils {//访问授权码public  static String accessKey = "";//秘密钥匙public  static String secretKey = "";//空间名称public  static String bucket = "";//外链域名public static String domain = "";//上传方式二:文件上传 通过上传文件的方式上传到存储空间public static String uploadQiniu(byte[] bytes, String fileName,String path){//构造一个带指定Zone对象的配置类Configuration cfg = new Configuration(Zone.zone0());//...其他参数参考类注释UploadManager uploadManager = new UploadManager(cfg);//默认不指定key的情况下,以文件内容的hash值作为文件名String key = path + fileName;Auth auth = Auth.create(accessKey, secretKey);String upToken = auth.uploadToken(bucket);try {Response response = uploadManager.put(bytes, key, upToken);//解析上传成功的结果DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);System.out.println(putRet.key);System.out.println(putRet.hash);//返回文件完整路径return domain+"/"+putRet.key;} catch (QiniuException ex) {Response r = ex.response;System.err.println(r.toString());try {System.err.println(r.bodyString());return "";} catch (QiniuException ex2) {//ignore}}return "";}//删除文件 参数:存储的图片文件名public static void deleteFileFromQiniu(String fileName,String path){//构造一个带指定Zone对象的配置类Configuration cfg = new Configuration(Zone.zone0());String key = path + fileName;Auth auth = Auth.create(accessKey, secretKey);BucketManager bucketManager = new BucketManager(auth, cfg);try {bucketManager.delete(bucket, key);} catch (QiniuException ex) {//如果遇到异常,说明删除失败System.err.println(ex.code());System.err.println(ex.response.toString());}}
}

 


http://www.ppmy.cn/embedded/88268.html

相关文章

基于opencv的答题卡识别

文章目录 一、背景需求二、处理步骤图片预处理检测到答题卡轮廓透视变换找每个圆圈的轮廓轮廓排序判断是否答题正确 一、背景需求 传统的手动评分方法耗时且容易出错&#xff0c;自动化评分可以可以显著提高评分过程的速度和准确性、减少人工成本。 答题卡图片处理效果如下&am…

达梦数据库系列—41.表连接方式

目录 连接方式 NEST LOOP&#xff08;嵌套循环连接&#xff09; HASH JOIN&#xff08;哈希连接&#xff09; MERGE JOIN&#xff08;排序归并连接&#xff09; 连接方式 创建测试表&#xff1a; create table tab1(c1 int,c2 int ,c3 int); create table tab2(c1 int,c…

C++ | Leetcode C++题解之第283题移动零

题目&#xff1a; 题解&#xff1a; class Solution { public:void moveZeroes(vector<int>& nums) {int n nums.size(), left 0, right 0;while (right < n) {if (nums[right]) {swap(nums[left], nums[right]);left;}right;}} };

环境如何搭建部署Nacos

这里我使用的是Centos7&#xff0c; Nacos 依赖 Java环境来运行。如果您是从代码开始构建并运行Nacos&#xff0c;还需要为此配置 Maven环境&#xff0c;请确保是在以下版本环境中安装使用 ## 1、下载安装JDK wget https://download.oracle.com/java/17/latest/jdk-17_linux-x6…

web前端开发一、VScode环境搭建

1、VScode安装live server插件&#xff0c;写完代码后&#xff0c;保存就会在浏览器自动更新&#xff0c;不需要再去浏览器点击刷新了 2、创建html文件 3、在文件中输入感叹号 &#xff01; 4、选择第一个&#xff0c;然后回车&#xff0c;就会自动输入html的标准程序 5、…

【数据结构】栈的实现

一、简述栈 1.栈的概念 栈&#xff1a;一种特殊的线性表&#xff0c;其只允许在固定的一端进行插入和删除元素操作。进行数据插入和删除操作的一端称为栈顶&#xff0c;另一端称为栈底。栈中的数据元素遵守后进先出LIFO&#xff08;Last In First Out&#xff09;的原则。压栈…

反爬虫限制:有哪些方法可以保护网络爬虫不被限制?

目前&#xff0c;爬虫已经成为互联网数据获取最主流的方式。但为了保证爬虫顺利采集数据&#xff0c;需要防范网站的反爬虫机制&#xff0c;降低IP被限制的风险&#xff0c;这样才能提高爬虫工作的效率。那么&#xff0c;如何防止网络爬虫被限制呢&#xff1f;下面介绍几种有效…

九大原则,轻松构建个人高效SOP

1、原则一、工作汇报SOP SCQA模型(升职加薪的关键!&#xff09; 清晰定义问题和提出解决方案 类别 关键词 解读 S - Situation 情景 陈述项目背景&#xff0c;目标&#xff0c;愿景 C - Complication 冲突 讲卡点&#xff0c;讲冲突 Q - Question 疑问-问题 这些冲…