index.js 5.0 KB

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