index.js 5.0 KB

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