l-file.vue 5.5 KB

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