index.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <template>
  2. <view class="jnpf-file">
  3. <view class="jnpf-file-box" :style="{textAlign:align}">
  4. <view class="jnpf-file-box-line">
  5. <!-- #ifndef APP-HARMONY -->
  6. <lsj-upload :ref="lsjUpload" :childId="childId" :width="width" :height="height" :option="option"
  7. :size="fileSize" :formats="getFormats" :instantly="instantly" @uploadEnd="onuploadEnd"
  8. :lsjUpload="lsjUpload">
  9. <text class="u-m-r-20 icon-ym icon-ym-generator-menu"></text>
  10. <text>上传文件</text>
  11. </lsj-upload>
  12. <!-- #endif -->
  13. <!-- #ifdef APP-HARMONY -->
  14. <view @click="chooseFile">
  15. <text class="u-m-r-20 icon-ym icon-ym-generator-menu"></text>
  16. <text>上传文件</text>
  17. </view>
  18. <!-- #endif -->
  19. </view>
  20. </view>
  21. </view>
  22. </template>
  23. <script>
  24. const imgTypeList = ['png', 'jpg', 'jpeg', 'bmp', 'gif']
  25. export default {
  26. name: 'jnpf-upload-img',
  27. inheritAttrs: false,
  28. props: {
  29. disabled: {
  30. type: Boolean,
  31. default: false
  32. },
  33. limit: {
  34. type: [Number, String],
  35. default: 9
  36. },
  37. fileSize: {
  38. type: Number,
  39. default: 5
  40. },
  41. accept: {
  42. type: [Number, String],
  43. default: ''
  44. },
  45. parentId: {
  46. type: [Number, String],
  47. default: 0
  48. },
  49. pathType: {
  50. type: String,
  51. default: 'defaultPath'
  52. },
  53. isAccount: {
  54. type: Number,
  55. default: 0
  56. },
  57. folder: {
  58. type: String,
  59. default: ''
  60. },
  61. vModel: {
  62. type: String,
  63. default: ''
  64. },
  65. detailed: {
  66. type: Boolean,
  67. default: false
  68. },
  69. align: {
  70. type: String,
  71. default: 'right'
  72. },
  73. sortRule: {
  74. type: Array,
  75. default: () => ([])
  76. },
  77. timeFormat: {
  78. type: String,
  79. default: ''
  80. },
  81. },
  82. data() {
  83. return {
  84. percent: '',
  85. fileList: [],
  86. // 上传接口参数
  87. option: {
  88. parentId: 0
  89. },
  90. params: {
  91. pathType: this.pathType,
  92. isAccount: this.isAccount,
  93. folder: this.folder,
  94. sortRule: (this.sortRule || []).join(),
  95. timeFormat: this.timeFormat
  96. },
  97. // 选择文件后是否立即自动上传,true=选择后立即上传
  98. instantly: true,
  99. size: 30,
  100. list: [],
  101. deletable: false,
  102. childId: 'upload' + this.$u.guid(3, false, 2),
  103. lsjUpload: 'lsjUpload' + this.$u.guid(3, false, 2),
  104. width: '638rpx',
  105. height: '60rpx',
  106. parId: 0
  107. }
  108. },
  109. watch: {
  110. parentId: {
  111. handler(val) {
  112. this.parId = val
  113. this.option.parentId = val
  114. },
  115. immediate: true
  116. }
  117. },
  118. computed: {
  119. baseURL() {
  120. return this.define.baseURL
  121. },
  122. comUploadUrl() {
  123. return this.define.comUploadUrl
  124. },
  125. getFormats() {
  126. let formats = this.accept
  127. formats = formats.replace("image/*", 'png,jpg,jpeg,bmp,gif,webp,psd,svg,tiff')
  128. formats = formats.replace("video/*", 'avi,wmv,mpg,mpeg,mov,rm,ram,swf,flv,mp4,wma,rm,rmvb,flv,mpg,mkv')
  129. formats = formats.replace("audio/*", 'mp3,wav,aif,midi,m4a')
  130. return formats
  131. },
  132. },
  133. created() {
  134. const token = uni.getStorageSync('token')
  135. this.option = {
  136. url: this.baseURL + `/api/file/Document/Uploader`, //替换为你的接口地址
  137. name: 'file',
  138. header: {
  139. 'Authorization': token,
  140. 'uid': '27682',
  141. 'client': 'app',
  142. 'accountid': 'DP',
  143. },
  144. data: this.params,
  145. parentId: this.parId
  146. }
  147. },
  148. methods: {
  149. // 某文件上传结束回调(成功失败都回调)
  150. onuploadEnd(item) {
  151. if (item['responseText']) {
  152. let response = JSON.parse(item.responseText)
  153. this.$emit('callback', response)
  154. }
  155. },
  156. chooseFile() {
  157. uni.chooseFile({
  158. count: 1, //默认100
  159. success: (res) => {
  160. const tempFilePaths = res.tempFilePaths;
  161. const fileName = res.tempFiles[0].name
  162. const token = uni.getStorageSync('token')
  163. uni.uploadFile({
  164. url: this.baseURL + '/api/file/Document/Uploader',
  165. filePath: tempFilePaths[0],
  166. name: 'file',
  167. formData: {
  168. ...this.params,
  169. parentId: this.parId,
  170. fileName
  171. },
  172. header: {
  173. 'Authorization': token,
  174. 'uid': '27682',
  175. 'client': 'app',
  176. 'accountid': 'DP',
  177. },
  178. success: (response) => {
  179. let item = JSON.parse(response.data)
  180. this.$emit('callback', {
  181. code: item.code,
  182. msg: '上传成功'
  183. })
  184. }
  185. });
  186. }
  187. });
  188. }
  189. }
  190. }
  191. </script>
  192. <style lang="scss" scoped>
  193. .jnpf-file {
  194. width: 100%;
  195. .jnpf-file-box {
  196. text-align: left !important;
  197. .jnpf-file-box-line {
  198. height: 1.9rem !important;
  199. line-height: 1.9rem;
  200. .lsj-file {
  201. .hFile {
  202. input {
  203. height: 23px !important;
  204. }
  205. }
  206. }
  207. }
  208. .tipText {
  209. color: #606266;
  210. word-break: break-all;
  211. line-height: 48rpx;
  212. }
  213. .jnpf-file-item {
  214. justify-content: space-between;
  215. flex-direction: row;
  216. .jnpf-file-item-txt {
  217. width: 230rpx;
  218. flex: auto;
  219. }
  220. .showLeft {
  221. text-align: left;
  222. }
  223. .closeBox {
  224. height: 60rpx;
  225. align-items: flex-end;
  226. justify-content: space-evenly;
  227. flex: 0.2;
  228. .closeTxt {
  229. width: 36rpx;
  230. height: 36rpx;
  231. border-radius: 50%;
  232. background-color: #fa3534;
  233. color: #FFFFFF;
  234. font-size: 20rpx;
  235. align-items: center;
  236. justify-content: center;
  237. line-height: 36rpx;
  238. }
  239. }
  240. }
  241. }
  242. }
  243. </style>