fa-upload-file.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <template>
  2. <view>
  3. <!-- #ifdef APP-PLUS -->
  4. <fa-file ref="faFile" @up-success="onAppSuccess"></fa-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 '@/uni_modules/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. vuex_token:'',
  82. vuex_user:{}
  83. };
  84. },
  85. mounted() {
  86. this.vuex_token=this.$db.get('token');
  87. this.vuex_user=this.$db.get('user');
  88. // #ifdef H5
  89. var input = document.createElement('input')
  90. input.type = 'file'
  91. input.onchange = (event) => {
  92. this.$api.goUpload({
  93. file:event.target.files[0]
  94. }).then(res=>{
  95. this.onSuccess(res)
  96. })
  97. }
  98. this.$refs.input.$el.appendChild(input)
  99. // #endif
  100. },
  101. methods: {
  102. /* 上传 */
  103. onUpload() {
  104. // #ifdef MP-WEIXIN
  105. this.wxChooseFile();
  106. // #endif
  107. // #ifdef H5
  108. this.h5ChooseFile()
  109. // #endif
  110. // #ifdef APP-PLUS
  111. var formData = {};
  112. let isObj = this.$u.test.object(this.vuex_config.upload.multipart);
  113. if (isObj) {
  114. formData = this.vuex_config.upload.multipart;
  115. }
  116. this.$refs.faFile.upload({
  117. // nvue页面使用时请查阅nvue获取当前webview的api,当前示例为vue窗口
  118. currentWebview: this.$mp.page.$getAppWebview(),
  119. //调试时ios有跨域,需要后端开启跨域并且接口地址不要使用http://localhost/
  120. url: this.vuex_config.upload.uploadurl,
  121. //默认file,上传文件的key
  122. name: 'file',
  123. header: {
  124. token: this.vuex_token || '',
  125. uid: this.vuex_user.id || 0
  126. },
  127. formData: formData
  128. //...其他参数
  129. });
  130. // #endif
  131. },
  132. //移除文件
  133. delfile(index) {
  134. this.fileList.splice(index,1);
  135. },
  136. // #ifdef MP-WEIXIN
  137. wxChooseFile() {
  138. wx.chooseMessageFile({
  139. count: 1,
  140. type: 'file',
  141. success: ({tempFiles}) => {
  142. let [{path:filePath,name:fileName}] = tempFiles;
  143. this.$api.goUpload({filePath:filePath}).then(res=>{
  144. this.onSuccess(res)
  145. })
  146. },
  147. fail:function(err){
  148. console.log(err)
  149. }
  150. })
  151. },
  152. // #endif
  153. // #ifdef H5
  154. h5ChooseFile(){
  155. this.$refs.input.$el.firstChild.click();
  156. },
  157. // #endif
  158. // #ifndef APP-PLUS
  159. onSuccess(res) {
  160. this.$u.toast(res.msg)
  161. if(res.code){
  162. if(this.isDom){
  163. this.fileList.push(res.data.url);
  164. }else{
  165. this.$emit('success',res.data.url);
  166. }
  167. }
  168. }
  169. // #endif
  170. // #ifdef APP-PLUS
  171. onAppSuccess(res){
  172. if(this.$u.test.jsonString(res.data)){
  173. res = JSON.parse(res.data);
  174. this.$common.errorToShow(res.msg)
  175. if(res.code){
  176. if(this.isDom){
  177. this.fileList.push(res.data.url);
  178. }else{
  179. this.$emit('success',res.data.url);
  180. }
  181. }
  182. }else{
  183. this.$common.errorToShow('上传失败!');
  184. }
  185. }
  186. // #endif
  187. }
  188. };
  189. </script>
  190. <style lang="scss">
  191. .fa-file {
  192. background-color: #f4f5f6;
  193. width: 150rpx;
  194. height: 150rpx;
  195. border-radius: 10rpx;
  196. .select-color {
  197. color: #606266;
  198. }
  199. text {
  200. font-size: 40rpx;
  201. }
  202. }
  203. .fa-flex {
  204. display: flex;
  205. flex-direction: column;
  206. align-items: center;
  207. position: relative;
  208. .u-delete-icon {
  209. background-color: #fa3534;
  210. width: 45rpx;
  211. height: 45rpx;
  212. border-radius: 100px;
  213. display: flex;
  214. align-items: center;
  215. justify-content: center;
  216. position: absolute;
  217. top: 15rpx;
  218. right: 15rpx;
  219. }
  220. }
  221. </style>