手册版本 V3.0
组件名称
graceEditor : /graceUI/组件目录/graceEditor
功能介绍
1. 支持类型 : 文本、居中标题、图片、引用、加粗、链接、分割线;
2. 提交时动态收集临时图片,循环完成图片上传工作;
3. 添加项目过程中项目数据已经记录,便于提交;
改进说明
您可以 graceEditer 为基础改进出更丰富、更适合您的项目的编辑器;
如何实现编辑某个文章
1. 获取要编辑文章的数据,利用组件的 setDefault 函数对组件赋初始值.
图片上传后端接口返回值格式
成功:
{status : 'ok', data : '上传后图片地址'}
失败:
{status : 'eror', data : '具体错误信息'}如何调试接口
写一个最简单的单图上传功能,调试接口返回值,保证接口无误再使用组件;
后端代码示例 php 版本
<?php
class uploadController extends grace{
public function index(){
$this->allowSize = 5;
if(!empty($_FILES['img'])){
//获取扩展名
$exename = $this->getExeName($_FILES['img']['name']);
//检查扩展名
if(!in_array($exename, array('png', 'gif', 'jpeg', 'jpg'))){$this->json('格式错误', 'error');}
//检查文件大小
if($_FILES['img']['size'] > $this->allowSize*1024*1024){$this->json('文件过大', 'error');}
//本地上传
$file = uniqid().'.'.$exename;
$imageSavePath = 'uploadimgs/'.$file;
if(move_uploaded_file($_FILES['img']['tmp_name'], $imageSavePath)){
$this->json('http://grace.hcoder.net/api/'.$imageSavePath);
}else{
$this->json('上传失败', 'error');
}
}
}
public function getExeName($file){
$pathinfo = pathinfo($file);
return strtolower($pathinfo['extension']);
}
}演示代码
<template>
<gracePage :customHeader="false">
<view slot="gBody">
<view class="grace-body grace-space-between grace-margin-top">
<view class=""></view>
<text class="grace-text grace-blue" @tap="sendArt">{{subText}}</text>
</view>
<view class="">
<graceEditor ref="graceEditor" @change="saveArticle"></graceEditor>
</view>
</view>
</gracePage>
</template>
<script>
import gracePage from "../../graceUI/components/gracePage.vue";
import graceEditor from "../../graceUI/components/graceEditor.vue";
export default {
data(){
return {
// 记录文章的数据
article : {title:'', contents : []},
// 发布按钮文本 可以利用它阻止重复提交
subText : '发表文章'
}
},
onLoad:function(){
setTimeout(()=>{
this.$refs.graceEditor.setDefault({
title : "默认标题",
contents : [
{type : 'txt', content : 'hi..'},
// 默认图片请设置 uploaded : ok 代表不需要上传
{type : 'img', content : 'https://staticimgs.oss-cn-beijing.aliyuncs.com/indexBanner01.png', uploaded : 'ok'}
]
});
}, 1000);
},
methods:{
sendArt : function () {
if(this.subText != '发表文章'){return ;}
this.subText = '数据提交中';
// 可以验证数据
// this.article.title 保存了文章标题
// this.article.contents 保存了文章项目[ 数组 可以使用 length 验证 ]
if(this.article.contents.length < 1){
this.subText = '发布文章';
uni.showToast({ title:"请填写文章内容", icon:"none" });
return ;
}
// 先完成图片上传
this.upLoadImg(0);
},
// 提交給api 保存文章数据
submit : function(){
// 准备好的文章数据
// 自行提交给api 即可
console.log(this.article);
this.subText = '发布成功';
},
// 动态保存文章信息
saveArticle : function (e) {
this.article = e;
},
// 收集数据并提交 包含图片上传
upLoadImg : function(step){
if(!step){step = 0;}
// 上传完毕
if(step > this.article.contents.length - 1){this.submit(); return ;}
// 是否为图片
if(this.article.contents[step].type != 'img'){this.upLoadImg(step+1); return ;}
// 图片是否已经上传或是默认值
if( this.article.contents[step].uploaded){
this.upLoadImg(step+1); return ;
}
uni.showLoading({title:"图片上传中"});
var uploadTask = uni.uploadFile({
url : 'http://您的图片上传服务器api地址',
filePath : this.article.contents[step].content,
fileType : 'image',
name : 'img',
success: (uploadFileRes) => {
uploadFileRes = JSON.parse(uploadFileRes.data);
//图片上传成功后返回服务器端路径,记录这个路径
if(uploadFileRes.status != 'ok'){
this.subText = '发布文章';
this.$refs.graceEditor.setError(step);
return ;
}else{
this.article.contents[step].content = uploadFileRes.data;
}
this.upLoadImg(step+1);
},
complete:function(){
uni.hideLoading();
},
fail : () => {
this.subText = '发布文章';
this.$refs.graceEditor.setError(step);
return ;
}
});
}
},
components:{
gracePage, graceEditor
}
}
</script>
<style>
</style>
<template>
<gracePage :customHeader="false">
<view slot="gBody">
<view class="grace-body grace-space-between grace-margin-top">
<view class=""></view>
<text class="grace-text grace-blue" @tap="sendArt">{{subText}}</text>
</view>
<view class="">
<graceEditor ref="graceEditor" @change="saveArticle"></graceEditor>
</view>
</view>
</gracePage>
</template>
<script>
import gracePage from "../../graceUI/weexComponents/gracePage.nvue";
import graceEditor from "../../graceUI/weexComponents/graceEditor.nvue";
export default {
data(){
return {
// 记录文章的数据
article : {title:'', contents : []},
// 发布按钮文本 可以利用它阻止重复提交
subText : '发表文章'
}
},
onLoad:function(){
setTimeout(()=>{
this.$refs.graceEditor.setDefault({
title : "默认标题",
contents : [
{type : 'txt', content : 'hi..'},
// 默认图片请设置 uploaded : ok 代表不需要上传
{type : 'img', content : 'https://staticimgs.oss-cn-beijing.aliyuncs.com/indexBanner01.png', uploaded : 'ok'}
]
});
}, 1000);
},
methods:{
sendArt : function () {
if(this.subText != '发表文章'){return ;}
this.subText = '数据提交中';
// 可以验证数据
// this.article.title 保存了文章标题
// this.article.contents 保存了文章项目[ 数组 可以使用 length 验证 ]
if(this.article.contents.length < 1){
this.subText = '发布文章';
uni.showToast({ title:"请填写文章内容", icon:"none" });
return ;
}
// 先完成图片上传
this.upLoadImg(0);
},
// 提交給api 保存文章数据
submit : function(){
// 准备好的文章数据
// 自行提交给api 即可
console.log(this.article);
this.subText = '发布成功';
},
// 动态保存文章信息
saveArticle : function (e) {
this.article = e;
},
// 收集数据并提交 包含图片上传
upLoadImg : function(step){
if(!step){step = 0;}
// 上传完毕
if(step > this.article.contents.length - 1){this.submit(); return ;}
// 是否为图片
if(this.article.contents[step].type != 'img'){this.upLoadImg(step+1); return ;}
// 图片是否已经上传或是默认值
if( this.article.contents[step].uploaded){
this.upLoadImg(step+1); return ;
}
uni.showLoading({title:"图片上传中"});
var uploadTask = uni.uploadFile({
url : 'http://您的图片上传服务器api地址',
filePath : this.article.contents[step].content,
fileType : 'image',
name : 'img',
success: (uploadFileRes) => {
uploadFileRes = JSON.parse(uploadFileRes.data);
//图片上传成功后返回服务器端路径,记录这个路径
if(uploadFileRes.status != 'ok'){
this.subText = '发布文章';
this.$refs.graceEditor.setError(step);
return ;
}else{
this.article.contents[step].content = uploadFileRes.data;
}
this.upLoadImg(step+1);
},
complete:function(){
uni.hideLoading();
},
fail : () => {
this.subText = '发布文章';
this.$refs.graceEditor.setError(step);
return ;
}
});
}
},
components:{
gracePage, graceEditor
}
}
</script>
<style>
.graceSelectMenuItem{width:200rpx; line-height:90rpx;}
</style>
.wxml
<gracePage customHeader="{{false}}">
<view slot="gBody">
<view class="grace-body grace-space-between grace-margin-top">
<view class=""></view>
<text class="grace-text grace-blue" bindtap="sendArt">{{subText}}</text>
</view>
<view class="">
<graceEditor id="graceEditor" bind:change="saveArticle"></graceEditor>
</view>
</view>
</gracePage>.js
Page({
data: {
// 记录文章的数据
article: { title: '', contents: [] },
// 发布按钮文本 可以利用它阻止重复提交
subText: '发表文章'
},
onLoad: function () {
setTimeout(() => {
this.selectComponent("#graceEditor").setDefault({
title: "默认标题",
contents: [
{ type: 'txt', content: 'hi..' },
// 默认图片请设置 uploaded : ok 代表不需要上传的默认图片
{ type: 'img', content: 'https://staticimgs.oss-cn-beijing.aliyuncs.com/indexBanner01.png', uploaded: 'ok' }
]
});
}, 1000);
},
sendArt: function () {
if (this.data.subText != '发表文章') { return; }
this.setData({ subText : '数据提交中'});
// 可以验证数据
// this.data.article.title 保存了文章标题
// this.data.article.contents 保存了文章项目[ 数组 可以使用 length 验证 ]
if (this.data.article.contents.length < 1) {
this.setData({ subText: '发布文章' });
wx.showToast({ title: "请填写文章内容", icon: "none" });
return;
}
// 先完成图片上传
this.upLoadImg(0);
},
// 提交給api 保存文章数据
submit: function () {
// 准备好的文章数据
// 自行提交给api 即可
console.log(this.data.article);
this.setData({ subText: '发布成功' });
},
// 动态保存文章信息
saveArticle: function (e) {
this.setData({ article : e.detail});
},
// 收集数据并提交 包含图片上传
upLoadImg: function (step) {
if (!step) { step = 0; }
// 上传完毕
if (step > this.data.article.contents.length - 1) { this.submit(); return; }
// 是否为图片
if (this.data.article.contents[step].type != 'img') { this.upLoadImg(step + 1); return; }
// 图片是否已经上传或是默认值
if (this.data.article.contents[step].uploaded) {
this.upLoadImg(step + 1); return;
}
wx.showLoading({ title: "图片上传中" });
var uploadTask = wx.uploadFile({
url: 'http://您的图片上传服务器api地址',
filePath: this.data.article.contents[step].content,
fileType: 'image',
name: 'img',
success: (uploadFileRes) => {
uploadFileRes = JSON.parse(uploadFileRes.data);
//图片上传成功后返回服务器端路径,记录这个路径
if (uploadFileRes.status != 'ok') {
this.setData({ subText: '发布文章'});
this.selectComponent("#graceEditor").setError(step);
return;
} else {
this.data.article.contents[step].content = uploadFileRes.data;
}
this.upLoadImg(step + 1);
},
complete: function () {
wx.hideLoading();
},
fail: () => {
this.setData({ subText: '发布文章' });
this.selectComponent("#graceEditor").setError(step);
return;
}
});
}
}).json
{
"usingComponents": {
"gracePage": "../../graceUI/components/gracePage",
"graceEditor": "../../graceUI/components/graceEditor"
}
}