tui-upload.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. Component({
  2. properties: {
  3. //初始化图片路径
  4. value: {
  5. type: Array,
  6. value: [],
  7. observer(val) {
  8. this.initImages()
  9. }
  10. },
  11. //禁用删除
  12. forbidDel: {
  13. type: Boolean,
  14. value: false
  15. },
  16. //禁用添加
  17. forbidAdd: {
  18. type: Boolean,
  19. value: false
  20. },
  21. //服务器地址
  22. serverUrl: {
  23. type: String,
  24. value: ""
  25. },
  26. //限制数
  27. limit: {
  28. type: Number,
  29. value: 9
  30. },
  31. //original 原图,compressed 压缩图,默认二者都有
  32. sizeType: {
  33. type: Array,
  34. value:['original', 'compressed']
  35. },
  36. //album 从相册选图,camera 使用相机,默认二者都有。如需直接开相机或直接选相册,请只使用一个选项
  37. sourceType: {
  38. type: Array,
  39. value:['album', 'camera']
  40. },
  41. //可上传图片类型,默认为空,不限制 Array<String> [jpg,png,gif]
  42. imageFormat: {
  43. type: Array,
  44. value:[]
  45. },
  46. //单张图片大小限制 MB
  47. size: {
  48. type: Number,
  49. value: 4
  50. },
  51. //项目名,默认为 file
  52. fileKeyName: {
  53. type: String,
  54. value: "file"
  55. },
  56. //HTTP 请求 Header, header 中不能设置 Referer。
  57. header: {
  58. type: Object,
  59. value: {}
  60. },
  61. //HTTP 请求中其他额外的 form data
  62. formData: {
  63. type: Object,
  64. value: {}
  65. }
  66. },
  67. lifetimes: {
  68. ready: function () {
  69. this.initImages()
  70. }
  71. },
  72. data: {
  73. //图片地址
  74. imageList: [],
  75. //上传状态:1-上传成功 2-上传中 3-上传失败
  76. statusArr: []
  77. },
  78. /**
  79. * 组件的方法列表
  80. */
  81. methods: {
  82. initImages() {
  83. let imgArr = [...this.data.value]
  84. let status = []
  85. for (let item of imgArr) {
  86. status.push("1")
  87. }
  88. this.setData({
  89. imageList: [...imgArr],
  90. statusArr: status
  91. })
  92. },
  93. // 重新上传
  94. reUpLoad(e) {
  95. let index = Number(e.currentTarget.dataset.index)
  96. let value = `statusArr[${index}]`
  97. this.setData({
  98. [value]: "2"
  99. })
  100. this.change()
  101. this.uploadImage(index, this.data.imageList[index]).then(() => {
  102. this.change()
  103. }).catch(() => {
  104. this.change()
  105. })
  106. },
  107. change() {
  108. let status = ~this.data.statusArr.indexOf("2") ? 2 : 1
  109. if (status != 2 && ~this.data.statusArr.indexOf("3")) {
  110. // 上传失败
  111. status = 3
  112. }
  113. this.triggerEvent('complete', {
  114. status: status,
  115. imgArr: this.data.imageList
  116. })
  117. },
  118. toast(text) {
  119. text && wx.showToast({
  120. title: text,
  121. icon: "none"
  122. });
  123. },
  124. chooseImage: function () {
  125. let _this = this;
  126. wx.chooseImage({
  127. count: _this.data.limit - _this.data.imageList.length,
  128. sizeType: _this.data.sizeType,
  129. sourceType: _this.data.sourceType,
  130. success: function (e) {
  131. let imageArr = [];
  132. let status = []
  133. for (let i = 0; i < e.tempFiles.length; i++) {
  134. let len = _this.data.imageList.length;
  135. if (len >= _this.data.limit) {
  136. _this.toast(`最多可上传${_this.data.limit}张图片`);
  137. break;
  138. }
  139. //过滤图片类型
  140. let path = e.tempFiles[i].path;
  141. if (_this.data.imageFormat.length > 0) {
  142. let format = path.split(".")[(path.split(".")).length - 1];
  143. if (_this.data.imageFormat.indexOf(format) == -1) {
  144. let text = `只能上传 ${_this.data.imageFormat.join(',')} 格式图片!`
  145. _this.toast(text);
  146. continue;
  147. }
  148. }
  149. //过滤超出大小限制图片
  150. let size = e.tempFiles[i].size;
  151. if (_this.data.size * 1024 * 1024 < size){
  152. let err=`单张图片大小不能超过:${_this.data.size}MB`
  153. _this.toast(err);
  154. continue;
  155. }
  156. imageArr.push(path)
  157. status.push("2")
  158. }
  159. _this.setData({
  160. imageList: _this.data.imageList.concat(imageArr),
  161. statusArr: _this.data.statusArr.concat(status)
  162. })
  163. _this.change()
  164. let start = _this.data.imageList.length - imageArr.length
  165. for (let j = 0; j < imageArr.length; j++) {
  166. let index = start + j
  167. //服务器地址
  168. if (_this.data.serverUrl) {
  169. _this.uploadImage(index, imageArr[j]).then(() => {
  170. _this.change()
  171. }).catch(() => {
  172. _this.change()
  173. })
  174. } else {
  175. //无服务器地址则直接返回成功
  176. let value = `statusArr[${index}]`
  177. _this.setData({
  178. [value]: "1"
  179. })
  180. _this.change()
  181. }
  182. }
  183. }
  184. })
  185. },
  186. uploadImage: function (index, url) {
  187. let _this = this;
  188. let status = `statusArr[${index}]`;
  189. return new Promise((resolve, reject) => {
  190. wx.uploadFile({
  191. url: this.data.serverUrl,
  192. name: this.data.fileKeyName,
  193. header: this.data.header,
  194. formData: this.data.formData,
  195. filePath: url,
  196. success: function (res) {
  197. console.log(res)
  198. if (res.statusCode == 200) {
  199. //返回结果 此处需要按接口实际返回进行修改
  200. let d = JSON.parse(res.data.replace(/\ufeff/g, "") || "{}")
  201. //判断code,以实际接口规范判断
  202. if (d.code % 100 === 0) {
  203. // 上传成功 d.url 为上传后图片地址,以实际接口返回为准
  204. if (d.url) {
  205. let value = `imageList[${index}]`
  206. _this.setData({
  207. [value]: d.url
  208. })
  209. }
  210. _this.setData({
  211. [status]: d.url ? "1" : "3"
  212. })
  213. } else {
  214. // 上传失败
  215. _this.setData({
  216. [status]: "3"
  217. })
  218. }
  219. resolve(index)
  220. } else {
  221. _this.setData({
  222. [status]: "3"
  223. })
  224. reject(index)
  225. }
  226. },
  227. fail: function (res) {
  228. _this.setData({
  229. [status]: "3"
  230. })
  231. reject(index)
  232. }
  233. })
  234. })
  235. },
  236. delImage: function (e) {
  237. let index = Number(e.currentTarget.dataset.index)
  238. let imgList = [...this.data.imageList]
  239. let status = [...this.data.statusArr]
  240. imgList.splice(index, 1)
  241. status.splice(index, 1)
  242. this.setData({
  243. imageList: imgList,
  244. statusArr: status
  245. })
  246. this.triggerEvent("remove", {
  247. index: index
  248. })
  249. this.change()
  250. },
  251. previewImage: function (e) {
  252. let index = Number(e.currentTarget.dataset.index)
  253. if (!this.data.imageList.length) return;
  254. wx.previewImage({
  255. current: this.data.imageList[index],
  256. loop: true,
  257. urls: this.data.imageList
  258. })
  259. }
  260. }
  261. })