index.js 9.8 KB

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