uniapp(uncloud) 使用生态开发接口详情4(wangeditor 富文本, 云对象, postman 网络请求)

wangeditor 官网: https://www.wangeditor.com/v4/pages/01-开始使用/01-基本使用.html

这里用vue2版本,用wangeditor 4

终端命令: npm i wangeditor --save

  1. 开始使用
    在项目pages => sy_news => add.vue 页面中
javascript 复制代码
<template>
//...
	<uni-forms-item name="content" label="文章内容" required>
		<div id="div1"></div>
	</uni-forms-item>
// ...
</template>
<script>
import E from 'wangeditor'
let editor = null
// ...
onReady() {
	this.onWangEdit()
},
methods:{
	onWangEdit() {
		editor = new E("#div1")
		editor.config.zIndex = 0
		// 失焦时触发的回调函数
		editor.config.onblur = (newHtml) => {
	
			this.formData.content = newHtml
		}
		// 将图片保存本地服务器
		editor.config.customUploadImg = function(resultFiles, insertImgFn) {
			resultFiles.forEach(item => {
				let path = URL.createObjectURL(item)
				let name = item.name
				uniCloud.uploadFile({
					filePath: path,
					cloudPath: name
				}).then(res => {
					console.log("res", res);
					insertImgFn(res.fileID)
				})
			})
		}
		editor.create()
	},
	// 提交表格
	submitForm(value) {
		// 修复(鼠标在富文本内,直接提交)
		value.content = editor.txt.html();
		//...
	}	
	// ...
	}
	// ...
</script>
  1. 运行项目,浏览器中刷新页面,新增页面, 内容变成我们需要的富文本了,
  2. 修改(edit)界面同样的代码, 不过不同是
javascript 复制代码
<uni-forms-item name="content" label="文章内容" required>
	<div id="div1">
		<div v-html="formData.content"></div>
	</div>
</uni-forms-item>
  1. 上面都是添加数据,接下来进入写接口了,
    项目 => uniCloud-aliyun => 新建函数或者云对象
javascript 复制代码
// index.obj.js
const db = uniCloud.database()
module.exports = {
	_before: function() { // 通用预处理器

	},

	async getList() {
		const res = await db.collection("sy_news").get()
		let result = {
			errCode: 0,
			errMsg: "查询成功",
			data: res.data
		}
		return result
	},

}

4.1 点击demoObj 目录, 右键, 运行本地云对象, 目录下面多一个demoObj.param.js 文件,

javascript 复制代码
getList()

4.2 保存一下, 点击demoObj 目录, 右键, 运行本地云对象, 终端打印的

  1. 如果带参数怎么弄?
javascript 复制代码
// index.obj.js
const db = uniCloud.database()
module.exports = {
	_before: function() { // 通用预处理器
		this.params = this.getParams()[0]
		this.startTime = Date.now()
	},
	async getList() {
		const res = await db.collection("sy_news").get()
		let result = {
			errCode: 0,
			errMsg: "查询成功",
			data: res.data
		}
		return result
	},
	
	async get() {
		console.log('this.params', this.params);
		let {
			num
		} = this.params
		const res = await db.collection("sy_product_nav").limit(num).get()
		let result = {
			errCode: 0,
			errMsg: "查询成功",
			data: res.data
		}
		return result
	},

	_after(error, result) {
		if (error) {
			throw error
		}
		result.timeCode = Date.now() - this.startTime

		return result
	}

}
javascript 复制代码
// demoObj.param.js
get({
	num: 2
})

在运行ok, 这都是本地运行的,

  1. 接下来将 demoObj 右键 , 上传部署, 用postman请求数据

6.1 打开 uniCloud Web 控制台

6.2 云函数/云对象 => 函数/对象列表 (找到demoObj, 点击详情)

6.3 点击编辑, 输入 /demoObj

6.4 确定, 在点击(复制路径),到postman里面,改为post请求,

  1. 如在postman 里面带参数, uniCloud 中 云对象 一个 getHttpInfo 的API
javascript 复制代码
const db = uniCloud.database()
module.exports = {
	_before: function() { // 通用预处理器
		this.params = JSON.parse(this.getHttpInfo().body)
		// this.httpInfo = JSON.parse(this.getHttpInfo().body)
		this.startTime = Date.now()
	},

	async getList() {
		const res = await db.collection("sy_news").get()
		let result = {
			errCode: 0,
			errMsg: "查询成功",
			data: res.data
		}
		return result
	},


	async get() {

		let {
			num
		} = this.params
		const res = await db.collection("sy_product_nav").limit(num).get()
		let result = {
			errCode: 0,
			errMsg: "查询成功",
			data: res.data
		}
		return result
	},

	_after(error, result) {
		if (error) {
			throw error
		}
		result.timeCode = Date.now() - this.startTime

		return result
	}

}
相关推荐
Jinkxs2 小时前
告别“测试滞后”:AI实时测试工具在敏捷开发中的落地经验
人工智能·测试工具·敏捷流程
小白_ysf2 小时前
uniapp 开发微信小程序,获取经纬度并且转化详细地址(单独封装版本)
微信小程序·uni-app
iOS阿玮5 小时前
三年期已满,你的产品不再更新将于90天后下架。
uni-app·app·apple
秃了也弱了。18 小时前
WireShark:非常好用的网络抓包工具
网络·测试工具·wireshark
敲上瘾21 小时前
Linux系统cgroups资源精细化控制基础
linux·测试工具·docker·压力测试·cgroups
bug总结1 天前
深入理解 uni-app 的 uni.createSelectorQuery()
uni-app
真上帝的左手1 天前
25. 移动端-uni-app
uni-app
编程猪猪侠1 天前
基于Uni-app+vue3实现微信小程序地图固定中心点范围内拖拽选择位置功能(分步骤详解)
uni-app
天才测试猿1 天前
常见的Jmeter压测问题
自动化测试·软件测试·python·测试工具·jmeter·职场和发展·压力测试
小徐_23331 天前
Trae 辅助下的 uni-app 跨端小程序工程化开发实践分享
前端·uni-app·trae