base.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. import '@/scss/iconfont/iconfont.css'
  2. import domToImage from 'dom-to-image'
  3. import {
  4. getProgram,
  5. updateProgram
  6. } from '@/api/program'
  7. import {
  8. getAssetUrl,
  9. getThumbnailUrl
  10. } from '@/api/asset'
  11. import {
  12. create,
  13. fix,
  14. getDuration,
  15. toJSON
  16. } from './utils'
  17. export default {
  18. provide () {
  19. return {
  20. control: this
  21. }
  22. },
  23. data () {
  24. return {
  25. program: null,
  26. snapping: false,
  27. loading: true,
  28. scale: 100,
  29. minScale: 100,
  30. maxScale: 100,
  31. padding: 0,
  32. selectedWidgetIndex: -1,
  33. node: null,
  34. bgm: '',
  35. muted: true
  36. }
  37. },
  38. computed: {
  39. transformStyles () {
  40. return {
  41. transform: `scale(${this.scale / 100})`,
  42. 'transform-origin': '0 0'
  43. }
  44. },
  45. styles () {
  46. if (this.node) {
  47. const { width, height, backgroundColor, backgroundImage } = this.node
  48. return {
  49. width: `${width}px`,
  50. height: `${height}px`,
  51. color: backgroundColor,
  52. 'background-image': backgroundImage[0] ? `url("${getThumbnailUrl(backgroundImage[0].keyName)}")` : 'none'
  53. }
  54. }
  55. return {}
  56. },
  57. widgets () {
  58. return this.node?.widgets || []
  59. },
  60. widget () {
  61. return this.widgets[this.selectedWidgetIndex] || this.node
  62. },
  63. hasAudio () {
  64. if (this.node) {
  65. if (this.node.bgm.length) {
  66. return true
  67. }
  68. return this.node.widgets.some(widget => widget.mute === 0 && (widget.sources?.length || widget.url))
  69. }
  70. return false
  71. },
  72. bgmUrl () {
  73. return this.bgm ? getAssetUrl(this.bgm) : ''
  74. },
  75. hasNext () {
  76. return !this.muted && this.node?.bgm.length > 1
  77. }
  78. },
  79. created () {
  80. this.fetch()
  81. },
  82. beforeRouteUpdate () {
  83. window.close()
  84. },
  85. beforeRouteLeave () {
  86. window.close()
  87. },
  88. watch: {
  89. 'node.bgm' () {
  90. this.setBgm()
  91. }
  92. },
  93. methods: {
  94. toggleMute () {
  95. this.muted = !this.muted
  96. },
  97. setBgm () {
  98. this.$bgmIndex = 0
  99. this.chooseBgm()
  100. },
  101. chooseBgm () {
  102. const { bgm, toggleType } = this.node
  103. const length = bgm.length
  104. if (length === 0) {
  105. this.bgm = ''
  106. return
  107. }
  108. if (length === 1) {
  109. this.bgm = bgm[0].keyName
  110. return
  111. }
  112. let index
  113. switch (toggleType) {
  114. case 'cycle':
  115. index = this.$bgmIndex
  116. this.$bgmIndex = (index + 1) % length
  117. break
  118. case 'random':
  119. index = Math.random() * length | 0
  120. if (bgm[index]?.keyName === this.bgm) {
  121. if (index === 0) {
  122. index += 1
  123. } else {
  124. index -= 1
  125. }
  126. }
  127. break
  128. default:
  129. return
  130. }
  131. this.bgm = bgm[index].keyName
  132. },
  133. onAudioEnded () {
  134. if (this.node.bgm.length > 1) {
  135. this.chooseBgm()
  136. }
  137. this.$refs.audio.play()
  138. },
  139. onAudioError (e) {
  140. console.warn('audio error', e)
  141. if (this.node?.bgm.length > 1) {
  142. this.chooseBgm()
  143. }
  144. },
  145. checkRatio () {
  146. const wrapper = this.$refs.wrapper
  147. if (wrapper && this.node) {
  148. const width = wrapper.offsetWidth - this.padding * 2
  149. const height = wrapper.offsetHeight - this.padding * 2
  150. const { width: cWidth, height: cHeight } = this.node
  151. const wScale = width / cWidth
  152. const hScale = height / cHeight
  153. if (cWidth > width || cHeight > height) {
  154. this.scale = Math.min(wScale, hScale) * 100 | 0
  155. this.maxScale = Math.max(1, wScale, hScale) * 100 | 0
  156. } else {
  157. this.scale = 100
  158. this.maxScale = Math.max(2, wScale, hScale) * 100 | 0
  159. }
  160. this.minScale = this.scale
  161. console.log('width: layout -> window', cWidth, '->', width, wScale)
  162. console.log('height: layout -> window', cHeight, '->', height, hScale)
  163. console.log(this.scale, this.maxScale)
  164. }
  165. },
  166. showMessage (type, message) {
  167. let count = 3
  168. const loading = this.$loading({
  169. lock: true,
  170. text: `${message},${count}秒后自动关闭`,
  171. spinner: `el-icon-${type}`,
  172. background: 'rgba(0, 0, 0, .8)',
  173. customClass: `c-lock ${type}`
  174. })
  175. const timer = setInterval(() => {
  176. count -= 1
  177. if (count === 0) {
  178. clearInterval(timer)
  179. window.close()
  180. } else {
  181. loading.text = `${message},${count}秒后自动关闭`
  182. }
  183. }, 1000)
  184. },
  185. initCanvas (canvas) {
  186. this.node = create(canvas)
  187. console.log(this.node)
  188. },
  189. checkStatus (status) {
  190. return true
  191. },
  192. onLoaded () { },
  193. fetch () {
  194. this.loading = true
  195. getProgram(this.$route.params.id).then(({ data }) => {
  196. try {
  197. if (!this.checkStatus(data.status)) {
  198. this.showMessage('warning', '布局已锁定')
  199. return
  200. }
  201. this.program = data
  202. const { resolutionRatio, itemJsonStr } = data
  203. const [width, height] = resolutionRatio.split('x')
  204. if (!width || !height) {
  205. this.showMessage('warning', '布局分辨率异常')
  206. return
  207. }
  208. this.initCanvas({
  209. width: Number(width),
  210. height: Number(height),
  211. ...JSON.parse(itemJsonStr)
  212. })
  213. this.checkRatio()
  214. window.addEventListener('resize', this.checkRatio)
  215. this.onLoaded()
  216. } catch (e) {
  217. this.showMessage('error', '布局解析失败')
  218. }
  219. }, err => {
  220. this.showMessage('error', err.errMessage || '获取布局失败')
  221. }).finally(() => {
  222. this.loading = false
  223. })
  224. },
  225. check () {
  226. const warning = fix(this.node)
  227. if (warning) {
  228. return this.$confirm(
  229. `${warning},确定保存?`,
  230. '提示',
  231. {
  232. type: 'warning',
  233. confirmButtonText: '确定',
  234. cancelButtonText: '取消'
  235. }
  236. )
  237. }
  238. return Promise.resolve()
  239. },
  240. async _save () {
  241. try {
  242. const base64 = await this.snap()
  243. const result = await updateProgram({
  244. id: this.program.id,
  245. duration: getDuration(this.node),
  246. itemJsonStr: JSON.stringify(toJSON(this.node)),
  247. base64
  248. })
  249. if (result) {
  250. window.opener?.postMessage({
  251. id: this.program.id,
  252. base64
  253. })
  254. this.$message({
  255. type: 'success',
  256. message: '保存成功'
  257. })
  258. } else {
  259. this.$message({
  260. type: 'warning',
  261. message: '保存失败'
  262. })
  263. }
  264. } catch (e) {
  265. console.warn(e)
  266. this.$message({
  267. type: 'warning',
  268. message: '保存失败'
  269. })
  270. }
  271. },
  272. save () {
  273. this.check().then(() => {
  274. this.selectedWidgetIndex = -1
  275. this.loading = true
  276. this.$nextTick(() => {
  277. this._save().finally(() => {
  278. this.loading = false
  279. })
  280. })
  281. })
  282. },
  283. snap () {
  284. this.snapping = true
  285. return domToImage.toJpeg(this.$refs.canvas, {
  286. filter (node) {
  287. const { tagName } = node
  288. if (tagName === 'CANVAS') {
  289. return /^data:.+;base64,.+/.test(node.toDataURL())
  290. }
  291. return tagName !== 'VIDEO' && tagName !== 'IFRAME'
  292. },
  293. width: this.node.width * this.scale / 100 | 0,
  294. height: this.node.height * this.scale / 100 | 0,
  295. quality: 0.1
  296. }).finally(() => {
  297. this.snapping = false
  298. })
  299. }
  300. }
  301. }