main.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. Page({
  2. /**
  3. * 页面的初始数据
  4. */
  5. data: {
  6. mainDatas: [],
  7. toggleEnable: true,
  8. filterList: 0,
  9. filterItem: ["", "", "", ""],
  10. filterName: [
  11. {},
  12. {
  13. type: "信息类型",
  14. list: [
  15. {
  16. sub: "通知",
  17. item : [
  18. {
  19. secondSub:"生活通知",
  20. subitem:["水电通知", "缴费通知", "假期通知", "后勤通知", "图书馆通知"]
  21. },
  22. {
  23. secondSub:"学业通知",
  24. subitem:["选课通知", "竞赛通知", "考试通知"]
  25. },
  26. {
  27. secondSub:"其他通知",
  28. subitem:["其他通知"]
  29. }
  30. ]
  31. },
  32. {
  33. sub: "活动",
  34. item: ["志愿活动", "文艺活动", "体育活动", "学术活动", "素质拓展", "精品课程", "其他活动"]
  35. },
  36. {
  37. sub: "纳新",
  38. item: ["社团纳新", "组织纳新"]
  39. }
  40. ]
  41. },
  42. {
  43. type: "发布来源",
  44. item: ["我关注的", "社团", "组织", "权服侠", "学校部门"]
  45. },
  46. {
  47. type: "我的需求",
  48. item: ["二课分", "三课分", "四课分", "勤工助学", "志愿者小时数", "综素/活动纪实"]
  49. }
  50. ],
  51. activity: null,
  52. inform: null,
  53. recruit: null
  54. },
  55. showList: function (id) {
  56. this.data.toggleEnable = false
  57. this.setData({
  58. filterList: id
  59. })
  60. this.animate("#arrow" + id, [
  61. { rotate: 0 },
  62. { rotate: 180 },
  63. ], 150)
  64. this.animate("#list" + id, [
  65. { opacity: 0 },
  66. { opacity: 1 },
  67. ], 150)
  68. setTimeout(function () {
  69. this.setData({
  70. toggleEnable: true
  71. })
  72. }.bind(this), 200)
  73. },
  74. hideList: function (id) {
  75. this.data.toggleEnable = false
  76. this.animate("#arrow" + id, [
  77. { rotate: 180 },
  78. { rotate: 360 },
  79. ], 150)
  80. this.animate("#list" + id, [
  81. { opacity: 1 },
  82. { opacity: 0 },
  83. ], 150, function () {
  84. this.setData({
  85. filterList: 0
  86. })
  87. }.bind(this))
  88. setTimeout(function () {
  89. this.setData({
  90. toggleEnable: true
  91. })
  92. }.bind(this), 200)
  93. },
  94. toggleFilterList: function (e) {
  95. if (!this.data.toggleEnable) return
  96. var list = e.currentTarget.dataset.filterList
  97. if (this.data.filterList == list) {
  98. if (this.data.filterList != 0) {
  99. this.hideList(this.data.filterList)
  100. }
  101. } else {
  102. if (this.data.filterList != 0) {
  103. this.hideList(this.data.filterList)
  104. }
  105. if (list != 0) {
  106. this.showList(list)
  107. }
  108. }
  109. },
  110. toggleFilterItem: function (e) {
  111. if (!this.data.toggleEnable) return
  112. if (this.data.filterList != 0 && this.data.filterList == e.currentTarget.dataset.filterList) {
  113. var item = e.currentTarget.dataset.filterItem
  114. var arr = this.data.filterItem
  115. arr[this.data.filterList] = arr[this.data.filterList] == item ? "" : item;
  116. this.setData({
  117. filterItem: arr
  118. })
  119. this.hideList(this.data.filterList)
  120. }
  121. },
  122. filterMsgType: function (e) {
  123. var arr = this.data.filterItem
  124. arr[1] = e.detail.attr
  125. this.setData({
  126. filterItem: arr
  127. })
  128. },
  129. // 计算时间差
  130. handleDate: function(date) {
  131. var now = new Date().getTime()
  132. var diffValue = now - date.getTime()
  133. if (diffValue < 0) {
  134. console.log("时间不同步")
  135. return "刚刚"
  136. }
  137. var result = ""
  138. var minute = 1000 * 60
  139. var hour = minute * 60
  140. var day = hour * 24
  141. var minC = diffValue / minute
  142. var hourC = diffValue / hour
  143. var dayC = diffValue / day
  144. if (parseInt(dayC) > 30) {
  145. result += date.getFullYear() + "/" + (date.getMonth() + 1) + "/" + date.getDate()
  146. } else if (parseInt(dayC) > 1) {
  147. result += parseInt(dayC) + "天前"
  148. } else if (parseInt(dayC) == 1) {
  149. result += "昨天"
  150. } else if (hourC >= 1) {
  151. result += parseInt(hourC) + "小时前"
  152. } else if (minC >= 5) {
  153. result += parseInt(minC) + "分钟前"
  154. } else {
  155. result += "刚刚"
  156. }
  157. return result
  158. },
  159. processData: function (start, arr) {
  160. const db = wx.cloud.database()
  161. for (let i = start; i < arr.length; i++) {
  162. arr[i].time = this.handleDate(arr[i].time)
  163. db.collection("publisherInfoData").where({
  164. publisherName: arr[i].publisherName
  165. }).get({
  166. success: function (res) {
  167. arr[i].publisherAttribute = res.data[0].publisherAttribute
  168. this.setData({
  169. mainDatas: arr
  170. })
  171. wx.hideNavigationBarLoading()
  172. }.bind(this)
  173. })
  174. }
  175. },
  176. /**
  177. * 生命周期函数--监听页面加载
  178. */
  179. onLoad: function (options) {
  180. wx.showNavigationBarLoading()
  181. const db = wx.cloud.database()
  182. db.collection("mainData").orderBy("time", "desc").limit(20).get({
  183. success: function (res) {
  184. this.processData(0, res.data)
  185. }.bind(this)
  186. })
  187. },
  188. /**
  189. * 生命周期函数--监听页面初次渲染完成
  190. */
  191. onReady: function () {
  192. },
  193. /**
  194. * 生命周期函数--监听页面显示
  195. */
  196. onShow: function () {
  197. },
  198. /**
  199. * 生命周期函数--监听页面隐藏
  200. */
  201. onHide: function () {
  202. },
  203. /**
  204. * 生命周期函数--监听页面卸载
  205. */
  206. onUnload: function () {
  207. },
  208. /**
  209. * 页面相关事件处理函数--监听用户下拉动作
  210. */
  211. onPullDownRefresh: function () {
  212. wx.showNavigationBarLoading()
  213. const db = wx.cloud.database()
  214. db.collection("mainData").orderBy("time", "desc").limit(20).get({
  215. success: function (res) {
  216. this.processData(0, res.data)
  217. wx.stopPullDownRefresh()
  218. }.bind(this)
  219. })
  220. },
  221. /**
  222. * 页面上拉触底事件的处理函数
  223. */
  224. onReachBottom: function () {
  225. wx.showNavigationBarLoading()
  226. const db = wx.cloud.database()
  227. db.collection("mainData").orderBy("time", "desc").skip(this.data.mainDatas.length).limit(20).get({
  228. success: function (res) {
  229. if (res.data.length == 0) {
  230. wx.hideNavigationBarLoading()
  231. return
  232. }
  233. res.data = this.data.mainDatas.concat(res.data)
  234. this.processData(this.data.mainDatas.length, res.data)
  235. }.bind(this)
  236. })
  237. },
  238. /**
  239. * 用户点击右上角分享
  240. */
  241. onShareAppMessage: function () {
  242. }
  243. })