fa-upload-file.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <view>
  3. <!-- #ifdef APP-PLUS -->
  4. <l-file ref="lFile" @up-success="onAppSuccess"></l-file>
  5. <!-- #endif -->
  6. <view class="u-flex u-flex-wrap" v-if="isDom">
  7. <view class="u-m-r-20 u-m-b-20" v-for="(item, index) in fileList" :key="index">
  8. <view class="fa-file fa-flex u-row-right">
  9. <view class="u-delete-icon" @click="delfile(index)"><u-icon name="close" color="#ffffff" size="20"></u-icon></view>
  10. <text class="u-tips-color u-m-b-15" v-text="getFileType(item)"></text>
  11. </view>
  12. </view>
  13. <view class="u-m-r-20 u-m-b-20" v-if="(fileType == 'many' && fileList.length >= 1) || fileList.length == 0">
  14. <view class="fa-file fa-flex u-row-center u-col-center u-p-t-10" @click="onUpload">
  15. <u-icon name="plus" size="40" color="#606266"></u-icon>
  16. <view class="select-color">选择文件</view>
  17. </view>
  18. </view>
  19. </view>
  20. <view ref="input" class="input" style="display: none;"></view>
  21. </view>
  22. </template>
  23. <script>
  24. import Emitter from '@/uview-ui/libs/util/emitter.js';
  25. export default {
  26. name: 'fa-upload-file',
  27. mixins: [Emitter],
  28. props: {
  29. value:{
  30. type:String,
  31. default:''
  32. },
  33. fileType: {
  34. type: String,
  35. default: 'single'
  36. },
  37. //页面展示
  38. isDom:{
  39. type:Boolean,
  40. default:false
  41. },
  42. showValue:{
  43. type:Array,
  44. default(){
  45. return []
  46. }
  47. }
  48. },
  49. watch: {
  50. fileList:{
  51. // immediate: true,
  52. deep:true,
  53. handler:function(val){
  54. let value = val.join(',')
  55. this.$emit('input', value);
  56. setTimeout(() => {
  57. this.dispatch('u-form-item', 'on-form-blur', value);
  58. }, 50);
  59. }
  60. },
  61. showValue:{
  62. immediate:true,
  63. handler:function(newValue){
  64. if(newValue.length){
  65. this.fileList = newValue;
  66. }
  67. }
  68. }
  69. },
  70. computed:{
  71. getFileType(){
  72. return item=>{
  73. let url = item.split('.');
  74. return '.'+url[1];
  75. }
  76. }
  77. },
  78. data() {
  79. return {
  80. fileList: []
  81. };
  82. },
  83. mounted() {
  84. // #ifdef H5
  85. var input = document.createElement('input')
  86. input.type = 'file'
  87. input.onchange = (event) => {
  88. this.$api.goUpload({
  89. file:event.target.files[0]
  90. }).then(res=>{
  91. this.onSuccess(res)
  92. })
  93. }
  94. this.$refs.input.$el.appendChild(input)
  95. // #endif
  96. },
  97. methods: {
  98. /* 上传 */
  99. onUpload() {
  100. // #ifdef MP-WEIXIN
  101. this.wxChooseFile();
  102. // #endif
  103. // #ifdef H5
  104. this.h5ChooseFile()
  105. // #endif
  106. // #ifdef APP-PLUS
  107. var formData = {};
  108. let isObj = this.$u.test.object(this.vuex_config.config.upload.multipart);
  109. if (isObj) {
  110. formData = this.vuex_config.config.upload.multipart;
  111. }
  112. this.$refs.lFile.upload({
  113. // nvue页面使用时请查阅nvue获取当前webview的api,当前示例为vue窗口
  114. currentWebview: this.$mp.page.$getAppWebview(),
  115. //调试时ios有跨域,需要后端开启跨域并且接口地址不要使用http://localhost/
  116. url: this.vuex_config.config.upload.uploadurl,
  117. //默认file,上传文件的key
  118. name: 'file',
  119. header: {
  120. token: this.vuex_token || '',
  121. uid: this.vuex_user.id || 0
  122. },
  123. formData: formData
  124. //...其他参数
  125. });
  126. // #endif
  127. },
  128. //移除文件
  129. delfile(index) {
  130. this.fileList.splice(index,1);
  131. },
  132. // #ifdef MP-WEIXIN
  133. wxChooseFile() {
  134. wx.chooseMessageFile({
  135. count: 1,
  136. type: 'file',
  137. success: ({tempFiles}) => {
  138. let [{path:filePath,name:fileName}] = tempFiles;
  139. this.$api.goUpload({filePath:filePath}).then(res=>{
  140. this.onSuccess(res)
  141. })
  142. },
  143. fail:function(err){
  144. console.log(err)
  145. }
  146. })
  147. },
  148. // #endif
  149. // #ifdef H5
  150. h5ChooseFile(){
  151. this.$refs.input.$el.firstChild.click();
  152. },
  153. // #endif
  154. // #ifndef APP-PLUS
  155. onSuccess(res) {
  156. this.$u.toast(res.msg)
  157. if(res.code){
  158. if(this.isDom){
  159. this.fileList.push(res.data.url);
  160. }else{
  161. this.$emit('success',res.data.url);
  162. }
  163. }
  164. }
  165. // #endif
  166. // #ifdef APP-PLUS
  167. onAppSuccess(res){
  168. if(this.$u.test.jsonString(res.data)){
  169. res = JSON.parse(res.data);
  170. this.$u.toast(res.msg)
  171. if(res.code){
  172. if(this.isDom){
  173. this.fileList.push(res.data.url);
  174. }else{
  175. this.$emit('success',res.data.url);
  176. }
  177. }
  178. }else{
  179. this.$u.toast('上传失败!');
  180. }
  181. }
  182. // #endif
  183. }
  184. };
  185. </script>
  186. <style lang="scss">
  187. .fa-file {
  188. background-color: #f4f5f6;
  189. width: 150rpx;
  190. height: 150rpx;
  191. border-radius: 10rpx;
  192. .select-color {
  193. color: #606266;
  194. }
  195. text {
  196. font-size: 40rpx;
  197. }
  198. }
  199. .fa-flex {
  200. display: flex;
  201. flex-direction: column;
  202. align-items: center;
  203. position: relative;
  204. .u-delete-icon {
  205. background-color: #fa3534;
  206. width: 45rpx;
  207. height: 45rpx;
  208. border-radius: 100px;
  209. display: flex;
  210. align-items: center;
  211. justify-content: center;
  212. position: absolute;
  213. top: 15rpx;
  214. right: 15rpx;
  215. }
  216. }
  217. </style>