123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- <template>
- <view class="tm-upload flex-start " id="tm-upload">
- <view v-for="(item,index) in list" :key="index" class="tm-upload-item " :class="[grid!=1?'ma-4':'']" :style="{
- width:itemWidth+'px',
- height:itemHeight+'px'
- }">
- <view v-if="!disabled" class="tm-upload-del " :class="[delDirection]">
- <slot name="del">
- <tm-icons @click="del(index)" :black="black_tmeme" name="icon-times-circle-fill" size="36" color="red"></tm-icons>
- </slot>
- </view>
- <view @click.stop="zhanshistop(item.url,list,'url')" class="tm-upload-item-ck text flex-center overflow"
- :class="[color_tmeme,black_tmeme?'grey-darken-4 bk':'',`round-${round}`]">
- <slot name="img" :info={itemWidth,itemHeight}>
- <tm-icons style="line-height: 0;" name="icon-exclamationcircle-f" v-if="item['loaderror']==true"></tm-icons>
- <image :mode="model" v-else :src="item.url" @error="errorFile(item,index)" :style="{
- width:itemWidth+'px',
- height:itemHeight+'px'
- }"></image>
- </slot>
- </view>
-
- <view v-if="tips&&!disabled" class="tm-upload-tips text-size-xs round-b-2"
- :class="[
- item.statusCode==2||item.statusCode==4?'red text':'',
- item.statusCode==1||item.statusCode==0?'black text':'',
- item.statusCode==3?color_tmeme+' text':'',
- black_tmeme?'bk':''
- ]"
- >{{item.status}}</view>
-
- <view v-if="item.progress>0&&item.progress!=100&&!disabled" class="tm-upload-pro green"
- :style="{width:item.progress+'%'}"></view>
- </view>
- <view @click="addfile" v-if="list.length<max&&!disabled&&showSheet" class="tm-upload-item ma-4 grey-lighten-4 " :class="[`round-${round}`]" :style="{
- width:itemWidth+'px',
- height:itemHeight+'px'
- }">
- <view class="tm-upload-item-ck border-a-0 flex-center text " :class="[color_tmeme,black_tmeme?'grey-darken-4 bk':'',`round-${round}`]">
- <slot name="upload">
- <tm-icons name="icon-plus" size="36" :color="color_tmeme"></tm-icons>
- </slot>
- </view>
- </view>
- </view>
- </template>
- <script>
-
- import tmIcons from "@/tm-vuetify/components/tm-icons/tm-icons.vue"
- export default {
- components:{tmIcons},
- name: "tm-upload",
- props: {
- model:{
- type:String,
- default:'scaleToFill'
- },
- black:{
- type:Boolean|String,
- default:null
- },
-
- grid: {
- type: String | Number,
- default: 5
- },
-
- imgHeight: {
- type: String | Number,
- default: 0
- },
-
- max: {
- type: String | Number,
- default: 9
- },
-
- maxsize: {
- type: String | Number,
- default: 10*1024*1024
- },
-
- color: {
- type: String,
- default: 'primary'
- },
-
-
- delDirection: {
- type: String,
- default: 'right'
- },
-
- disabled: String | Boolean,
-
- url: {
- type: String,
- default: ''
- },
-
- filelist: {
- type: Array,
- default: () => {
- return [];
- }
- },
-
-
- urlKey:{
- type:String,
- default:''
- },
-
- header:{
- type:Object,
- default:()=>{
- return {};
- }
- },
-
- fileName:{
- type:String,
- default:'file'
- },
-
- tips: {
- type: Boolean|String,
- default: true,
- },
-
- autoUpload: {
- type: Boolean,
- default: false,
- },
-
- name:{
- type:String,
- default:''
- },
- round:{
- type:Number|String,
- default:3
- },
-
- fllowTheme:{
- type:Boolean|String,
- default:true
- },
-
- code:{
- type:Number,
- default:0
- },
- width:{
- type:Number,
- default:0
- },
-
- responseStu:{
- type:Object,
- default:()=>{
- return {
- code:'code',
- data:'data',
- msg:'msg'
- }
- }
- }
- },
- computed: {
- header_obj:function () {
- return this.header;
- },
- black_tmeme: function() {
- if (this.black !== null) return this.black;
- return this.$tm.vx.state().tmVuetify.black;
- },
- color_tmeme:function(){
- if(this.$tm.vx.state().tmVuetify.color!==null&&this.$tm.vx.state().tmVuetify.color && this.fllowTheme){
- return this.$tm.vx.state().tmVuetify.color;
- }
- return this.color;
- },
- },
- data() {
- return {
- maxWidth: 0,
- itemWidth: 0,
- itemHeight: 0,
- list: [],
-
- showSheet:true,
- upObje:null,
- };
- },
- created() {
-
- this.showSheet = false;
-
- },
- async mounted() {
- let t = this;
-
- this.$nextTick(async function(){
-
- this.$Querey('.tm-upload', this,30).then(o=>{
-
- t.showSheet = true;
-
- t.maxWidth = o[0].width||t.width;
- let itemWidth = (t.maxWidth - (parseInt(t.grid) - 1) * uni.upx2px(12)) / parseInt(t.grid);
- t.itemWidth = itemWidth;
- t.itemHeight = t.itemWidth;
- if (t.imgHeight > 0) {
- t.itemHeight = parseInt(uni.upx2px(t.imgHeight));
- }
-
- if (typeof t.filelist === 'object' && Array.isArray(t.filelist)) {
- let plist = [...t.filelist];
- plist.forEach((item, index) => {
- let url = "";
- if (typeof item === 'string') {
- url = item;
- } else if (typeof item === 'object') {
- url = item[t.urlKey]
- }
- t.list.push({
- url: url,
- status: "上传成功",
- progress: 100,
- fileId: t.$tm.guid(),
- statusCode: 3,
- data: item,
- })
- })
-
- }
- })
- })
-
- },
- methods: {
- zhanshistop(url,list,stringurl){
-
- },
- errorFile(item,index){
- let id = item;
- id['loaderror'] = true;
- this.list.splice(index,1,id)
- },
-
- pushFile(list){
- let t= this;
- let plist = list||[];
- plist.forEach((item, index) => {
- let url = "";
- if (typeof item === 'string') {
- url = item;
- } else if (typeof item === 'object') {
- url = item[t.urlKey]
- }
- t.list.push({
- url: url,
- status: "上传成功",
- progress: 100,
- fileId: t.$tm.guid(),
- statusCode: 3,
- data: item,
- })
- })
- },
- async addfile() {
- if(this.disabled) return;
- let t = this;
- let maxfile = parseInt(this.max) - this.list.length;
- if (maxfile <= 0) {
- this.$tm.toast("已达上传上限");
- return;
- };
- let url = this.url;
- if(!this.upObje){
- this.upObje = new this.$tm.upload.uploadfile({
- opts:{header:this.header_obj,name:this.fileName},
- maxfile:maxfile,
- uploadUrl:url,
- isAuto:this.autoUpload,
- maxsize:this.maxsize,
- code:this.code,
- responseStu:this.responseStu
- });
-
- this.upObje.addfile(this.list);
- this.upObje.success = function(item){
- t.changeSuccess();
- }
- }else{
- this.upObje.setConfig({maxsize:this.maxsize,maxfile:maxfile,code:this.code,responseStu:this.responseStu,opts:{header:this.header_obj,name:this.fileName}});
- }
-
- let clist = await this.upObje.chooesefile().catch(e=>{});
- if(clist){
- t.list = clist;
- }
- },
-
- stopupload(){
- if(this.disabled) return;
- if(this.upObje){
- this.upObje.stop();
- }
- },
-
- startupload(){
- if(this.disabled) return;
- if(this.upObje){
- this.upObje.start();
- }
- },
-
- del(index) {
- if(this.disabled) return;
- this.$emit("del",this.list[index])
- this.list.splice(index, 1);
- this.changeSuccess();
- },
-
- changeSuccess() {
- let filelist = [];
- this.list.forEach((item, index) => {
- if (item.statusCode === 3) {
- filelist.push(item.data);
- }
- })
- this.$emit('change', filelist);
- this.$emit('update:filelist', filelist);
- },
-
- getFile(){
- let filelist = [];
- this.list.forEach((item, index) => {
- if (item.statusCode === 3) {
- filelist.push(item.data);
- }
- })
- return filelist;
- },
-
- clearAllFile(){
- if(this.disabled) return;
- this.$emit("clear",[])
- this.list=[];
- this.changeSuccess();
- }
- },
- }
- </script>
- <style lang="scss" scoped>
- .tm-upload {
- flex-flow: wrap;
- .tm-upload-item {
- position: relative;
- .tm-upload-tips {
- position: absolute;
- z-index: 10;
- left: 0;
- bottom: 0;
- height: 40upx;
- line-height: 40upx;
- text-align: center;
- font-size: 23upx;
- width: 100%;
- }
- .tm-upload-pro {
- position: absolute;
- z-index: 11;
- left: 0;
- bottom: 0;
- height: 6upx;
- width: 0%;
- }
- .tm-upload-del {
- position: absolute;
- z-index: 10;
- &.right {
- right: -6upx;
- top: -8upx;
- }
- &.left {
- left: -6upx;
- top: -8upx;
- }
- &.center {
- width: 100%;
- height: 100%;
- left: 0;
- top: 0;
- display: flex;
- justify-content: center;
- align-items: center;
- }
- }
- .tm-upload-item-ck {
- width: 100%;
- height: 100%;
- }
- }
- }
- </style>
|