123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- // 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);
- }
- },
- },
- });
|