123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <template>
- <view class="tm-upload-img">
- <view class="row" @click="uploadPicture">
- <view class="label-view">
- <text>{{ label }}</text>
- </view>
- <text class="placeholder" v-show="filePaths.length === 0">点击上传图片</text>
- <image class="img-icon" src="/static/img-icon.png"></image>
- </view>
- <!-- 预览图片 -->
- <view class="img-preview">
- <view class="img-item" v-for="(src, i) in filePaths" :key="i">
- <image class="imgItem" :src="src" @click="previewHandle(i)"></image>
- <image class="del-close" src="/static/del-close.png" v-if="!disabled" @click="delImg(i)" />
- </view>
- </view>
- </view>
- </template>
- <script>
- /**
- * 图片上传(支持单张图片上传 和 多张图片上传)
- * 芦荟
- * 2021.2.3
- * props:属性说明看tm-radio-gruop.vue
- */
- import uploadImage from "../../utils/uploadImg.js";
- // #ifdef APP
- const pictureModule = uni.requireNativePlugin("Wlake-PictureView")
- const modal = uni.requireNativePlugin('modal');
- // #endif
-
- import {URL} from '../../utils/requestUrl.js';
- export default {
- props: {
- // 上传的图片路径列表
- filePaths: {
- type: Array,
- default: () => {
- return []
- }
- },
- // 是否多选 (默认单选)
- isMultiple: {
- type: Boolean,
- default: false
- },
- // 是否禁用
- disabled: {
- type: Boolean,
- default: false
- },
- label: {
- type: String,
- default: '上传图片'
- },
- // 下标(多个动态的图片上传组件, 遍历的下标)
- pickIndex: {
- type: Number,
- default: 0
- }
- },
- mounted() {
- },
- methods: {
- previewHandle(index) {
- // #ifdef APP
- console.log(this.filePaths[0],index);
- const picList = this.filePaths;
- pictureModule.PictureViewerMain({
- 'listPic': picList,
- 'position':index, // 0 开始算 最大值为 listPic 数组数量 减一
- },
- (ret) => {
- modal.toast({
- message: ret,
- duration: 1.5
- });
- });
- // #endif
- },
- // 上传图片
- uploadPicture() {
- if (this.disabled) return;
- const URLParms = URL.split('/');
- const IP = URLParms[0];
- uni.chooseImage({
- count: this.isMultiple ? 9 : 1, // 是否多选
- sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
- // sourceType: ['camera'], //从相册选择,默认时可以选择从相机和相册中选取
- success: res1 => {
- uploadImage(res1).then(fileList => {
- // const _fileList = fileList.map(t=>`http://${IP}${t}`); //横店特殊处理
- const _fileList = fileList;
-
- if (this.isMultiple) {
- this.$emit('changeFilePaths',{
- files:[...this.filePaths, ..._fileList],
- index:this.pickIndex
- });
- } else {
- this.$emit('changeFilePaths',{
- files:_fileList,
- index:this.pickIndex
- });
- }
- })
- },
- error: e => {
- console.log('选择图片失败', e);
- }
- });
- },
- // 删除图片
- delImg(i) {
- const currentIndex = this.pickIndex;
- this.$emit('changeFilePaths', {
- files: this.filePaths.filter((item, index) => index != i),
- index: currentIndex
- });
- }
- }
- }
- </script>
- <style lang="less">
- .tm-upload-img {
- background-color: #fff;
- padding-left: 25rpx;
- .row {
- position: relative;
- display: flex;
- justify-content: space-between;
- align-items: center;
- // flex: 1;
- padding-right: 25rpx;
- margin-bottom: 30rpx;
- .label-view {
- width: 175rpx;
- line-height: 22.5rpx;
- padding: 31.25rpx 0;
- text {
- font-size: 22.5rpx;
- color: #666F80;
- }
- }
- .placeholder {
- position: absolute;
- top: 31.25rpx;
- left: 175rpx;
- font-size: 22.5rpx;
- color: #B8BECC;
- }
- .img-icon {
- width: 30rpx;
- height: 25rpx;
- }
- }
- .img-preview {
- display: flex;
- flex-wrap: wrap;
- flex-direction: row;
- justify-content: flex-start;
- img {
- opacity: 1 !important;
- }
- .img-item {
- position: relative;
- margin-bottom: 25rpx;
- width: 150rpx;
- height: 150rpx;
- margin-right:35rpx;
- >image {
- width:100%;
- height:100%;
- opacity: 1;
- }
- .imgItem>img {
- opacity: 1;
- }
- .del-close {
- position: absolute;
- top: -12.5rpx;
- right: -12.5rpx;
- width: 37.5rpx;
- height: 37.5rpx;
- }
- &:nth-child(4n) {
- margin-right: 0;
- }
- }
- }
- }
- </style>
|