index.js 5.0 KB

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