index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 timer = null;
  8. var g_app = getApp();
  9. import Router from "../../utils/routes";
  10. VueLikePage([], {
  11. data: {
  12. cdn_url: CDN_URL,
  13. isIphoneX: false,
  14. canTap: true,
  15. rndWord: randomWord(false, 8),
  16. background: [
  17. {
  18. img: "lunbo1.png",
  19. id: "1",
  20. },
  21. {
  22. img: "lunbo2.png",
  23. id: "2",
  24. },
  25. {
  26. img: "lunbo3.png",
  27. id: "3",
  28. },
  29. ],
  30. scenes: [
  31. {
  32. img: "1.png",
  33. id: "1",
  34. },
  35. {
  36. img: "2.png",
  37. id: "2",
  38. },
  39. {
  40. img: "3.png",
  41. id: "3",
  42. },
  43. ],
  44. autoplay: true,
  45. interval: 3000,
  46. duration: 500,
  47. current: 0,
  48. currentScene: 1,
  49. daojishi: 0,
  50. isShot: false,
  51. isRecord: false,
  52. recordjishi: 10,
  53. type: "",
  54. },
  55. methods: {
  56. swiperChange: function (e) {
  57. this.setData({
  58. current: e.detail.current,
  59. });
  60. },
  61. onShow(){
  62. this.setData({
  63. rndWord: randomWord(false, 8)
  64. })
  65. },
  66. gotoWork() {
  67. timer && clearTimeout(timer);
  68. timer = null;
  69. Router.push({
  70. url: "work",
  71. query: {
  72. type: this.data.type,
  73. id: this.data.currentScene,
  74. rdw: encodeURIComponent(this.data.rndWord),
  75. },
  76. });
  77. this.reset();
  78. },
  79. reset() {
  80. this.setData({
  81. daojishi: 0,
  82. isShot: false,
  83. isRecord: false,
  84. currentScene: 1,
  85. recordjishi: 10,
  86. type: "",
  87. });
  88. this.clearLongTime();
  89. app.websocket && app.websocket.send({ data: "switch 1" });
  90. setTimeout(() => {
  91. app.hideLoading();
  92. });
  93. },
  94. clearLongTime() {
  95. app.longtime && clearInterval(app.longtime);
  96. app.longtime = null;
  97. },
  98. closeServer(cb=null) {
  99. app.closeByVideoHandle = true
  100. if (app.websocket) {
  101. app.send("close");
  102. app.websocket.close();
  103. app.websocket = null;
  104. }
  105. if (app.longtime) {
  106. setTimeout(() => {
  107. cb && cb()
  108. },100);
  109. }
  110. },
  111. handleWebSocket: function () {
  112. if (app.websocket) {
  113. return;
  114. }
  115. app.websocket = wx.connectSocket({
  116. url: API_BASE_URL,
  117. timeout: 180 * 1000,
  118. success: () => {},
  119. });
  120. app.websocket.onOpen(() => {
  121. app.is_connect = true;
  122. app.is_connect_ok = null;
  123. app.send("request");
  124. });
  125. app.websocket.onMessage((msg) => {
  126. console.log(msg);
  127. app.hideLoading();
  128. if (typeof msg.data == "string") {
  129. switch (msg.data) {
  130. case "error 101":
  131. console.log("发送的消息有误");
  132. break;
  133. case "error 302":
  134. app.hideLoading();
  135. app.showAlert("网络异常,生成失败");
  136. break;
  137. case "error 404":
  138. app.showAlert("网络异常,请稍后再试");
  139. break;
  140. case "error 500":
  141. app.is_connect_ok = false;
  142. app.is_other_used = true;
  143. if (app.is_send_msg) {
  144. app.showAlert("服务器连接失败,请重新连接", () => {
  145. wx.reLaunch({
  146. url: "index",
  147. });
  148. },'重新连接');
  149. }
  150. break;
  151. case "error 808":
  152. app.is_connect_ok = true;
  153. app.is_other_used = true;
  154. if (app.is_send_msg) {
  155. app.showAlert("当前画屏正在被使用,请稍后");
  156. }
  157. break;
  158. case "error 909":
  159. if (app.is_send_msg) {
  160. app.showAlert("录像失败");
  161. }
  162. break;
  163. case "ok 200":
  164. console.log("切换画风成功");
  165. break;
  166. case "ok 201":
  167. app.is_connect_ok = false;
  168. app.is_other_used = true;
  169. app.checkOperationTimeout(true);
  170. break;
  171. case "ok 300":
  172. app.is_other_used = null;
  173. app.is_connect_ok = true;
  174. app.checkOperationTimeout();
  175. this.reset();
  176. break;
  177. case "ok 301":
  178. console.log("拍照成功,开始等待照片");
  179. // 轮询拿视频/图片
  180. this.clearLongTime();
  181. app.longtime = setInterval(() => {
  182. let link = "",
  183. m_type = "";
  184. if (this.data.type == "0") {
  185. link = `${VIDEO_BASE_URL}4dvedio/vedio${this.data.rndWord}.mp4`;
  186. m_type = "video";
  187. } else {
  188. link = `${VIDEO_BASE_URL}4dpic/pic${this.data.rndWord}.jpg`;
  189. m_type = "jpeg";
  190. }
  191. console.log(link, 1111);
  192. console.log(m_type, 1111);
  193. wx.downloadFile({
  194. url: link,
  195. success: (res) => {
  196. //判断是否为数组
  197. let typeType =
  198. (Object.prototype.toString.call(res.header["Content-Type"]) == "[object String]")?
  199. res.header["Content-Type"] : res.header["Content-Type"][0]
  200. console.log(typeType,8888);
  201. //判断不是xml文件
  202. if (typeType.indexOf(m_type) > -1) {
  203. this.clearLongTime();
  204. g_app.globalData.m_file = res.tempFilePath;
  205. this.gotoWork();
  206. }
  207. },
  208. fail: (e) => {
  209. console.log(e, 1111);
  210. },
  211. });
  212. }, 5 * 1000);
  213. // 3分钟后不可以就弹出生成失败
  214. timer = setTimeout(() => {
  215. this.clearLongTime();
  216. app.hideLoading();
  217. app.showAlert("生成失败,请重新连接", () => {
  218. wx.reLaunch({
  219. url: "index",
  220. });
  221. });
  222. }, 180 * 1000);
  223. break;
  224. default:
  225. try {
  226. // var json = JSON.parse(msg.data);
  227. // if (json.url) {
  228. // if (json.url.indexOf(this.data.rndWord) > -1) {
  229. // timer && clearTimeout(timer);
  230. // timer = null
  231. // this.clearLongTime()
  232. // this.gotoWork();
  233. // }
  234. // }
  235. } catch (error) {
  236. console.error(error);
  237. }
  238. break;
  239. }
  240. app.is_send_msg = false;
  241. } else {
  242. console.log("error 600");
  243. }
  244. });
  245. app.websocket.onClose((res) => {
  246. app.is_connect = false;
  247. app.checkOperationTimeout(true);
  248. app.hideLoading();
  249. app.websocket = null;
  250. if (!app.longtime&&!app.closeByVideoHandle) {
  251. app.showAlert("服务器连接已关闭,请重新连接", () => {
  252. wx.reLaunch({
  253. url: "index",
  254. });
  255. },'重新连接');
  256. app.closeByVideoHandle = false
  257. }
  258. });
  259. },
  260. onLoad: function () {
  261. isPhoneX().then((res) => {
  262. this.setData({
  263. isIphoneX: res,
  264. });
  265. });
  266. this.handleWebSocket();
  267. },
  268. daojishiFn(key, time, fn = () => {}) {
  269. intel && clearInterval(intel);
  270. let intel = null;
  271. let titop = time;
  272. intel = setInterval(() => {
  273. titop -= 1;
  274. let kv = {};
  275. kv[key] = titop;
  276. this.setData(kv);
  277. if (titop <= 0) {
  278. fn();
  279. intel && clearInterval(intel);
  280. return;
  281. }
  282. }, 1000);
  283. },
  284. shot: function (e) {
  285. app.checkOperationTimeout(true);
  286. let { id } = e.currentTarget.dataset;
  287. this.setData({
  288. type: id,
  289. rndWord: randomWord(false, 8),
  290. });
  291. if (!app.sendCheck()) {
  292. return;
  293. }
  294. //录像
  295. if (id == "0") {
  296. let intel = null;
  297. intel && clearInterval(intel);
  298. this.setData({
  299. isShot: true,
  300. });
  301. setTimeout(() => {
  302. this.setData({
  303. daojishi: 3,
  304. });
  305. app.send("switch " + this.data.currentScene);
  306. setTimeout(() => {
  307. app.send("photo " + "0#" + this.data.rndWord);
  308. // 为了同步机器的倒计时
  309. setTimeout(() => {
  310. this.daojishiFn("daojishi", this.data.daojishi, () => {
  311. this.setData({
  312. isRecord: true,
  313. });
  314. this.daojishiFn("recordjishi", this.data.recordjishi, () => {
  315. wx.showLoading({
  316. title: "生成视频中...",
  317. });
  318. setTimeout(() => {
  319. this.closeServer(()=>{
  320. wx.showLoading({
  321. title: "生成视频中...",
  322. });
  323. });
  324. }, 20 * 1000);
  325. });
  326. });
  327. }, 500);
  328. }, 1000);
  329. }, 500);
  330. }
  331. //拍照
  332. else {
  333. let intel = null;
  334. intel && clearInterval(intel);
  335. this.setData({
  336. isShot: true,
  337. });
  338. setTimeout(() => {
  339. this.setData({
  340. daojishi: 3,
  341. });
  342. app.send("switch " + this.data.currentScene);
  343. setTimeout(() => {
  344. app.send("photo " + "1#" + this.data.rndWord);
  345. // 为了同步机器的倒计时
  346. setTimeout(() => {
  347. this.daojishiFn("daojishi", this.data.daojishi, () => {
  348. this.closeServer(()=>{
  349. wx.showLoading({
  350. title: "生成图片中...",
  351. // mask: true,
  352. });
  353. });
  354. });
  355. }, 500);
  356. }, 1000);
  357. }, 500);
  358. }
  359. // 1秒后拍照
  360. setTimeout(() => {
  361. }, 1000);
  362. },
  363. tapSelect: function (e) {
  364. app.checkOperationTimeout();
  365. if (!this.data.canTap) {
  366. return;
  367. }
  368. this.clearLongTime();
  369. let { id } = e.currentTarget.dataset;
  370. if (id == this.data.currentScene) {
  371. return;
  372. }
  373. this.setData({
  374. canTap: false,
  375. });
  376. setTimeout(() => {
  377. this.setData({
  378. canTap: true,
  379. });
  380. app.hideLoading();
  381. }, 3 * 1000);
  382. if (!app.sendCheck()) {
  383. this.setData({
  384. canTap: true,
  385. });
  386. app.hideLoading();
  387. } else {
  388. wx.showLoading({
  389. title: "切换中...",
  390. mask: true,
  391. });
  392. this.setData({
  393. currentScene: id
  394. });
  395. app.send("switch " + id);
  396. }
  397. },
  398. },
  399. });