index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // nova-werun/pages/home/medal/index.js
  2. const Parse = getApp().Parse;
  3. const company = getApp().globalData.company;
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. //屏幕高度
  10. statusBarHeight: 0, // 状态栏高度
  11. screenHeight: 0, // 屏幕高度
  12. customHeight: 0, // 自定义导航栏高度(如小程序右上角胶囊按钮)
  13. bottomNavHeight: 0, // 底部导航栏高度
  14. contentHeight: 0, // 可用内容高度
  15. contentHeight2: 0,
  16. contentpadding: 0, //顶部padding高度
  17. userList: [],
  18. alreadyList: [],
  19. MedalList: [],
  20. show: false,
  21. title:''
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: function (options) {
  27. // 计算
  28. const systemInfo = wx.getSystemInfoSync();
  29. const statusBarHeight = systemInfo.statusBarHeight || 0;
  30. const screenHeight = systemInfo.screenHeight || 0;
  31. const custom = wx.getMenuButtonBoundingClientRect();
  32. const customHeight = custom.height + 10 + 2 || 0;
  33. const bottomNavHeight = systemInfo.screenHeight - systemInfo.safeArea.bottom || 0;
  34. const contentpadding = (statusBarHeight + customHeight) * 750 / systemInfo.windowWidth;
  35. const contentHeight = (screenHeight - bottomNavHeight - statusBarHeight - customHeight) * 750 / systemInfo.windowWidth;
  36. this.setData({
  37. statusBarHeight,
  38. screenHeight,
  39. customHeight,
  40. bottomNavHeight,
  41. contentpadding,
  42. contentHeight
  43. });
  44. this.getuser()
  45. this.alreadyMedal()
  46. },
  47. /**
  48. * 生命周期函数--监听页面初次渲染完成
  49. */
  50. onReady: function () {
  51. },
  52. /**
  53. * 生命周期函数--监听页面显示
  54. */
  55. onShow: function () {
  56. },
  57. /**
  58. * 生命周期函数--监听页面隐藏
  59. */
  60. onHide: function () {
  61. },
  62. /**
  63. * 生命周期函数--监听页面卸载
  64. */
  65. onUnload: function () {
  66. },
  67. /**
  68. * 页面相关事件处理函数--监听用户下拉动作
  69. */
  70. onPullDownRefresh: function () {
  71. },
  72. /**
  73. * 页面上拉触底事件的处理函数
  74. */
  75. onReachBottom: function () {
  76. },
  77. /**
  78. * 用户点击右上角分享
  79. */
  80. onShareAppMessage: function () {
  81. },
  82. async getuser() {
  83. const currentUser = Parse.User.current();
  84. let userquery = new Parse.Query('_User');
  85. userquery.equalTo('company', company);
  86. userquery.equalTo('objectId', currentUser.id);
  87. userquery.notEqualTo('isDeleted', true)
  88. let user = await userquery.find();
  89. let userList = user.map(item => item.toJSON());
  90. this.setData({
  91. userList
  92. })
  93. console.log(this.data.userList);
  94. },
  95. // 已获得勋章
  96. async alreadyMedal() {
  97. const currentUser = Parse.User.current();
  98. let MedalLogquery = new Parse.Query('MedalLog');
  99. MedalLogquery.equalTo('company', company);
  100. MedalLogquery.equalTo('user', currentUser.id);
  101. MedalLogquery.notEqualTo('isDeleted', true);
  102. MedalLogquery.include('medal');
  103. let MedalLog = await MedalLogquery.find();
  104. let alreadyList = MedalLog.map(item => {
  105. let itemData = item.toJSON();
  106. // 格式化 createdAt 为 YYYY-M-D
  107. const createdAt = new Date(itemData.createdAt);
  108. const formattedDate = `${createdAt.getFullYear()}-${createdAt.getMonth() + 1}-${createdAt.getDate()}`;
  109. itemData.createdAt = formattedDate; // 将格式化后的日期赋值回 createdAt
  110. return itemData;
  111. });
  112. this.setData({
  113. alreadyList
  114. });
  115. console.log('alreadyList', this.data.alreadyList);
  116. // 获取 MedalList,并剔除 alreadyList 中的勋章
  117. await this.lossMedal(alreadyList);
  118. },
  119. // 为获得勋章
  120. async lossMedal(alreadyList) {
  121. let Medalquery = new Parse.Query('Medal');
  122. Medalquery.equalTo('company', company);
  123. Medalquery.notEqualTo('isDeleted', true);
  124. Medalquery.ascending('order');
  125. let r = await Medalquery.find();
  126. let MedalList = r.map(item => item.toJSON());
  127. // 创建一个集合存储 alreadyList 中的 medal.objectId
  128. const alreadyMedalIds = new Set(alreadyList.map(item => item.medal.objectId));
  129. // 从 MedalList 中剔除已获得的勋章
  130. MedalList = MedalList.filter(item => !alreadyMedalIds.has(item.objectId));
  131. this.setData({
  132. MedalList
  133. });
  134. console.log('MedalList', this.data.MedalList);
  135. },
  136. showPopup(e) {
  137. const objectId = e.currentTarget.dataset.id
  138. const type = e.currentTarget.dataset.type
  139. if(type=='alreadyList'){
  140. this.data.alreadyList.forEach((item)=>{
  141. if(item.objectId==objectId){
  142. this.setData({
  143. title:item.medal.title
  144. })
  145. }
  146. })
  147. console.log(this.data.title);
  148. }else{
  149. this.data.MedalList.forEach((item)=>{
  150. if(item.objectId==objectId){
  151. this.setData({
  152. title:item.title
  153. })
  154. }
  155. })
  156. console.log(this.data.title);
  157. }
  158. this.setData({ show: true });
  159. },
  160. onClose() {
  161. this.setData({ show: false });
  162. },
  163. })