index.js 5.1 KB

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