uni-events-helper-wx
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
5.2 KiB

3 years ago
  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. if (res.data.hasOwnProperty('type') && res.data.type === 'Buffer') {
  96. dataString = String.fromCharCode.apply(null, res.data.data)
  97. }
  98. try {
  99. var dataObject = JSON.parse(dataString);
  100. //do something
  101. var imageUrl = config.qiniuImageURLPrefix + '/' + dataObject.key;
  102. dataObject.imageURL = imageUrl;
  103. if (success) {
  104. success(dataObject);
  105. }
  106. } catch (e) {
  107. console.log('parse JSON failed, origin String is: ' + dataString)
  108. if (fail) {
  109. fail(e);
  110. }
  111. }
  112. },
  113. fail: function (error) {
  114. console.error(error);
  115. if (fail) {
  116. fail(error);
  117. }
  118. }
  119. })
  120. uploadTask.onProgressUpdate((res) => {
  121. progress && progress(res)
  122. })
  123. cancelTask && cancelTask(() => {
  124. uploadTask.abort()
  125. })
  126. }
  127. function getQiniuToken(callback) {
  128. wx.request({
  129. url: config.qiniuUploadTokenURL,
  130. success: function (res) {
  131. var token = res.data.uptoken;
  132. if (token && token.length > 0) {
  133. config.qiniuUploadToken = token;
  134. if (callback) {
  135. callback();
  136. }
  137. } else {
  138. console.error('qiniuUploader cannot get your token, please check the uptokenURL or server')
  139. }
  140. },
  141. fail: function (error) {
  142. console.error('qiniu UploadToken is null, please check the init config or networking: ' + error);
  143. }
  144. })
  145. }
  146. function uploadURLFromRegionCode(code) {
  147. var uploadURL = null;
  148. switch (code) {
  149. case 'ECN': uploadURL = 'https://up.qbox.me'; break;
  150. case 'NCN': uploadURL = 'https://up-z1.qbox.me'; break;
  151. case 'SCN': uploadURL = 'https://up-z2.qbox.me'; break;
  152. case 'NA': uploadURL = 'https://up-na0.qbox.me'; break;
  153. case 'ASG': uploadURL = 'https://up-as0.qbox.me'; break;
  154. default: console.error('please make the region is with one of [ECN, SCN, NCN, NA, ASG]');
  155. }
  156. return uploadURL;
  157. }
  158. })();