qiniuUploader.js 5.0 KB

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