移动端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

187 行
5.8 KiB

  1. <template>
  2. <div class="van-ov-upload-root">
  3. <van-uploader ref="van_uploader"
  4. capture="camera"
  5. v-model="value"
  6. :max-count="maxCount"
  7. preview-size="65px"
  8. @delete="deleteFile"
  9. @oversize="uploadFileOverSize"
  10. :loop="false" max-size="5242880">
  11. <img class="el-ov-uploader-file" slot="default" @click.stop="showActionSheet($event)"
  12. src="../../assets/common/icon_upload.png" width="72px"
  13. alt="">
  14. </van-uploader>
  15. </div>
  16. </template>
  17. <script>
  18. import {Toast} from "vant";
  19. import {getFileInfoList} from "@/api/common";
  20. export default {
  21. name: "van-ov-uploader",
  22. props: {
  23. value: {
  24. default: Array
  25. },
  26. pictureid: {
  27. default: String
  28. },
  29. maxCount: {
  30. default: 6
  31. }
  32. },
  33. data() {
  34. return {
  35. deleteFileIds: [],
  36. pictureList: []
  37. }
  38. },
  39. watch: {
  40. value: function (val) {
  41. this.deleteFileInput()
  42. }
  43. },
  44. mounted() {
  45. this.deleteFileInput()
  46. }, beforeUpdate() {
  47. this.deleteFileInput()
  48. },
  49. updated() {
  50. this.deleteFileInput()
  51. },
  52. methods: {
  53. deleteFileInput() {
  54. document.getElementsByClassName('van-uploader__input')[0].remove()
  55. },
  56. showActionSheet(event) {
  57. event.preventDefault();
  58. let that = this
  59. uexWindow.actionSheet({
  60. title: "",
  61. cancel: "取消",
  62. buttons: "拍照上传,本地上传"
  63. }, function (index) {
  64. if (index == 0) {
  65. try {
  66. uexCamera.open(1, '', function (data) {
  67. let uploader = uexUploaderMgr.create({
  68. url: process.env.VUE_APP_BASE_API + "/api/mongodb/fileInfo/uploadFileInfo?parentId=" + that.pictureid,
  69. type: 1
  70. });
  71. if (!uploader) {
  72. alert("创建失败!");
  73. }
  74. uexUploaderMgr.uploadFile(uploader, data, "file", 1, 640, function (packageSize, percent, responseString, status) {
  75. if (status == 1) {
  76. let data = {
  77. parentId: that.pictureid,
  78. }
  79. this.pictureList = []
  80. getFileInfoList(data).then((resp) => {
  81. if (resp.code == 1 && resp.data.rows.length != 0) {
  82. let item = resp.data.rows[resp.data.rows.length - 1]
  83. that.value.push({
  84. url: process.env.VUE_APP_BASE_API + '/api/mongodb/downloadFile?fileId=' + item.id,
  85. isImage: true
  86. })
  87. that.deleteFileInput()
  88. }
  89. }).catch()
  90. } else if (status == 2) {
  91. alert("上传失败");
  92. }
  93. that.deleteFileInput()
  94. });
  95. })
  96. } catch (e) {
  97. alert(JSON.stringify(e))
  98. }
  99. } else if (index == 1) {
  100. //打开多图片选择器
  101. var data = {
  102. min: 1,
  103. max: 1,
  104. quality: 1,
  105. detailedInfo: true,
  106. // style: 1,
  107. };
  108. uexImage.openPicker(data, function (error, info) {
  109. if (error == -1) {
  110. } else if (error == 0) {
  111. if (info.detailedImageInfo) {
  112. info.detailedImageInfo.map(async (item) => {
  113. var uploader = uexUploaderMgr.create({
  114. url: process.env.VUE_APP_BASE_API + "/api/mongodb/fileInfo/uploadFileInfo?parentId=" + that.pictureid,
  115. type: 1
  116. });
  117. if (!uploader) {
  118. alert("创建失败!");
  119. }
  120. uexUploaderMgr.uploadFile(uploader, item.localPath, "file", 0, 640, function (packageSize, percent, responseString, status) {
  121. if (status == 1) {
  122. let data = {
  123. parentId: that.pictureid,
  124. }
  125. this.pictureList = []
  126. getFileInfoList(data).then((resp) => {
  127. if (resp.code == 1 && resp.data.rows.length != 0) {
  128. let item = resp.data.rows[resp.data.rows.length - 1]
  129. that.value.push({
  130. url: process.env.VUE_APP_BASE_API + '/api/mongodb/downloadFile?fileId=' + item.id,
  131. isImage: true
  132. })
  133. }
  134. that.deleteFileInput()
  135. })
  136. }
  137. })
  138. })
  139. }
  140. }
  141. });
  142. that.deleteFileInput()
  143. }
  144. });
  145. },
  146. deleteFile(obj) {
  147. let url = obj.url
  148. let fileId = url.substr(url.lastIndexOf("fileId=") + 7)
  149. this.deleteFileIds.push(fileId)
  150. }
  151. ,
  152. uploadFileOverSize(obj) {
  153. Toast("图片大小超出限制,图片大小应在 5MB 之内");
  154. }
  155. ,
  156. }
  157. }
  158. </script>
  159. <style lang="scss">
  160. .van-ov-upload-root {
  161. .van-uploader__input-wrapper {
  162. width: 72px;
  163. height: 72px;
  164. }
  165. .el-ov-uploader-file {
  166. position: absolute;
  167. top: 0;
  168. left: 0;
  169. width: 72px;
  170. height: 72px;
  171. overflow: hidden;
  172. cursor: pointer;
  173. }
  174. }
  175. </style>
  176. ————————————————
  177. 版权声明:本文为CSDN博主「bydongxin」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
  178. 原文链接:https://blog.csdn.net/bydongxin/article/details/108367086