cl-upload.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <view class="cl-upload-list" :class="[classList]">
  3. <!-- 加载框 -->
  4. <cl-loading-mask :loading="loading" text="上传图片中"></cl-loading-mask>
  5. <!-- 上传列表 -->
  6. <view
  7. class="cl-upload"
  8. v-for="(url, index) in urls"
  9. :key="index"
  10. @tap="choose(index)"
  11. :style="{
  12. height: size[0],
  13. width: size[1],
  14. }"
  15. >
  16. <image class="cl-upload__target" v-show="url" :src="url | format" :mode="imageMode" />
  17. <text class="cl-upload__remove cl-icon-delete-fill" @tap.stop="remove(index)"></text>
  18. </view>
  19. <!-- 添加按钮 -->
  20. <view
  21. v-if="isAppend"
  22. class="cl-upload"
  23. :style="{
  24. height: size[0],
  25. width: size[1],
  26. }"
  27. @tap="choose()"
  28. >
  29. <text class="cl-upload__add cl-icon-plus"></text>
  30. </view>
  31. </view>
  32. </template>
  33. <script>
  34. import { isArray, isString } from "../../utils";
  35. import Form from "../../mixins/form";
  36. /**
  37. * upload 图片上传
  38. * @description 图片上传,支持单图和多图
  39. * @tutorial https://docs.cool-js.com/uni/components/form/upload.html
  40. * @property {String, Array} value 绑定值,图片链接
  41. * @property {String} sizeType 压缩方式,original | compressed
  42. * @property {String} sourceType 选择方式,album | camera
  43. * @property {Array} size 图片大小,默认["200rpx", "200rpx"]
  44. * @property {String} imageMode 图片裁剪、缩放模式,默认aspectFill
  45. * @property {Boolean} multiple 是否支持多选文件
  46. * @property {Number} limit 最大允许上传个数,默认9
  47. * @property {String} action 上传的地址
  48. * @property {Object} headers 设置上传的请求头部
  49. * @property {Object} data 上传时附带的额外参数
  50. * @property {String} name 上传的文件字段名,默认file
  51. * @property {Function} beforeUpload 上传前钩子
  52. * @property {Boolean} disabled 是否禁用
  53. * @property {Boolean} autoUpload 是否自动上传
  54. * @event {Function} success 上传成功时触发,function(response)
  55. * @event {Function} error 上传失败时触发,function(error)
  56. * @event {Function} custom-upload 自定义上传时触发,function(file)
  57. * @event {Function} remove 移除时触发,function(index)
  58. * @example <cl-upload action="http://" />
  59. */
  60. export default {
  61. name: "cl-upload",
  62. props: {
  63. // 绑定值,图片链接
  64. value: [String, Array],
  65. // 压缩方式
  66. sizeType: {
  67. type: Array,
  68. default: () => ["original", "compressed"],
  69. },
  70. // 选择方式
  71. sourceType: {
  72. type: Array,
  73. default: () => ["album", "camera"],
  74. },
  75. // 图片大小
  76. size: {
  77. type: Array,
  78. default: () => ["200rpx", "200rpx"],
  79. },
  80. // 图片裁剪、缩放模式
  81. imageMode: {
  82. type: String,
  83. default: "aspectFill",
  84. },
  85. // 是否支持多选文件
  86. multiple: Boolean,
  87. // 最大允许上传个数
  88. limit: {
  89. type: Number,
  90. default: 9,
  91. },
  92. // 上传的地址
  93. action: String,
  94. // 设置上传的请求头部
  95. headers: Object,
  96. // 上传时附带的额外参数
  97. data: Object,
  98. // 上传的文件字段名
  99. name: {
  100. type: String,
  101. default: "file",
  102. },
  103. // 上传前钩子
  104. beforeUpload: {
  105. type: Function,
  106. },
  107. // 是否禁用
  108. disabled: Boolean,
  109. test: {
  110. type: Boolean,
  111. default: false,
  112. },
  113. // 是否自动上传
  114. autoUpload: {
  115. type: Boolean,
  116. default: true,
  117. },
  118. },
  119. mixins: [Form],
  120. data() {
  121. return {
  122. loading: false,
  123. urls: [],
  124. index: undefined,
  125. };
  126. },
  127. filters: {
  128. format(url) {
  129. return url.replace(/\\/g, "");
  130. },
  131. },
  132. watch: {
  133. value: {
  134. immediate: true,
  135. handler(val) {
  136. this.urls = isArray(val)
  137. ? val
  138. : isString(val)
  139. ? val.split(",").filter(Boolean)
  140. : [];
  141. },
  142. },
  143. urls(val) {
  144. let d = this.multiple ? val : val[0];
  145. this.$emit("input", d);
  146. this.$emit("change", d);
  147. },
  148. },
  149. computed: {
  150. isAppend() {
  151. return this.urls.length < (this.multiple ? this.limit : 1);
  152. },
  153. classList() {
  154. let list = [];
  155. if (this.isDisabled) {
  156. list.push("is-disabled");
  157. }
  158. return list.join(" ");
  159. },
  160. },
  161. methods: {
  162. choose(index) {
  163. if (this.isDisabled) {
  164. return false;
  165. }
  166. this.index = index;
  167. // 可选数量
  168. let count = this.index === undefined ? this.limit - this.urls.length : 1;
  169. // 可选数量验证
  170. if (count <= 0) {
  171. this.$emit("exceed", this.urls);
  172. return false;
  173. }
  174. uni.chooseImage({
  175. count,
  176. sizeType: this.sizeType,
  177. sourceType: this.sourceType,
  178. success: (res) => {
  179. // 文件信息
  180. res.tempFiles.forEach(async (file) => {
  181. // 进度
  182. this.loading = true;
  183. // 上传前验证
  184. if (this.beforeUpload) {
  185. const valid = await this.beforeUpload(file, index);
  186. if (valid === false) {
  187. this.done();
  188. return;
  189. }
  190. }
  191. // 测试
  192. if (this.test) {
  193. this.update(file.path);
  194. this.done();
  195. return;
  196. }
  197. // 自动上传
  198. if (this.autoUpload) {
  199. uni.uploadFile({
  200. url: this.action,
  201. filePath: file.path,
  202. name: this.name,
  203. header: this.headers,
  204. formData: this.data,
  205. success: (res) => {
  206. const { data } = JSON.parse(res.data);
  207. this.update(data);
  208. this.$emit("success", data);
  209. },
  210. fail: (err) => {
  211. this.$emit("error", err);
  212. },
  213. complete: () => {
  214. this.done();
  215. },
  216. });
  217. } else {
  218. // 自定义上传
  219. this.$emit("custom-upload", file);
  220. }
  221. });
  222. },
  223. });
  224. },
  225. update(url) {
  226. if (this.index !== undefined) {
  227. this.urls[this.index] = url;
  228. } else {
  229. this.urls.push(url);
  230. }
  231. this.done();
  232. },
  233. remove(index) {
  234. if (this.disabled) {
  235. return false;
  236. }
  237. this.urls.splice(index, 1);
  238. this.$emit("remove", index);
  239. },
  240. done() {
  241. this.loading = false;
  242. },
  243. },
  244. };
  245. </script>