tui-upload.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <template>
  2. <view class="tui-container">
  3. <view class="tui-upload-box">
  4. <view class="tui-image-item" v-for="(item,index) in imageList" :key="index">
  5. <image :src="item" class="tui-item-img" @tap.stop="previewImage(index)" mode="aspectFill"></image>
  6. <view v-if="!forbidDel" class="tui-img-del" @tap.stop="delImage(index)"></view>
  7. <view v-if="statusArr[index]!=1" class="tui-upload-mask">
  8. <view class="tui-upload-loading" v-if="statusArr[index]==2"></view>
  9. <text class="tui-tips">{{statusArr[index]==2?'上传中...':'上传失败'}}</text>
  10. <view class="tui-mask-btn" v-if="statusArr[index]==3" @tap.stop="reUpLoad(index)" hover-class="tui-hover"
  11. :hover-stay-time="150">重新上传</view>
  12. </view>
  13. </view>
  14. <view v-if="isShowAdd" class="tui-upload-add" @tap="chooseImage">
  15. <view class="tui-upload-icon tui-icon-plus"></view>
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. export default {
  22. name: 'tuiUpload',
  23. props: {
  24. //初始化图片路径
  25. value: {
  26. type: Array,
  27. default () {
  28. return []
  29. }
  30. },
  31. //禁用删除
  32. forbidDel: {
  33. type: Boolean,
  34. default: false
  35. },
  36. //禁用添加
  37. forbidAdd: {
  38. type: Boolean,
  39. default: false
  40. },
  41. //服务器地址
  42. serverUrl: {
  43. type: String,
  44. default: ""
  45. },
  46. //限制数
  47. limit: {
  48. type: Number,
  49. default: 9
  50. },
  51. //项目名,默认为 file
  52. fileKeyName: {
  53. type: String,
  54. default: "file"
  55. }
  56. },
  57. data() {
  58. return {
  59. //图片地址
  60. imageList: [],
  61. //上传状态:1-上传成功 2-上传中 3-上传失败
  62. statusArr: []
  63. }
  64. },
  65. created() {
  66. this.imageList = [...this.value];
  67. for (let item of this.imageList) {
  68. this.statusArr.push("1")
  69. }
  70. },
  71. computed: {
  72. isShowAdd() {
  73. let isShow = true;
  74. if (this.forbidAdd || (this.limit && this.imageList.length >= this.limit)) {
  75. isShow = false;
  76. }
  77. return isShow
  78. }
  79. },
  80. methods: {
  81. // 重新上传
  82. reUpLoad(index) {
  83. this.$set(this.statusArr, index, "2")
  84. this.change()
  85. this.uploadImage(index, this.imageList[index]).then(() => {
  86. this.change()
  87. }).catch(() => {
  88. this.change()
  89. })
  90. },
  91. change() {
  92. let status = ~this.statusArr.indexOf("2") ? 2 : 1
  93. if (status != 2 && ~this.statusArr.indexOf("3")) {
  94. // 上传失败
  95. status = 3
  96. }
  97. this.$emit('complete', {
  98. status: status,
  99. imgArr: this.imageList
  100. })
  101. },
  102. chooseImage: function() {
  103. let _this = this;
  104. uni.chooseImage({
  105. count: _this.limit - _this.imageList.length,
  106. success: function(e) {
  107. let imageArr = [];
  108. for (let i = 0; i < e.tempFilePaths.length; i++) {
  109. let len = _this.imageList.length;
  110. if (len >= _this.limit) {
  111. uni.showToast({
  112. title: `最多可上传${_this.limit}张图片`,
  113. icon: "none"
  114. });
  115. break;
  116. }
  117. let path = e.tempFilePaths[i]
  118. imageArr.push(path)
  119. _this.imageList.push(path)
  120. _this.statusArr.push("2")
  121. }
  122. _this.change()
  123. let start = _this.imageList.length - imageArr.length
  124. for (let j = 0; j < imageArr.length; j++) {
  125. let index = start + j
  126. //服务器地址
  127. if (_this.serverUrl) {
  128. _this.uploadImage(index, imageArr[j]).then(() => {
  129. _this.change()
  130. }).catch(() => {
  131. _this.change()
  132. })
  133. } else {
  134. //无服务器地址则直接返回成功
  135. _this.$set(_this.statusArr, index, "1")
  136. _this.change()
  137. }
  138. }
  139. }
  140. })
  141. },
  142. uploadImage: function(index, url) {
  143. let _this = this;
  144. return new Promise((resolve, reject) => {
  145. uni.uploadFile({
  146. url: this.serverUrl,
  147. name: this.fileKeyName,
  148. header: {
  149. //设置请求头
  150. },
  151. formData: {},
  152. filePath: url,
  153. success: function(res) {
  154. console.log(res)
  155. if (res.statusCode == 200) {
  156. //返回结果 此处需要按接口实际返回进行修改
  157. let d = JSON.parse(res.data.replace(/\ufeff/g, "") || "{}")
  158. //判断code,以实际接口规范判断
  159. if (d.code % 100 === 0) {
  160. // 上传成功 d.url 为上传后图片地址,以实际接口返回为准
  161. d.url && (_this.imageList[index] = d.url)
  162. _this.$set(_this.statusArr, index, d.url ? "1" : "3")
  163. } else {
  164. // 上传失败
  165. _this.$set(_this.statusArr, index, "3")
  166. }
  167. resolve(index)
  168. } else {
  169. _this.$set(_this.statusArr, index, "3")
  170. reject(index)
  171. }
  172. },
  173. fail: function(res) {
  174. _this.$set(_this.statusArr, index, "3")
  175. reject(index)
  176. }
  177. })
  178. })
  179. },
  180. delImage: function(index) {
  181. this.imageList.splice(index, 1)
  182. this.statusArr.splice(index, 1)
  183. this.$emit("remove", {
  184. index: index
  185. })
  186. this.change()
  187. },
  188. previewImage: function(index) {
  189. if (!this.imageList.length) return;
  190. uni.previewImage({
  191. current: this.imageList[index],
  192. loop: true,
  193. urls: this.imageList
  194. })
  195. }
  196. }
  197. }
  198. </script>
  199. <style>
  200. @font-face {
  201. font-family: 'tuiUpload';
  202. src: url(data:application/font-woff;charset=utf-8;base64,d09GRgABAAAAAATcAA0AAAAAByQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABGRlRNAAAEwAAAABoAAAAciR52BUdERUYAAASgAAAAHgAAAB4AKQALT1MvMgAAAaAAAABCAAAAVjxvR/tjbWFwAAAB+AAAAEUAAAFK5ibpuGdhc3AAAASYAAAACAAAAAj//wADZ2x5ZgAAAkwAAADXA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7iFa2WpDOFhMEFgnEFjig3jAjEcLJIyBtahOfRmEsxMTzd6ETubOBso71dilwMeaDnngCntPbdmvkon/mDLgdSYbh4FS7YpjS4idCgbXyyc1d2oc7D9nu22tNi/a4E1x+xRDWzU/D3bM9JIbAyvkJI18jK3pBJTj2hrrPG7ZynW814IiU68y/SIx5o0dTr3bmniwOLn8owcfbS5kj33qBw+Y1kIeb/dTsQgil2GP5PYcRkAAAB42mNgYoAALjDJyIAOWMGiTIxMjMxsKak5qSWpbFmZiRmJ+QAmgAUIAAAAAf//AAIAAQAAAAwAAAAWAAAAAgABAAMABAABAAQAAAACAAAAAHjaY2BgYGQAgqtL1DlA9M296nUwGgA+8QYgAAA=) format('woff');
  203. font-weight: normal;
  204. font-style: normal;
  205. }
  206. .tui-upload-icon {
  207. font-family: "tuiUpload" !important;
  208. font-style: normal;
  209. -webkit-font-smoothing: antialiased;
  210. -moz-osx-font-smoothing: grayscale;
  211. padding: 10rpx;
  212. }
  213. .tui-upload-box {
  214. width: 100%;
  215. display: flex;
  216. flex-wrap: wrap;
  217. height: 100%;
  218. }
  219. .tui-upload-add {
  220. width: 100rpx;
  221. height: 100rpx;
  222. font-size: 68rpx;
  223. font-weight: 100;
  224. color: #888;
  225. background-color: #F7F7F7;
  226. display: flex;
  227. align-items: center;
  228. justify-content: center;
  229. padding: 0;
  230. background-image: url(../../static/img/shop/1.jpg);
  231. background-repeat:no-repeat;
  232. background-size: cover;
  233. }
  234. .tui-image-item {
  235. width: 220rpx;
  236. height: 220rpx;
  237. position: relative;
  238. margin-right: 20rpx;
  239. margin-bottom: 20rpx;
  240. }
  241. .tui-image-item:nth-of-type(3n) {
  242. margin-right: 0;
  243. }
  244. .tui-item-img {
  245. width: 220rpx;
  246. height: 220rpx;
  247. display: block;
  248. }
  249. .tui-img-del {
  250. width: 36rpx;
  251. height: 36rpx;
  252. position: absolute;
  253. right: -12rpx;
  254. top: -12rpx;
  255. background: #EB0909;
  256. border-radius: 50%;
  257. color: white;
  258. font-size: 34rpx;
  259. z-index: 999;
  260. }
  261. .tui-img-del::before {
  262. content: '';
  263. width: 16rpx;
  264. height: 1px;
  265. position: absolute;
  266. left: 10rpx;
  267. top: 18rpx;
  268. background: #fff;
  269. }
  270. .tui-upload-mask {
  271. width: 100%;
  272. height: 100%;
  273. position: absolute;
  274. left: 0;
  275. top: 0;
  276. display: flex;
  277. flex-direction: column;
  278. align-items: center;
  279. justify-content: space-around;
  280. padding: 40rpx 0;
  281. box-sizing: border-box;
  282. background: rgba(0, 0, 0, 0.6);
  283. }
  284. .tui-upload-loading {
  285. width: 28rpx;
  286. height: 28rpx;
  287. border-radius: 50%;
  288. border: 2px solid;
  289. border-color: #B2B2B2 #B2B2B2 #B2B2B2 #fff;
  290. animation: tui-rotate 0.7s linear infinite;
  291. }
  292. @keyframes tui-rotate {
  293. 0% {
  294. transform: rotate(0);
  295. }
  296. 100% {
  297. transform: rotate(360deg);
  298. }
  299. }
  300. .tui-tips {
  301. font-size: 26rpx;
  302. color: #fff;
  303. }
  304. .tui-mask-btn {
  305. padding: 6rpx 16rpx;
  306. border-radius: 40rpx;
  307. text-align: center;
  308. font-size: 24rpx;
  309. color: #fff;
  310. border: 1rpx solid #fff;
  311. display: flex;
  312. align-items: center;
  313. justify-content: center;
  314. }
  315. .tui-hover {
  316. opacity: 0.5;
  317. }
  318. </style>