index.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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: this.data.scenes[0] ? this.data.scenes[0].id : 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. wx.showLoading({
  148. title: "资源加载中",
  149. });
  150. if (options && options.scene) {
  151. const [machineid, authorCode] = options.scene.split("_");
  152. app.id = machineid;
  153. // 1、获取二维码传参
  154. if (authorCode) {
  155. this.setData({
  156. authorCode: decodeURIComponent(authorCode),
  157. });
  158. this.start(this.data.authorCode);
  159. }
  160. if (wx.getStorageSync("records")) {
  161. this.setData({
  162. records: JSON.parse(wx.getStorageSync("records")),
  163. });
  164. }
  165. } else {
  166. this.getData();
  167. }
  168. // wx.setStorageSync('records', JSON.stringify(this.data.records))
  169. isPhoneX().then((res) => {
  170. this.setData({
  171. isIphoneX: res,
  172. });
  173. });
  174. // let tmp = g_app.globalData.scenes.map(item=>{
  175. // return {id:item}
  176. // })
  177. // this.setData({
  178. // scenes: tmp,
  179. // currentScene:tmp[0].id
  180. // })
  181. },
  182. getData(prjId = "ZHS2305758-1", scenes) {
  183. this.setData({
  184. cdn_url: CDN_URL + "/" + prjId,
  185. });
  186. wx.request({
  187. url:
  188. `${VIDEO_BASE_URL}project/4dage-sxb/${prjId}/config.json?t=` +
  189. new Date().getTime(),
  190. success: ({ data: { scenes: sourceScenes, title, ...rest } }) => {
  191. let scenesData = sourceScenes;
  192. if (scenes) {
  193. // 只展示 scenes 中存在的id
  194. scenesData = scenes.map((id) =>
  195. sourceScenes.find((scene) => id === scene.id)
  196. );
  197. }
  198. this.setData(
  199. {
  200. currentScene: scenesData[0].id,
  201. scenes: scenesData,
  202. info: rest,
  203. },
  204. () => {
  205. wx.hideLoading();
  206. }
  207. );
  208. wx.setNavigationBarTitle({
  209. title: title,
  210. });
  211. },
  212. });
  213. projectid = prjId;
  214. },
  215. daojishiFn(key, time, fn = () => {}) {
  216. intel && clearInterval(intel);
  217. let intel = null;
  218. let titop = time;
  219. intel = setInterval(() => {
  220. titop -= 1;
  221. let kv = {};
  222. kv[key] = titop;
  223. this.setData(kv);
  224. if (titop <= 0) {
  225. fn();
  226. intel && clearInterval(intel);
  227. return;
  228. }
  229. }, 1000);
  230. },
  231. shot: function (e) {
  232. if (isLoading) return;
  233. isLoading = true;
  234. app.checkOperationTimeout(true);
  235. let { id } = e.currentTarget.dataset;
  236. this.setData({
  237. type: id,
  238. popupVisible: true,
  239. });
  240. // if (!app.sendCheck()) {
  241. // return;
  242. // }
  243. //录像
  244. if (id == "0") {
  245. let intel = null;
  246. intel && clearInterval(intel);
  247. this.setData({
  248. isShot: true,
  249. isVideo: true,
  250. });
  251. setTimeout(() => {
  252. this.setData({
  253. daojishi: 3,
  254. });
  255. app.emitEvent.switchMachine(this.data.currentScene);
  256. setTimeout(() => {
  257. app.emitEvent.Video(this.data.authorCode);
  258. // 为了同步机器的倒计时
  259. setTimeout(() => {
  260. this.daojishiFn("daojishi", this.data.daojishi, () => {
  261. this.setData({
  262. isRecord: true,
  263. });
  264. this.daojishiFn("recordjishi", this.data.recordjishi, () => {
  265. isLoading = false;
  266. wx.showLoading({
  267. title: "生成视频中...",
  268. });
  269. this.setData({
  270. popupVisible: false,
  271. });
  272. setTimeout(() => {
  273. // 不发送switch
  274. this.gotoWork();
  275. this.disconnect(true);
  276. wx.hideLoading();
  277. // 上传倒计时
  278. }, 30 * 1000);
  279. });
  280. });
  281. }, 500);
  282. }, 1000);
  283. }, 500);
  284. }
  285. //拍照
  286. else {
  287. let intel = null;
  288. intel && clearInterval(intel);
  289. this.setData({
  290. isShot: true,
  291. isVideo: false,
  292. });
  293. setTimeout(() => {
  294. this.setData({
  295. daojishi: 3,
  296. });
  297. app.emitEvent.switchMachine(this.data.currentScene);
  298. setTimeout(() => {
  299. app.emitEvent.Photo(this.data.authorCode);
  300. // 为了同步机器的倒计时
  301. setTimeout(() => {
  302. this.daojishiFn("daojishi", this.data.daojishi, () => {
  303. isLoading = false;
  304. wx.showLoading({
  305. title: "生成图片中...",
  306. // mask: true,
  307. });
  308. this.setData({
  309. popupVisible: false,
  310. });
  311. setTimeout(() => {
  312. // 不发送switch
  313. this.gotoWork();
  314. this.disconnect(true);
  315. wx.hideLoading();
  316. }, 4 * 1000);
  317. });
  318. }, 500);
  319. }, 1000);
  320. }, 500);
  321. }
  322. },
  323. tapSelect: function (e) {
  324. app.checkOperationTimeout(false, () => {
  325. app.showAlert("抱歉,因您长时间使用,让下一位小伙伴体验吧", () => {
  326. this.disconnect();
  327. });
  328. });
  329. if (!this.data.canTap) {
  330. return;
  331. }
  332. let { id } = e.currentTarget.dataset;
  333. if (id == this.data.currentScene) {
  334. return;
  335. }
  336. this.setData({
  337. canTap: false,
  338. });
  339. setTimeout(() => {
  340. this.setData({
  341. canTap: true,
  342. });
  343. app.hideLoading();
  344. }, 3 * 1000);
  345. wx.showLoading({
  346. title: "切换中...",
  347. mask: true,
  348. });
  349. this.setData({
  350. currentScene: id,
  351. });
  352. app.emitEvent.switchMachine(id);
  353. },
  354. disconnect(noswitch = false) {
  355. this.reset(noswitch);
  356. if (app.websocket) {
  357. app.emitEvent.close();
  358. app.websocket.disconnect();
  359. app.websocket = null;
  360. this.setData({
  361. isOtherZhanYong: true,
  362. });
  363. }
  364. },
  365. start: function (authorCode) {
  366. if (app.websocket) {
  367. return;
  368. }
  369. app.websocket = socket_io(API_BASE_URL, {
  370. transports: ["websocket"],
  371. });
  372. app.websocket.on("connect", () => {
  373. console.log("SOCKET连接成功");
  374. wx.showLoading({
  375. mask: true,
  376. title: "正在连接机器",
  377. });
  378. let linktimer = setTimeout(() => {
  379. clearTimeout(linktimer);
  380. wx.showToast({
  381. title: "网络异常,请稍后再试",
  382. icon: "none",
  383. });
  384. wx.hideLoading();
  385. }, 5000);
  386. app.emitEvent.request((data) => {
  387. // 校验成功后解除被他人占用情况
  388. clearTimeout(linktimer);
  389. wx.hideLoading();
  390. this.setData({
  391. isOtherZhanYong: false,
  392. });
  393. app.checkOperationTimeout(false, () => {
  394. app.showAlert("抱歉,因您长时间使用,让下一位小伙伴体验吧", () => {
  395. this.disconnect();
  396. });
  397. });
  398. app.websocket.on("ForceOffline", () => {
  399. console.log("ForceOffline成功");
  400. app.checkOperationTimeout(true);
  401. this.disconnect();
  402. });
  403. app.websocket.on("error", () => {
  404. this.disconnect();
  405. app.showAlert("服务器异常,请稍后再试");
  406. });
  407. if (data == 808) {
  408. // return app.showAlert("机器被占用,请稍后再试");
  409. app.checkOperationTimeout(true);
  410. this.disconnect();
  411. }
  412. if (data == 404 || data == 505) {
  413. app.checkOperationTimeout(true);
  414. this.disconnect();
  415. // app.showAlert("网络异常,请稍后再试");
  416. }
  417. try {
  418. const res = JSON.parse(data);
  419. this.getData(res.projectID, res.scenes);
  420. } catch (err) {
  421. console.log(err);
  422. }
  423. console.log(data, "data");
  424. }, authorCode);
  425. });
  426. },
  427. },
  428. });