index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // index.js
  2. // 获取应用实例
  3. import {
  4. VueLikePage
  5. } from "../../utils/page";
  6. import {
  7. CDN_URL,
  8. API_BASE_URL,
  9. VIDEO_BASE_URL,
  10. app
  11. } from "../../config/index";
  12. var g_app = getApp();
  13. VueLikePage([], {
  14. data: {
  15. cdn_url: CDN_URL,
  16. baseUrl: API_BASE_URL + "/",
  17. url_link: "",
  18. id: "1",
  19. type: "",
  20. loadCompele: false,
  21. filePath: ''
  22. },
  23. methods: {
  24. onLoad: function (options) {
  25. let {
  26. rdw,
  27. id,
  28. type
  29. } = options;
  30. let link = "";
  31. if (type == "0") {
  32. link = `${VIDEO_BASE_URL}4dvedio/${rdw}.mp4`;
  33. } else {
  34. link = `${VIDEO_BASE_URL}4dpic/${rdw}.jpg`;
  35. }
  36. this.setData({
  37. url_link: link,
  38. id,
  39. type,
  40. });
  41. this.downloadF(data => {
  42. this.setData({
  43. filePath: data
  44. })
  45. })
  46. },
  47. loadcompele() {
  48. this.setData({
  49. loadCompele: true,
  50. });
  51. },
  52. cancel() {
  53. // wx.reLaunch({
  54. // url: "/pages/work/index",
  55. // });
  56. wx.navigateBack();
  57. },
  58. downloadF(cb = () => {}) {
  59. let link = this.data.url_link,
  60. m_type = "";
  61. if (this.data.type == "1") {
  62. m_type = "jpeg";
  63. } else {
  64. m_type = "video";
  65. }
  66. wx.downloadFile({
  67. url: link,
  68. success: (res) => {
  69. if (res.statusCode == '404') {
  70. return app.showAlert("作品暂未生成,请稍后再试", () => {
  71. wx.navigateBack()
  72. });
  73. }
  74. //判断是否为数组
  75. let typeType =
  76. Object.prototype.toString.call(res.header["Content-Type"]) == "[object String]" ? res.header["Content-Type"] : res.header["Content-Type"][0];
  77. //判断不是xml文件
  78. if (typeType.indexOf(m_type) > -1) {
  79. cb(res.tempFilePath)
  80. }
  81. },
  82. fail: () => {
  83. app.showAlert("作品暂未生成,请稍后再试");
  84. },
  85. });
  86. this.setData({
  87. showModal: false,
  88. });
  89. },
  90. saveAlbum() {
  91. let type = this.data.type;
  92. wx.showLoading({
  93. title: "保存中…",
  94. mask: true,
  95. });
  96. if (this.data.filePath) {
  97. let api =
  98. type == "0" ? "saveVideoToPhotosAlbum" : "saveImageToPhotosAlbum";
  99. wx[api]({
  100. filePath: this.data.filePath,
  101. success() {
  102. wx.showModal({
  103. title: "提示",
  104. content: "已保存到相册,快去分享吧",
  105. showCancel: false,
  106. });
  107. },
  108. fail: (e) => {
  109. if (!(e.errMsg.indexOf('cancel') > -1)) {
  110. wx.showModal({
  111. title: "提示",
  112. content: "保存失败,请检查是否开启相册保存权限,可在「右上角」 - 「设置」里查看",
  113. showCancel: false,
  114. });
  115. }
  116. },
  117. complete: () => {
  118. wx.hideLoading();
  119. },
  120. });
  121. }
  122. },
  123. },
  124. });