index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. var _page = _interopRequireDefault(require("../../common/page"));
  3. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
  4. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  5. var _my = require("../../__antmove/api/index.js")(my);
  6. var wx = _my;
  7. (0, _page["default"])({
  8. data: {
  9. fileList1: [],
  10. fileList2: [{
  11. url: "https://img.yzcdn.cn/vant/leaf.jpg"
  12. }, {
  13. url: "https://img.yzcdn.cn/vant/tree.jpg"
  14. }],
  15. fileList3: [{
  16. url: "https://img.yzcdn.cn/vant/sand.jpg"
  17. }],
  18. fileList4: [],
  19. fileList5: [],
  20. fileList6: [],
  21. cloudPath: [],
  22. fileList7: [],
  23. fileList8: [{
  24. url: "https://img.yzcdn.cn/vant/leaf.jpg",
  25. status: "uploading",
  26. message: "上传中"
  27. }, {
  28. url: "https://img.yzcdn.cn/vant/tree.jpg",
  29. status: "failed",
  30. message: "上传失败"
  31. }]
  32. },
  33. beforeRead: function beforeRead(event) {
  34. var _event$detail = event.detail,
  35. file = _event$detail.file,
  36. _event$detail$callbac = _event$detail.callback,
  37. callback = _event$detail$callbac === void 0 ? function () {} : _event$detail$callbac;
  38. if (file.path && file.path.indexOf("jpeg") < 0 || file.url && file.url.indexOf("jpeg") < 0) {
  39. wx.showToast({
  40. title: "请选择jpg图片上传",
  41. icon: "none"
  42. });
  43. callback(false);
  44. return;
  45. }
  46. callback(true);
  47. },
  48. afterRead: function afterRead(event) {
  49. var _event$detail2 = event.detail,
  50. file = _event$detail2.file,
  51. name = _event$detail2.name;
  52. console.log(JSON.stringify(file, null, 2));
  53. var fileList = this.data["fileList".concat(name)];
  54. this.setData(_defineProperty({}, "fileList".concat(name), fileList.concat(file)));
  55. },
  56. oversize: function oversize() {
  57. wx.showToast({
  58. title: "文件超出大小限制",
  59. icon: "none"
  60. });
  61. },
  62. "delete": function _delete(event) {
  63. var _event$detail3 = event.detail,
  64. index = _event$detail3.index,
  65. name = _event$detail3.name;
  66. var fileList = JSON.parse(JSON.stringify(this.data["fileList".concat(name)]));
  67. fileList.splice(index, 1);
  68. this.setData(_defineProperty({}, "fileList".concat(name), fileList));
  69. },
  70. clickPreview: function clickPreview() {},
  71. uploadToCloud: function uploadToCloud() {
  72. var _this = this;
  73. wx.cloud.init();
  74. var _this$data$fileList = this.data.fileList6,
  75. fileList = _this$data$fileList === void 0 ? [] : _this$data$fileList;
  76. if (!fileList.length) {
  77. wx.showToast({
  78. title: "请选择图片",
  79. icon: "none"
  80. });
  81. } else {
  82. var uploadTasks = fileList.map(function (file, index) {
  83. return _this.uploadFilePromise("my-photo".concat(index, ".png"), file);
  84. });
  85. Promise.all(uploadTasks).then(function (data) {
  86. wx.showToast({
  87. title: "上传成功",
  88. icon: "none"
  89. });
  90. var fileList = data.map(function (item) {
  91. return {
  92. url: item.fileID
  93. };
  94. });
  95. _this.setData({
  96. cloudPath: data,
  97. fileList6: fileList
  98. });
  99. })["catch"](function (e) {
  100. wx.showToast({
  101. title: "上传失败",
  102. icon: "none"
  103. });
  104. console.log(e);
  105. });
  106. }
  107. },
  108. uploadFilePromise: function uploadFilePromise(fileName, chooseResult) {
  109. return wx.cloud.uploadFile({
  110. cloudPath: fileName,
  111. filePath: chooseResult.path
  112. });
  113. }
  114. });