Authored by 陈峰

多图片上传

// 懒加载VueHtml5Editor
import _ from 'lodash';
import MultiImage from './multi-image';
const component = (resolve) => {
require.ensure([], () => {
console.log(MultiImage);
const VueHtml5Editor = require('vue-html5-editor');
const editorInstance = new VueHtml5Editor({
hiddenModules: ['info'],
hiddenModules: ['info', 'image'],
visibleModules: [
'text',
'color',
'font',
'align',
'list',
'link',
'unlink',
'tabulation',
'multi-image',
'hr',
'eraser',
'undo',
'full-screen',
'info',
],
image: {
sizeLimit: 512 * 1024,
upload: {
... ... @@ -27,7 +46,13 @@ const component = (resolve) => {
}
},
language: 'zh-cn',
modules: {}
modules: [{
name: 'multi-image',
icon: 'fa fa-file-image-o',
i18n: '多图片上传',
show: true,
dashboard: MultiImage
}]
});
resolve(editorInstance);
... ...
import Editor from './editor';
export default Editor;
... ...
<template>
<div>
<Modal
width="800"
v-model="showModal"
title="图片"
@on-ok="insertImage"
ok-text="保存"
class="multi-image">
<Row :gutter="16">
<Col span="10">
<Input v-model="file" placeholder="请输入地址" @on-enter="addFile"/>
</Col>
<Col span="8">
<Button type="primary" @click="addFile">确定</Button>
</Col>
</Row>
<br />
<Upload
ref="upload"
multiple
type="drag"
:data="{bucket: 'goodsimg'}"
:max-size="2048"
:format="['jpg','jpeg','png']"
:show-upload-list="false"
:on-format-error="handleFormatError"
:on-exceeded-size="handleMaxSize"
:on-success="handleSuccess"
:on-error="handleError"
action="/Api/upload/image">
<div style="padding: 20px 0">
<Icon type="ios-cloud-upload" size="52" style="color: #3399ff"></Icon>
<p>点击或将文件拖拽到这里上传</p>
</div>
</Upload>
<div class="upload-list" v-for="item in fileList">
<template v-if="item.status === 'finished'">
<img :src="item.url">
<div class="upload-list-cover">
<Icon type="ios-eye-outline" @click.native="handleView(item)"></Icon>
<Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
</div>
</template>
<template v-else>
<Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
</template>
</div>
</Modal>
<Modal title="查看图片" v-model="visible">
<img :src="imgUrl" v-if="visible" style="width: 100%">
</Modal>
</div>
</template>
<script>
import _ from 'lodash';
export default {
name: 'MultiImage',
created() {
this.$watch('$parent.dashboard', () => {
this.$parent.dashboard === 'dashboard-multi-image' && (this.showModal = true);
});
},
data() {
return {
showModal: true,
fileList: [],
imgUrl: '',
file: '',
visible: false
};
},
methods: {
handleView(item) {
this.imgUrl = item.url;
this.visible = true;
},
handleRemove(file) {
// 从 upload 实例删除数据
let fileList = this.$refs.upload.fileList;
this.$refs.upload.fileList.splice(fileList.indexOf(file), 1);
},
handleFormatError(file) {
this.$Notice.warning({
title: '文件格式不正确',
desc: '文件 ' + file.name + ' 格式不正确,请上传 jpg 或 png 格式的图片。'
});
},
handleMaxSize(file) {
this.$Notice.warning({
title: '超出文件大小限制',
desc: '文件 ' + file.name + ' 太大,不能超过 2M。'
});
},
handleSuccess(response, file) {
let url = _.get(response, 'data.imagesList[0]');
if (url) {
file.url = url;
}
},
handleError(error) {
this.$Notice.error(error);
},
insertImage() {
_.each(this.fileList, file => {
this.$parent.execCommand('insertImage', file.url);
});
},
addFile() {
if (this.file) {
this.fileList.push({
name: '',
status: 'finished',
url: this.file,
uid: ''
});
this.file = '';
}
}
},
watch: {
showModal(val) {
if (!val) {
this.fileList = this.$refs.upload.fileList = [];
this.$parent.dashboard = null;
}
}
},
mounted() {
this.fileList = this.$refs.upload.fileList;
}
};
</script>
<style lang="scss">
.multi-image {
.upload-list {
margin-top: 20px;
display: inline-block;
width: 60px;
height: 60px;
text-align: center;
line-height: 60px;
border: 1px solid transparent;
border-radius: 4px;
overflow: hidden;
background: #fff;
position: relative;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
margin-right: 4px;
}
.upload-list img {
width: 100%;
height: 100%;
}
.upload-list-cover {
display: none;
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(0, 0, 0, 0.6);
}
.upload-list:hover .upload-list-cover {
display: block;
}
.upload-list-cover i {
color: #fff;
font-size: 20px;
cursor: pointer;
margin: 0 2px;
}
}
</style>
... ...
... ... @@ -24,14 +24,14 @@
<em class="upload-img-tip">尺寸要求150px*150px&nbsp;&nbsp;不大于500KB</em>
</Form-item>
<Form-item label="店铺简介:">
<editor :content="shopData.shopIntro" @change="updateData" z-index="2">
<editor :content="shopData.shopIntro" @change="updateData" :z-index="2">
</editor>
</Form-item>
<Form-item label="品牌-供应商:">
<Table :columns="tableCols" :data="tableData"></Table>
</Form-item>
<Form-item>
<Button type="primary" @click="submit">提交</Button>
<Button type="primary" @click="submit">保存</Button>
</Form-item>
</Form>
</LayoutBody>
... ...
... ... @@ -5,7 +5,7 @@
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon server/app.js",
"dev": "nodemon --watch server server/app.js",
"static": "cd app && node ./build/dev-server.js",
"build": "cd app && node ./build/build.js",
"lint-js": "eslint --ext .js,.vue -c .eslintrc --cache app server",
... ...
... ... @@ -41,7 +41,7 @@ app.use(cookieSession({
name: 'yoho-shop',
secret: 'yoho!shop@manage',
cookie: {
maxAge: 24 * 60 * 60 * 1000
maxAge: 2 * 60 * 60 * 1000
}
}));
... ...