l-file.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. let wv = plus.webview.create("","/hybrid/html/index.html",{
  63. 'uni-app': 'none', //不加载uni-app渲染层框架,避免样式冲突
  64. top: 0,
  65. height: '100%',
  66. background: 'transparent'
  67. },{
  68. url,
  69. header,
  70. formData,
  71. key: name,
  72. });
  73. wv.loadURL("/hybrid/html/index.html")
  74. currentWebview.append(wv);
  75. wv.overrideUrlLoading({mode:'reject'},(e)=>{
  76. let {fileName,id} = this.getRequest(e.url);
  77. fileName = unescape(fileName);
  78. id = unescape(id);
  79. return this.onCommit(
  80. this.$emit('up-success',{fileName,data: id})
  81. );
  82. });
  83. },
  84. /*
  85. 上传
  86. */
  87. upload(param = {}) {
  88. if (!param.url) {
  89. this.toast('上传地址不正确');
  90. return;
  91. }
  92. if (this.loading) {
  93. this.toast('还有个文件玩命处理中,请稍候..');
  94. return;
  95. }
  96. return this.appChooseFile(param);
  97. },
  98. onCommit(resolve) {
  99. this.msg = '执行完毕~';
  100. this.loading = false;
  101. this.cubgColor = 'rgba(57, 181, 74, 0.5)';
  102. setTimeout(()=>{this.show = false;},1500);
  103. return resolve;
  104. },
  105. setdefUI() {
  106. this.cubgColor = this.bgColor;
  107. this.value = 0;
  108. this.loading = true;
  109. this.show = true;
  110. },
  111. upErr(errText) {
  112. this.$emit('up-error',errText);
  113. },
  114. errorHandler(errText,reject) {
  115. this.msg = errText;
  116. this.loading = false;
  117. this.cubgColor = 'rgba(229, 77, 66, 0.5)';
  118. setTimeout(()=>{this.show = false;},1500);
  119. return reject(errText);
  120. }
  121. }
  122. }
  123. </script>
  124. <style scoped>
  125. .t-toptips {
  126. width: 100%;
  127. padding: 18upx 30upx;
  128. box-sizing: border-box;
  129. position: fixed;
  130. z-index: 90;
  131. color: #fff;
  132. font-size: 30upx;
  133. left: 0;
  134. display: flex;
  135. align-items: center;
  136. justify-content: center;
  137. word-break: break-all;
  138. opacity: 0;
  139. transform: translateZ(0) translateY(-100%);
  140. transition: all 0.3s ease-in-out;
  141. }
  142. .t-top-show {
  143. transform: translateZ(0) translateY(0);
  144. opacity: 1;
  145. }
  146. </style>