index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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.scene) {
  164. console.log('------',options.scene);
  165. // const aa ='309c23aeaf7a_YW0z69ed02eefe0'
  166. const str =encodeURIComponent(options.scene)
  167. const jiQiCode =str.split('_')[0]
  168. const zuoPinId =str.split('_')[1]
  169. app.id=jiQiCode
  170. this.setData({
  171. authorCode: decodeURIComponent(zuoPinId),
  172. })
  173. this.start(this.data.authorCode)
  174. }
  175. if (wx.getStorageSync('records')) {
  176. this.setData({
  177. records: JSON.parse(wx.getStorageSync('records'))
  178. })
  179. }
  180. // wx.setStorageSync('records', JSON.stringify(this.data.records))
  181. isPhoneX().then((res) => {
  182. this.setData({
  183. isIphoneX: res,
  184. });
  185. });
  186. // let tmp = g_app.globalData.scenes.map(item=>{
  187. // return {id:item}
  188. // })
  189. // this.setData({
  190. // scenes: tmp,
  191. // currentScene:tmp[0].id
  192. // })
  193. },
  194. daojishiFn(key, time, fn = () => {}) {
  195. intel && clearInterval(intel);
  196. let intel = null;
  197. let titop = time;
  198. intel = setInterval(() => {
  199. titop -= 1;
  200. let kv = {};
  201. kv[key] = titop;
  202. this.setData(kv);
  203. if (titop <= 0) {
  204. fn();
  205. intel && clearInterval(intel);
  206. return;
  207. }
  208. }, 1000);
  209. },
  210. shot: function (e) {
  211. app.checkOperationTimeout(true);
  212. let {
  213. id
  214. } = e.currentTarget.dataset;
  215. this.setData({
  216. type: id,
  217. });
  218. // if (!app.sendCheck()) {
  219. // return;
  220. // }
  221. //录像
  222. if (id == "0") {
  223. let intel = null;
  224. intel && clearInterval(intel);
  225. this.setData({
  226. isShot: true,
  227. });
  228. setTimeout(() => {
  229. this.setData({
  230. daojishi: 3,
  231. });
  232. app.emitEvent.switchMachine(this.data.currentScene)
  233. setTimeout(() => {
  234. app.emitEvent.Video(this.data.authorCode)
  235. // 为了同步机器的倒计时
  236. setTimeout(() => {
  237. this.daojishiFn("daojishi", this.data.daojishi, () => {
  238. this.setData({
  239. isRecord: true,
  240. });
  241. this.daojishiFn("recordjishi", this.data.recordjishi, () => {
  242. wx.showLoading({
  243. title: "生成视频中...",
  244. });
  245. setTimeout(() => {
  246. // 不发送switch
  247. this.gotoWork()
  248. this.disconnect(true)
  249. wx.hideLoading()
  250. // 上传倒计时
  251. }, 30 * 1000);
  252. });
  253. });
  254. }, 500);
  255. }, 1000);
  256. }, 500);
  257. }
  258. //拍照
  259. else {
  260. let intel = null;
  261. intel && clearInterval(intel);
  262. this.setData({
  263. isShot: true,
  264. });
  265. setTimeout(() => {
  266. this.setData({
  267. daojishi: 3,
  268. });
  269. app.emitEvent.switchMachine(this.data.currentScene)
  270. setTimeout(() => {
  271. app.emitEvent.Photo(this.data.authorCode)
  272. // 为了同步机器的倒计时
  273. setTimeout(() => {
  274. this.daojishiFn("daojishi", this.data.daojishi, () => {
  275. wx.showLoading({
  276. title: "生成图片中...",
  277. // mask: true,
  278. });
  279. setTimeout(() => {
  280. // 不发送switch
  281. this.gotoWork()
  282. this.disconnect(true)
  283. wx.hideLoading()
  284. }, 4 * 1000);
  285. });
  286. }, 500);
  287. }, 1000);
  288. }, 500);
  289. }
  290. },
  291. tapSelect: function (e) {
  292. app.checkOperationTimeout(false, () => {
  293. app.showAlert("抱歉,因您长时间使用,让下一位小伙伴体验吧", () => {
  294. this.disconnect();
  295. });
  296. });
  297. if (!this.data.canTap) {
  298. return;
  299. }
  300. let {
  301. id
  302. } = e.currentTarget.dataset;
  303. if (id == this.data.currentScene) {
  304. return;
  305. }
  306. this.setData({
  307. canTap: false,
  308. });
  309. setTimeout(() => {
  310. this.setData({
  311. canTap: true,
  312. });
  313. app.hideLoading();
  314. }, 3 * 1000);
  315. wx.showLoading({
  316. title: "切换中...",
  317. mask: true,
  318. });
  319. this.setData({
  320. currentScene: id
  321. });
  322. app.emitEvent.switchMachine(id)
  323. },
  324. disconnect(noswitch=false) {
  325. this.reset(noswitch);
  326. if (app.websocket) {
  327. app.emitEvent.close()
  328. app.websocket.disconnect();
  329. app.websocket = null;
  330. this.setData({
  331. isOtherZhanYong: true
  332. })
  333. }
  334. },
  335. start: function (authorCode) {
  336. if (app.websocket) {
  337. return;
  338. }
  339. app.websocket = socket_io(API_BASE_URL, {
  340. transports: ["websocket"],
  341. });
  342. app.websocket.on("connect", () => {
  343. console.log("SOCKET连接成功");
  344. wx.showLoading({
  345. mask: true,
  346. title: '正在连接机器',
  347. })
  348. let linktimer = setTimeout(() => {
  349. clearTimeout(linktimer)
  350. wx.showToast({
  351. title: '网络异常,请稍后再试',
  352. icon: 'none'
  353. })
  354. wx.hideLoading()
  355. }, 5000);
  356. app.emitEvent.request((data) => {
  357. // 校验成功后解除被他人占用情况
  358. clearTimeout(linktimer)
  359. wx.hideLoading()
  360. this.setData({
  361. isOtherZhanYong: false
  362. })
  363. app.checkOperationTimeout(false, () => {
  364. app.showAlert("抱歉,因您长时间使用,让下一位小伙伴体验吧", () => {
  365. this.disconnect();
  366. });
  367. });
  368. app.websocket.on("ForceOffline", () => {
  369. console.log("ForceOffline成功");
  370. app.checkOperationTimeout(true);
  371. this.disconnect();
  372. });
  373. app.websocket.on("error", () => {
  374. this.disconnect();
  375. app.showAlert("服务器异常,请稍后再试");
  376. });
  377. if (data == 808) {
  378. // return app.showAlert("机器被占用,请稍后再试");
  379. app.checkOperationTimeout(true);
  380. this.disconnect();
  381. }
  382. if (data == 404 || data == 505) {
  383. app.checkOperationTimeout(true);
  384. this.disconnect();
  385. // app.showAlert("网络异常,请稍后再试");
  386. }
  387. console.log(data, 'data')
  388. }, authorCode);
  389. });
  390. },
  391. },
  392. });