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