index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // pages/index/index.js
  2. const app = getApp();
  3. const net = require('../../utils/net.js');
  4. const url = require('../../utils/url.js');
  5. const util = require('../../utils/util.js');
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. file: url.file,
  12. list: [],
  13. lastTime: 0,
  14. animation: {},
  15. tabs: {
  16. width: 0,
  17. left: 0,
  18. list: [],
  19. current: 0,
  20. }
  21. },
  22. /**
  23. * 生命周期函数--监听页面加载
  24. */
  25. onLoad: function(options) {
  26. //不使用群标识,支持长按转发
  27. wx.showShareMenu({
  28. withShareTicket: false
  29. })
  30. //获取分类列表
  31. this.getTagList();
  32. net.login();
  33. },
  34. /**
  35. * 生命周期函数--监听页面初次渲染完成
  36. */
  37. onReady: function() {
  38. },
  39. /**
  40. * 生命周期函数--监听页面显示
  41. */
  42. onShow: function() {
  43. var offset = new Date().getTime() - this.data.lastTime;
  44. if (offset > 600000) {
  45. this.initList();
  46. }
  47. },
  48. /**
  49. * 生命周期函数--监听页面隐藏
  50. */
  51. onHide: function() {
  52. },
  53. /**
  54. * 生命周期函数--监听页面卸载
  55. */
  56. onUnload: function() {
  57. },
  58. /**
  59. * 页面相关事件处理函数--监听用户下拉动作
  60. */
  61. onPullDownRefresh: function() {
  62. },
  63. /**
  64. * 页面上拉触底事件的处理函数
  65. */
  66. onReachBottom: function() {
  67. },
  68. /**
  69. * 用户点击右上角分享
  70. */
  71. onShareAppMessage: function() {
  72. return {
  73. title: '总是被KO,不要啊!快来帮帮我!',
  74. path: '/pages/index/index',
  75. imageUrl: 'http://adminwxstore.xsbanruo.com/wxstore-admin/file/upload/2/8280/828020180831642627961906.jpg'
  76. }
  77. },
  78. setTabUnderLineAnimation: function(id){
  79. var animation = wx.createAnimation({
  80. duration: 400,
  81. timingFunction: 'ease',
  82. });
  83. var x = this.data.tabs.width * id + this.data.tabs.left;
  84. animation.translate(x, 0).step();
  85. this.setData({
  86. animation: animation.export()
  87. });
  88. },
  89. // 当tab切换时执行方法,修改tab样式,获取数据
  90. onTabChange: function(e) {
  91. var id = e.currentTarget.id;
  92. if (this.data.tabs.currentTabId === id) {
  93. return;
  94. }
  95. //执行tab下划线移动动画
  96. this.setTabUnderLineAnimation(id);
  97. //设置tab文字颜色变化
  98. this.data.tabs.list[this.data.tabs.current].class = '';
  99. this.data.tabs.list[id].class = 'active';
  100. this.data.tabs.current = id;
  101. this.setData({
  102. tabs: this.data.tabs
  103. });
  104. //当前列表无数据时,获取数据
  105. if (this.data.list[this.data.tabs.current].length <= 0) {
  106. this.getList();
  107. }
  108. },
  109. onSwiperChange: function(e) {
  110. //swiper为手动滑动
  111. if (e.detail.source === 'touch') {
  112. //tab发生变化,执行切换方法
  113. this.onTabChange({
  114. currentTarget: {
  115. id: e.detail.current
  116. }
  117. });
  118. }
  119. },
  120. initList: function(){
  121. //根据分类,初始化游戏列表
  122. var len = this.data.tabs.list.length;
  123. this.data.list = new Array(len);
  124. for (var i = 0; i < len; i++) {
  125. this.data.list[i] = [];
  126. }
  127. this.setData({
  128. list: this.data.list
  129. })
  130. //获取游戏列表
  131. this.getList();
  132. },
  133. getTagList: function() {
  134. this.data.lastTime = new Date().getTime();
  135. wx.showLoading({
  136. title: '',
  137. });
  138. net.request({
  139. url: url.tags,
  140. success: res => {
  141. //根据分类,初始化分类列表tabs
  142. this.data.tabs.list = res.data;
  143. this.data.tabs.list[0].class = 'active';
  144. var len = this.data.tabs.list.length;
  145. this.data.tabs.width = app.config.width / len;
  146. this.data.tabs.left = (this.data.tabs.width - app.config.width * 120 / 750) / 2;
  147. this.setTabUnderLineAnimation(0);
  148. this.setData({
  149. tabs: this.data.tabs
  150. });
  151. this.initList();
  152. },
  153. fail: err => {
  154. wx.showModal({
  155. title: '',
  156. content: '获取数据失败,请检查网络,重新获取',
  157. showCancel: false,
  158. success: res => {
  159. this.getTagList();
  160. }
  161. })
  162. }
  163. });
  164. },
  165. getList: function() {
  166. wx.showLoading({
  167. title: '',
  168. });
  169. net.request({
  170. url: url.list,
  171. data: {
  172. tagId: this.data.tabs.list[this.data.tabs.current].tagId
  173. },
  174. success: res => {
  175. this.data.lastTime = new Date().getTime();
  176. this.data.list[this.data.tabs.current] = res.data;
  177. this.setData({
  178. list: this.data.list
  179. })
  180. }
  181. });
  182. },
  183. toTask: function(e) {
  184. //进入详情页
  185. wx.showLoading({
  186. title: '',
  187. })
  188. const task = this.data.list[this.data.tabs.current][e.currentTarget.id];
  189. net.request({
  190. url: url.task,
  191. method: 'GET',
  192. data: {
  193. programId: task.programId
  194. },
  195. success: res => {
  196. const result = res.data || {
  197. gtaskId: 0,
  198. programId: task.programId,
  199. title: task.memo,
  200. jumpPath: '/pages/index/index',
  201. card: ''
  202. };
  203. result.appId = task.appId
  204. result.logo = task.logo
  205. result.appName = task.appName
  206. result.memo = task.memo
  207. result.playRefer = 1
  208. result.jumpPath = encodeURIComponent(result.jumpPath);
  209. var param = util.parseUrlParamByJson(result);
  210. wx.navigateTo({
  211. url: '/pages/task/task?' + param,
  212. });
  213. }
  214. });
  215. },
  216. })