qiniuUploader.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // created by gpake
  2. export const qiniuUploader = (function() {
  3. var config = {
  4. qiniuRegion: '',
  5. qiniuImageURLPrefix: '',
  6. qiniuUploadToken: '',
  7. qiniuUploadTokenURL: '',
  8. qiniuUploadTokenFunction: null,
  9. qiniuShouldUseQiniuFileName: false
  10. }
  11. // 在整个程序生命周期中,只需要 init 一次即可
  12. // 如果需要变更参数,再调用 init 即可
  13. function init(options) {
  14. config = {
  15. qiniuRegion: '',
  16. qiniuImageURLPrefix: '',
  17. qiniuUploadToken: '',
  18. qiniuUploadTokenURL: '',
  19. qiniuUploadTokenFunction: null,
  20. qiniuShouldUseQiniuFileName: false
  21. };
  22. updateConfigWithOptions(options);
  23. }
  24. function updateConfigWithOptions(options) {
  25. if (options.region) {
  26. config.qiniuRegion = options.region;
  27. } else {
  28. console.error('qiniu uploader need your bucket region');
  29. }
  30. if (options.uptoken) {
  31. config.qiniuUploadToken = options.uptoken;
  32. } else if (options.uptokenURL) {
  33. config.qiniuUploadTokenURL = options.uptokenURL;
  34. } else if (options.uptokenFunc) {
  35. config.qiniuUploadTokenFunction = options.uptokenFunc;
  36. }
  37. if (options.domain) {
  38. config.qiniuImageURLPrefix = options.domain;
  39. }
  40. config.qiniuShouldUseQiniuFileName = options.shouldUseQiniuFileName
  41. }
  42. function upload(filePath, success, fail, options, progress, cancelTask) {
  43. if (null == filePath) {
  44. console.error('qiniu uploader need filePath to upload');
  45. return;
  46. }
  47. if (options) {
  48. updateConfigWithOptions(options);
  49. }
  50. if (config.qiniuUploadToken) {
  51. doUpload(filePath, success, fail, options, progress, cancelTask);
  52. } else if (config.qiniuUploadTokenURL) {
  53. getQiniuToken(function() {
  54. doUpload(filePath, success, fail, options, progress, cancelTask);
  55. });
  56. } else if (config.qiniuUploadTokenFunction) {
  57. config.qiniuUploadToken = config.qiniuUploadTokenFunction();
  58. if (null == config.qiniuUploadToken && config.qiniuUploadToken.length > 0) {
  59. console.error('qiniu UploadTokenFunction result is null, please check the return value');
  60. return
  61. }
  62. doUpload(filePath, success, fail, options, progress, cancelTask);
  63. } else {
  64. console.error('qiniu uploader need one of [uptoken, uptokenURL, uptokenFunc]');
  65. return;
  66. }
  67. }
  68. function doUpload(filePath, success, fail, options, progress, cancelTask) {
  69. if (null == config.qiniuUploadToken && config.qiniuUploadToken.length > 0) {
  70. console.error('qiniu UploadToken is null, please check the init config or networking');
  71. return
  72. }
  73. var url = uploadURLFromRegionCode(config.qiniuRegion);
  74. var fileName = filePath.split('//')[1];
  75. if (options && options.key) {
  76. fileName = options.key;
  77. }
  78. var formData = {
  79. 'token': config.qiniuUploadToken
  80. };
  81. if (!config.qiniuShouldUseQiniuFileName) {
  82. formData['key'] = fileName
  83. }
  84. var uploadTask = wx.uploadFile({
  85. url: url,
  86. filePath: filePath,
  87. name: 'file',
  88. formData: formData,
  89. success: function(res) {
  90. var dataString = res.data
  91. if (res.data.hasOwnProperty('type') && res.data.type === 'Buffer') {
  92. dataString = String.fromCharCode.apply(null, res.data.data)
  93. }
  94. try {
  95. var dataObject = JSON.parse(dataString);
  96. //do something
  97. var imageUrl = config.qiniuImageURLPrefix + '/' + dataObject.key;
  98. dataObject.imageURL = imageUrl;
  99. if (success) {
  100. success(dataObject);
  101. }
  102. } catch (e) {
  103. console.log('parse JSON failed, origin String is: ' + dataString)
  104. if (fail) {
  105. fail(e);
  106. }
  107. }
  108. },
  109. fail: function(error) {
  110. console.error(error);
  111. if (fail) {
  112. fail(error);
  113. }
  114. }
  115. })
  116. uploadTask.onProgressUpdate((res) => {
  117. progress && progress(res)
  118. })
  119. cancelTask && cancelTask(() => {
  120. uploadTask.abort()
  121. })
  122. }
  123. function getQiniuToken(callback) {
  124. wx.request({
  125. url: config.qiniuUploadTokenURL,
  126. success: function(res) {
  127. var token = res.data.uptoken;
  128. if (token && token.length > 0) {
  129. config.qiniuUploadToken = token;
  130. if (callback) {
  131. callback();
  132. }
  133. } else {
  134. console.error('qiniuUploader cannot get your token, please check the uptokenURL or server')
  135. }
  136. },
  137. fail: function(error) {
  138. console.error('qiniu UploadToken is null, please check the init config or networking: ' + error);
  139. }
  140. })
  141. }
  142. function uploadURLFromRegionCode(code) {
  143. var uploadURL = null;
  144. switch (code) {
  145. case 'ECN':
  146. uploadURL = 'https://up.qbox.me';
  147. break;
  148. case 'NCN':
  149. uploadURL = 'https://up-z1.qbox.me';
  150. break;
  151. case 'SCN':
  152. uploadURL = 'https://up-z2.qbox.me';
  153. break;
  154. case 'NA':
  155. uploadURL = 'https://up-na0.qbox.me';
  156. break;
  157. case 'ASG':
  158. uploadURL = 'https://up-as0.qbox.me';
  159. break;
  160. default:
  161. console.error('please make the region is with one of [ECN, SCN, NCN, NA, ASG]');
  162. }
  163. return uploadURL;
  164. }
  165. return {
  166. init: init,
  167. upload: upload,
  168. }
  169. })();