index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // index.js
  2. // 获取应用实例
  3. import { VueLikePage } from "../../utils/page";
  4. import { CDN_URL, API_BASE_URL, VIDEO_BASE_URL, app } from "../../config/index";
  5. import Router from "../../utils/routes";
  6. let socket_io = require("../../utils/socket.io-mp.js");
  7. var g_app = getApp();
  8. VueLikePage([], {
  9. data: {
  10. cdn_url: CDN_URL,
  11. rndWord: "",
  12. showModal: false,
  13. type:0
  14. },
  15. methods: {
  16. onLoad: function () {},
  17. disconnect() {
  18. app.websocket.disconnect();
  19. app.websocket = null;
  20. },
  21. wish_put: function (e) {
  22. this.setData({
  23. rndWord: e.detail.value,
  24. });
  25. },
  26. back() {
  27. this.setData({
  28. showModal: false,
  29. });
  30. },
  31. ok() {
  32. let link = "",
  33. m_type = "";
  34. if (this.data.rndWord.substring(0,3) == "pic") {
  35. this.setData({
  36. type:1
  37. })
  38. link = `${VIDEO_BASE_URL}4dpic/${this.data.rndWord}.jpg`;
  39. m_type = "jpeg";
  40. } else {
  41. link = `${VIDEO_BASE_URL}4dvedio/${this.data.rndWord}.mp4`;
  42. m_type = "video";
  43. this.setData({
  44. type:0
  45. })
  46. }
  47. console.log(link);
  48. wx.downloadFile({
  49. url: link,
  50. success: (res) => {
  51. console.log(res);
  52. if (res.statusCode == '404') {
  53. return app.showAlert("作品暂未生成,请稍后再试");
  54. }
  55. //判断是否为数组
  56. let typeType =
  57. Object.prototype.toString.call(res.header["Content-Type"]) == "[object String]" ? res.header["Content-Type"] : res.header["Content-Type"][0];
  58. //判断不是xml文件
  59. if (typeType.indexOf(m_type) > -1) {
  60. g_app.globalData.m_file = res.tempFilePath;
  61. this.gotoWork();
  62. }
  63. },
  64. fail: () => {
  65. app.showAlert("作品暂未生成,请稍后再试");
  66. },
  67. });
  68. this.setData({
  69. showModal: false,
  70. });
  71. },
  72. gotoWork() {
  73. Router.push({
  74. url: "work",
  75. query: {
  76. type: this.data.type,
  77. id: this.data.currentScene,
  78. rdw: encodeURIComponent(this.data.rndWord),
  79. },
  80. });
  81. },
  82. getWork() {
  83. this.setData({
  84. showModal: true,
  85. });
  86. // Router.push({
  87. // url: "work",
  88. // });
  89. },
  90. onShow(){
  91. if (app.websocket) {
  92. this.disconnect();
  93. }
  94. },
  95. checkTextDetail: function () {
  96. wx.showModal({
  97. title: "作品标识码",
  98. content: this.data.rndWord,
  99. showCancel: true,
  100. confirmText: "复制文本",
  101. success: (result) => {
  102. if (result.confirm) {
  103. wx.setClipboardData({
  104. data: this.data.rndWord,
  105. success: () => {
  106. wx.getClipboardData({
  107. success: (data) => {
  108. console.log(data);
  109. },
  110. });
  111. },
  112. });
  113. }
  114. },
  115. });
  116. },
  117. bindStart: function () {
  118. if (app.websocket) {
  119. return;
  120. }
  121. app.websocket = socket_io(API_BASE_URL, {
  122. transports: ["websocket"],
  123. });
  124. app.websocket.on("connect", () => {
  125. console.log("SOCKET连接成功");
  126. app.emitEvent.request((data) => {
  127. app.websocket.on("ForceOffline", () => {
  128. console.log("ForceOffline成功");
  129. app.showAlert("服务器连接失败,请稍后再试", () => {
  130. wx.reLaunch({
  131. url: "start",
  132. });
  133. });
  134. this.disconnect();
  135. });
  136. app.websocket.on("error", () => {
  137. this.disconnect();
  138. app.showAlert("服务器异常,请稍后再试");
  139. });
  140. if (data == 808) {
  141. return app.showAlert("机器被占用,请稍后再试");
  142. }
  143. console.log(data,4545454);
  144. try {
  145. var json = JSON.parse(data);
  146. g_app.globalData.scenes = [...json.scenes];
  147. if (g_app.globalData.scenes.length > 0) {
  148. Router.push({
  149. url: "index",
  150. });
  151. } else {
  152. app.showAlert("暂无场景");
  153. }
  154. } catch (error) {
  155. this.disconnect();
  156. app.showAlert("网络异常,请稍后再试");
  157. }
  158. });
  159. });
  160. },
  161. },
  162. });