preview.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. Page({
  2. data: {
  3. type: 'image',
  4. url: '',
  5. showPreview: false,
  6. },
  7. onShareAppMessage: function (res) {
  8. var typeMap = {
  9. image: '图片',
  10. video: '视频',
  11. };
  12. var data = {
  13. title: 'COS 上传示例 - 预览' + (typeMap[this.data.type] || '文件'),
  14. path: this.route + '?type=' + this.data.type + '&url=' + encodeURIComponent(this.data.url),
  15. };
  16. if (this.data.type === 'image') {
  17. data.imageUrl = this.data.url;
  18. }
  19. return data;
  20. },
  21. onShow() {
  22. this.setData({showPreview: false});
  23. this.setData({
  24. type: this.options.type || 'image',
  25. url: decodeURIComponent(this.options.url) || '',
  26. });
  27. },
  28. showPreviewBox() {
  29. this.setData({showPreview: true});
  30. },
  31. copyLink() {
  32. wx.setClipboardData({
  33. data: this.data.url || '',
  34. success: function () {
  35. wx.showToast({title: '复制成功', icon: 'success', duration: 2000});
  36. },
  37. fail: function () {
  38. wx.showToast({title: '复制失败', icon: 'error', duration: 2000});
  39. },
  40. });
  41. },
  42. saveImage() {
  43. wx.downloadFile({
  44. url: this.data.url,
  45. success: function(res) {
  46. // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
  47. if (res.statusCode === 200) {
  48. wx.saveImageToPhotosAlbum({
  49. filePath: res.tempFilePath,
  50. });
  51. }
  52. }
  53. });
  54. },
  55. });