// index.js // 获取应用实例 import { VueLikePage } from "../../utils/page"; import { randomWord } from "../../utils/utils"; import { isPhoneX } from "./../../utils/tools"; import { CDN_URL, API_BASE_URL, VIDEO_BASE_URL, app } from "../../config/index"; let timer = null; var g_app = getApp(); import Router from "../../utils/routes"; VueLikePage([], { data: { cdn_url: CDN_URL, isIphoneX: false, canTap: true, rndWord: randomWord(false, 8), background: [ { img: "lunbo1.png", id: "1", }, { img: "lunbo2.png", id: "2", }, { img: "lunbo3.png", id: "3", }, ], scenes: [ { img: "1.png", id: "1", }, { img: "2.png", id: "2", }, { img: "3.png", id: "3", }, ], autoplay: true, interval: 3000, duration: 500, current: 0, currentScene: 1, daojishi: 0, isShot: false, isRecord: false, recordjishi: 10, type: "", }, methods: { swiperChange: function (e) { this.setData({ current: e.detail.current, }); }, onShow(){ this.setData({ rndWord: randomWord(false, 8) }) }, gotoWork() { timer && clearTimeout(timer); timer = null; Router.push({ url: "work", query: { type: this.data.type, id: this.data.currentScene, rdw: encodeURIComponent(this.data.rndWord), }, }); this.reset(); }, reset() { this.setData({ daojishi: 0, isShot: false, isRecord: false, currentScene: 1, recordjishi: 10, type: "", }); this.clearLongTime(); app.websocket && app.websocket.send({ data: "switch 1" }); setTimeout(() => { app.hideLoading(); }); }, clearLongTime() { app.longtime && clearInterval(app.longtime); app.longtime = null; }, closeServer(cb=null) { app.closeByVideoHandle = true if (app.websocket) { app.send("close"); app.websocket.close(); app.websocket = null; } if (app.longtime) { setTimeout(() => { cb && cb() },100); } }, handleWebSocket: function () { if (app.websocket) { return; } app.websocket = wx.connectSocket({ url: API_BASE_URL, timeout: 180 * 1000, success: () => {}, }); app.websocket.onOpen(() => { app.is_connect = true; app.is_connect_ok = null; app.send("request"); }); app.websocket.onMessage((msg) => { console.log(msg); app.hideLoading(); if (typeof msg.data == "string") { switch (msg.data) { case "error 101": console.log("发送的消息有误"); break; case "error 302": app.hideLoading(); app.showAlert("网络异常,生成失败"); break; case "error 404": app.showAlert("网络异常,请稍后再试"); break; case "error 500": app.is_connect_ok = false; app.is_other_used = true; if (app.is_send_msg) { app.showAlert("服务器连接失败,请重新连接", () => { wx.reLaunch({ url: "index", }); },'重新连接'); } break; case "error 808": app.is_connect_ok = true; app.is_other_used = true; if (app.is_send_msg) { app.showAlert("当前画屏正在被使用,请稍后"); } break; case "error 909": if (app.is_send_msg) { app.showAlert("录像失败"); } break; case "ok 200": console.log("切换画风成功"); break; case "ok 201": app.is_connect_ok = false; app.is_other_used = true; app.checkOperationTimeout(true); break; case "ok 300": app.is_other_used = null; app.is_connect_ok = true; app.checkOperationTimeout(); this.reset(); break; case "ok 301": console.log("拍照成功,开始等待照片"); // 轮询拿视频/图片 this.clearLongTime(); app.longtime = setInterval(() => { let link = "", m_type = ""; if (this.data.type == "0") { link = `${VIDEO_BASE_URL}4dvedio/vedio${this.data.rndWord}.mp4`; m_type = "video"; } else { link = `${VIDEO_BASE_URL}4dpic/pic${this.data.rndWord}.jpg`; m_type = "jpeg"; } console.log(link, 1111); console.log(m_type, 1111); wx.downloadFile({ url: link, success: (res) => { //判断是否为数组 let typeType = (Object.prototype.toString.call(res.header["Content-Type"]) == "[object String]")? res.header["Content-Type"] : res.header["Content-Type"][0] console.log(typeType,8888); //判断不是xml文件 if (typeType.indexOf(m_type) > -1) { this.clearLongTime(); g_app.globalData.m_file = res.tempFilePath; this.gotoWork(); } }, fail: (e) => { console.log(e, 1111); }, }); }, 5 * 1000); // 3分钟后不可以就弹出生成失败 timer = setTimeout(() => { this.clearLongTime(); app.hideLoading(); app.showAlert("生成失败,请重新连接", () => { wx.reLaunch({ url: "index", }); }); }, 180 * 1000); break; default: try { // var json = JSON.parse(msg.data); // if (json.url) { // if (json.url.indexOf(this.data.rndWord) > -1) { // timer && clearTimeout(timer); // timer = null // this.clearLongTime() // this.gotoWork(); // } // } } catch (error) { console.error(error); } break; } app.is_send_msg = false; } else { console.log("error 600"); } }); app.websocket.onClose((res) => { app.is_connect = false; app.checkOperationTimeout(true); app.hideLoading(); app.websocket = null; if (!app.longtime&&!app.closeByVideoHandle) { app.showAlert("服务器连接已关闭,请重新连接", () => { wx.reLaunch({ url: "index", }); },'重新连接'); app.closeByVideoHandle = false } }); }, onLoad: function () { isPhoneX().then((res) => { this.setData({ isIphoneX: res, }); }); this.handleWebSocket(); }, daojishiFn(key, time, fn = () => {}) { intel && clearInterval(intel); let intel = null; let titop = time; intel = setInterval(() => { titop -= 1; let kv = {}; kv[key] = titop; this.setData(kv); if (titop <= 0) { fn(); intel && clearInterval(intel); return; } }, 1000); }, shot: function (e) { app.checkOperationTimeout(true); let { id } = e.currentTarget.dataset; this.setData({ type: id, rndWord: randomWord(false, 8), }); if (!app.sendCheck()) { return; } //录像 if (id == "0") { let intel = null; intel && clearInterval(intel); this.setData({ isShot: true, }); setTimeout(() => { this.setData({ daojishi: 3, }); app.send("switch " + this.data.currentScene); setTimeout(() => { app.send("photo " + "0#" + this.data.rndWord); // 为了同步机器的倒计时 setTimeout(() => { this.daojishiFn("daojishi", this.data.daojishi, () => { this.setData({ isRecord: true, }); this.daojishiFn("recordjishi", this.data.recordjishi, () => { wx.showLoading({ title: "生成视频中...", }); setTimeout(() => { this.closeServer(()=>{ wx.showLoading({ title: "生成视频中...", }); }); }, 20 * 1000); }); }); }, 500); }, 1000); }, 500); } //拍照 else { let intel = null; intel && clearInterval(intel); this.setData({ isShot: true, }); setTimeout(() => { this.setData({ daojishi: 3, }); app.send("switch " + this.data.currentScene); setTimeout(() => { app.send("photo " + "1#" + this.data.rndWord); // 为了同步机器的倒计时 setTimeout(() => { this.daojishiFn("daojishi", this.data.daojishi, () => { this.closeServer(()=>{ wx.showLoading({ title: "生成图片中...", // mask: true, }); }); }); }, 500); }, 1000); }, 500); } // 1秒后拍照 setTimeout(() => { }, 1000); }, tapSelect: function (e) { app.checkOperationTimeout(); if (!this.data.canTap) { return; } this.clearLongTime(); let { id } = e.currentTarget.dataset; if (id == this.data.currentScene) { return; } this.setData({ canTap: false, }); setTimeout(() => { this.setData({ canTap: true, }); app.hideLoading(); }, 3 * 1000); if (!app.sendCheck()) { this.setData({ canTap: true, }); app.hideLoading(); } else { wx.showLoading({ title: "切换中...", mask: true, }); this.setData({ currentScene: id }); app.send("switch " + id); } }, }, });