l-file.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <template>
  2. <view class='t-toptips' :style="{top: top,background: cubgColor}" :class="[show?'t-top-show':'']">
  3. <view v-if="loading" class="flex flex-sub">
  4. <view class="cu-progress flex-sub round striped active sm">
  5. <view :style="{ background: color,width: value + '%'}"></view>
  6. </view>
  7. <text class="margin-left">{{value}}%</text>
  8. </view>
  9. <block v-else>{{msg}}</block>
  10. </view>
  11. </template>
  12. <script>
  13. export default {
  14. props: {
  15. top: {
  16. type: String,
  17. default: 'auto'
  18. },
  19. bgColor: {
  20. type: String,
  21. default: 'rgba(49, 126, 243, 0.5)'
  22. },
  23. color: {
  24. type: String,
  25. default: '#e54d42'
  26. }
  27. },
  28. data () {
  29. return {
  30. cubgColor: '',
  31. loading: false,
  32. value: 5,
  33. show: false,
  34. msg: '执行完毕~'
  35. }
  36. },
  37. methods: {
  38. toast (title = '', { duration = 2000, icon = 'none'} = {}) {
  39. uni.showToast({title, duration, icon})
  40. },
  41. getRequest (url) {
  42. let theRequest = new Object()
  43. let index = url.indexOf('?')
  44. if (index != -1) {
  45. let str = url.substring(index + 1)
  46. let strs = str.split('&')
  47. for (let i = 0; i < strs.length; i++) {
  48. theRequest[strs[i].split('=')[0]] = unescape(strs[i].split('=')[1])
  49. }
  50. }
  51. return theRequest
  52. },
  53. /*
  54. 上传说明:
  55. currentWebview: 当前窗口webview对象
  56. url:上传接口地址
  57. name:上传文件key值
  58. header: 上传接口请求头
  59. ...:body内其他参数
  60. */
  61. appChooseFile ({currentWebview, url, name = 'file', header, ...formData} = {}) {
  62. // #ifdef APP-PLUS
  63. let wv = plus.webview.create('', '/hybrid/html/index.html', {
  64. 'uni-app': 'none', // 不加载uni-app渲染层框架,避免样式冲突
  65. top: 0,
  66. height: '100%',
  67. background: 'transparent'
  68. }, {
  69. url,
  70. header,
  71. formData,
  72. key: name
  73. })
  74. wv.loadURL('/hybrid/html/index.html')
  75. currentWebview.append(wv)
  76. wv.overrideUrlLoading({mode: 'reject'}, (e) => {
  77. let {fileName, id} = this.getRequest(e.url)
  78. fileName = unescape(fileName)
  79. id = unescape(id)
  80. return this.onCommit(
  81. this.$emit('up-success', {fileName, data: {id, statusCode: 200}})
  82. )
  83. })
  84. // #endif
  85. },
  86. wxChooseFile ({url, name = 'file', header, ...formData} = {}) {
  87. wx.chooseMessageFile({
  88. count: 1,
  89. type: 'file',
  90. success: ({tempFiles}) => {
  91. let [{path: filePath, name: fileName}] = tempFiles
  92. this.setdefUI()
  93. return uni.uploadFile({
  94. url,
  95. name,
  96. filePath,
  97. formData,
  98. header,
  99. success: (res) => {
  100. if (res.statusCode == 200) {
  101. let data = JSON.parse(res.data)
  102. // 可自行添加后台返回状态验证
  103. return this.onCommit(this.$emit('up-success', {fileName, data}))
  104. }
  105. return this.errorHandler('文件上传失败', this.upErr)
  106. },
  107. fail: () => this.errorHandler('文件上传失败', this.upErr)
  108. })
  109. },
  110. fail: () => this.errorHandler('文件选择失败', this.upErr)
  111. })
  112. },
  113. /*
  114. 上传
  115. */
  116. upload (param = {}) {
  117. if (!param.url) {
  118. this.toast('上传地址不正确')
  119. return
  120. }
  121. if (this.loading) {
  122. this.toast('还有个文件玩命处理中,请稍候..')
  123. return
  124. }
  125. // #ifdef APP-PLUS
  126. return this.appChooseFile(param)
  127. // #endif
  128. // #ifdef MP-WEIXIN
  129. return this.wxChooseFile(param)
  130. // #endif
  131. },
  132. /*
  133. 打开文件
  134. */
  135. open (filePath) {
  136. let system = uni.getSystemInfoSync().platform
  137. if (system == 'ios') {
  138. filePath = encodeURI(filePath)
  139. }
  140. uni.openDocument({
  141. filePath,
  142. success: (res) => { console.log('打开文档成功') }
  143. })
  144. },
  145. /*
  146. 下载
  147. type: temporary=返回临时地址,local=长期保存到本地
  148. */
  149. download (url, type = 'temporary') {
  150. if (this.loading) {
  151. this.toast('还有个文件玩命处理中,请稍候..')
  152. return
  153. }
  154. this.setdefUI()
  155. return new Promise((resolve, reject) => {
  156. let downloadTask = uni.downloadFile({
  157. url,
  158. success: ({statusCode, tempFilePath}) => {
  159. if (statusCode === 200) {
  160. if (type == 'local') {
  161. uni.saveFile({
  162. tempFilePath,
  163. success: ({savedFilePath}) => this.onCommit(resolve(savedFilePath)),
  164. fail: () => this.errorHandler('下载失败', reject)
  165. })
  166. } else {
  167. this.onCommit(resolve(tempFilePath))
  168. }
  169. }
  170. },
  171. fail: () => this.errorHandler('下载失败', reject)
  172. })
  173. downloadTask.onProgressUpdate(({progress = 0}) => {
  174. if (progress <= 100) {
  175. this.$nextTick(() => {
  176. this.value = progress
  177. })
  178. }
  179. })
  180. })
  181. },
  182. onCommit (resolve) {
  183. this.msg = '执行完毕~'
  184. this.loading = false
  185. this.cubgColor = 'rgba(57, 181, 74, 0.5)'
  186. setTimeout(() => { this.show = false }, 1500)
  187. return resolve
  188. },
  189. setdefUI () {
  190. this.cubgColor = this.bgColor
  191. this.value = 0
  192. this.loading = true
  193. this.show = true
  194. },
  195. upErr (errText) {
  196. this.$emit('up-error', errText)
  197. },
  198. errorHandler (errText, reject) {
  199. this.msg = errText
  200. this.loading = false
  201. this.cubgColor = 'rgba(229, 77, 66, 0.5)'
  202. setTimeout(() => { this.show = false }, 1500)
  203. return reject(errText)
  204. }
  205. }
  206. }
  207. </script>
  208. <style scoped>
  209. .t-toptips {
  210. width: 100%;
  211. padding: 18upx 30upx;
  212. box-sizing: border-box;
  213. position: fixed;
  214. z-index: 90;
  215. color: #fff;
  216. font-size: 30upx;
  217. left: 0;
  218. display: flex;
  219. align-items: center;
  220. justify-content: center;
  221. word-break: break-all;
  222. opacity: 0;
  223. transform: translateZ(0) translateY(-100%);
  224. transition: all 0.3s ease-in-out;
  225. }
  226. .t-top-show {
  227. transform: translateZ(0) translateY(0);
  228. opacity: 1;
  229. }
  230. </style>