index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const app = getApp()
  2. import {
  3. setUserInfo
  4. } from '~/api/user'
  5. import {
  6. storeBindingsBehavior
  7. } from 'mobx-miniprogram-bindings'
  8. import {
  9. store
  10. } from '~/store/index'
  11. let timer
  12. Component({
  13. // 自动绑定
  14. behaviors: [storeBindingsBehavior],
  15. storeBindings: {
  16. store,
  17. fields: {
  18. userInfo: 'userInfo',
  19. androidMask: 'androidMaskFun'
  20. },
  21. actions: {
  22. setUser: 'setUser'
  23. }
  24. },
  25. properties: {
  26. title: {
  27. type: String,
  28. value: '朗读小咖秀',
  29. },
  30. showNav: {
  31. type: Boolean,
  32. value: true
  33. }
  34. },
  35. data: {
  36. navBarHeight: app.globalData.navBarHeight,
  37. menuRight: app.globalData.menuRight,
  38. menuTop: app.globalData.menuTop,
  39. menuHeight: app.globalData.menuHeight,
  40. isGradeShow: false,
  41. temporaryGrade: null
  42. },
  43. pageLifetimes: {
  44. show() {
  45. if (!this.data.userInfo.grade) {
  46. timer = setInterval(() => {
  47. if (this.data.userInfo.uid && !this.data.userInfo.grade) {
  48. this.showGrade()
  49. clearInterval(timer)
  50. } else if (this.data.userInfo.uid && this.data.userInfo.grade) {
  51. clearInterval(timer)
  52. }
  53. }, 500)
  54. }
  55. },
  56. hide() {
  57. clearInterval(timer)
  58. }
  59. },
  60. methods: {
  61. closeGrade() {
  62. if (!this.data.userInfo.grade) {
  63. return
  64. }
  65. this.setData({
  66. isGradeShow: false,
  67. })
  68. if (typeof this.getTabBar === 'function') {
  69. this.getTabBar().setData({
  70. mask: false
  71. })
  72. }
  73. },
  74. // 选择年级
  75. selectGrade({
  76. target
  77. }) {
  78. let code = target.dataset.code
  79. if (!code) {
  80. return
  81. }
  82. this.setData({
  83. temporaryGrade: code
  84. })
  85. },
  86. showGrade() {
  87. if (typeof this.getTabBar === 'function') {
  88. this.getTabBar().setData({
  89. mask: true
  90. })
  91. }
  92. if (this.data.isGradeShow) {
  93. return
  94. }
  95. this.setData({
  96. isGradeShow: true,
  97. temporaryGrade: this.data.userInfo.grade
  98. })
  99. },
  100. // 修改年级
  101. async changeGrade(e) {
  102. const grade = this.data.temporaryGrade
  103. if (!grade) {
  104. return wx.showToast({
  105. title: '请选择年级',
  106. icon: 'none',
  107. duration: 2000
  108. })
  109. }
  110. let res = await setUserInfo({
  111. grade
  112. }, 'put')
  113. this.setUser(res)
  114. this.closeGrade()
  115. setTimeout(() => {
  116. this.triggerEvent('reload')
  117. }, 300)
  118. },
  119. agree(e) {
  120. console.log("用户同意隐私授权, 接下来可以调用隐私协议中声明的隐私接口")
  121. },
  122. toAndroidLogin() {
  123. wx.redirectTo({
  124. url: '/pages/donutLogin/index',
  125. })
  126. }
  127. }
  128. })