task.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. var net = require('../../utils/net.js')
  2. var url = require('../../utils/url.js')
  3. var common = require('../../utils/common.js')
  4. var lock = require('../../utils/lock.js')
  5. const app = getApp()
  6. const oldT = 1514736000000
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. //ty: 1签到,2邀请,3领取,4已领取,5绑定
  12. data: {
  13. formId: 0,
  14. currentFragment: 0,//从0开始,跟接口文档不一致
  15. cellSplitSpeed: 0,
  16. cellSplitMax: 0,
  17. currentSplitCell: 0,//当前分裂数值
  18. phone: 0,
  19. data: [
  20. { id: '1', t: '时时签到', d: '24小时内,时时可领取', b: 0, ty: 1, p: '0/0', w: 0 },
  21. { id: '2', t: '好友对战', d: '完成一场好友对战,每天可领1次', b: 0, ty: 2 },
  22. { id: '3', t: '邀请好友', d: '每邀请1位新用户加入即享', b: 0, ty: 2 },
  23. { id: '4', t: '绑定手机', d: '不怕微信账号故障而导致脑力值丢失', b: 0, ty: 5 }
  24. ],
  25. getCellResult: {
  26. hidden: true,
  27. c: '0',
  28. u: ''
  29. }
  30. },
  31. goToAdd: function (sender) {
  32. wx.navigateTo({
  33. url: '../add/add'
  34. })
  35. },
  36. getCell: function (sender) {
  37. if (sender.currentTarget.id == '2') {
  38. this.getPKCell()
  39. } else if (sender.currentTarget.id == '3') {
  40. this.getInvitationCell()
  41. }
  42. },
  43. getPhoneNumber: function (e) {
  44. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  45. wx.showLoading({
  46. title: '',
  47. })
  48. net.connect({
  49. url: url.host + url.bind_phone,
  50. data: {
  51. encryptedData: e.detail.encryptedData,
  52. iv: e.detail.iv
  53. },
  54. method: 'POST',
  55. success: res => {
  56. wx.hideLoading()
  57. app.server.phone = res.data.data.phone
  58. this.data.phone = app.server.phone
  59. this.getCellResult(res.data.data.addCell)
  60. }
  61. })
  62. }
  63. // app.log(e.detail.errMsg)
  64. // app.log(e.detail.iv)
  65. // app.log(e.detail.encryptedData)
  66. },
  67. itemAction: function (sender) {
  68. if (sender.currentTarget.id == '1') {
  69. if (this.data.currentSplitCell <= 0) {
  70. return
  71. }
  72. this.signIn()
  73. }
  74. },
  75. getCellResult: function (cell) {
  76. app.globalData.user.cell += cell
  77. var c
  78. var u
  79. if (cell >= 10000) {
  80. var c = cell / 10000
  81. var u = '亿'
  82. } else {
  83. var c = cell
  84. var u = '万'
  85. }
  86. this.data.getCellResult = {
  87. hidden: false,
  88. c: c,
  89. u: u
  90. }
  91. this.setData({
  92. data: this.data.data,
  93. getCellResult: this.data.getCellResult,
  94. phone: this.data.phone
  95. })
  96. setTimeout(function () {
  97. this.data.getCellResult.hidden = true
  98. this.setData({
  99. getCellResult: this.data.getCellResult
  100. })
  101. }.bind(this), 2000)
  102. },
  103. getInvitationCell: function () {
  104. if (lock.lockTapDelay(1000)) {
  105. return
  106. }
  107. net.connect({
  108. url: url.host + url.recom_receive_cell,
  109. method: 'POST',
  110. data: {},
  111. success: res => {
  112. app.log(res.data)
  113. this.data.data[2].ty = 2
  114. this.getCellResult(res.data.data)
  115. }
  116. })
  117. },
  118. getPKCell: function () {
  119. if (lock.lockTapDelay(1000)) {
  120. return
  121. }
  122. net.connect({
  123. url: url.host + url.pk_receive_cell,
  124. method: 'POST',
  125. data: {},
  126. success: res => {
  127. app.log(res.data)
  128. this.data.data[1].ty = 4
  129. this.getCellResult(res.data.data)
  130. }
  131. })
  132. },
  133. signIn: function () {
  134. //点击金币,获取脑力值
  135. if (lock.lockTapDelay(1000)) {
  136. return
  137. }
  138. net.connect({
  139. url: url.host + url.receive_cell,
  140. method: 'POST',
  141. data: {
  142. formId: this.data.formId,
  143. },
  144. success: res => {
  145. this.data.currentSplitCell = 0
  146. this.refreshUI(0)
  147. this.getCellResult(res.data.data)
  148. //重新启动计时器
  149. if (this.interval) {
  150. clearInterval(this.interval)
  151. }
  152. this.interval = setInterval(function (data) {
  153. this.refreshUI(this.data.cellSplitSpeed / 2)
  154. }.bind(this), 1000 * 60)
  155. }
  156. })
  157. },
  158. refreshUI: function (add) {
  159. this.data.currentSplitCell = this.data.currentSplitCell + add
  160. if (this.data.currentSplitCell > this.data.cellSplitMax) {
  161. this.data.currentSplitCell = this.data.cellSplitMax
  162. if (this.interval) {
  163. clearInterval(this.interval)
  164. }
  165. }
  166. app.log(this.data.currentSplitCell)
  167. this.data.data[0].w = this.data.currentSplitCell / this.data.cellSplitMax * 100
  168. this.data.data[0].p = Math.floor(this.data.currentSplitCell) + '/' + this.data.cellSplitMax
  169. app.log(this.data.data[0].w)
  170. this.setData({
  171. data: this.data.data
  172. })
  173. },
  174. //ty: 1签到,2邀请,3领取,4已领取,5绑定
  175. refreshData: function () {
  176. //启动计时器计算分裂数值
  177. this.interval = setInterval(function (data) {
  178. this.refreshUI(this.data.cellSplitSpeed / 2)
  179. }.bind(this), 1000 * 60)
  180. //当前签到数值
  181. var nc = Math.floor((app.globalData.user.nowTime - app.globalData.user.splitTime) / 1000 / 60 / 2) * this.data.cellSplitSpeed
  182. var curT = new Date(app.globalData.user.nowTime).format("yyyy-MM-dd")
  183. app.log(curT)
  184. //判断好友对战
  185. var pkTime = app.globalData.user.pkTime || oldT
  186. var pkT = new Date(pkTime).format("yyyy-MM-dd")
  187. app.log(pkT)
  188. if (pkT == curT) {
  189. this.data.data[1].ty = 4
  190. } else if (app.globalData.user.pkTodayC > 0) {
  191. this.data.data[1].ty = 3
  192. } else {
  193. this.data.data[1].ty = 2
  194. }
  195. //判断邀请好友
  196. var recomTime = app.globalData.user.recomTime || oldT
  197. var recomT = new Date(recomTime).format("yyyy-MM-dd")
  198. app.log(recomT)
  199. if (recomT == curT) {
  200. if (app.globalData.user.recomTodayC <= app.globalData.user.recomC) {
  201. this.data.data[2].ty = 2
  202. } else {
  203. this.data.data[2].ty = 3
  204. }
  205. } else if (app.globalData.user.recomTodayC > 0) {
  206. this.data.data[2].ty = 3
  207. } else {
  208. this.data.data[2].ty = 2
  209. }
  210. this.refreshUI(nc)
  211. },
  212. getUserData: function () {
  213. net.connect({
  214. url: url.host + url.get_user_info,
  215. success: res => {
  216. app.globalData.user = res.data.data
  217. this.refreshData()
  218. }
  219. })
  220. },
  221. /**
  222. * 生命周期函数--监听页面加载
  223. */
  224. onLoad: function (options) {
  225. wx.hideShareMenu({
  226. })
  227. app.log(app.server)
  228. app.log(app.globalData)
  229. this.data.formId = options.formId || 0
  230. //计算段位
  231. if (app.server.rankStarMap.topic.starC > app.server.rankStarMap.main.starC) {
  232. var star = app.server.rankStarMap.topic.starC
  233. } else {
  234. var star = app.server.rankStarMap.main.starC
  235. }
  236. var f = common.starToFragment(app.server.config.rankDanList, star)
  237. this.data.currentFragment = f[0]
  238. //记录分裂数值
  239. this.data.cellSplitMax = app.server.config.rankDanList[this.data.currentFragment].cellSplitMax
  240. this.data.cellSplitSpeed = app.server.config.rankDanList[this.data.currentFragment].cellSplitSpeed
  241. //记录页面各项脑力值
  242. this.data.data[0].b = this.data.cellSplitMax
  243. this.data.data[0].p = '0/' + this.data.cellSplitMax
  244. this.data.data[1].b = app.server.config.rankDanList[this.data.currentFragment].cellPk
  245. this.data.data[2].b = app.server.config.rankDanList[this.data.currentFragment].cellRecom
  246. this.data.data[3].b = app.server.config.rankDanList[this.data.currentFragment].cellBindPhone
  247. //phone
  248. this.data.phone = app.server.phone || 0
  249. this.setData({
  250. data: this.data.data,
  251. phone: this.data.phone
  252. })
  253. },
  254. /**
  255. * 生命周期函数--监听页面初次渲染完成
  256. */
  257. onReady: function () {
  258. },
  259. /**
  260. * 生命周期函数--监听页面显示
  261. */
  262. onShow: function () {
  263. this.getUserData()
  264. },
  265. /**
  266. * 生命周期函数--监听页面隐藏
  267. */
  268. onHide: function () {
  269. if (this.interval) {
  270. clearInterval(this.interval)
  271. }
  272. },
  273. /**
  274. * 生命周期函数--监听页面卸载
  275. */
  276. onUnload: function () {
  277. if (this.interval) {
  278. clearInterval(this.interval)
  279. }
  280. },
  281. /**
  282. * 页面相关事件处理函数--监听用户下拉动作
  283. */
  284. onPullDownRefresh: function () {
  285. },
  286. /**
  287. * 页面上拉触底事件的处理函数
  288. */
  289. onReachBottom: function () {
  290. },
  291. /**
  292. * 用户点击右上角分享
  293. */
  294. onShareAppMessage: function (e) {
  295. app.log(e)
  296. if (e.target.id === '2') {
  297. return common.shareAction({
  298. title: app.server.nickName + '向你发起脑力挑战,输的罚XXX!',
  299. path: '&page=pk&type=2&uid=' + app.server.userId,
  300. imageUrl: '../../resource/share_friend_pk.png',
  301. type: 8,
  302. success: res => {
  303. wx.navigateTo({
  304. url: '/pages/pk/pk?type=2'
  305. })
  306. }
  307. })
  308. } else if (e.target.id === '3') {
  309. return common.shareAction({
  310. title: app.server.nickName + '在显摆脑力,邀请你一起瓜分888元现金大奖',
  311. path: '',
  312. imageUrl: '../../resource/share_invite.png',
  313. type: 1,
  314. success: res => {
  315. }
  316. })
  317. }
  318. }
  319. })
  320. Date.prototype.format = function (fmt) {
  321. var o = {
  322. "M+": this.getMonth() + 1, //月份
  323. "d+": this.getDate(), //日
  324. "h+": this.getHours(), //小时
  325. "m+": this.getMinutes(), //分
  326. "s+": this.getSeconds(), //秒
  327. "q+": Math.floor((this.getMonth() + 3) / 3), //季度
  328. "S": this.getMilliseconds() //毫秒
  329. };
  330. if (/(y+)/.test(fmt)) {
  331. fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  332. }
  333. for (var k in o) {
  334. if (new RegExp("(" + k + ")").test(fmt)) {
  335. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  336. }
  337. }
  338. return fmt;
  339. }