From eb26adabb5de2f6367899a20a41307f5174db0c2 Mon Sep 17 00:00:00 2001 From: xujiang Date: Mon, 5 Oct 2020 00:12:56 +0800 Subject: [PATCH] =?UTF-8?q?:new:=20=E6=B7=BB=E5=8A=A0=E5=9B=BE=E7=89=87?= =?UTF-8?q?=E5=BA=93,=E6=94=AF=E6=8C=81=E7=94=A8=E6=88=B7=E5=9C=A8?= =?UTF-8?q?=E7=BA=BF=E9=80=89=E6=8B=A9=E5=9B=BE=E7=89=87=E7=B4=A0=E6=9D=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PanelComponents/Upload/index.less | 48 ++++ .../PanelComponents/Upload/index.tsx | 210 +++++++++++++++--- src/utils/tool.ts | 9 + 3 files changed, 232 insertions(+), 35 deletions(-) diff --git a/src/components/PanelComponents/Upload/index.less b/src/components/PanelComponents/Upload/index.less index fc82a15..6f448ab 100644 --- a/src/components/PanelComponents/Upload/index.less +++ b/src/components/PanelComponents/Upload/index.less @@ -7,3 +7,51 @@ margin-top: 8px; color: #666; } + +.avatarUploader { + display: inline-block; +} + +.wallBtn { + position: absolute; + left: 120px; + bottom: 56px; + display: inline-block; + color: #2f54eb; + cursor: pointer; + border-bottom: 1px solid #2f54eb; +} + +.imgBox { + display: flex; + flex-wrap: wrap; + max-height: 520px; + overflow: auto; + .imgItem { + position: relative; + margin-right: 16px; + margin-bottom: 16px; + width: 320px; + max-height: 220px; + overflow: hidden; + cursor: pointer; + img { + width: 100%; + } + &:hover, + &.seleted { + .iconBtn { + visibility: visible; + } + } + + .iconBtn { + position: absolute; + visibility: hidden; + top: 6px; + right: 10px; + font-size: 18px; + color: rgb(8, 156, 8); + } + } +} diff --git a/src/components/PanelComponents/Upload/index.tsx b/src/components/PanelComponents/Upload/index.tsx index 641f523..4809fe4 100644 --- a/src/components/PanelComponents/Upload/index.tsx +++ b/src/components/PanelComponents/Upload/index.tsx @@ -1,10 +1,22 @@ import React from 'react'; -import { Upload, Modal, message } from 'antd'; -import { PlusOutlined } from '@ant-design/icons'; +import { Upload, Modal, message, Tabs, Result } from 'antd'; +import { PlusOutlined, CheckCircleFilled } from '@ant-design/icons'; import ImgCrop from 'antd-img-crop'; -import styles from './index.less'; +import classnames from 'classnames'; import { UploadFile, UploadChangeParam, RcFile } from 'antd/lib/upload/interface'; -import { isDev } from '@/utils/tool'; +import { isDev, unParams, uuid } from '@/utils/tool'; +import req from '@/utils/req'; +import styles from './index.less'; + +const { TabPane } = Tabs; + +// 维护图片分类映射 +const wallCateName: any = { + photo: '照片', + bg: '背景', + chahua: '插画', +}; + function getBase64(file: File | Blob) { return new Promise((resolve, reject) => { const reader = new FileReader(); @@ -13,6 +25,7 @@ function getBase64(file: File | Blob) { reader.onerror = error => reject(error); }); } + interface PicturesWallType { fileList?: UploadFile[]; action?: string; @@ -20,7 +33,7 @@ interface PicturesWallType { withCredentials?: boolean; maxLen?: number; onChange?: (v: any) => void; - cropRate?: boolean; + cropRate?: number | boolean; isCrop?: boolean; } @@ -28,12 +41,21 @@ class PicturesWall extends React.Component { state = { previewVisible: false, previewImage: '', + wallModalVisible: false, previewTitle: '', + imgBed: { + photo: [], + bg: [], + chahua: [], + }, + curSelectedImg: '', fileList: this.props.fileList || [], }; handleCancel = () => this.setState({ previewVisible: false }); + handleModalCancel = () => this.setState({ wallModalVisible: false }); + handlePreview = async (file: UploadFile) => { if (!file.url && !file.preview) { file.preview = await getBase64(file.originFileObj!); @@ -46,6 +68,37 @@ class PicturesWall extends React.Component { }); }; + handleWallSelect = (url: string) => { + this.setState({ + wallModalVisible: true, + }); + }; + + handleImgSelected = (url: string) => { + this.setState({ + curSelectedImg: url, + }); + }; + + handleWallShow = () => { + this.setState({ + wallModalVisible: true, + }); + }; + + handleModalOk = () => { + const fileList = [ + { + uid: uuid(8, 16), + name: 'h5-dooring图片库', + status: 'done', + url: this.state.curSelectedImg, + }, + ]; + this.props.onChange && this.props.onChange(fileList); + this.setState({ fileList, wallModalVisible: false }); + }; + handleChange = ({ file, fileList }: UploadChangeParam>) => { this.setState({ fileList }); if (file.status === 'done') { @@ -74,14 +127,32 @@ class PicturesWall extends React.Component { return isJpgOrPng && isLt2M; }; + componentDidMount() { + req.get(`/visible/bed/get?tid=${unParams(location.search)!.tid}`).then(res => { + res && + this.setState({ + imgBed: res, + }); + }); + } + render() { - const { previewVisible, previewImage, fileList, previewTitle } = this.state; const { - // 配置自己的服务器地址 - action = isDev ? 'http://192.168.1.6:3000/api/xxx' : 'http://xxxx', + previewVisible, + previewImage, + fileList, + previewTitle, + wallModalVisible, + imgBed, + curSelectedImg, + } = this.state; + const { + action = isDev ? 'http://192.168.1.8:3000/api/v0/files/upload/free' : '你的服务器地址', headers, withCredentials = true, maxLen = 1, + cropRate = 375 / 158, + isCrop, } = this.props; const uploadButton = ( @@ -91,41 +162,110 @@ class PicturesWall extends React.Component { ); + const cates = Object.keys(imgBed); + return ( - - - {fileList.length >= maxLen ? null : uploadButton} - + <> + {isCrop ? ( + + + {fileList.length >= maxLen ? null : uploadButton} + + + ) : ( + + {fileList.length >= maxLen ? null : uploadButton} + + )} +
+ 从图片库中选择 +
- example + 预览图片 -
+ + + {cates.map((item, i) => { + return ( + +
+ {(imgBed as any)[item] && + (imgBed as any)[item].map((item: string, i: number) => { + return ( +
this.handleImgSelected(item)} + > + 趣谈前端-h5-dooring + + + +
+ ); + })} +
+
+ ); + })} + + + +
+
+ ); } } diff --git a/src/utils/tool.ts b/src/utils/tool.ts index 7bb0e78..1fcfd6d 100644 --- a/src/utils/tool.ts +++ b/src/utils/tool.ts @@ -81,3 +81,12 @@ export function useAnimation(state: boolean, delay: number) { }, [delay, display, state]); return [display, setDisplay]; } + +export function unParams(params = '?a=1&b=2&c=3') { + let obj: any = {}; + params && + params.replace(/((\w*)=([\.a-z0-9A-Z]*)?)?/g, (m, a, b, c): any => { + if (b || c) obj[b] = c; + }); + return obj; +}