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
	}

}
相关推荐
2401_857026232 小时前
Postman中Pre-request Script的编写与应用实践
测试工具·lua·postman
2401_857638032 小时前
精通 Postman 测试脚本:自动化接口测试的艺术
自动化·lua·postman
F2E_Zhangmo3 小时前
uniapp+nodejs实现小程序支付
小程序·uni-app
进击的小白菜4 小时前
ES|使用Postman更新ES内所有文档的指定字段
elasticsearch·postman·es
小青年一枚4 小时前
uniapp开发企业微信内部应用
uni-app·企业微信
星月前端7 小时前
uniapp中实现跳转链接到游览器(安卓-h5)
uni-app
HHH 9177 小时前
uniapp使用 movable-area movable-view 实现按双指中心位置缩放及拖拽功能
javascript·vue.js·uni-app
来之梦10 小时前
uniapp自定义富文本现实组件(支持查看和收起)
前端·javascript·uni-app
xxxxfdsax11 小时前
网址封装app-小猪APP分发平台:提升内测效率与用户体验的得力助手
uni-app·web app
彭世瑜12 小时前
微信小程序/uniapp:class和style不生效的问题
微信小程序·小程序·uni-app