index.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. const app = getApp()
  2. import {
  3. getCategoryList,
  4. getResourceList
  5. } from "~/api/works"
  6. import {
  7. getBannerList,
  8. setSubscribe,
  9. } from '~/api/global'
  10. import event from '~/mixins/event'
  11. import share from '~/mixins/share'
  12. import {
  13. createStoreBindings
  14. } from 'mobx-miniprogram-bindings'
  15. import {
  16. store
  17. } from '~/store/index'
  18. Page({
  19. behaviors: [share, event],
  20. data: {
  21. navBarHeight: app.globalData.navBarHeight,
  22. desktopTips: app.globalData.desktopTips,
  23. bannerList: [],
  24. categoryList: [],
  25. listOptions: {},
  26. subscribeShow: false,
  27. tmplIds: []
  28. },
  29. onLoad(options) {
  30. this.getLocUserInfo()
  31. if (Object.keys(this.data.userInfo).length > 0) {
  32. this.requestAgain()
  33. } else {
  34. getApp().callBack = (res) => {
  35. this.getLocUserInfo()
  36. this.requestAgain()
  37. }
  38. }
  39. let {
  40. desktopTips
  41. } = app.globalData
  42. if (desktopTips) {
  43. setTimeout(() => {
  44. this.setData({
  45. desktopTips: false
  46. })
  47. wx.setStorage({
  48. key: "preDesktopTime",
  49. data: new Date()
  50. })
  51. }, 6000)
  52. }
  53. },
  54. onShow() {
  55. if (typeof this.getTabBar === 'function') {
  56. this.getTabBar().setData({
  57. selected: 2
  58. })
  59. }
  60. },
  61. requestAgain() {
  62. this.getBannerList()
  63. this.getResource()
  64. this.getCategoryList()
  65. this.getSubscribe()
  66. },
  67. async getLocUserInfo() {
  68. this.storeBindings = createStoreBindings(this, {
  69. store,
  70. fields: {
  71. userInfo: 'userInfo'
  72. },
  73. })
  74. this.storeBindings.updateStoreBindings()
  75. },
  76. async getCategoryList() {
  77. let grade = this.data.userInfo.grade
  78. let categoryList = await getCategoryList({
  79. grade
  80. })
  81. this.setData({
  82. categoryList
  83. })
  84. },
  85. async getResource() {
  86. let data = await getResourceList({
  87. grade: this.data.userInfo.grade
  88. })
  89. this.setData({
  90. listOptions: data,
  91. })
  92. },
  93. async getBannerList() {
  94. let bannerList = await getBannerList({
  95. grade: this.data.userInfo.grade,
  96. })
  97. this.setData({
  98. bannerList,
  99. })
  100. },
  101. jumpChildClassify({
  102. currentTarget
  103. }) {
  104. let firstInfo = currentTarget.dataset.item
  105. wx.navigateTo({
  106. url: `/pages/childClassify/index?type=class&title=${firstInfo.title}&id=${firstInfo.id}`,
  107. })
  108. },
  109. showTips() {
  110. wx.showModal({
  111. title: '新栏目更新中',
  112. content: '敬请期待….',
  113. showCancel: false,
  114. confirmColor: '#333333',
  115. success(res) {}
  116. })
  117. },
  118. closeDesktop() {
  119. this.setData({
  120. desktopTips: false
  121. })
  122. wx.setStorage({
  123. key: "preDesktopTime",
  124. data: new Date()
  125. })
  126. },
  127. jumpSearch() {
  128. wx.navigateTo({
  129. url: '/pages/childClassify/index?type=search',
  130. })
  131. },
  132. async getSubscribe() {
  133. let tmplIds = await setSubscribe()
  134. this.setData({
  135. tmplIds: tmplIds ? tmplIds : [],
  136. subscribeShow: tmplIds ? true : false,
  137. })
  138. },
  139. requestMessage() {
  140. wx.requestSubscribeMessage({
  141. tmplIds: this.data.tmplIds,
  142. success: async (res) => {
  143. let accept = []
  144. this.data.tmplIds.forEach(item => {
  145. if (res[item] == 'accept') {
  146. accept.push(item)
  147. }
  148. })
  149. await setSubscribe({
  150. ids: accept
  151. }, 'post')
  152. this.getSubscribe()
  153. },
  154. fail: async (err) => {
  155. await setSubscribe({
  156. ids: []
  157. }, 'post')
  158. this.getSubscribe()
  159. /* console.log(err);
  160. if (err.errCode == '20004') {
  161. // 20004
  162. wx.showModal({
  163. title: '温馨提示',
  164. content: '请同意允许我们向您发送订阅信息,请打开设置勾选订阅消息,这样能随时接到关于您作品的最新消息',
  165. complete: (res) => {
  166. if (res.cancel) {}
  167. if (res.confirm) {}
  168. }
  169. })
  170. } */
  171. }
  172. })
  173. },
  174. onUnload() {
  175. this.storeBindings.destroyStoreBindings()
  176. },
  177. })