123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- // pages/open/open.js
- var app = getApp()
- var lock = require('../../utils/lock.js')
- var verify = require('../../utils/verify.js')
- var net = require('../../utils/net.js')
- var url = require('../../utils/url.js')
- var common = require('../../utils/common.js')
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- array: [
- { id: 1, itype: 'number', rname: '奖励个数', name: 'count', placeholder: '填写个数', unit: '个' },
- { id: 2, itype: 'digit', rname: '奖励总额', name: 'amount', placeholder: '填写金额', unit: '元', func: "inputAmount" }
- ],
- sTags: [
- { id: 1, name: '文学', selected: false },
- { id: 2, name: '数学', selected: false },
- { id: 3, name: '地理', selected: false },
- { id: 6, name: '体育', selected: false },
- { id: 7, name: '娱乐', selected: false },
- { id: 9, name: '常识', selected: false },
- { id: 12, name: '理化', selected: false },
- { id: 13, name: '艺术', selected: false },
- { id: 15, name: '外语', selected: false },
- { id: 18, name: '健康', selected: false },
- { id: 20, name: '教育', selected: false },
- ],
- isOtherInvalid: false,
- gameId: null,
- questionIds: null,
- questions: [],
- uTags: [],
- dialogRoom: {
- hidden: true,
- title: '专属房间已成功开启',
- content: '点击“立即邀好友答题”后,该房间将会进入60秒倒计时,需在规定时间内邀好友加入,参与答题',
- tip: '60秒内一定要告诉好友加入哦',
- button: '立即邀好友答题',
- share: 'share',
- tap: '',
- },
- dialogTag: { hidden: true }
- },
- onLoad: function () {
- wx.hideShareMenu()
- wx.updateShareMenu({
- withShareTicket: true
- })
- this.getQuestions()
- },
- onShareAppMessage: function (res) {
- if (res.from === 'button') {
- wx.showLoading({
- title: '',
- })
- return common.shareAction({
- title: '金主 ' + app.globalData.userInfo.nickName + ' 撒钱了,60秒内速速加入抢大钱',
- path: '&page=room&gameId=' + this.data.gameId + '&type=2&shareUserId=' + app.server.userId,
- gameId: this.data.gameId,
- imageUrl: '../../resource/share_open_img.png',
- type: 7,
- success: res => {
- this.hideDialog()
- setTimeout(function () {
- wx.hideLoading()
- this.toRoom()
- }.bind(this), 1000)
- }
- })
- }
- },
- getQuestions: function () {
- wx.showLoading({
- title: '',
- })
- var tags = this.getUserTags()
- net.connect({
- url: url.host + url.question_random,
- data: {
- tags: tags,
- },
- success: res => {
- wx.hideLoading()
- var questions = res.data.data
- var len = questions.length
- for (var i = 0; i < len; i++) {
- questions[i].tagName = '#' + common.getQuestionType(questions[i].tag)
- }
- this.setData({
- questions: questions
- })
- }
- })
- },
- getQuestionsByTag: function () {
- this.hideTagDialog()
- this.getQuestions()
- },
- getAgain: function () {
- this.showTagDialog()
- },
- inputAmount: function (e) {
- var amount = e.detail.value
- amount = amount == "" ? 0 : amount
- if (!verify.isTwoDecimals(amount)) {
- //不允许输入三位小数,截取到两位小数
- amount = amount.toString()
- amount = amount.substring(0, amount.indexOf('.') + 3)
- }
- amount = parseFloat(amount)
- amount = isNaN(amount) ? 0 : amount
- return amount
- },
- open: function (e) {
- if (lock.lockTapDelay()) {
- return
- }
- var name = '智力比拼趣味问答';
- var volume = 600
- var count = new Number(e.detail.value.count)
- if (count <= 0 || count > volume || !verify.isPositiveInteger(count)) {
- wx.showModal({
- title: '提示',
- content: '奖励个数需要至少一个,且不能大于600个',
- showCancel: false
- })
- return
- }
- var amount = new Number(e.detail.value.amount)
- amount = Math.round(amount * 100)
- if (amount < count) {
- wx.showModal({
- content: '奖励金额不能低于' + (count * 0.01) + '元',
- showCancel: false
- })
- return
- }
- if (amount > 99900) {
- wx.showModal({
- content: '奖励金额超出限制',
- showCancel: false
- })
- return
- }
- wx.showLoading({
- title: '',
- })
- var questions = this.data.questions
- var len = questions.length
- var tags = ''
- for (var i = 0; i < len; i++) {
- tags += (questions[i].tag + '#')
- }
- tags = tags.substring(0, tags.length - 1)
- net.connect({
- url: url.host + url.build_room,
- method: 'POST',
- data: {
- title: name,
- maxUser: volume,
- awardUser: count,
- awardFee: amount,
- questions: this.getQuestionIds(),
- tags: tags
- },
- success: res => {
- this.data.gameId = res.data.data
- this.pay(res.data.data)
- }
- })
- },
- pay: function (id) {
- net.wxpay({
- url: url.host + url.build_room_order,
- data: {
- gameId: id
- },
- success: res => {
- //支付完成
- wx.hideLoading()
- this.showDialog()
- }
- })
- },
- getUserTags: function () {
- var uTags = this.data.uTags
- var len = uTags.length
- if (len <= 0) {
- return ''
- }
- var tags = ''
- for (var i = 0; i < len; i++) {
- tags += (uTags[i].id + ',')
- }
- tags = tags.substring(0, tags.length - 1)
- return tags
- },
- getQuestionIds: function () {
- var questions = this.data.questions
- var len = questions.length
- if (len <= 0) {
- return ''
- }
- var ids = ''
- for (var i = 0; i < len; i++) {
- ids += (questions[i].questionId + '#')
- }
- ids = ids.substring(0, ids.length - 1)
- return ids
- },
- changeQuestion: function (e) {
- if (lock.lockTapDelay()) {
- return
- }
- wx.showLoading({
- title: '',
- mask: true
- })
- var index = new Number(e.target.id)
- var tag = this.data.questions[index].tag
- var ids = this.getQuestionIds()
- net.connect({
- url: url.host + url.change_question,
- data: {
- tag: tag,
- filterQuestionIds: ids
- },
- success: res => {
- wx.hideLoading()
- var questions = this.data.questions
- questions[index] = res.data.data
- questions[index].tagName = '#' + common.getQuestionType(questions[index].tag)
- this.setData({
- questions: questions
- })
- }
- })
- },
- selectTag: function (e) {
- if (lock.lockTapDelay()) {
- return
- }
- var index = e.target.id
- var uTags = this.data.uTags
- var tag = this.data.sTags[index];
- if (uTags.length >= 8 && !tag.selected) {
- //已选择8个标签,无法继续选择
- return
- }
- tag.selected = !tag.selected
- if (tag.selected) {
- uTags.push(tag)
- } else {
- var len = uTags.length
- if (len > 0) {
- for (var i = 0; i < len; i++) {
- if (tag.id == uTags[i].id) {
- uTags.splice(i, 1)
- break
- }
- }
- }
- }
- if (uTags.length >= 8) {
- this.data.isOtherInvalid = true
- } else {
- this.data.isOtherInvalid = false
- }
- this.data.sTags[index] = tag
- this.data.uTags = uTags
- this.setData({
- sTags: this.data.sTags,
- isOtherInvalid: this.data.isOtherInvalid
- })
- },
- toRoom: function () {
- wx.redirectTo({
- url: '../room/room?gameId=' + this.data.gameId
- })
- },
- showDialog: function () {
- this.data.dialogRoom.hidden = false
- this.setData({
- dialogRoom: this.data.dialogRoom
- })
- },
- hideDialog: function () {
- this.data.dialogRoom.hidden = true
- this.setData({
- dialogRoom: this.data.dialogRoom
- })
- },
- showTagDialog: function () {
- this.setData({
- dialogTag: { hidden: false }
- })
- },
- hideTagDialog: function () {
- this.setData({
- dialogTag: { hidden: true }
- })
- }
- })
|