index.js 4.7 KB

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