tour.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import { getApp, getNum } from '@/app';
  2. import { base64ToBlob, convertBlob2File } from '@/utils/file';
  3. export default {
  4. namespaced: true,
  5. state() {
  6. return {
  7. // showPath: false,
  8. // showFrame: false,
  9. toursList: [],
  10. sourceList: null,
  11. frameId: 0,
  12. partId: 0,
  13. tours: [],
  14. musicList: [],
  15. delList: [],
  16. isPlay: false,
  17. showTours: false,
  18. isHome: true,
  19. };
  20. },
  21. getters: {
  22. tours: (state) => {
  23. return state.tours;
  24. },
  25. partId: (state) => {
  26. return state.partId;
  27. },
  28. frameId: (state) => {
  29. return state.frameId;
  30. },
  31. isPlay: (state) => {
  32. return state.isPlay;
  33. },
  34. isHome: (state) => {
  35. return state.isHome;
  36. },
  37. showTours: (state) => {
  38. return state.showTours;
  39. },
  40. },
  41. mutations: {
  42. setData(state, payload) {
  43. if (payload) {
  44. for (let key in payload) {
  45. state[key] = payload[key];
  46. }
  47. }
  48. },
  49. loaded(state, payload) {
  50. state.tours = payload;
  51. },
  52. setBackUp(state) {
  53. state.toursList = JSON.parse(JSON.stringify(state.tours));
  54. },
  55. update(state, tours) {
  56. this.commit('enterEdit');
  57. //如果删除片段和备份数据一样则退出编辑模式
  58. let t = setTimeout(() => {
  59. clearTimeout(t);
  60. if (state.tours.length == 0 && state.toursList.length == 0) {
  61. this.commit('leaveEdit');
  62. }
  63. }, 0);
  64. },
  65. async setFrame(state, payload) {
  66. const recordr = await getApp().TourManager.recorder;
  67. recordr.setFrame(state.frameId, payload);
  68. this.commit('tour/update');
  69. },
  70. async setPart(state, payload) {
  71. const recordr = await getApp().TourManager.recorder;
  72. let data = JSON.parse(JSON.stringify(payload));
  73. for (let key in data) {
  74. if (key === 'file') {
  75. delete data[key];
  76. }
  77. }
  78. recordr.setPart(state.partId, data);
  79. for (let key in payload) {
  80. state.tours[state.partId][key] = payload[key];
  81. }
  82. this.commit('tour/update');
  83. },
  84. getUploadMuscis(state, payload) {
  85. let musics = state.tours.filter((item) => {
  86. console.log(item.file);
  87. if (item.file) {
  88. return item;
  89. }
  90. });
  91. console.log(musics);
  92. return musics;
  93. },
  94. // setTime(state, time) {
  95. // // state.tours[state.partId].list[state.frameId].time = time * 1000
  96. // this.commit('tour/setFrame', { time: time * 1000 })
  97. // },
  98. delList(state, payload) {
  99. let frameId = payload.frameId || 0;
  100. let list = state.tours[state.partId].list;
  101. let music = state.tours[state.partId].music;
  102. let name = state.tours[state.partId].list[frameId].enter.cove;
  103. switch (payload.type) {
  104. case 'part':
  105. // if (music.indexOf('blob:') == -1 && music != '') {
  106. if (music != '') {
  107. let file = state.tours[state.partId].musicUrl ? state.tours[state.partId].musicUrl.split('/').pop().split('?').shift() : music.split('/').pop().split('?').shift();
  108. state.delList.push(file);
  109. }
  110. if (list.length > 0) {
  111. list.forEach((i, index) => {
  112. // if (i.enter.coverUrl || i.enter.cover.indexOf('data:image') == -1) {
  113. // state.delList.push(i.enter.cover.split('/').pop().split('?').shift())
  114. // }
  115. let cover = i.enter.coverUrl ? i.enter.coverUrl.split('/').pop().split('?').shift() : i.enter.cover.split('/').pop().split('?').shift();
  116. state.delList.push(cover);
  117. });
  118. }
  119. console.log(state.delList);
  120. break;
  121. case 'frame':
  122. // if (name.indexOf('data:image') == -1) {
  123. // state.delList.push(name.split('/').pop().split('?').shift())
  124. // }
  125. let cover = state.tours[state.partId].list[frameId].enter.coverUrl
  126. ? state.tours[state.partId].list[frameId].enter.coverUrl.split('/').pop().split('?').shift()
  127. : state.tours[state.partId].list[frameId].enter.cover.split('/').pop().split('?').shift();
  128. state.delList.push(cover);
  129. break;
  130. case 'music':
  131. // if (music.indexOf('blob:') == -1 && music != '') {
  132. // state.delList.push(name.split('/').pop().split('?').shift())
  133. // }
  134. if (music != '') {
  135. let file = state.tours[state.partId].musicUrl ? state.tours[state.partId].musicUrl.split('/').pop().split('?').shift() : music.split('/').pop().split('?').shift();
  136. state.delList.push(file);
  137. }
  138. break;
  139. // case 'all':
  140. // for (var i = 0; i < state.tours.length; i++) {
  141. // let music = state.tours[i].music
  142. // if (music.indexOf('blob:') == -1 && music != '') {
  143. // state.delList.push(music.split('/').pop().split('?').shift())
  144. // }
  145. // state.tours[i].list.forEach((frame, index) => {
  146. // if (frame.enter.cover.indexOf('data:image') == -1) {
  147. // state.delList.push(frame.enter.cover.split('/').pop().split('?').shift())
  148. // }
  149. // })
  150. // }
  151. // break
  152. }
  153. console.log(state.delList);
  154. },
  155. cancel(state) {
  156. state.tours = JSON.parse(JSON.stringify(state.toursList));
  157. getApp().TourManager.load(JSON.parse(JSON.stringify(state.toursList))); //通知回滚
  158. state.toursList = JSON.parse(JSON.stringify(state.tours));
  159. this.commit('leaveEdit');
  160. },
  161. save(state, newdData) {
  162. state.toursList = JSON.parse(JSON.stringify(newdData));
  163. for (let i = 0; i < state.tours.length; i++) {
  164. state.tours[i].musicUrl = newdData[i].music;
  165. for (let j = 0; j < newdData[i].list.length; j++) {
  166. state.tours[i].list[j].coverUrl = newdData[i].list[j].cover;
  167. }
  168. }
  169. console.log(state.tours);
  170. // state.tours = JSON.parse(JSON.stringify(newdData))
  171. this.commit('leaveEdit');
  172. },
  173. // filterFiles(state,list){
  174. // for()
  175. // }
  176. },
  177. actions: {
  178. delTours({ commit, state }, payload) {
  179. // tour/delete
  180. return getApp()
  181. .remote_editor.tour_delete({ num: getNum() })
  182. .then((res) => {
  183. if (res.success) {
  184. state.tours = [];
  185. commit('save', []);
  186. getApp().TourManager.load([]);
  187. }
  188. return res;
  189. })
  190. .catch((err) => {
  191. console.log(err);
  192. return err;
  193. });
  194. },
  195. async delFile({ commit, state }, payload) {
  196. let params = {
  197. num: getNum(),
  198. bizType: 'tour',
  199. fileNames: state.delList,
  200. };
  201. let res = await this.dispatch('delFiles', params);
  202. console.log(res);
  203. if (res.success) {
  204. state.delList = [];
  205. }
  206. },
  207. async save({ commit, dispatch, state }, payload) {
  208. if (state.delList.length > 0) {
  209. dispatch('delFile');
  210. }
  211. let recorder = await getApp().TourManager.recorder;
  212. let exportFiles = recorder.exportFiles();
  213. let files = [];
  214. console.log(exportFiles);
  215. exportFiles.map((item) => {
  216. let obj = {};
  217. if (item.type == 'base64') {
  218. obj.file = convertBlob2File(base64ToBlob(item.file), item.name);
  219. obj.filename = item.name;
  220. } else if (item.type == 'file') {
  221. state.tours.filter((i) => {
  222. let fileId = item.name.split('.')[0].split('-')[2];
  223. if (fileId == i.sid) {
  224. obj.file = i.file;
  225. obj.filename = item.name;
  226. }
  227. });
  228. }
  229. files.push(obj);
  230. });
  231. console.log(files);
  232. let res = await this.dispatch('scene/upload_files', { bizType: 'tour', num: getNum(), type: 0, files: files });
  233. if (res.success) {
  234. }
  235. let data = recorder.exportData();
  236. // let audioFiles = files.filter(i => {
  237. // return i.type === 'file'
  238. // })
  239. // let imageFiles = files.filter(i => {
  240. // return i.type === 'base64'
  241. // })
  242. // if (audioFiles.length > 0) {
  243. // for (let i = 0; i < audioFiles.length; i++) {
  244. // for (let j = 0; j < state.tours.length; j++) {
  245. // let fileId = audioFiles[i].name.split('.')[0].split('-')[2]
  246. // console.log(fileId)
  247. // if (fileId == state.tours[j].sid) {
  248. // let res = await this.dispatch('scene/upload_files', { files: state.tours[j].file, bizType: 'tour', num: getNum(), type: 0, fileName: audioFiles[i].name })
  249. // if (res.success) {
  250. // }
  251. // }
  252. // }
  253. // }
  254. // }
  255. // if (imageFiles.length > 0) {
  256. // for (let i = 0; i < imageFiles.length; i++) {
  257. // let res = await this.dispatch('scene/upload_files', { base64: imageFiles[i].file, bizType: 'tour', num: getNum(), type: 0, fileName: imageFiles[i].name })
  258. // if (res.success) {
  259. // }
  260. // }
  261. // }
  262. console.log('data', data);
  263. console.log('tours', state.tours);
  264. let params = {
  265. num: getNum(),
  266. data: JSON.stringify(data),
  267. };
  268. return getApp()
  269. .remote_editor.tour_save(params)
  270. .then((res) => {
  271. if (res.success) {
  272. commit('save', data);
  273. }
  274. return res;
  275. })
  276. .catch((err) => {
  277. console.log(err);
  278. return err;
  279. });
  280. },
  281. },
  282. };