w-upload.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <template>
  2. <view class="w-upload">
  3. <view class="fileBox" v-if="fileShow">
  4. <view class="filePath" v-for="(j, index1) in fileList" :key="index1">
  5. <view class="text1" @click="wpriven(j.src)">{{ j.name }}</view>
  6. <view class="w-edit" @click="wdelete(index1, fileList, 1)"><text class="w-btn1">x</text></view>
  7. </view>
  8. </view>
  9. <view class="imgList" v-if="imgShow">
  10. <view class="imgItem" v-for="(k, index2) in imgList" :key="index2">
  11. <image class="w-img" :src="k.src" mode="" @click="wpriven(k.src)"></image>
  12. <text class="cancel" @click="wdelete(index2, imgList, 2)">x</text>
  13. </view>
  14. <view class="addItem" @click="upLoadImg(1)">+</view>
  15. </view>
  16. <view class="w-drawer">
  17. <view class="w-setbox" :class="{ wShow: isshow }">
  18. <view class="w-header">
  19. <view class="w-item w-item1" @click="wselect(index)" v-for="(i, index) in selectList" :key="index">{{ i }}</view>
  20. <view class="w-line"></view>
  21. <view class="w-item" @click="wclose">取消</view>
  22. </view>
  23. </view>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. import { WUpload } from '@/components/file-img-upload/upload.js';
  29. export default {
  30. props: {
  31. token : {
  32. type: String,
  33. default: ''
  34. },
  35. fileShow: {
  36. type: Boolean,
  37. default: false
  38. },
  39. imgShow: {
  40. type: Boolean,
  41. default: false
  42. },
  43. fileList: {
  44. type: Array,
  45. default: []
  46. },
  47. imgList: {
  48. type: Array,
  49. default: []
  50. },
  51. requestUrl: {
  52. type: String,
  53. default: ''
  54. },
  55. uploadName: {
  56. type: String,
  57. default: 'file'
  58. },
  59. fileType: {
  60. type: String,
  61. default: ''
  62. },
  63. imgType: {
  64. type: String,
  65. default: ''
  66. }
  67. },
  68. name: 'WUpload',
  69. data() {
  70. return {
  71. isshow: false,
  72. selectList: ['文档', '图片']
  73. };
  74. },
  75. created() {},
  76. methods: {
  77. wclose() {
  78. this.isshow = false;
  79. },
  80. uploadOpen() {
  81. this.isshow = true;
  82. },
  83. wselect(index) {
  84. if (index == 0) {
  85. this.upLoadFile();
  86. } else {
  87. this.upLoadImg();
  88. }
  89. this.isshow = false;
  90. },
  91. wpriven(url) {
  92. let that = this;
  93. uni.showLoading({
  94. title: '下载中...',
  95. mask: true
  96. });
  97. that.udownload(url, 'temporary')
  98. .then(path => {
  99. uni.hideLoading();
  100. that.uopen(path);
  101. })
  102. .catch(() => {
  103. uni.hideLoading();
  104. uni.showToast({
  105. title: '下载失败',
  106. icon: 'none'
  107. });
  108. });
  109. },
  110. wdelete(index, list, num) {
  111. list.forEach((i, j) => {
  112. if (j == index) {
  113. if (num == 1) {
  114. this.$emit('updateFileList', [j,i]); //返回被删除的文件
  115. } else {
  116. this.$emit('updateImgList', [j,i]); //返回被删除的图片
  117. }
  118. }
  119. });
  120. },
  121. // 下载临时储存 temporary 临时 local 永久
  122. udownload(url, type = 'temporary') {
  123. let that = this;
  124. return new Promise((resolve, reject) => {
  125. uni.downloadFile({
  126. url,
  127. success: ({ statusCode, tempFilePath }) => {
  128. if (statusCode === 200) {
  129. if (type == 'local') {
  130. uni.saveFile({
  131. tempFilePath,
  132. success: ({ savedFilePath }) => that.onCommit(resolve(savedFilePath)),
  133. fail: () => that.errorHandler('下载失败', reject)
  134. });
  135. } else {
  136. that.onCommit(resolve(tempFilePath));
  137. }
  138. }
  139. },
  140. fail: () => that.errorHandler('下载失败', reject)
  141. });
  142. });
  143. },
  144. onCommit(resolve) {
  145. return resolve;
  146. },
  147. errorHandler(errText, reject) {
  148. uni.showToast({
  149. title: errText,
  150. icon: 'none'
  151. });
  152. return reject(errText);
  153. },
  154. // 打开文件
  155. uopen(filePath) {
  156. let system = uni.getSystemInfoSync().platform;
  157. if (system == 'ios') {
  158. filePath = encodeURI(filePath);
  159. }
  160. uni.openDocument({
  161. showMenu: true,
  162. filePath,
  163. success: res => {
  164. console.log('打开文档成功');
  165. },
  166. fail: res1 => {
  167. uni.getImageInfo({
  168. src: filePath,
  169. success: imgInfo => {
  170. uni.previewImage({
  171. current: filePath,
  172. urls: [filePath]
  173. });
  174. },
  175. fail: err => {
  176. uni.showToast({
  177. title: '不支持该格式',
  178. icon: 'none'
  179. });
  180. return;
  181. }
  182. });
  183. }
  184. });
  185. },
  186. // 文件上传
  187. upLoadFile() {
  188. let that = this;
  189. uni.chooseMessageFile({
  190. type: 'file',
  191. success: function(source) {
  192. if (source.tempFiles[0].size < 1024 * 1024 * 5) {
  193. WUpload(
  194. that.requestUrl,
  195. that.uploadName,
  196. that.token,
  197. {
  198. // token: uni.getStorageSync('token'),
  199. // 看项目接口要求的格式修改关键字
  200. upload_type: that.fileType
  201. },
  202. source
  203. )
  204. .then(res2 => {
  205. if (res2.state == true) {
  206. let Res2 = res2.data;
  207. Res2.name = source.tempFiles[0].name;
  208. that.$emit('fileSuccess', Res2); //返回上传成功的数据
  209. uni.showToast({
  210. title: '上传成功'
  211. });
  212. } else {
  213. uni.showToast({
  214. title: '上传失败',
  215. icon: 'none'
  216. });
  217. }
  218. })
  219. .catch(catchRes => {
  220. console.log(catchRes);
  221. uni.showToast({
  222. title: '上传失败',
  223. icon: 'none'
  224. });
  225. });
  226. } else {
  227. uni.showToast({
  228. title: '文件过大,无法上传',
  229. icon: 'none'
  230. });
  231. }
  232. }
  233. });
  234. },
  235. // 图片上传
  236. upLoadImg(num) {
  237. let that = this;
  238. uni.chooseImage({
  239. count: 1, // 默认9
  240. sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
  241. sourceType: ['camera', 'album'], // 可以指定来源是相册还是相机,默认二者都有
  242. success: function(source) {
  243. if (source.tempFiles[0].size < 1024 * 1024 * 2) {
  244. uni.getImageInfo({
  245. src: source.tempFilePaths[0],
  246. success(res) {
  247. WUpload(
  248. that.requestUrl,
  249. that.uploadName,
  250. that.token,
  251. {
  252. // token: uni.getStorageSync('token'),
  253. // 根据接口要求修改对应的格式关键字
  254. upload_type: that.imgType
  255. },
  256. source
  257. )
  258. .then(res1 => {
  259. if (res1.state == true) {
  260. let Res = res1.data;
  261. Res.name = source.tempFilePaths[0];
  262. if (num == 1) {
  263. that.$emit('imgSuccess', Res); //返回上传成功的数据
  264. } else {
  265. that.$emit('fileSuccess', Res); //返回上传成功的数据
  266. }
  267. uni.showToast({
  268. title: '上传成功'
  269. });
  270. } else {
  271. uni.showToast({
  272. title: '上传失败',
  273. icon: 'none'
  274. });
  275. }
  276. })
  277. .catch(catchRes => {
  278. console.log(catchRes);
  279. uni.showToast({
  280. title: '上传失败',
  281. icon: 'none'
  282. });
  283. });
  284. },
  285. fail(error) {
  286. console.log(error);
  287. }
  288. });
  289. } else {
  290. uni.showToast({
  291. title: '图片过大,无法上传',
  292. icon: 'none'
  293. });
  294. }
  295. }
  296. });
  297. }
  298. }
  299. };
  300. </script>
  301. <style scoped>
  302. .w-drawer {
  303. box-sizing: border-box;
  304. width: 100%;
  305. height: 100%;
  306. display: flex;
  307. display: -webkit-flex;
  308. flex-direction: column;
  309. }
  310. .w-setbox {
  311. box-sizing: border-box;
  312. position: fixed;
  313. z-index: 1000;
  314. left: 0px;
  315. right: 0px;
  316. width: 100%;
  317. height: 100%;
  318. background-color: rgba(0, 0, 0, 0.7);
  319. box-shadow: 0px 3px 12px rgba(0, 0, 0, 0.12);
  320. -webkit-transition: all 0.2 ease;
  321. transition: all 0.2 ease;
  322. bottom: -100%;
  323. /* -webkit-transform: scale(1);
  324. transform: scale(1); */
  325. }
  326. .wShow {
  327. box-sizing: border-box;
  328. bottom: 0;
  329. /* transform: scale(0); */
  330. }
  331. .w-header {
  332. box-sizing: border-box;
  333. width: 100%;
  334. background: #ffffff;
  335. line-height: 40px;
  336. position: absolute;
  337. bottom: 0;
  338. border-top-left-radius: 20px;
  339. border-top-right-radius: 20px;
  340. -webkit-transition: all 0.4 ease;
  341. transition: all 0.4 ease;
  342. /* border-bottom: 10rpx solid #ffffff; */
  343. }
  344. .w-line {
  345. width: 100%;
  346. background-color: #f7f8fa;
  347. height: 8px;
  348. }
  349. .w-item {
  350. box-sizing: border-box;
  351. height: 100rpx;
  352. line-height: 100rpx;
  353. width: 100%;
  354. text-align: center;
  355. }
  356. .w-item1:first-child {
  357. border-bottom: 1px solid #ebedf0;
  358. }
  359. .fileBox {
  360. box-sizing: border-box;
  361. margin: 20rpx;
  362. }
  363. .filePath {
  364. box-sizing: border-box;
  365. width: 100%;
  366. /* padding: 10rpx 0; */
  367. display: flex;
  368. /* flex-direction: column; */
  369. justify-content: space-between;
  370. align-items: center;
  371. border-bottom: 1px solid #c0c0c0;
  372. }
  373. .text1 {
  374. flex: 1;
  375. /* width: 100%; */
  376. /* margin: 10rpx 0 20rpx; */
  377. text-overflow: ellipsis;
  378. overflow: hidden;
  379. white-space: nowrap;
  380. padding-left: 10rpx;
  381. /* text-align: center; */
  382. vertical-align: middle;
  383. }
  384. .w-btn1 {
  385. padding: 10rpx;
  386. padding-right: 20rpx;
  387. /* color: #f56c6c; */
  388. font-size: 38rpx;
  389. }
  390. .w-edit {
  391. color: #cccccc;
  392. /* width: 100%; */
  393. display: flex;
  394. justify-content: flex-end;
  395. }
  396. .imgList {
  397. display: flex;
  398. justify-content: flex-start;
  399. flex-wrap: wrap;
  400. padding: 0 10rpx;
  401. }
  402. .imgItem {
  403. margin: 20rpx 10rpx;
  404. position: relative;
  405. width: 160rpx;
  406. height: 160rpx;
  407. border-radius: 5px;
  408. }
  409. .cancel {
  410. position: absolute;
  411. height: 35rpx;
  412. width: 35rpx;
  413. line-height: 28rpx;
  414. font-size: 30rpx;
  415. text-align: center;
  416. vertical-align: middle;
  417. right: 0px;
  418. top: 0px;
  419. background-color: #f56c6c;
  420. color: #ffffff;
  421. z-index: 999;
  422. border-radius: 0 5px 0 0;
  423. }
  424. .w-img {
  425. border-radius: 5px;
  426. width: 160rpx;
  427. height: 160rpx;
  428. }
  429. .addItem {
  430. margin: 20rpx 10rpx;
  431. width: 160rpx;
  432. height: 160rpx;
  433. border: 1px solid #cccccc;
  434. display: flex;
  435. justify-content: center;
  436. align-items: center;
  437. font-size: 50rpx;
  438. color: #999;
  439. border-radius: 5px;
  440. }
  441. </style>