tui-upload.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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-btn-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. //HTTP 请求 Header, header 中不能设置 Referer。
  57. header:{
  58. type: Object,
  59. default(){
  60. return {}
  61. }
  62. },
  63. //HTTP 请求中其他额外的 form data
  64. formData:{
  65. type: Object,
  66. default(){
  67. return {}
  68. }
  69. }
  70. },
  71. data() {
  72. return {
  73. //图片地址
  74. imageList: [],
  75. //上传状态:1-上传成功 2-上传中 3-上传失败
  76. statusArr: []
  77. }
  78. },
  79. created() {
  80. this.initImages()
  81. },
  82. watch:{
  83. value(val){
  84. if(val){
  85. this.initImages()
  86. }
  87. }
  88. },
  89. computed: {
  90. isShowAdd() {
  91. let isShow = true;
  92. if (this.forbidAdd || (this.limit && this.imageList.length >= this.limit)) {
  93. isShow = false;
  94. }
  95. return isShow
  96. }
  97. },
  98. methods: {
  99. initImages(){
  100. this.imageList = [...this.value];
  101. for (let item of this.imageList) {
  102. this.statusArr.push("1")
  103. }
  104. },
  105. // 重新上传
  106. reUpLoad(index) {
  107. this.$set(this.statusArr, index, "2")
  108. this.change()
  109. this.uploadImage(index, this.imageList[index]).then(() => {
  110. this.change()
  111. }).catch(() => {
  112. this.change()
  113. })
  114. },
  115. change() {
  116. let status = ~this.statusArr.indexOf("2") ? 2 : 1
  117. if (status != 2 && ~this.statusArr.indexOf("3")) {
  118. // 上传失败
  119. status = 3
  120. }
  121. this.$emit('complete', {
  122. status: status,
  123. imgArr: this.imageList
  124. })
  125. },
  126. chooseImage: function() {
  127. let _this = this;
  128. uni.chooseImage({
  129. count: _this.limit - _this.imageList.length,
  130. success: function(e) {
  131. let imageArr = [];
  132. for (let i = 0; i < e.tempFilePaths.length; i++) {
  133. let len = _this.imageList.length;
  134. if (len >= _this.limit) {
  135. uni.showToast({
  136. title: `最多可上传${_this.limit}张图片`,
  137. icon: "none"
  138. });
  139. break;
  140. }
  141. let path = e.tempFilePaths[i]
  142. imageArr.push(path)
  143. _this.imageList.push(path)
  144. _this.statusArr.push("2")
  145. }
  146. _this.change()
  147. let start = _this.imageList.length - imageArr.length
  148. for (let j = 0; j < imageArr.length; j++) {
  149. let index = start + j
  150. //服务器地址
  151. if (_this.serverUrl) {
  152. _this.uploadImage(index, imageArr[j]).then(() => {
  153. _this.change()
  154. }).catch(() => {
  155. _this.change()
  156. })
  157. } else {
  158. //无服务器地址则直接返回成功
  159. _this.$set(_this.statusArr, index, "1")
  160. _this.change()
  161. }
  162. }
  163. }
  164. })
  165. },
  166. uploadImage: function(index, url) {
  167. let _this = this;
  168. return new Promise((resolve, reject) => {
  169. uni.uploadFile({
  170. url: this.serverUrl,
  171. name: this.fileKeyName,
  172. header: this.header,
  173. formData: this.formData,
  174. filePath: url,
  175. success: function(res) {
  176. console.log(res)
  177. if (res.statusCode == 200) {
  178. //返回结果 此处需要按接口实际返回进行修改
  179. let d = JSON.parse(res.data.replace(/\ufeff/g, "") || "{}")
  180. //判断code,以实际接口规范判断
  181. if (d.code % 100 === 0) {
  182. // 上传成功 d.url 为上传后图片地址,以实际接口返回为准
  183. d.url && (_this.imageList[index] = d.url)
  184. _this.$set(_this.statusArr, index, d.url ? "1" : "3")
  185. } else {
  186. // 上传失败
  187. _this.$set(_this.statusArr, index, "3")
  188. }
  189. resolve(index)
  190. } else {
  191. _this.$set(_this.statusArr, index, "3")
  192. reject(index)
  193. }
  194. },
  195. fail: function(res) {
  196. _this.$set(_this.statusArr, index, "3")
  197. reject(index)
  198. }
  199. })
  200. })
  201. },
  202. delImage: function(index) {
  203. this.imageList.splice(index, 1)
  204. this.statusArr.splice(index, 1)
  205. this.$emit("remove", {
  206. index: index
  207. })
  208. this.change()
  209. },
  210. previewImage: function(index) {
  211. if (!this.imageList.length) return;
  212. uni.previewImage({
  213. current: this.imageList[index],
  214. loop: true,
  215. urls: this.imageList
  216. })
  217. }
  218. }
  219. }
  220. </script>
  221. <style scoped>
  222. @font-face {
  223. font-family: 'tuiU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K6Wle7BLS3f/bTzQLmfAF2T1NsQAeNp9kD1OAzEQhZ/zByQSQiCoXVEA2vyUKRMp9Ailo0g23pBo1155nUg5AS0VB6DlGByAGyDRcgpelkmTImvt6PObmeexAZzjGwr/3yXuhBWO8ShcwREy4Sr1F+Ea+V24jhY+hRvUf4SbuFUD4RYu1BsdVO2Eu5vSbcsKZxgIV3CKJ+Eq9ZVwjfwqXMcVPoQb1L+EmxjjV7iFa2WpDOFhMEFgnEFjig3jAjEcLJIyBtahOfRmEsxMTzd6ETubOBso71dilwMeaDnngCntPbdmvkon/mDLgdSYbh4FS7YpjS4idCgbXyyc1d2oc7D9nu22tNi/a4E1x+xRDWzU/D3bM9JIbAyvkJI18jK3pBJTj2hrrPG7ZynW814IiU68y/SIx5o0dTr3bmniwOLn8owcfbS5kj33qBw+Y1kIeb/dTsQgil2GP5PYcRkAAAB42mNgYoAALjDJyIAOWMGiTIxMjMxsKak5qSWpbFmZiRmJ+QAmgAUIAAAAAf//AAIAAQAAAAwAAAAWAAAAAgABAAMABAABAAQAAAACAAAAAHjaY2BgYGQAgqtL1DlA9M296nUwGgA+8QYgAAA=) format('woff');
  225. font-weight: normal;
  226. font-style: normal;
  227. }
  228. .tui-upload-icon {
  229. font-family: "tuiUpload" !important;
  230. font-style: normal;
  231. -webkit-font-smoothing: antialiased;
  232. -moz-osx-font-smoothing: grayscale;
  233. padding: 10rpx;
  234. }
  235. .tui-icon-delete:before {
  236. content: "\e601";
  237. }
  238. .tui-icon-plus:before {
  239. content: "\e609";
  240. }
  241. .tui-upload-box {
  242. width: 100%;
  243. display: flex;
  244. flex-wrap: wrap;
  245. }
  246. .tui-upload-add {
  247. width: 110rpx;
  248. height: 110rpx;
  249. font-size: 68rpx;
  250. font-weight: 100;
  251. color: #888;
  252. background-color: #F7F7F7;
  253. display: flex;
  254. align-items: center;
  255. justify-content: center;
  256. padding: 0;
  257. }
  258. .tui-image-item {
  259. width: 110rpx;
  260. height: 110rpx;
  261. position: relative;
  262. margin-right: 20rpx;
  263. margin-bottom: 20rpx;
  264. }
  265. .tui-image-item:nth-of-type(3n) {
  266. margin-right: 0;
  267. }
  268. .tui-item-img {
  269. width: 110rpx;
  270. height: 110rpx;
  271. display: block;
  272. }
  273. .tui-img-del {
  274. width: 36rpx;
  275. height: 36rpx;
  276. position: absolute;
  277. right: -12rpx;
  278. top: -12rpx;
  279. background-color: #EB0909;
  280. border-radius: 50%;
  281. color: white;
  282. font-size: 34rpx;
  283. z-index: 99;
  284. }
  285. .tui-img-del::before {
  286. content: '';
  287. width: 16rpx;
  288. height: 1px;
  289. position: absolute;
  290. left: 10rpx;
  291. top: 18rpx;
  292. background-color: #fff;
  293. }
  294. .tui-upload-mask {
  295. width: 100%;
  296. height: 100%;
  297. position: absolute;
  298. left: 0;
  299. top: 0;
  300. display: flex;
  301. flex-direction: column;
  302. align-items: center;
  303. justify-content: space-around;
  304. padding: 40rpx 0;
  305. box-sizing: border-box;
  306. background-color: rgba(0, 0, 0, 0.6);
  307. }
  308. .tui-upload-loading {
  309. width: 28rpx;
  310. height: 28rpx;
  311. border-radius: 50%;
  312. border: 2px solid;
  313. border-color: #B2B2B2 #B2B2B2 #B2B2B2 #fff;
  314. animation: tui-rotate 0.7s linear infinite;
  315. }
  316. @keyframes tui-rotate {
  317. 0% {
  318. transform: rotate(0);
  319. }
  320. 100% {
  321. transform: rotate(360deg);
  322. }
  323. }
  324. .tui-tips {
  325. font-size: 26rpx;
  326. color: #fff;
  327. }
  328. .tui-mask-btn {
  329. padding: 4rpx 16rpx;
  330. border-radius: 40rpx;
  331. text-align: center;
  332. font-size: 24rpx;
  333. color: #fff;
  334. border: 1rpx solid #fff;
  335. display: flex;
  336. align-items: center;
  337. justify-content: center;
  338. }
  339. .tui-btn-hover {
  340. opacity: 0.8;
  341. }
  342. </style>