fa-download.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <template>
  2. <view>
  3. <view class="u-m-b-30 u-flex u-col-center">
  4. <view class="u-item-title" v-text="item.title"></view>
  5. </view>
  6. <view class="content u-flex" v-if="item.type == 'files'">
  7. <u-grid :col="4" :border="true">
  8. <u-grid-item v-for="(res, ink) in item.value" :key="ink" @click="click(res)">
  9. <u-icon name="download" :size="46"></u-icon>
  10. <view class="grid-text">{{ fileType(res) }}</view>
  11. </u-grid-item>
  12. </u-grid>
  13. </view>
  14. <view class="content u-flex" v-else>
  15. <u-grid :col="4" :border="true">
  16. <u-grid-item @click="click(item.value)">
  17. <u-icon name="download" :size="46"></u-icon>
  18. <view class="grid-text">{{ fileType(item.value) }}</view>
  19. </u-grid-item>
  20. </u-grid>
  21. </view>
  22. <view class="" v-if="downtips">
  23. <u-alert-tips type="warning" title="下载成功,保存路径为:" :close-able="true" :show="downtips" :description="description" @close="downtips = false"></u-alert-tips>
  24. </view>
  25. </view>
  26. </template>
  27. <script>
  28. export default {
  29. name:'fa-download',
  30. props: {
  31. item: {
  32. type: Object,
  33. default: {}
  34. }
  35. },
  36. computed: {
  37. fileType() {
  38. return filename => {
  39. var index1 = filename.lastIndexOf('.');
  40. var index2 = filename.length;
  41. return filename.substring(index1, index2);
  42. };
  43. }
  44. },
  45. data() {
  46. return {
  47. downtips: false,
  48. description: ''
  49. };
  50. },
  51. methods: {
  52. click(url) {
  53. let that = this;
  54. // #ifndef H5
  55. uni.downloadFile({
  56. url: url,
  57. success: res => {
  58. if (res.statusCode === 200) {
  59. uni.saveFile({
  60. tempFilePath: res.tempFilePath,
  61. success: function(res) {
  62. that.downtips = true;
  63. that.description = res.savedFilePath;
  64. console.log(res);
  65. },
  66. fail(err) {
  67. console.log(err);
  68. }
  69. });
  70. }
  71. },
  72. fail: function(e) {
  73. console.log(e);
  74. }
  75. });
  76. // #endif
  77. // #ifdef H5
  78. window.open(url);
  79. // #endif
  80. }
  81. }
  82. };
  83. </script>
  84. <style lang="scss" scoped></style>