qiniuUploader.js 5.3 KB

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