Authored by 陈峰

多图片上传

1 // 懒加载VueHtml5Editor 1 // 懒加载VueHtml5Editor
2 import _ from 'lodash'; 2 import _ from 'lodash';
  3 +import MultiImage from './multi-image';
3 4
4 const component = (resolve) => { 5 const component = (resolve) => {
5 require.ensure([], () => { 6 require.ensure([], () => {
  7 + console.log(MultiImage);
  8 +
6 const VueHtml5Editor = require('vue-html5-editor'); 9 const VueHtml5Editor = require('vue-html5-editor');
7 const editorInstance = new VueHtml5Editor({ 10 const editorInstance = new VueHtml5Editor({
8 - hiddenModules: ['info'], 11 + hiddenModules: ['info', 'image'],
  12 + visibleModules: [
  13 + 'text',
  14 + 'color',
  15 + 'font',
  16 + 'align',
  17 + 'list',
  18 + 'link',
  19 + 'unlink',
  20 + 'tabulation',
  21 + 'multi-image',
  22 + 'hr',
  23 + 'eraser',
  24 + 'undo',
  25 + 'full-screen',
  26 + 'info',
  27 + ],
9 image: { 28 image: {
10 sizeLimit: 512 * 1024, 29 sizeLimit: 512 * 1024,
11 upload: { 30 upload: {
@@ -27,7 +46,13 @@ const component = (resolve) => { @@ -27,7 +46,13 @@ const component = (resolve) => {
27 } 46 }
28 }, 47 },
29 language: 'zh-cn', 48 language: 'zh-cn',
30 - modules: {} 49 + modules: [{
  50 + name: 'multi-image',
  51 + icon: 'fa fa-file-image-o',
  52 + i18n: '多图片上传',
  53 + show: true,
  54 + dashboard: MultiImage
  55 + }]
31 }); 56 });
32 57
33 resolve(editorInstance); 58 resolve(editorInstance);
  1 +import Editor from './editor';
  2 +
  3 +export default Editor;
  1 +<template>
  2 + <div>
  3 + <Modal
  4 + width="800"
  5 + v-model="showModal"
  6 + title="图片"
  7 + @on-ok="insertImage"
  8 + ok-text="保存"
  9 + class="multi-image">
  10 + <Row :gutter="16">
  11 + <Col span="10">
  12 + <Input v-model="file" placeholder="请输入地址" @on-enter="addFile"/>
  13 +
  14 + </Col>
  15 + <Col span="8">
  16 + <Button type="primary" @click="addFile">确定</Button>
  17 + </Col>
  18 + </Row>
  19 + <br />
  20 + <Upload
  21 + ref="upload"
  22 + multiple
  23 + type="drag"
  24 + :data="{bucket: 'goodsimg'}"
  25 + :max-size="2048"
  26 + :format="['jpg','jpeg','png']"
  27 + :show-upload-list="false"
  28 + :on-format-error="handleFormatError"
  29 + :on-exceeded-size="handleMaxSize"
  30 + :on-success="handleSuccess"
  31 + :on-error="handleError"
  32 + action="/Api/upload/image">
  33 + <div style="padding: 20px 0">
  34 + <Icon type="ios-cloud-upload" size="52" style="color: #3399ff"></Icon>
  35 + <p>点击或将文件拖拽到这里上传</p>
  36 + </div>
  37 + </Upload>
  38 + <div class="upload-list" v-for="item in fileList">
  39 + <template v-if="item.status === 'finished'">
  40 + <img :src="item.url">
  41 + <div class="upload-list-cover">
  42 + <Icon type="ios-eye-outline" @click.native="handleView(item)"></Icon>
  43 + <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
  44 + </div>
  45 + </template>
  46 + <template v-else>
  47 + <Progress v-if="item.showProgress" :percent="item.percentage" hide-info></Progress>
  48 + </template>
  49 + </div>
  50 + </Modal>
  51 + <Modal title="查看图片" v-model="visible">
  52 + <img :src="imgUrl" v-if="visible" style="width: 100%">
  53 + </Modal>
  54 + </div>
  55 +</template>
  56 +
  57 +<script>
  58 +import _ from 'lodash';
  59 +export default {
  60 + name: 'MultiImage',
  61 + created() {
  62 + this.$watch('$parent.dashboard', () => {
  63 + this.$parent.dashboard === 'dashboard-multi-image' && (this.showModal = true);
  64 + });
  65 + },
  66 + data() {
  67 + return {
  68 + showModal: true,
  69 + fileList: [],
  70 + imgUrl: '',
  71 + file: '',
  72 + visible: false
  73 + };
  74 + },
  75 + methods: {
  76 + handleView(item) {
  77 + this.imgUrl = item.url;
  78 + this.visible = true;
  79 + },
  80 + handleRemove(file) {
  81 + // 从 upload 实例删除数据
  82 + let fileList = this.$refs.upload.fileList;
  83 +
  84 + this.$refs.upload.fileList.splice(fileList.indexOf(file), 1);
  85 + },
  86 + handleFormatError(file) {
  87 + this.$Notice.warning({
  88 + title: '文件格式不正确',
  89 + desc: '文件 ' + file.name + ' 格式不正确,请上传 jpg 或 png 格式的图片。'
  90 + });
  91 + },
  92 + handleMaxSize(file) {
  93 + this.$Notice.warning({
  94 + title: '超出文件大小限制',
  95 + desc: '文件 ' + file.name + ' 太大,不能超过 2M。'
  96 + });
  97 + },
  98 + handleSuccess(response, file) {
  99 + let url = _.get(response, 'data.imagesList[0]');
  100 +
  101 + if (url) {
  102 + file.url = url;
  103 + }
  104 + },
  105 + handleError(error) {
  106 + this.$Notice.error(error);
  107 + },
  108 + insertImage() {
  109 + _.each(this.fileList, file => {
  110 + this.$parent.execCommand('insertImage', file.url);
  111 + });
  112 + },
  113 + addFile() {
  114 + if (this.file) {
  115 + this.fileList.push({
  116 + name: '',
  117 + status: 'finished',
  118 + url: this.file,
  119 + uid: ''
  120 + });
  121 + this.file = '';
  122 + }
  123 + }
  124 + },
  125 + watch: {
  126 + showModal(val) {
  127 + if (!val) {
  128 + this.fileList = this.$refs.upload.fileList = [];
  129 + this.$parent.dashboard = null;
  130 + }
  131 + }
  132 + },
  133 + mounted() {
  134 + this.fileList = this.$refs.upload.fileList;
  135 + }
  136 +};
  137 +</script>
  138 +<style lang="scss">
  139 +.multi-image {
  140 + .upload-list {
  141 + margin-top: 20px;
  142 + display: inline-block;
  143 + width: 60px;
  144 + height: 60px;
  145 + text-align: center;
  146 + line-height: 60px;
  147 + border: 1px solid transparent;
  148 + border-radius: 4px;
  149 + overflow: hidden;
  150 + background: #fff;
  151 + position: relative;
  152 + box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
  153 + margin-right: 4px;
  154 + }
  155 +
  156 + .upload-list img {
  157 + width: 100%;
  158 + height: 100%;
  159 + }
  160 +
  161 + .upload-list-cover {
  162 + display: none;
  163 + position: absolute;
  164 + top: 0;
  165 + bottom: 0;
  166 + left: 0;
  167 + right: 0;
  168 + background: rgba(0, 0, 0, 0.6);
  169 + }
  170 +
  171 + .upload-list:hover .upload-list-cover {
  172 + display: block;
  173 + }
  174 +
  175 + .upload-list-cover i {
  176 + color: #fff;
  177 + font-size: 20px;
  178 + cursor: pointer;
  179 + margin: 0 2px;
  180 + }
  181 +}
  182 +
  183 +</style>
@@ -24,14 +24,14 @@ @@ -24,14 +24,14 @@
24 <em class="upload-img-tip">尺寸要求150px*150px&nbsp;&nbsp;不大于500KB</em> 24 <em class="upload-img-tip">尺寸要求150px*150px&nbsp;&nbsp;不大于500KB</em>
25 </Form-item> 25 </Form-item>
26 <Form-item label="店铺简介:"> 26 <Form-item label="店铺简介:">
27 - <editor :content="shopData.shopIntro" @change="updateData" z-index="2"> 27 + <editor :content="shopData.shopIntro" @change="updateData" :z-index="2">
28 </editor> 28 </editor>
29 </Form-item> 29 </Form-item>
30 <Form-item label="品牌-供应商:"> 30 <Form-item label="品牌-供应商:">
31 <Table :columns="tableCols" :data="tableData"></Table> 31 <Table :columns="tableCols" :data="tableData"></Table>
32 </Form-item> 32 </Form-item>
33 <Form-item> 33 <Form-item>
34 - <Button type="primary" @click="submit">提交</Button> 34 + <Button type="primary" @click="submit">保存</Button>
35 </Form-item> 35 </Form-item>
36 </Form> 36 </Form>
37 </LayoutBody> 37 </LayoutBody>
@@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
5 "main": "app.js", 5 "main": "app.js",
6 "scripts": { 6 "scripts": {
7 "test": "echo \"Error: no test specified\" && exit 1", 7 "test": "echo \"Error: no test specified\" && exit 1",
8 - "dev": "nodemon server/app.js", 8 + "dev": "nodemon --watch server server/app.js",
9 "static": "cd app && node ./build/dev-server.js", 9 "static": "cd app && node ./build/dev-server.js",
10 "build": "cd app && node ./build/build.js", 10 "build": "cd app && node ./build/build.js",
11 "lint-js": "eslint --ext .js,.vue -c .eslintrc --cache app server", 11 "lint-js": "eslint --ext .js,.vue -c .eslintrc --cache app server",
@@ -41,7 +41,7 @@ app.use(cookieSession({ @@ -41,7 +41,7 @@ app.use(cookieSession({
41 name: 'yoho-shop', 41 name: 'yoho-shop',
42 secret: 'yoho!shop@manage', 42 secret: 'yoho!shop@manage',
43 cookie: { 43 cookie: {
44 - maxAge: 24 * 60 * 60 * 1000 44 + maxAge: 2 * 60 * 60 * 1000
45 } 45 }
46 })); 46 }));
47 47