uploadFile.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <view class="uploadFile">
  3. <uni-menubar title="文件上传" />
  4. <tm-sheet>
  5. <tm-input :padding="[0,12]" title="文件名称" v-model="form.name" placeholder="请输入文件名称">
  6. </tm-input>
  7. <view class="mt-40 flex flex-between">
  8. <view style="font-size: 26rpx;">
  9. 选择分类
  10. </view>
  11. <view>
  12. <picker @change="bindPickerChange" :value="form.cate_id" range-key="name" :range="cateList">
  13. <view style="font-size: 26rpx;">{{form.cate_id ? cateList.find(v=>v.id == form.cate_id)['name'] : '请选择分类'}}</view>
  14. </picker>
  15. </view>
  16. </view>
  17. <view class="mt-40">
  18. <view class="mb-20" style="font-size: 26rpx;">
  19. 上传文件缩略图
  20. </view>
  21. <view>
  22. <tm-upload ref="upload" :code="200" :max="1" :tips="false" :grid="4"
  23. url="https://cardoa.platomix.net/api/upload" url-key="data" :auto-upload="true" :header="header"
  24. :filelist.sync="form.thumb"></tm-upload>
  25. </view>
  26. </view>
  27. <view class="mt-40">
  28. <view class="mb-20" style="font-size: 26rpx;">
  29. 上传文件<text style="color:#F44336;">(只能选择微信聊天中的文件)</text>
  30. </view>
  31. <view class="mt-20" style="display: flex;justify-content: space-between;">
  32. <view style="color: #2196F3;width: 80%;" v-if="form.path">{{form.name}}.{{form.ext}}</view>
  33. <tm-button theme="blue" size="m" @click="fileAllClick">{{ form.path ? '重新选择' : '选择文件' }}
  34. </tm-button>
  35. </view>
  36. </view>
  37. <view class="mt-40">
  38. <view class="mb-20" style="font-size: 26rpx;">
  39. 是否开放
  40. </view>
  41. <view>
  42. <tm-switch color="blue" v-model="form.showc"></tm-switch>
  43. </view>
  44. </view>
  45. <view class="mt-40">
  46. <tm-button theme="blue" block @click="uploadFile">提交保存</tm-button>
  47. </view>
  48. </tm-sheet>
  49. </view>
  50. </template>
  51. <script>
  52. export default {
  53. data() {
  54. return {
  55. form: {
  56. path: '',
  57. name: '',
  58. cate_id: '',
  59. thumb: [],
  60. showc: true,
  61. },
  62. cateList: [],
  63. header: {
  64. 'Authorization': uni.getStorageSync('token'),
  65. 'Content-Type': 'multipart/form-data'
  66. }
  67. }
  68. },
  69. onLoad(option) {
  70. this.$tm.request.post('file/cate').then(res => {
  71. this.cateList = res.data
  72. })
  73. if (option.id) {
  74. this.form.id = option.id
  75. this.$tm.request.post('file/detail', this.form).then(res => {
  76. this.form = res.data
  77. this.form.cate_id = res.data.pid
  78. this.form.thumb = [this.form.thumb]
  79. })
  80. }
  81. if (option.pid > 0) {
  82. this.form.cate_id = option.pid
  83. }
  84. },
  85. methods: {
  86. bindPickerChange(o) {
  87. this.form.cate_id = this.cateList[o.detail.value].id
  88. },
  89. // 上传任意文件
  90. fileAllClick() {
  91. let that = this
  92. that.isUpload = 0
  93. let uploadTask
  94. uploadTask = uni.chooseMessageFile({
  95. count: 1,
  96. type: 'all',
  97. success(res) {
  98. that.form.size = res.tempFiles[0].size
  99. that.form.name = res.tempFiles[0].name.substring(0, res.tempFiles[0].name.lastIndexOf("."))
  100. that.form.ext = res.tempFiles[0].name.substring(res.tempFiles[0].name.lastIndexOf(".") + 1)
  101. uploadTask = uni.uploadFile({
  102. url: 'https://cardoa.platomix.net/api/upload', //仅为示例,非真实的接口地址
  103. filePath: res.tempFiles[0].path,
  104. name: 'file',
  105. header: that.header,
  106. success(res) {
  107. const data = JSON.parse(res.data)
  108. that.form.path = data.data
  109. uploadTask.abort()
  110. }
  111. })
  112. uploadTask.onProgressUpdate((res) => {
  113. uni.showToast({
  114. title: '已上传' + res.progress + '%',
  115. icon: "none",
  116. mask: true
  117. });
  118. });
  119. }
  120. })
  121. },
  122. uploadFile() {
  123. if (this.form.path == '') {
  124. uni.showToast({
  125. title: "请上传文件",
  126. icon: "none"
  127. })
  128. return false
  129. }
  130. this.form.thumb = this.form.thumb ? this.form.thumb[0] : ''
  131. this.$tm.request.post('file/update', this.form).then(res => {
  132. uni.showToast({
  133. title: "上传成功!",
  134. icon: "none"
  135. })
  136. setTimeout(() => {
  137. uni.navigateBack({
  138. success: () => {
  139. uni.$emit('refresh')
  140. }
  141. })
  142. }, 1000)
  143. })
  144. },
  145. }
  146. }
  147. </script>
  148. <style lang="scss" scoped>
  149. .fileButton {
  150. width: 100%;
  151. position: fixed;
  152. bottom: 0
  153. }
  154. </style>