index.js 5.3 KB

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