310 lines
6.9 KiB
JavaScript
310 lines
6.9 KiB
JavaScript
// pages/index/index.js
|
||
var util = require('../../utils/util.js')
|
||
var formOpt = require('../../utils/text.js')
|
||
var app = getApp()
|
||
|
||
// 一些选项列表
|
||
var xiaojuedingArr = require('../../utils/xiaojueding.js')
|
||
|
||
function randomsort(a, b) {
|
||
return Math.random() > 0.5 ? -1 : 1
|
||
}
|
||
|
||
var page = {
|
||
data: {
|
||
textId: '', // 剧本ID
|
||
size: {
|
||
w: 599,
|
||
h: 600,
|
||
}, //转盘大小可配置
|
||
musicflg: false,
|
||
fastJuedin: false,
|
||
repeat: false,
|
||
xiaojuedingArr: [
|
||
{
|
||
id: 0,
|
||
awards: [],
|
||
},
|
||
],
|
||
s_awards: {}, //结果
|
||
title: '', // 剧情标题
|
||
tip: '', //剧情导语
|
||
text: [], //当前文本数据
|
||
allText: [], // 全文本
|
||
story: [],
|
||
bg_img: '',
|
||
bg_music: '',
|
||
share: false,
|
||
canvasWidth: 400,
|
||
canvasHeight: 650,
|
||
showCanvasFlag: false,
|
||
saveFrameFlag: false,
|
||
isTbc: false,
|
||
isRestart: true,
|
||
isCompleted: false,
|
||
canGo: true,
|
||
isDev: true, //调试模式
|
||
player: null
|
||
},
|
||
onLoad: function(options) {
|
||
const _id = options._id
|
||
// TODO: 播放器
|
||
const player = wx.getBackgroundAudioManager()
|
||
this.setData({
|
||
textId: _id,
|
||
player: player
|
||
})
|
||
this.data.player.title = 'music'
|
||
this.data.player.src = 'https://resource.kingsome.cn/game_file/5dc502d63aa670e8d3335426.wav'
|
||
this.data.player.play()
|
||
|
||
console.log(this.data.player)
|
||
this.zhuanpan = this.selectComponent('#zhuanpan')
|
||
},
|
||
onShow: function(e) {
|
||
this.restart()
|
||
this.setData({
|
||
isCompleted: false,
|
||
canGo: true,
|
||
})
|
||
wx.setStorageSync('temp', '')
|
||
},
|
||
|
||
//接收当前转盘初始化时传来的参数
|
||
getData(e) {
|
||
this.setData({
|
||
awardsConfig: e.detail,
|
||
})
|
||
},
|
||
|
||
//接收当前转盘结束后的答案选项
|
||
getAwards(e) {
|
||
console.log('getAwards', e)
|
||
|
||
this.setData({
|
||
s_awards: e.detail.end ? '?' : e.detail.s_awards,
|
||
share: e.detail.end ? true : false,
|
||
})
|
||
|
||
const story = this.data.story
|
||
// 显示导语
|
||
if (this.data.story.length === 0) {
|
||
story.unshift({
|
||
name: this.data.tip,
|
||
})
|
||
}
|
||
|
||
story.unshift(e.detail.s_awards)
|
||
console.log('转盘结果', e.detail.s_awards)
|
||
this.setData({
|
||
story: story,
|
||
tip: e.detail.s_awards.content,
|
||
})
|
||
if (this.data.text[this.data.s_awards.idx].children.length > 0) {
|
||
this.setData({
|
||
isTbc: true,
|
||
canGo: false,
|
||
})
|
||
this.getText(true, this.data.s_awards.idx)
|
||
} else {
|
||
this.endStory()
|
||
}
|
||
},
|
||
|
||
//开始转
|
||
startZhuan(e) {
|
||
this.setData({
|
||
zhuanflg: e.detail ? true : false,
|
||
})
|
||
},
|
||
|
||
// 点击继续
|
||
goNext() {
|
||
console.log('goNext')
|
||
const gotoReg = /^goto:(.*)$/
|
||
this.setData({
|
||
isTbc: false,
|
||
canGo: true,
|
||
})
|
||
const text = this.data.text
|
||
if (text.length === 0) {
|
||
this.endStory()
|
||
return
|
||
}
|
||
|
||
if (text.length === 1) {
|
||
console.log('---', text)
|
||
if (gotoReg.exec(text[0].title)) {
|
||
const goto = gotoReg.exec(text[0].title)[1].split('-')
|
||
console.log('goto!!!', goto)
|
||
this.getText(false, goto)
|
||
} else {
|
||
const newStory = this.data.story
|
||
newStory.unshift({
|
||
name: text[0].title,
|
||
content: text[0].content,
|
||
idx: 0,
|
||
})
|
||
this.setData({
|
||
story: newStory,
|
||
})
|
||
this.getText(true, 0)
|
||
}
|
||
this.goNext()
|
||
} else {
|
||
this.loadZhuanpan()
|
||
}
|
||
},
|
||
// 获取转盘文本
|
||
getText(normal = true, goto) {
|
||
let newText = []
|
||
if (normal) {
|
||
// 普通模式,从text中按idx截取newText
|
||
newText = this.data.text[goto].children
|
||
} else {
|
||
// 跳转模式,从allText中按goto获取newText
|
||
newText = this.data.allText
|
||
|
||
goto.map((item, index) => {
|
||
if (index === 0) {
|
||
newText = newText[item - 1]
|
||
} else {
|
||
newText = newText.children[item - 1]
|
||
}
|
||
console.log('nnn', newText)
|
||
// newText = newText[item - 1].children
|
||
})
|
||
newText = [newText]
|
||
console.log('newText', newText)
|
||
}
|
||
this.setData({
|
||
text: newText,
|
||
})
|
||
},
|
||
// 加载转盘选项
|
||
loadZhuanpan() {
|
||
console.log('😂', this.data.text)
|
||
const opts = formOpt(this.data.text)
|
||
this.zhuanpan.switchZhuanpan(opts)
|
||
},
|
||
|
||
// GOGOGO
|
||
zhuan() {
|
||
this.zhuanpan._zhuan()
|
||
},
|
||
|
||
// 音乐播放器
|
||
player(opt) {
|
||
const myPlayer = wx.getBackgroundAudioManager()
|
||
const self = this
|
||
myPlayer.title = opt.title
|
||
myPlayer.src = opt.src
|
||
myPlayer.onEnded(() => {
|
||
console.log('播放结束', myPlayer.src, opt.src)
|
||
this.player(opt)
|
||
// myPlayer.play()
|
||
})
|
||
},
|
||
|
||
// 重新开始
|
||
restart() {
|
||
this.setData({
|
||
story: [],
|
||
tip: '',
|
||
isTbc: false,
|
||
canGo: true,
|
||
isCompleted: false,
|
||
})
|
||
|
||
wx.request({
|
||
// url: `https://mp-test.kingsome.cn/api/open/zp/text?_id=${this.data.textId}`, //开发者服务器接口地址",
|
||
url: `http://localhost:2333/api/open/zp/text?_id=${this.data.textId}`, //开发者服务器接口地址",
|
||
method: 'GET',
|
||
dataType: 'json', //如果设为json,会尝试对返回的数据做一次 JSON.parse
|
||
success: res => {
|
||
const data = res.data
|
||
if (data.errcode === 0) {
|
||
console.log(data)
|
||
this.setData({
|
||
text: data.result,
|
||
allText: data.result,
|
||
tip: data.tip,
|
||
title: data.title,
|
||
bg_img: data.bg_img,
|
||
bg_music: JSON.parse(data.bg_music)[0].url,
|
||
})
|
||
// TODO: 播放背景音乐
|
||
// this.player({
|
||
// title: 'music',
|
||
// src: this.data.bg_music || '',
|
||
// })
|
||
|
||
|
||
this.goNext()
|
||
}
|
||
},
|
||
fail: () => {
|
||
this.setData({
|
||
canGo: false,
|
||
})
|
||
wx.showToast({
|
||
title: '选项数据错误!', //提示的内容,
|
||
icon: 'fail', //图标,
|
||
})
|
||
},
|
||
})
|
||
},
|
||
|
||
// 游戏结束
|
||
endStory() {
|
||
this.setData({
|
||
isTbc: false,
|
||
canGo: false,
|
||
isCompleted: true,
|
||
})
|
||
|
||
wx.showToast({
|
||
title: '已完成!', //提示的内容,
|
||
icon: 'success', //图标,
|
||
})
|
||
|
||
const temp = {
|
||
title: this.data.title,
|
||
tip: this.data.tip,
|
||
story: this.data.story,
|
||
}
|
||
|
||
wx.setStorageSync('temp', JSON.stringify(temp))
|
||
},
|
||
|
||
//查看剧情
|
||
goStory() {
|
||
const temp = wx.getStorageSync('temp')
|
||
if (temp === '') return
|
||
wx.navigateTo({url: '/pages/story/story'})
|
||
},
|
||
|
||
// 调试模式
|
||
devSelect(e) {
|
||
console.log('调试: 手动选择')
|
||
|
||
if (this.data.isCompleted) return
|
||
const idx = e.target.dataset.index
|
||
this.getAwards({
|
||
detail: {
|
||
end: false,
|
||
s_awards: {
|
||
content: this.data.text[idx].content,
|
||
name: this.data.text[idx].title,
|
||
idx: idx,
|
||
},
|
||
},
|
||
})
|
||
if (this.data.isCompleted) return
|
||
this.goNext()
|
||
|
||
console.log(e)
|
||
},
|
||
}
|
||
Page(page)
|