index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. var g_app = getApp();
  10. let TYPESTR = {
  11. 0:'media',
  12. 1:'pic'
  13. }
  14. import Router from "../../utils/routes";
  15. VueLikePage([], {
  16. data: {
  17. cdn_url: CDN_URL,
  18. isIphoneX: false,
  19. canTap: true,
  20. rndWord: randomWord(false, 8),
  21. scenes: [
  22. {
  23. id: "4",
  24. },
  25. {
  26. id: "5",
  27. },
  28. {
  29. id: "6",
  30. },
  31. ],
  32. autoplay: true,
  33. interval: 3000,
  34. duration: 500,
  35. current: 0,
  36. currentScene: 4,
  37. daojishi: 0,
  38. isShot: false,
  39. isRecord: false,
  40. recordjishi: 10,
  41. type: "",
  42. },
  43. methods: {
  44. swiperChange: function (e) {
  45. this.setData({
  46. current: e.detail.current,
  47. });
  48. },
  49. onShow(){
  50. this.setData({
  51. rndWord: randomWord(false, 8)
  52. })
  53. },
  54. gotoWork() {
  55. timer && clearTimeout(timer);
  56. timer = null;
  57. Router.push({
  58. url: "work",
  59. query: {
  60. type: this.data.type,
  61. id: this.data.currentScene,
  62. rdw: encodeURIComponent(this.data.rndWord),
  63. },
  64. });
  65. this.reset();
  66. },
  67. reset() {
  68. this.setData({
  69. daojishi: 0,
  70. isShot: false,
  71. isRecord: false,
  72. currentScene: 1,
  73. recordjishi: 10,
  74. type: "",
  75. });
  76. this.clearLongTime();
  77. app.websocket && app.websocket.send({ data: "switch 1" });
  78. setTimeout(() => {
  79. app.hideLoading();
  80. });
  81. },
  82. clearLongTime() {
  83. app.longtime && clearInterval(app.longtime);
  84. app.longtime = null;
  85. },
  86. closeServer(cb=null) {
  87. app.closeByVideoHandle = true
  88. if (app.websocket) {
  89. app.send("close");
  90. app.websocket.close();
  91. app.websocket = null;
  92. }
  93. if (app.longtime) {
  94. setTimeout(() => {
  95. cb && cb()
  96. },100);
  97. }
  98. },
  99. handleWebSocket: function () {
  100. if (app.websocket) {
  101. return;
  102. }
  103. app.websocket = socket_io(API_BASE_URL,{
  104. transports: ['websocket']
  105. });
  106. app.websocket.on('connect', () => {
  107. console.log('SOCKET连接成功');
  108. app.emitEvent.request(data=>{
  109. console.log(data,1111111);
  110. });
  111. })
  112. app.websocket.on('ForceOffline', () => {
  113. app.showAlert("服务器连接失败,请稍后再试", () => {
  114. wx.reLaunch({
  115. url: "/pages/start/index",
  116. });
  117. });
  118. console.log('ForceOffline成功');
  119. })
  120. },
  121. checkTextDetail:function () {
  122. wx.showModal({
  123. title: '作品标识码(在首页获取作品)',
  124. content: TYPESTR[this.data.type]+this.data.rndWord + app.id,
  125. showCancel: false,
  126. confirmText: '复制文本',
  127. success: (result) => {
  128. if (result.confirm) {
  129. wx.setClipboardData({
  130. data: TYPESTR[this.data.type]+this.data.rndWord + app.id,
  131. success: () => {
  132. wx.getClipboardData({
  133. success: () => {
  134. setTimeout(() => {
  135. wx.navigateBack({
  136. delta: 3
  137. });
  138. }, 1000);
  139. },
  140. });
  141. }
  142. });
  143. }
  144. },
  145. });
  146. },
  147. onLoad: function () {
  148. isPhoneX().then((res) => {
  149. this.setData({
  150. isIphoneX: res,
  151. });
  152. });
  153. let tmp = g_app.globalData.scenes.map(item=>{
  154. return {id:item}
  155. })
  156. this.setData({
  157. scenes: tmp,
  158. currentScene:tmp[0].id
  159. })
  160. },
  161. daojishiFn(key, time, fn = () => {}) {
  162. intel && clearInterval(intel);
  163. let intel = null;
  164. let titop = time;
  165. intel = setInterval(() => {
  166. titop -= 1;
  167. let kv = {};
  168. kv[key] = titop;
  169. this.setData(kv);
  170. if (titop <= 0) {
  171. fn();
  172. intel && clearInterval(intel);
  173. return;
  174. }
  175. }, 1000);
  176. },
  177. shot: function (e) {
  178. // app.checkOperationTimeout(true);
  179. let { id } = e.currentTarget.dataset;
  180. this.setData({
  181. type: id,
  182. rndWord: randomWord(false, 8),
  183. });
  184. // if (!app.sendCheck()) {
  185. // return;
  186. // }
  187. //录像
  188. if (id == "0") {
  189. let intel = null;
  190. intel && clearInterval(intel);
  191. this.setData({
  192. isShot: true,
  193. });
  194. setTimeout(() => {
  195. this.setData({
  196. daojishi: 3,
  197. });
  198. app.emitEvent.switchMachine(this.data.currentScene)
  199. setTimeout(() => {
  200. app.emitEvent.Video(TYPESTR[this.data.type]+this.data.rndWord)
  201. // 为了同步机器的倒计时
  202. setTimeout(() => {
  203. this.daojishiFn("daojishi", this.data.daojishi, () => {
  204. this.setData({
  205. isRecord: true,
  206. });
  207. this.daojishiFn("recordjishi", this.data.recordjishi, () => {
  208. wx.showLoading({
  209. title: "生成视频中...",
  210. });
  211. setTimeout(() => {
  212. wx.hideLoading()
  213. this.checkTextDetail()
  214. }, 1000);
  215. });
  216. });
  217. }, 500);
  218. }, 1000);
  219. }, 500);
  220. }
  221. //拍照
  222. else {
  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.Photo(TYPESTR[this.data.type]+this.data.rndWord)
  235. // 为了同步机器的倒计时
  236. setTimeout(() => {
  237. this.daojishiFn("daojishi", this.data.daojishi, () => {
  238. wx.showLoading({
  239. title: "生成图片中...",
  240. // mask: true,
  241. });
  242. setTimeout(() => {
  243. this.checkTextDetail()
  244. wx.hideLoading()
  245. }, 1000);
  246. });
  247. }, 500);
  248. }, 1000);
  249. }, 500);
  250. }
  251. // 1秒后拍照
  252. setTimeout(() => {
  253. }, 1000);
  254. },
  255. tapSelect: function (e) {
  256. app.checkOperationTimeout();
  257. if (!this.data.canTap) {
  258. return;
  259. }
  260. this.clearLongTime();
  261. let { id } = e.currentTarget.dataset;
  262. if (id == this.data.currentScene) {
  263. return;
  264. }
  265. this.setData({
  266. canTap: false,
  267. });
  268. setTimeout(() => {
  269. this.setData({
  270. canTap: true,
  271. });
  272. app.hideLoading();
  273. }, 3 * 1000);
  274. wx.showLoading({
  275. title: "切换中...",
  276. mask: true,
  277. });
  278. this.setData({
  279. currentScene: id
  280. });
  281. app.emitEvent.switchMachine(id)
  282. },
  283. },
  284. });