open.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. // pages/open/open.js
  2. var app = getApp()
  3. var lock = require('../../utils/lock.js')
  4. var verify = require('../../utils/verify.js')
  5. var net = require('../../utils/net.js')
  6. var url = require('../../utils/url.js')
  7. var common = require('../../utils/common.js')
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. array: [
  14. { id: 1, itype: 'number', rname: '奖励个数', name: 'count', placeholder: '填写个数', unit: '个' },
  15. { id: 2, itype: 'digit', rname: '奖励总额', name: 'amount', placeholder: '填写金额', unit: '元', func: "inputAmount" }
  16. ],
  17. sTags: [
  18. { id: 1, name: '文学', selected: false },
  19. { id: 2, name: '数学', selected: false },
  20. { id: 3, name: '地理', selected: false },
  21. { id: 6, name: '体育', selected: false },
  22. { id: 7, name: '娱乐', selected: false },
  23. { id: 9, name: '常识', selected: false },
  24. { id: 12, name: '理化', selected: false },
  25. { id: 13, name: '艺术', selected: false },
  26. { id: 15, name: '外语', selected: false },
  27. { id: 18, name: '健康', selected: false },
  28. { id: 20, name: '教育', selected: false },
  29. ],
  30. isOtherInvalid: false,
  31. gameId: null,
  32. questionIds: null,
  33. questions: [],
  34. uTags: [],
  35. dialogRoom: {
  36. hidden: true,
  37. title: '专属房间已成功开启',
  38. content: '点击“立即邀好友答题”后,该房间将会进入60秒倒计时,需在规定时间内邀好友加入,参与答题',
  39. tip: '60秒内一定要告诉好友加入哦',
  40. button: '立即邀好友答题',
  41. share: 'share',
  42. tap: '',
  43. },
  44. dialogTag: { hidden: true }
  45. },
  46. onLoad: function () {
  47. wx.hideShareMenu()
  48. wx.updateShareMenu({
  49. withShareTicket: true
  50. })
  51. this.getQuestions()
  52. },
  53. onShareAppMessage: function (res) {
  54. if (res.from === 'button') {
  55. wx.showLoading({
  56. title: '',
  57. })
  58. return common.shareAction({
  59. title: '金主 ' + app.globalData.userInfo.nickName + ' 撒钱了,60秒内速速加入抢大钱',
  60. path: '&page=room&gameId=' + this.data.gameId + '&type=2&shareUserId=' + app.server.userId,
  61. gameId: this.data.gameId,
  62. imageUrl: '../../resource/share_open_img.png',
  63. type: 7,
  64. success: res => {
  65. this.hideDialog()
  66. setTimeout(function () {
  67. wx.hideLoading()
  68. this.toRoom()
  69. }.bind(this), 1000)
  70. }
  71. })
  72. }
  73. },
  74. getQuestions: function () {
  75. wx.showLoading({
  76. title: '',
  77. })
  78. var tags = this.getUserTags()
  79. net.connect({
  80. url: url.host + url.question_random,
  81. data: {
  82. tags: tags,
  83. },
  84. success: res => {
  85. wx.hideLoading()
  86. var questions = res.data.data
  87. var len = questions.length
  88. for (var i = 0; i < len; i++) {
  89. questions[i].tagName = '#' + common.getQuestionType(questions[i].tag)
  90. }
  91. this.setData({
  92. questions: questions
  93. })
  94. }
  95. })
  96. },
  97. getQuestionsByTag: function () {
  98. this.hideTagDialog()
  99. this.getQuestions()
  100. },
  101. getAgain: function () {
  102. this.showTagDialog()
  103. },
  104. inputAmount: function (e) {
  105. var amount = e.detail.value
  106. amount = amount == "" ? 0 : amount
  107. if (!verify.isTwoDecimals(amount)) {
  108. //不允许输入三位小数,截取到两位小数
  109. amount = amount.toString()
  110. amount = amount.substring(0, amount.indexOf('.') + 3)
  111. }
  112. amount = parseFloat(amount)
  113. amount = isNaN(amount) ? 0 : amount
  114. return amount
  115. },
  116. open: function (e) {
  117. if (lock.lockTapDelay()) {
  118. return
  119. }
  120. var name = '智力比拼趣味问答';
  121. var volume = 600
  122. var count = new Number(e.detail.value.count)
  123. if (count <= 0 || count > volume || !verify.isPositiveInteger(count)) {
  124. wx.showModal({
  125. title: '提示',
  126. content: '奖励个数需要至少一个,且不能大于600个',
  127. showCancel: false
  128. })
  129. return
  130. }
  131. var amount = new Number(e.detail.value.amount)
  132. amount = Math.round(amount * 100)
  133. if (amount < count) {
  134. wx.showModal({
  135. content: '奖励金额不能低于' + (count * 0.01) + '元',
  136. showCancel: false
  137. })
  138. return
  139. }
  140. if (amount > 99900) {
  141. wx.showModal({
  142. content: '奖励金额超出限制',
  143. showCancel: false
  144. })
  145. return
  146. }
  147. wx.showLoading({
  148. title: '',
  149. })
  150. var questions = this.data.questions
  151. var len = questions.length
  152. var tags = ''
  153. for (var i = 0; i < len; i++) {
  154. tags += (questions[i].tag + '#')
  155. }
  156. tags = tags.substring(0, tags.length - 1)
  157. net.connect({
  158. url: url.host + url.build_room,
  159. method: 'POST',
  160. data: {
  161. title: name,
  162. maxUser: volume,
  163. awardUser: count,
  164. awardFee: amount,
  165. questions: this.getQuestionIds(),
  166. tags: tags
  167. },
  168. success: res => {
  169. this.data.gameId = res.data.data
  170. this.pay(res.data.data)
  171. }
  172. })
  173. },
  174. pay: function (id) {
  175. net.wxpay({
  176. url: url.host + url.build_room_order,
  177. data: {
  178. gameId: id
  179. },
  180. success: res => {
  181. //支付完成
  182. wx.hideLoading()
  183. this.showDialog()
  184. }
  185. })
  186. },
  187. getUserTags: function () {
  188. var uTags = this.data.uTags
  189. var len = uTags.length
  190. if (len <= 0) {
  191. return ''
  192. }
  193. var tags = ''
  194. for (var i = 0; i < len; i++) {
  195. tags += (uTags[i].id + ',')
  196. }
  197. tags = tags.substring(0, tags.length - 1)
  198. return tags
  199. },
  200. getQuestionIds: function () {
  201. var questions = this.data.questions
  202. var len = questions.length
  203. if (len <= 0) {
  204. return ''
  205. }
  206. var ids = ''
  207. for (var i = 0; i < len; i++) {
  208. ids += (questions[i].questionId + '#')
  209. }
  210. ids = ids.substring(0, ids.length - 1)
  211. return ids
  212. },
  213. changeQuestion: function (e) {
  214. if (lock.lockTapDelay()) {
  215. return
  216. }
  217. wx.showLoading({
  218. title: '',
  219. mask: true
  220. })
  221. var index = new Number(e.target.id)
  222. var tag = this.data.questions[index].tag
  223. var ids = this.getQuestionIds()
  224. net.connect({
  225. url: url.host + url.change_question,
  226. data: {
  227. tag: tag,
  228. filterQuestionIds: ids
  229. },
  230. success: res => {
  231. wx.hideLoading()
  232. var questions = this.data.questions
  233. questions[index] = res.data.data
  234. questions[index].tagName = '#' + common.getQuestionType(questions[index].tag)
  235. this.setData({
  236. questions: questions
  237. })
  238. }
  239. })
  240. },
  241. selectTag: function (e) {
  242. if (lock.lockTapDelay()) {
  243. return
  244. }
  245. var index = e.target.id
  246. var uTags = this.data.uTags
  247. var tag = this.data.sTags[index];
  248. if (uTags.length >= 8 && !tag.selected) {
  249. //已选择8个标签,无法继续选择
  250. return
  251. }
  252. tag.selected = !tag.selected
  253. if (tag.selected) {
  254. uTags.push(tag)
  255. } else {
  256. var len = uTags.length
  257. if (len > 0) {
  258. for (var i = 0; i < len; i++) {
  259. if (tag.id == uTags[i].id) {
  260. uTags.splice(i, 1)
  261. break
  262. }
  263. }
  264. }
  265. }
  266. if (uTags.length >= 8) {
  267. this.data.isOtherInvalid = true
  268. } else {
  269. this.data.isOtherInvalid = false
  270. }
  271. this.data.sTags[index] = tag
  272. this.data.uTags = uTags
  273. this.setData({
  274. sTags: this.data.sTags,
  275. isOtherInvalid: this.data.isOtherInvalid
  276. })
  277. },
  278. toRoom: function () {
  279. wx.redirectTo({
  280. url: '../room/room?gameId=' + this.data.gameId
  281. })
  282. },
  283. showDialog: function () {
  284. this.data.dialogRoom.hidden = false
  285. this.setData({
  286. dialogRoom: this.data.dialogRoom
  287. })
  288. },
  289. hideDialog: function () {
  290. this.data.dialogRoom.hidden = true
  291. this.setData({
  292. dialogRoom: this.data.dialogRoom
  293. })
  294. },
  295. showTagDialog: function () {
  296. this.setData({
  297. dialogTag: { hidden: false }
  298. })
  299. },
  300. hideTagDialog: function () {
  301. this.setData({
  302. dialogTag: { hidden: true }
  303. })
  304. }
  305. })