qiniuUploader.js 4.8 KB

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