index.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <view class="upload">
  3. <view class="upload_1">
  4. <view class="list" v-for="(item,index) in list" :key="index">
  5. <view class="name">
  6. <text @tap="toView(index,item)" class="text">{{item.name}}</text>
  7. <uni-icons @tap="uplDel(index,item)" type="close" size="20"></uni-icons>
  8. </view>
  9. </view>
  10. <view class="list" v-if="list&&list.length<count">
  11. <button size="mini" class="button" @tap="uplSuc()">文件上传</button>
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. export default {
  18. props: {
  19. list: {
  20. type: Array,
  21. },
  22. name: {
  23. type: String,
  24. },
  25. count: {
  26. type: Number,
  27. default: 1
  28. }
  29. },
  30. data() {
  31. return {
  32. };
  33. },
  34. methods: {
  35. uplSuc() {
  36. const that = this;
  37. let serverFile = that.$config.serverFile;
  38. uni.chooseFile({
  39. count: 1,
  40. extension: ['.doc', '.xls', '.ppt', '.pdf', '.docx', '.xlsx', '.pptx'],
  41. success: async function(res) {
  42. let tempFile = JSON.parse(JSON.stringify(res.tempFilePaths));
  43. const arr = await that.$apifile(`/jcyjdt/basic/upload`, 'file', tempFile[0],
  44. 'file');
  45. that.$emit('uplSuc', {
  46. data: {
  47. name: arr.name,
  48. uri: arr.uri,
  49. url: serverFile + arr.uri
  50. },
  51. name: that.name
  52. })
  53. }
  54. });
  55. },
  56. // 删除图片
  57. uplDel(index, e) {
  58. const that = this;
  59. that.$emit('uplDel', {
  60. data: {
  61. file: e,
  62. index: index
  63. },
  64. name: that.name
  65. })
  66. },
  67. // 文件预览
  68. toView(index, item) {
  69. const that = this;
  70. let serverUrl = that.$config.serverFile;
  71. let url = item[0].url
  72. // 判断前面是否有http
  73. const http = url.lastIndexOf("http");
  74. if (http == -1) url = serverUrl + url
  75. //获取最后一个.的位置
  76. const arr = url.lastIndexOf(".");
  77. //获取后缀
  78. const typeName = url.substr(arr + 1);
  79. //支持预览的文件类型
  80. //微信小程序
  81. let fileType = ['doc', 'xls', 'ppt', 'pdf', 'docx', 'xlsx', 'pptx'];
  82. uni.showLoading({
  83. title: '加载中',
  84. mask: true
  85. })
  86. //下载文件资源到本地
  87. uni.downloadFile({
  88. url: url,
  89. success: function(res) {
  90. uni.hideLoading();
  91. var filePath = res.tempFilePath;
  92. if (!fileType.includes(typeName)) {
  93. return false;
  94. }
  95. uni.showLoading({
  96. title: '正在打开',
  97. mask: true
  98. })
  99. // 新开页面打开文档,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。
  100. uni.openDocument({
  101. filePath: filePath,
  102. fileType: typeName, // 文件类型,指定文件类型打开文件,有效值 doc, xls, ppt, pdf, docx, xlsx, pptx
  103. success: res => {
  104. uni.hideLoading();
  105. console.log('打开文档成功', res);
  106. },
  107. fail: openError => {
  108. uni.hideLoading();
  109. console.log('fail:' + JSON.stringify(openError));
  110. }
  111. });
  112. },
  113. fail: function(err) {
  114. uni.hideLoading();
  115. console.log('fail:' + JSON.stringify(err));
  116. }
  117. });
  118. }
  119. }
  120. }
  121. </script>
  122. <style lang="scss">
  123. .upload {
  124. padding: 0 2vw;
  125. .upload_1 {
  126. .list {
  127. .name {
  128. display: flex;
  129. justify-content: space-between;
  130. margin: 0 0 1vw 0;
  131. .text {
  132. font-size: var(--font14Size);
  133. color: var(--f85Color);
  134. }
  135. }
  136. .button {
  137. color: var(--mainColor);
  138. background-color: var(--f07CColor);
  139. }
  140. }
  141. }
  142. }
  143. </style>