index.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var util = require('../../lib/util');
  2. var config = require('../../config');
  3. var cos = require('../../lib/cos');
  4. Page({
  5. onLoad: function () {
  6. },
  7. onShareAppMessage: function (res) {
  8. return {
  9. title: 'COS 上传示例',
  10. path: this.route,
  11. }
  12. },
  13. // 前往相册页
  14. uploadImage() {
  15. wx.chooseImage({
  16. count: 1,
  17. camera: 'back',
  18. sizeType: ['compressed'],
  19. sourceType: ['album', 'camera'],
  20. success: function (res) {
  21. var filePath = res.tempFilePaths[0];
  22. if (filePath) {
  23. var Key = util.getRandFileName(filePath);
  24. wx.showLoading({title: '正在上传...'});
  25. cos.postObject({
  26. Bucket: config.Bucket,
  27. Region: config.Region,
  28. Key: Key,
  29. FilePath: filePath,
  30. }, function (err, data) {
  31. wx.hideLoading();
  32. if (data && data.Location) {
  33. wx.navigateTo({url: '../preview/preview?type=image&url=' + encodeURIComponent('https://' + data.Location)});
  34. } else {
  35. wx.showToast({title: '上传失败', icon: 'error', duration: 2000});
  36. }
  37. });
  38. }
  39. }
  40. })
  41. },
  42. // 前往相册页
  43. uploadVideo() {
  44. wx.chooseVideo({
  45. count: 1,
  46. sizeType: ['compressed'],
  47. sourceType: ['album', 'camera'],
  48. maxDuration: 60,
  49. camera: 'back',
  50. success: function (res) {
  51. var filePath = res.tempFilePath;
  52. if (filePath) {
  53. var Key = util.getRandFileName(filePath);
  54. wx.showLoading({title: '正在上传...'});
  55. cos.postObject({
  56. Bucket: config.Bucket,
  57. Region: config.Region,
  58. Key: Key,
  59. FilePath: filePath,
  60. }, function (err, data) {
  61. wx.hideLoading();
  62. if (data && data.Location) {
  63. wx.navigateTo({url: '../preview/preview?type=video&url=' + encodeURIComponent('https://' + data.Location)});
  64. } else {
  65. wx.showToast({title: '上传失败', icon: 'error', duration: 2000});
  66. }
  67. });
  68. }
  69. }
  70. });
  71. },
  72. // 前往相册页
  73. gotoAlbum() {
  74. wx.navigateTo({url: '../album/album'});
  75. },
  76. });