fragmentsList.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. // pages/fragmentsList/fragmentsList.js
  2. var net = require('../../utils/net.js')
  3. var url = require('../../utils/url.js')
  4. var common = require('../../utils/common.js')
  5. var lock = require('../../utils/lock.js')
  6. const app = getApp()
  7. // const fragments = [
  8. // { id: 0, fragment: '胎教期', star: 1, consume: '100万' },
  9. // { id: 1, fragment: '幼儿园', star: 4, consume: '200万' },
  10. // { id: 2, fragment: '小学', star: 6, consume: '500万' },
  11. // { id: 3, fragment: '初中', star: 3, consume: '900万' },
  12. // { id: 4, fragment: '高中', star: 3, consume: '1100万' },
  13. // { id: 5, fragment: '本科', star: 4, consume: '1500万' },
  14. // { id: 6, fragment: '硕士', star: 3, consume: '2000万' },
  15. // { id: 7, fragment: '博士', star: 3, consume: '2700万' },
  16. // { id: 8, fragment: '讲师', star: 5, consume: '3000万' },
  17. // { id: 9, fragment: '教授', star: 5, consume: '1亿' },
  18. // { id: 10, fragment: '院士', star: 0, consume: '3亿', top: true }]
  19. Page({
  20. /**
  21. * 页面的初始数据
  22. */
  23. data: {
  24. isFirstLoad: true,
  25. currentFragment: 0,//从0开始,跟接口文档不一致
  26. currentStarNum:0,
  27. isCurrentFragment: false,//是否在当前段位pk
  28. fragmentsList: [],
  29. allFragmentsNum: 0,//一共有多少个段位
  30. banner: '',
  31. poster: '',
  32. rank: 0,
  33. scrollBottomId: '',//列表最后一个id
  34. dialogBrainLack: {
  35. hidden: true,
  36. title: '哎呀,你的脑力值不足',
  37. subtitle: '无法参加排位赛、冲刺院士荣誉并争夺至尊大奖哦',
  38. button: '分享到群',
  39. },
  40. dialogRank: {
  41. hidden: true
  42. },
  43. },
  44. showRankDialog: function () {
  45. this.setData({
  46. dialogRank: {
  47. hidden: false,
  48. headImg: '../../resource/head_rank.png'
  49. }
  50. })
  51. },
  52. hideRankDialog: function () {
  53. this.setData({
  54. dialogRank: { hidden: true }
  55. })
  56. },
  57. hideBrainLackDialog: function () {
  58. this.data.dialogBrainLack.hidden = true
  59. this.setData({
  60. dialogBrainLack: this.data.dialogBrainLack
  61. })
  62. },
  63. showBrainLackDialog: function (cell) {
  64. this.data.dialogBrainLack.button = '分享到群 赚取' + common.unit(cell) + '脑力值'
  65. this.data.dialogBrainLack.hidden = false
  66. this.setData({
  67. dialogBrainLack: this.data.dialogBrainLack
  68. })
  69. },
  70. goToPK: function (e) {
  71. if (lock.lockTapDelay()) {
  72. return
  73. }
  74. if (app.globalData.timeDifference + new Date().getTime() > app.globalData.rankInfo.endTime) {
  75. this.showRankDialog()
  76. return
  77. }
  78. var rankDan = Number(e.currentTarget.id.substring(1)) + 1
  79. var fragment = rankDan - 1
  80. var cell = app.server.config.rankDanList[fragment].cellRankCost
  81. if (app.globalData.user.cell < cell) {
  82. if (app.server.shareRankCellLack < app.server.config.shareRankCellLackMax) {
  83. var c = app.server.config.rankDanList[this.data.currentFragment].cellRankCost
  84. this.showBrainLackDialog(c)
  85. } else {
  86. wx.showToast({
  87. title: '脑力值不足',
  88. icon: 'none'
  89. })
  90. }
  91. return
  92. }
  93. if (rankDan - 1 == this.data.currentFragment) {
  94. this.data.isCurrentFragment = true
  95. } else {
  96. this.data.isCurrentFragment = false
  97. }
  98. wx.navigateTo({
  99. url: '/pages/pk/pk?type=1&rankDan=' + rankDan
  100. })
  101. },
  102. goToRecord: function () {
  103. wx.navigateTo({
  104. url: '../fragmentsRecord/fragmentsRecord',
  105. })
  106. },
  107. joinFragments: function () {
  108. net.connect({
  109. url: url.host + url.join_fragments,
  110. data: {},
  111. method: 'POST',
  112. success: res => {
  113. app.server.rank = res.data.data.rank
  114. this.data.rank = app.server.rank
  115. this.setData({
  116. rank: this.data.rank
  117. })
  118. },
  119. })
  120. },
  121. goToPoster: function () {
  122. wx.previewImage({
  123. urls: [this.data.poster],
  124. })
  125. },
  126. confirmFragmentsList: function () {
  127. if (this.data.currentFragment + 3 >= this.data.allFragmentsNum) {
  128. this.data.fragmentsList = app.server.config.rankDanList.slice(0)
  129. var { ...item } = this.data.fragmentsList[this.data.allFragmentsNum - 1]
  130. item.star = 0
  131. this.data.fragmentsList[this.data.allFragmentsNum - 1] = item
  132. } else {
  133. this.data.fragmentsList = app.server.config.rankDanList.slice(0, this.data.currentFragment + 3)
  134. }
  135. },
  136. getFragmentsResult: function (result) {
  137. if (this.data.isCurrentFragment) {
  138. if (result) {
  139. app.server.starC++
  140. var r = common.starToFragment(app.server.config.rankDanList, app.server.starC)
  141. this.data.currentFragment = r[0]
  142. this.data.currentStarNum = r[1]
  143. } else {
  144. var r = common.decreaseStar(app.server.config.rankDanList, app.server.starC)
  145. this.data.currentFragment = r[0]
  146. this.data.currentStarNum = r[1]
  147. app.server.starC = r[2]
  148. }
  149. }
  150. },
  151. /**
  152. * 生命周期函数--监听页面加载
  153. */
  154. onLoad: function (options) {
  155. app.log(app.server)
  156. wx.hideShareMenu({})
  157. this.data.banner = app.globalData.rankInfo.banner
  158. this.data.poster = app.globalData.rankInfo.poster
  159. var f = common.starToFragment(app.server.config.rankDanList, app.server.starC)
  160. this.data.currentFragment = f[0]
  161. this.data.currentStarNum = f[1]
  162. this.data.allFragmentsNum = app.server.config.rankDanList.length
  163. this.confirmFragmentsList()
  164. this.data.scrollBottomId = 'a' + this.data.currentFragment
  165. app.log(this.data.scrollBottomId)
  166. this.setData({
  167. fragmentsList: this.data.fragmentsList,
  168. currentFragment: this.data.currentFragment,
  169. currentStarNum: this.data.currentStarNum,
  170. allFragmentsNum: this.data.allFragmentsNum,
  171. banner: this.data.banner,
  172. scrollBottomId: this.data.scrollBottomId
  173. })
  174. },
  175. /**
  176. * 生命周期函数--监听页面初次渲染完成
  177. */
  178. onReady: function () {
  179. },
  180. /**
  181. * 生命周期函数--监听页面显示
  182. */
  183. onShow: function () {
  184. if (this.data.isFirstLoad) {
  185. this.data.rank = app.server.rank
  186. if (this.data.rank == 0) {
  187. this.joinFragments()
  188. }
  189. else {
  190. this.setData({
  191. rank: this.data.rank
  192. })
  193. }
  194. this.data.isFirstLoad = false
  195. } else {
  196. this.data.rank = app.server.rank
  197. this.confirmFragmentsList()
  198. this.setData({
  199. fragmentsList: this.data.fragmentsList,
  200. currentFragment: this.data.currentFragment,
  201. currentStarNum: this.data.currentStarNum,
  202. rank: this.data.rank
  203. })
  204. net.connect({
  205. url: url.host + url.get_my_ranking,
  206. data: { gameId: app.globalData.rankInfo.gameId },
  207. success: res => {
  208. if (app.server.starC != res.data.data.starC) {
  209. app.log('change')
  210. app.server.starC = res.data.data.starC
  211. app.server.rank = res.data.data.rank
  212. }
  213. this.data.rank = app.server.rank
  214. var r = common.starToFragment(app.server.config.rankDanList, app.server.starC)
  215. this.data.currentFragment = r[0]
  216. this.data.currentStarNum = r[1]
  217. this.confirmFragmentsList()
  218. this.setData({
  219. fragmentsList: this.data.fragmentsList,
  220. currentFragment: this.data.currentFragment,
  221. currentStarNum: this.data.currentStarNum,
  222. rank: this.data.rank
  223. })
  224. }
  225. })
  226. }
  227. },
  228. /**
  229. * 生命周期函数--监听页面隐藏
  230. */
  231. onHide: function () {
  232. },
  233. /**
  234. * 生命周期函数--监听页面卸载
  235. */
  236. onUnload: function () {
  237. },
  238. /**
  239. * 页面相关事件处理函数--监听用户下拉动作
  240. */
  241. onPullDownRefresh: function () {
  242. },
  243. /**
  244. * 页面上拉触底事件的处理函数
  245. */
  246. onReachBottom: function () {
  247. },
  248. /**
  249. * 用户点击右上角分享
  250. */
  251. onShareAppMessage: function () {
  252. wx.showShareMenu({
  253. withShareTicket: true
  254. })
  255. this.hideBrainLackDialog()
  256. return common.shareAction({
  257. title: app.globalData.userInfo.nickName + '邀请你来瓜分8万8千8百8十8元现金奖励!',
  258. path: '',
  259. type: 12,
  260. success: res => {
  261. if (res.data.data.addCell > 0) {
  262. wx.showToast({
  263. title: '分享+' + common.unit(res.data.data.addCell) + '脑力值',
  264. icon: 'none',
  265. duration: 2500
  266. })
  267. }
  268. this.setData({
  269. user: app.globalData.user
  270. })
  271. wx.hideShareMenu({})
  272. },
  273. fail: e =>{
  274. wx.hideShareMenu({})
  275. }
  276. })
  277. }
  278. })