index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // index.js
  2. // 获取应用实例
  3. import { VueLikePage } from "../../utils/page";
  4. import { randomWord } from "../../utils/utils";
  5. import { isPhoneX } from "./../../utils/tools";
  6. import { CDN_URL, API_BASE_URL, VIDEO_BASE_URL, app } from "../../config/index";
  7. let socket_io = require("../../utils/socket.io-mp.js");
  8. let timer = null;
  9. let projectid = null;
  10. let isLoading = false;
  11. import Router from "../../utils/routes";
  12. VueLikePage([], {
  13. data: {
  14. cdn_url: "",
  15. isIphoneX: false,
  16. canTap: true,
  17. rndWord: randomWord(false, 8),
  18. info: {
  19. banner: {},
  20. sceneTitleImg: {},
  21. recordTitleImg: {},
  22. rescan: {},
  23. activeSceneBdImg: {},
  24. },
  25. scenes: [],
  26. records: [],
  27. autoplay: true,
  28. interval: 3000,
  29. duration: 500,
  30. current: 0,
  31. currentScene: 1,
  32. daojishi: 0,
  33. isShot: false,
  34. isRecord: false,
  35. isVideo: false,
  36. popupVisible: false,
  37. recordjishi: 10,
  38. type: "",
  39. authorCode: "",
  40. isOtherZhanYong: true,
  41. },
  42. methods: {
  43. scanCode: function (e) {
  44. wx.scanCode({
  45. onlyFromCamera: true,
  46. success(res) {
  47. wx.reLaunch({
  48. url: `/${res.path}`,
  49. });
  50. },
  51. });
  52. },
  53. swiperChange: function (e) {
  54. this.setData({
  55. current: e.detail.current,
  56. });
  57. },
  58. onShow() {
  59. this.setData({
  60. rndWord: randomWord(false, 8),
  61. });
  62. },
  63. gotoRecord(e) {
  64. let { id } = e.currentTarget.dataset;
  65. let item = this.data.records.find((i) => i.id == id);
  66. Router.push({
  67. url: "work",
  68. query: item.codeData,
  69. });
  70. console.log(item);
  71. },
  72. gotoWork() {
  73. //日期 new Date().Format("yyyy.MM.dd hh.mm.ss");
  74. //编号 new Date().Format("yyyyMMddhhmmss");
  75. timer && clearTimeout(timer);
  76. timer = null;
  77. let record = {
  78. projectid,
  79. type: this.data.type,
  80. id: this.data.currentScene,
  81. rdw: encodeURIComponent(this.data.authorCode),
  82. };
  83. Router.push({
  84. url: "work",
  85. query: record,
  86. });
  87. this.setData({
  88. records: [
  89. {
  90. id: new Date().Format("yyyyMMddhhmmss"),
  91. date: new Date().Format("yyyy.MM.dd hh.mm.ss"),
  92. type: this.data.type,
  93. codeData: record,
  94. },
  95. ]
  96. .concat(this.data.records)
  97. .slice(0, 15),
  98. });
  99. wx.setStorageSync("records", JSON.stringify(this.data.records));
  100. this.reset();
  101. },
  102. reset(noswitch = false) {
  103. this.setData({
  104. daojishi: 0,
  105. isShot: false,
  106. isRecord: false,
  107. currentScene: 1,
  108. recordjishi: 10,
  109. });
  110. if (!noswitch) {
  111. app.websocket &&
  112. app.websocket.send({
  113. data: "switch 1",
  114. });
  115. }
  116. setTimeout(() => {
  117. app.hideLoading();
  118. });
  119. },
  120. handleWebSocket: function () {
  121. if (app.websocket) {
  122. return;
  123. }
  124. app.websocket = socket_io(API_BASE_URL, {
  125. transports: ["websocket"],
  126. });
  127. app.websocket.on("connect", () => {
  128. console.log("SOCKET连接成功");
  129. app.emitEvent.request((data) => {
  130. console.log(data, 1111111);
  131. });
  132. });
  133. app.websocket.on("ForceOffline", () => {
  134. app.showAlert("服务器连接失败,请稍后再试", () => {
  135. wx.reLaunch({
  136. url: "/pages/start/index",
  137. });
  138. });
  139. console.log("ForceOffline成功");
  140. });
  141. },
  142. onHide: function () {
  143. this.disconnect();
  144. },
  145. onLoad: function (options) {
  146. console.log("options: ", options);
  147. if (options.machineid) {
  148. app.id = options.machineid;
  149. }
  150. this.getData(options.projectid);
  151. // 1、获取二维码传参
  152. if (options.authorCode) {
  153. this.setData({
  154. authorCode: decodeURIComponent(options.authorCode),
  155. });
  156. this.start(this.data.authorCode);
  157. }
  158. if (wx.getStorageSync("records")) {
  159. this.setData({
  160. records: JSON.parse(wx.getStorageSync("records")),
  161. });
  162. }
  163. // wx.setStorageSync('records', JSON.stringify(this.data.records))
  164. isPhoneX().then((res) => {
  165. this.setData({
  166. isIphoneX: res,
  167. });
  168. });
  169. // let tmp = g_app.globalData.scenes.map(item=>{
  170. // return {id:item}
  171. // })
  172. // this.setData({
  173. // scenes: tmp,
  174. // currentScene:tmp[0].id
  175. // })
  176. },
  177. getData(prjId = "ZHS2305758-1") {
  178. wx.showLoading({
  179. title: "资源加载中",
  180. });
  181. this.setData({
  182. cdn_url: CDN_URL + "/" + prjId,
  183. });
  184. wx.request({
  185. url:
  186. `${VIDEO_BASE_URL}project/4dage-sxb/${prjId}/config.json?t=` +
  187. new Date().getTime(),
  188. success: ({ data: { scenes, title, ...rest } }) => {
  189. this.setData(
  190. {
  191. scenes: scenes,
  192. info: rest,
  193. },
  194. () => {
  195. wx.hideLoading();
  196. }
  197. );
  198. wx.setNavigationBarTitle({
  199. title: title,
  200. });
  201. },
  202. });
  203. projectid = prjId;
  204. },
  205. daojishiFn(key, time, fn = () => {}) {
  206. intel && clearInterval(intel);
  207. let intel = null;
  208. let titop = time;
  209. intel = setInterval(() => {
  210. titop -= 1;
  211. let kv = {};
  212. kv[key] = titop;
  213. this.setData(kv);
  214. if (titop <= 0) {
  215. fn();
  216. intel && clearInterval(intel);
  217. return;
  218. }
  219. }, 1000);
  220. },
  221. shot: function (e) {
  222. if (isLoading) return;
  223. isLoading = true;
  224. app.checkOperationTimeout(true);
  225. let { id } = e.currentTarget.dataset;
  226. this.setData({
  227. type: id,
  228. popupVisible: true,
  229. });
  230. // if (!app.sendCheck()) {
  231. // return;
  232. // }
  233. //录像
  234. if (id == "0") {
  235. let intel = null;
  236. intel && clearInterval(intel);
  237. this.setData({
  238. isShot: true,
  239. isVideo: true,
  240. });
  241. setTimeout(() => {
  242. this.setData({
  243. daojishi: 3,
  244. });
  245. app.emitEvent.switchMachine(this.data.currentScene);
  246. setTimeout(() => {
  247. app.emitEvent.Video(this.data.authorCode);
  248. // 为了同步机器的倒计时
  249. setTimeout(() => {
  250. this.daojishiFn("daojishi", this.data.daojishi, () => {
  251. this.setData({
  252. isRecord: true,
  253. });
  254. this.daojishiFn("recordjishi", this.data.recordjishi, () => {
  255. isLoading = false;
  256. wx.showLoading({
  257. title: "生成视频中...",
  258. });
  259. this.setData({
  260. popupVisible: false,
  261. });
  262. setTimeout(() => {
  263. // 不发送switch
  264. this.gotoWork();
  265. this.disconnect(true);
  266. wx.hideLoading();
  267. // 上传倒计时
  268. }, 30 * 1000);
  269. });
  270. });
  271. }, 500);
  272. }, 1000);
  273. }, 500);
  274. }
  275. //拍照
  276. else {
  277. let intel = null;
  278. intel && clearInterval(intel);
  279. this.setData({
  280. isShot: true,
  281. isVideo: false,
  282. });
  283. setTimeout(() => {
  284. this.setData({
  285. daojishi: 3,
  286. });
  287. app.emitEvent.switchMachine(this.data.currentScene);
  288. setTimeout(() => {
  289. app.emitEvent.Photo(this.data.authorCode);
  290. // 为了同步机器的倒计时
  291. setTimeout(() => {
  292. this.daojishiFn("daojishi", this.data.daojishi, () => {
  293. isLoading = false;
  294. wx.showLoading({
  295. title: "生成图片中...",
  296. // mask: true,
  297. });
  298. this.setData({
  299. popupVisible: false,
  300. });
  301. setTimeout(() => {
  302. // 不发送switch
  303. this.gotoWork();
  304. this.disconnect(true);
  305. wx.hideLoading();
  306. }, 4 * 1000);
  307. });
  308. }, 500);
  309. }, 1000);
  310. }, 500);
  311. }
  312. },
  313. tapSelect: function (e) {
  314. app.checkOperationTimeout(false, () => {
  315. app.showAlert("抱歉,因您长时间使用,让下一位小伙伴体验吧", () => {
  316. this.disconnect();
  317. });
  318. });
  319. if (!this.data.canTap) {
  320. return;
  321. }
  322. let { id } = e.currentTarget.dataset;
  323. if (id == this.data.currentScene) {
  324. return;
  325. }
  326. this.setData({
  327. canTap: false,
  328. });
  329. setTimeout(() => {
  330. this.setData({
  331. canTap: true,
  332. });
  333. app.hideLoading();
  334. }, 3 * 1000);
  335. wx.showLoading({
  336. title: "切换中...",
  337. mask: true,
  338. });
  339. this.setData({
  340. currentScene: id,
  341. });
  342. app.emitEvent.switchMachine(id);
  343. },
  344. disconnect(noswitch = false) {
  345. this.reset(noswitch);
  346. if (app.websocket) {
  347. app.emitEvent.close();
  348. app.websocket.disconnect();
  349. app.websocket = null;
  350. this.setData({
  351. isOtherZhanYong: true,
  352. });
  353. }
  354. },
  355. start: function (authorCode) {
  356. if (app.websocket) {
  357. return;
  358. }
  359. app.websocket = socket_io(API_BASE_URL, {
  360. transports: ["websocket"],
  361. });
  362. app.websocket.on("connect", () => {
  363. console.log("SOCKET连接成功");
  364. wx.showLoading({
  365. mask: true,
  366. title: "正在连接机器",
  367. });
  368. let linktimer = setTimeout(() => {
  369. clearTimeout(linktimer);
  370. wx.showToast({
  371. title: "网络异常,请稍后再试",
  372. icon: "none",
  373. });
  374. wx.hideLoading();
  375. }, 5000);
  376. app.emitEvent.request((data) => {
  377. // 校验成功后解除被他人占用情况
  378. clearTimeout(linktimer);
  379. wx.hideLoading();
  380. this.setData({
  381. isOtherZhanYong: false,
  382. });
  383. app.checkOperationTimeout(false, () => {
  384. app.showAlert("抱歉,因您长时间使用,让下一位小伙伴体验吧", () => {
  385. this.disconnect();
  386. });
  387. });
  388. app.websocket.on("ForceOffline", () => {
  389. console.log("ForceOffline成功");
  390. app.checkOperationTimeout(true);
  391. this.disconnect();
  392. });
  393. app.websocket.on("error", () => {
  394. this.disconnect();
  395. app.showAlert("服务器异常,请稍后再试");
  396. });
  397. if (data == 808) {
  398. // return app.showAlert("机器被占用,请稍后再试");
  399. app.checkOperationTimeout(true);
  400. this.disconnect();
  401. }
  402. if (data == 404 || data == 505) {
  403. app.checkOperationTimeout(true);
  404. this.disconnect();
  405. // app.showAlert("网络异常,请稍后再试");
  406. }
  407. console.log(data, "data");
  408. }, authorCode);
  409. });
  410. },
  411. },
  412. });