tm-upload.vue 13 KB

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