tm-upload.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <template>
  2. <view class="tm-upload flex-start relative" id="tm-upload">
  3. <view v-for="(item,index) in list" :key="index" class="tm-upload-item " :class="[grid!=1?'ma-4':'']" :style="{
  4. width:itemWidth+'px',
  5. height:itemHeight+'px'
  6. }">
  7. <view v-if="!disabled" class="tm-upload-del " :class="[delDirection]">
  8. <slot name="del">
  9. <tm-icons @click="del(index)" :black="black_tmeme" name="icon-times-circle-fill" size="36" color="red"></tm-icons>
  10. </slot>
  11. </view>
  12. <view @click.stop="$tm.preview(item.url,list,'url')" class="tm-upload-item-ck text flex-center overflow"
  13. :class="[color_tmeme,black_tmeme?'grey-darken-4 bk':'',`round-${round}`]">
  14. <slot name="img" :info={itemWidth,itemHeight}>
  15. <tm-icons style="line-height: 0;" name="icon-exclamationcircle-f" v-if="item['loaderror']==true"></tm-icons>
  16. <image :mode="model" v-else :src="item.url" @error="errorFile(item,index)" :style="{
  17. width:itemWidth+'px',
  18. height:itemHeight+'px'
  19. }"></image>
  20. </slot>
  21. </view>
  22. <!-- 上传提示语。 -->
  23. <view v-if="tips&&!disabled" class="tm-upload-tips text-size-xs round-b-2"
  24. :class="[
  25. item.statusCode==2||item.statusCode==4?'red text':'',
  26. item.statusCode==1||item.statusCode==0?'black text':'',
  27. item.statusCode==3?color_tmeme+' text':'',
  28. black_tmeme?'bk':''
  29. ]"
  30. >{{item.status}}</view>
  31. <!-- 上传的进度。 -->
  32. <view v-if="item.progress>0&&item.progress!=100&&!disabled" class="tm-upload-pro green"
  33. :style="{width:item.progress+'%'}"></view>
  34. <!-- 上传的排序。 -->
  35. <view v-if="showSort" class="absolute l-0 fulled flex-between" :class="[disabled?'b-0':'b-40']" :style="{height:'46rpx'}">
  36. <view @click.stop="prevSort(item,index,'prev')" class="round-r-24 flex-center px-16 py-6" :class="[index==0?'opacity-0':'']" style="background-color: rgba(0, 0, 0, 0.3);">
  37. <tm-icons name="icon-angle-left" size="24" :color="color_tmeme"></tm-icons>
  38. </view>
  39. <view @click.stop="prevSort(item,index,'next')" class="round-l-24 flex-center px-16 py-6" :class="[index==list.length-1?'opacity-0':'']" style="background-color: rgba(0, 0, 0, 0.3);">
  40. <tm-icons name="icon-angle-right" size="24" :color="color_tmeme"></tm-icons>
  41. </view>
  42. </view>
  43. </view>
  44. <view @click="addfile" v-if="list.length<max&&!disabled" class="tm-upload-item ma-4 grey-lighten-4 " :class="[`round-${round}`]" :style="{
  45. width:itemWidth+'px',
  46. height:itemHeight+'px'
  47. }">
  48. <view class="tm-upload-item-ck border-a-0 flex-center text " :class="[color_tmeme,black_tmeme?'grey-darken-4 bk':'',`round-${round}`]">
  49. <slot name="upload">
  50. <tm-icons name="icon-plus" size="36" :color="color_tmeme"></tm-icons>
  51. </slot>
  52. </view>
  53. </view>
  54. </view>
  55. </template>
  56. <script>
  57. /**
  58. * 上传图片组件
  59. * @property {Function} change 每一张图片上传成功都传动触发,并返回上传成功的图片列表。
  60. * @property {Function} del 删除一张图片时触发,返回当前删除的图片数据。
  61. * @property {Number|String} grid = [1|2|3|4|5] 默认:5,一行排几个。
  62. * @property {Number} code = [] 默认:0,服务器上传返回数据中表示成功的标志码。
  63. * @property {Number} width = [] 默认:0,自定义组件宽度。如果0,自动获取。
  64. * @property {Number|String} img-height = [0] 默认:0,宽高相等。单位upx,自定义图片高度。
  65. * @property {Number|String} max = [9] 默认:9,最大上传数量
  66. * @property {String} del-direction = [left|right|center] 默认:right, 删除按钮的方向。left,right,center
  67. * @property {String|Boolean} disabled = [true|false] 默认:false, 如果禁用,会隐藏上传和删除按钮,只显示已上传的图片。
  68. * @property {String} url = [] 默认:"",上传的地址。
  69. * @property {Array} filelist = [] 默认:[],默认上传显示的图片。如果加上filelist.sync的话,会自动更新数据实现双向绑定。类似于v-model;
  70. * @property {String} url-key = [] 默认:"",返回数据时,如果返回的是对象。则需要提供对象图像地址的key。默认没有,返回的即是图片地址。
  71. * @property {Object} header = [] 默认:{},上传的头部参数。
  72. * @property {String} file-name = [file] 默认:file,上传时的文件key名。
  73. * @property {String} model = [scaleToFill|aspectFit|aspectFill|widthFix|heightFix|top|bottom|center|left|right|top left|top right|bottom left|bottom right] 默认:scaleToFill,图片展现模式,同官方。
  74. * @property {String} name = [] 默认:'',提交表单时的的字段名称标识
  75. * @property {Boolean|String} tips = [true|false] 默认:true,是否显示底部的上传提示语。上传中,失败等。
  76. * @property {Boolean|String} black = [true|false] 默认:null,暗黑模式。
  77. * @property {Boolean|String} auto-upload = [true|false] 默认:false,是否自动上传,即添加完图片后立即上传。
  78. * @property {Number|String} round = [] 默认:3,圆角
  79. * @property {Object} responseStu = [] 默认: {code:'code',//服务器返回的码的字段名称data:'data',//服务上传成功后返回 的数据字段名称msg:'msg'//服务器响应信息的字段名称。},服务器响应结构字段映射表
  80. * @property {Number|String} maxsize = [] 默认:10*1024*1024,最大上传的图片大小,10mb大小
  81. * @example <tm-upload></tm-upload>
  82. * @description 可以通过refs.组件获得:addfile主动触发添加文件,stopupload停止上传,startupload开始或者继续上传,del删除一张图片。
  83. */
  84. import tmIcons from "@/tm-vuetify/components/tm-icons/tm-icons.vue"
  85. export default {
  86. components:{tmIcons},
  87. name: "tm-upload",
  88. props: {
  89. showSort:{
  90. type:Boolean|String,
  91. default:false
  92. },
  93. model:{
  94. type:String,
  95. default:'scaleToFill'
  96. },
  97. black:{
  98. type:Boolean|String,
  99. default:null
  100. },
  101. // 一行几个。
  102. grid: {
  103. type: String | Number,
  104. default: 5
  105. },
  106. // 默认0即为宽高相等。单位upx
  107. imgHeight: {
  108. type: String | Number,
  109. default: 0
  110. },
  111. // 最大上传数量,默认9
  112. max: {
  113. type: String | Number,
  114. default: 9
  115. },
  116. // 最大上传数量,默认9
  117. maxsize: {
  118. type: String | Number,
  119. default: 10*1024*1024
  120. },
  121. // 主题色
  122. color: {
  123. type: String,
  124. default: 'primary'
  125. },
  126. // 删除按钮的方向。left,right,center
  127. delDirection: {
  128. type: String,
  129. default: 'right'
  130. },
  131. // 如果禁用,会隐藏上传和删除按钮。
  132. disabled: String | Boolean,
  133. // 上传的地址。
  134. url: {
  135. type: String,
  136. default: ''
  137. },
  138. // 默认上传显示的图片。如果加上filelist.sync的话,会自动更新数据实现双向绑定。类似于v-model;
  139. filelist: {
  140. type: Array,
  141. default: () => {
  142. return [];
  143. }
  144. },
  145. //返回数据时,如果返回的是对象。则图像地址的key名。默认没有,返回的即是图片地址。
  146. urlKey:{
  147. type:String,
  148. default:''
  149. },
  150. // 上传的头部参数。
  151. header:{
  152. type:Object,
  153. default:()=>{
  154. return {};
  155. }
  156. },
  157. // 上传时的文件key名。默认file
  158. fileName:{
  159. type:String,
  160. default:'file'
  161. },
  162. // 是否显示底部的上传提示语。上传中,失败等。
  163. tips: {
  164. type: Boolean|String,
  165. default: true,
  166. },
  167. // 是否自动上传,即添加完图片后立即上传。
  168. autoUpload: {
  169. type: Boolean,
  170. default: false,
  171. },
  172. //提交表单时的的字段名称
  173. name:{
  174. type:String,
  175. default:''
  176. },
  177. round:{
  178. type:Number|String,
  179. default:3
  180. },
  181. // 跟随主题色的改变而改变。
  182. fllowTheme:{
  183. type:Boolean|String,
  184. default:true
  185. },
  186. //定义上传成功返回的code码,默认是0表示上传成功 。
  187. code:{
  188. type:Number,
  189. default:0
  190. },
  191. width:{
  192. type:Number,
  193. default:0
  194. },
  195. //上成功后,服务器顺应数据的字段映射表。
  196. responseStu:{
  197. type:Object,
  198. default:()=>{
  199. return {
  200. code:'code',//服务器返回的码的字段名称
  201. data:'data',//服务上传成功后返回 的数据字段名称
  202. msg:'msg'//服务器响应信息的字段名称。
  203. }
  204. }
  205. }
  206. },
  207. computed: {
  208. header_obj:function () {
  209. return this.header;
  210. },
  211. black_tmeme: function() {
  212. if (this.black !== null) return this.black;
  213. return this.$tm.vx.state().tmVuetify.black;
  214. },
  215. color_tmeme:function(){
  216. if(this.$tm.vx.state().tmVuetify.color!==null&&this.$tm.vx.state().tmVuetify.color && this.fllowTheme){
  217. return this.$tm.vx.state().tmVuetify.color;
  218. }
  219. return this.color;
  220. },
  221. },
  222. data() {
  223. return {
  224. maxWidth: 0,
  225. itemWidth: 0,
  226. itemHeight: 0,
  227. list: [],
  228. upObje:null,
  229. };
  230. },
  231. created() {
  232. // #ifdef APP-VUE || APP-PLUS || MP
  233. this.showSheet = false;
  234. // #endif
  235. },
  236. async mounted() {
  237. let t = this;
  238. if (typeof t.filelist === 'object' && Array.isArray(t.filelist)) {
  239. let plist = [...t.filelist];
  240. plist.forEach((item, index) => {
  241. let url = "";
  242. if (typeof item === 'string') {
  243. url = item;
  244. } else if (typeof item === 'object') {
  245. url = item[t.urlKey]
  246. }
  247. t.list.push({
  248. url: url,
  249. status: "上传成功",
  250. progress: 100,
  251. fileId: t.$tm.guid(),
  252. statusCode: 3,
  253. data: item,
  254. })
  255. })
  256. }
  257. this.getRect()
  258. },
  259. updated() {
  260. this.getRect()
  261. },
  262. methods: {
  263. prevSort(item,index,type){
  264. if((index==0&&type=='prev')||(index==this.list.length-1&&type=='next')){
  265. return;
  266. }
  267. let nowindex = type=='prev'?index-1:index+1
  268. let nowItem = this.list[index];
  269. let newnowItem = this.list[nowindex];
  270. let nowfilelist= [...this.list]
  271. nowfilelist.splice(index,1,newnowItem)
  272. nowfilelist.splice(nowindex,1,nowItem)
  273. this.list = [...nowfilelist]
  274. this.$emit('update:filelist', nowfilelist);
  275. },
  276. getRect(){
  277. let t = this;
  278. this.$Querey('.tm-upload', this,0).then(o=>{
  279. if(!o[0].width&&t.maxWidth) return;
  280. t.maxWidth = o[0].width||t.width;
  281. let itemWidth = (t.maxWidth - (parseInt(t.grid) - 1) * uni.upx2px(12)) / parseInt(t.grid);
  282. t.itemWidth = itemWidth;
  283. t.itemHeight = t.itemWidth;
  284. if (t.imgHeight > 0) {
  285. t.itemHeight = parseInt(uni.upx2px(t.imgHeight));
  286. }
  287. })
  288. },
  289. errorFile(item,index){
  290. let id = item;
  291. id['loaderror'] = true;
  292. this.list.splice(index,1,id)
  293. },
  294. //动态添加默认已上传的文件。
  295. pushFile(list){
  296. let t= this;
  297. let plist = list||[];
  298. plist.forEach((item, index) => {
  299. let url = "";
  300. if (typeof item === 'string') {
  301. url = item;
  302. } else if (typeof item === 'object') {
  303. url = item[t.urlKey]
  304. }
  305. t.list.push({
  306. url: url,
  307. status: "上传成功",
  308. progress: 100,
  309. fileId: t.$tm.guid(),
  310. statusCode: 3,
  311. data: item,
  312. })
  313. })
  314. },
  315. async addfile() {
  316. if(this.disabled) return;
  317. let t = this;
  318. let maxfile = parseInt(this.max) - this.list.length;
  319. if (maxfile <= 0) {
  320. this.$tm.toast("已达上传上限");
  321. return;
  322. };
  323. let url = this.url;
  324. if(!this.upObje){
  325. this.upObje = new this.$tm.upload.uploadfile({
  326. opts:{header:this.header_obj,name:this.fileName},
  327. maxfile:maxfile,
  328. uploadUrl:url,
  329. isAuto:this.autoUpload,
  330. maxsize:this.maxsize,
  331. code:this.code,
  332. responseStu:this.responseStu
  333. });
  334. // 添加已有的图片。
  335. this.upObje.addfile(this.list);
  336. this.upObje.success = function(item){
  337. t.changeSuccess();
  338. }
  339. }else{
  340. this.upObje.setConfig({maxsize:this.maxsize,maxfile:maxfile,code:this.code,responseStu:this.responseStu,opts:{header:this.header_obj,name:this.fileName}});
  341. }
  342. let clist = await this.upObje.chooesefile().catch(e=>{});
  343. if(clist){
  344. t.list = clist;
  345. }
  346. },
  347. // 停止下载
  348. stopupload(){
  349. if(this.disabled) return;
  350. if(this.upObje){
  351. this.upObje.stop();
  352. }
  353. },
  354. // 继续上传或者开始上传。
  355. startupload(){
  356. if(this.disabled) return;
  357. if(this.upObje){
  358. this.upObje.start();
  359. }
  360. },
  361. // 删除一张图片。
  362. del(index) {
  363. if(this.disabled) return;
  364. this.$emit("del",this.list[index])
  365. this.list.splice(index, 1);
  366. this.changeSuccess();
  367. },
  368. // 只有上传成功才会触发change。并更新发送数据。
  369. changeSuccess() {
  370. let filelist = [];
  371. this.list.forEach((item, index) => {
  372. if (item.statusCode === 3) {
  373. filelist.push(item.data);
  374. }
  375. })
  376. this.$emit('change', filelist);
  377. this.$emit('update:filelist', filelist);
  378. },
  379. //获取已经上传的图像。
  380. getFile(){
  381. let filelist = [];
  382. this.list.forEach((item, index) => {
  383. if (item.statusCode === 3) {
  384. filelist.push(item.data);
  385. }
  386. })
  387. return filelist;
  388. },
  389. //清除所有已上传的文件。
  390. clearAllFile(){
  391. if(this.disabled) return;
  392. this.$emit("clear",[])
  393. this.list=[];
  394. this.changeSuccess();
  395. }
  396. },
  397. }
  398. </script>
  399. <style lang="scss" scoped>
  400. .tm-upload {
  401. flex-flow: wrap;
  402. .tm-upload-item {
  403. position: relative;
  404. .tm-upload-tips {
  405. position: absolute;
  406. z-index: 10;
  407. left: 0;
  408. bottom: 0;
  409. height: 40upx;
  410. line-height: 40upx;
  411. text-align: center;
  412. font-size: 23upx;
  413. width: 100%;
  414. }
  415. .tm-upload-pro {
  416. position: absolute;
  417. z-index: 11;
  418. left: 0;
  419. bottom: 0;
  420. height: 6upx;
  421. width: 0%;
  422. }
  423. .tm-upload-del {
  424. position: absolute;
  425. z-index: 10;
  426. &.right {
  427. right: -6upx;
  428. top: -8upx;
  429. }
  430. &.left {
  431. left: -6upx;
  432. top: -8upx;
  433. }
  434. &.center {
  435. width: 100%;
  436. height: 100%;
  437. left: 0;
  438. top: 0;
  439. display: flex;
  440. justify-content: center;
  441. align-items: center;
  442. }
  443. }
  444. .tm-upload-item-ck {
  445. width: 100%;
  446. height: 100%;
  447. }
  448. }
  449. }
  450. </style>