uploader.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. module.exports =
  2. /******/ (function(modules) { // webpackBootstrap
  3. /******/ // The module cache
  4. /******/ var installedModules = {};
  5. /******/
  6. /******/ // The require function
  7. /******/ function __webpack_require__(moduleId) {
  8. /******/
  9. /******/ // Check if module is in cache
  10. /******/ if(installedModules[moduleId]) {
  11. /******/ return installedModules[moduleId].exports;
  12. /******/ }
  13. /******/ // Create a new module (and put it into the cache)
  14. /******/ var module = installedModules[moduleId] = {
  15. /******/ i: moduleId,
  16. /******/ l: false,
  17. /******/ exports: {}
  18. /******/ };
  19. /******/
  20. /******/ // Execute the module function
  21. /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
  22. /******/
  23. /******/ // Flag the module as loaded
  24. /******/ module.l = true;
  25. /******/
  26. /******/ // Return the exports of the module
  27. /******/ return module.exports;
  28. /******/ }
  29. /******/
  30. /******/
  31. /******/ // expose the modules object (__webpack_modules__)
  32. /******/ __webpack_require__.m = modules;
  33. /******/
  34. /******/ // expose the module cache
  35. /******/ __webpack_require__.c = installedModules;
  36. /******/
  37. /******/ // define getter function for harmony exports
  38. /******/ __webpack_require__.d = function(exports, name, getter) {
  39. /******/ if(!__webpack_require__.o(exports, name)) {
  40. /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
  41. /******/ }
  42. /******/ };
  43. /******/
  44. /******/ // define __esModule on exports
  45. /******/ __webpack_require__.r = function(exports) {
  46. /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
  47. /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
  48. /******/ }
  49. /******/ Object.defineProperty(exports, '__esModule', { value: true });
  50. /******/ };
  51. /******/
  52. /******/ // create a fake namespace object
  53. /******/ // mode & 1: value is a module id, require it
  54. /******/ // mode & 2: merge all properties of value into the ns
  55. /******/ // mode & 4: return value when already ns object
  56. /******/ // mode & 8|1: behave like require
  57. /******/ __webpack_require__.t = function(value, mode) {
  58. /******/ if(mode & 1) value = __webpack_require__(value);
  59. /******/ if(mode & 8) return value;
  60. /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
  61. /******/ var ns = Object.create(null);
  62. /******/ __webpack_require__.r(ns);
  63. /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
  64. /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
  65. /******/ return ns;
  66. /******/ };
  67. /******/
  68. /******/ // getDefaultExport function for compatibility with non-harmony modules
  69. /******/ __webpack_require__.n = function(module) {
  70. /******/ var getter = module && module.__esModule ?
  71. /******/ function getDefault() { return module['default']; } :
  72. /******/ function getModuleExports() { return module; };
  73. /******/ __webpack_require__.d(getter, 'a', getter);
  74. /******/ return getter;
  75. /******/ };
  76. /******/
  77. /******/ // Object.prototype.hasOwnProperty.call
  78. /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
  79. /******/
  80. /******/ // __webpack_public_path__
  81. /******/ __webpack_require__.p = "";
  82. /******/
  83. /******/
  84. /******/ // Load entry module and return exports
  85. /******/ return __webpack_require__(__webpack_require__.s = 22);
  86. /******/ })
  87. /************************************************************************/
  88. /******/ ({
  89. /***/ 22:
  90. /***/ (function(module, exports, __webpack_require__) {
  91. "use strict";
  92. Component({
  93. options: {
  94. addGlobalClass: true
  95. },
  96. properties: {
  97. title: {
  98. type: String,
  99. value: '图片上传'
  100. },
  101. sizeType: {
  102. type: Array,
  103. value: ['original', 'compressed']
  104. },
  105. sourceType: {
  106. type: Array,
  107. value: ['album', 'camera']
  108. },
  109. maxSize: {
  110. type: Number,
  111. value: 5 * 1024 * 1024
  112. },
  113. maxCount: {
  114. // 最多上传多少个文件
  115. type: Number,
  116. value: 1
  117. },
  118. files: {
  119. // 当前的图片列表, {url, error, loading}
  120. type: Array,
  121. value: [],
  122. observer(newVal) {
  123. this.setData({
  124. currentFiles: newVal
  125. });
  126. }
  127. },
  128. select: {
  129. // 过滤某个文件
  130. type: null,
  131. value: () => {}
  132. },
  133. upload: {
  134. // 返回Promise的一个文件上传的函数
  135. type: null,
  136. value: null
  137. },
  138. tips: {
  139. type: String,
  140. value: ''
  141. },
  142. extClass: {
  143. type: String,
  144. value: ''
  145. },
  146. showDelete: {
  147. // 是否显示delete按钮
  148. type: Boolean,
  149. value: true
  150. }
  151. },
  152. data: {
  153. currentFiles: [],
  154. showPreview: false,
  155. previewImageUrls: []
  156. },
  157. ready() {},
  158. methods: {
  159. previewImage(e) {
  160. const {
  161. index
  162. } = e.currentTarget.dataset;
  163. const previewImageUrls = [];
  164. this.data.files.forEach(item => {
  165. previewImageUrls.push(item.url);
  166. });
  167. this.setData({
  168. previewImageUrls,
  169. previewCurrent: index,
  170. showPreview: true
  171. });
  172. },
  173. chooseImage() {
  174. if (this.uploading) return;
  175. wx.chooseImage({
  176. count: this.data.maxCount - this.data.files.length,
  177. sizeType: this.data.sizeType,
  178. sourceType: this.data.sourceType,
  179. success: res => {
  180. // console.log('chooseImage resp', res)
  181. // 首先检查文件大小
  182. let invalidIndex = -1; // @ts-ignore
  183. res.tempFiles.forEach((item, index) => {
  184. if (item.size > this.data.maxSize) {
  185. invalidIndex = index;
  186. }
  187. });
  188. if (typeof this.data.select === 'function') {
  189. const ret = this.data.select(res);
  190. if (ret === false) {
  191. return;
  192. }
  193. }
  194. if (invalidIndex >= 0) {
  195. this.triggerEvent('fail', {
  196. type: 1,
  197. errMsg: `chooseImage:fail size exceed ${this.data.maxSize}`,
  198. total: res.tempFilePaths.length,
  199. index: invalidIndex
  200. }, {});
  201. return;
  202. } // 获取文件内容
  203. const mgr = wx.getFileSystemManager();
  204. const contents = res.tempFilePaths.map(item => {
  205. // @ts-ignore
  206. const fileContent = mgr.readFileSync(item);
  207. return fileContent;
  208. });
  209. const obj = {
  210. tempFilePaths: res.tempFilePaths,
  211. tempFiles: res.tempFiles,
  212. contents
  213. }; // 触发选中的事件,开发者根据内容来上传文件,上传了把上传的结果反馈到files属性里面
  214. this.triggerEvent('select', obj, {});
  215. const files = res.tempFilePaths.map((item, i) => ({
  216. loading: true,
  217. // @ts-ignore
  218. url: res.tempFilePaths[i] || `data:image/jpg;base64,${wx.arrayBufferToBase64(contents[i])}`
  219. }));
  220. if (!files || !files.length) return;
  221. if (typeof this.data.upload === 'function') {
  222. const len = this.data.files.length;
  223. const newFiles = this.data.files.concat(files);
  224. this.setData({
  225. files: newFiles,
  226. currentFiles: newFiles
  227. });
  228. this.loading = true;
  229. this.data.upload(obj).then(json => {
  230. this.loading = false;
  231. if (json.urls) {
  232. const oldFiles = this.data.files;
  233. json.urls.forEach((url, index) => {
  234. oldFiles[len + index].url = url;
  235. oldFiles[len + index].loading = false;
  236. });
  237. this.setData({
  238. files: oldFiles,
  239. currentFiles: newFiles
  240. });
  241. this.triggerEvent('success', json, {});
  242. } else {
  243. this.triggerEvent('fail', {
  244. type: 3,
  245. errMsg: 'upload file fail, urls not found'
  246. }, {});
  247. }
  248. }).catch(err => {
  249. this.loading = false;
  250. const oldFiles = this.data.files;
  251. res.tempFilePaths.forEach((item, index) => {
  252. oldFiles[len + index].error = true;
  253. oldFiles[len + index].loading = false;
  254. });
  255. this.setData({
  256. files: oldFiles,
  257. currentFiles: newFiles
  258. });
  259. this.triggerEvent('fail', {
  260. type: 3,
  261. errMsg: 'upload file fail',
  262. error: err
  263. }, {});
  264. });
  265. }
  266. },
  267. fail: fail => {
  268. if (fail.errMsg.indexOf('chooseImage:fail cancel') >= 0) {
  269. this.triggerEvent('cancel', {}, {});
  270. return;
  271. }
  272. fail.type = 2;
  273. this.triggerEvent('fail', fail, {});
  274. }
  275. });
  276. },
  277. deletePic(e) {
  278. const index = e.detail.index;
  279. const files = this.data.files;
  280. const file = files.splice(index, 1);
  281. this.setData({
  282. files,
  283. currentFiles: files
  284. });
  285. this.triggerEvent('delete', {
  286. index,
  287. item: file[0]
  288. });
  289. }
  290. }
  291. });
  292. /***/ })
  293. /******/ });