index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. },
  20. actions: {
  21. setUser: 'setUser'
  22. }
  23. },
  24. properties: {
  25. title: {
  26. type: String,
  27. value: '朗读小咖秀',
  28. },
  29. showNav: {
  30. type: Boolean,
  31. value: true
  32. }
  33. },
  34. data: {
  35. navBarHeight: app.globalData.navBarHeight,
  36. menuRight: app.globalData.menuRight,
  37. menuTop: app.globalData.menuTop,
  38. menuHeight: app.globalData.menuHeight,
  39. isGradeShow: false,
  40. temporaryGrade: null
  41. },
  42. pageLifetimes: {
  43. show() {
  44. if (!this.data.userInfo.grade) {
  45. timer = setInterval(() => {
  46. if (this.data.userInfo.uid && !this.data.userInfo.grade) {
  47. this.showGrade()
  48. clearInterval(timer)
  49. } else if (this.data.userInfo.uid && this.data.userInfo.grade) {
  50. clearInterval(timer)
  51. }
  52. }, 500)
  53. }
  54. },
  55. hide() {
  56. clearInterval(timer)
  57. }
  58. },
  59. methods: {
  60. closeGrade() {
  61. if (!this.data.userInfo.grade) {
  62. return
  63. }
  64. this.setData({
  65. isGradeShow: false,
  66. })
  67. if (typeof this.getTabBar === 'function') {
  68. this.getTabBar().setData({
  69. mask: false
  70. })
  71. }
  72. },
  73. // 选择年级
  74. selectGrade({
  75. target
  76. }) {
  77. let code = target.dataset.code
  78. if (!code) {
  79. return
  80. }
  81. this.setData({
  82. temporaryGrade: code
  83. })
  84. },
  85. showGrade() {
  86. if (typeof this.getTabBar === 'function') {
  87. this.getTabBar().setData({
  88. mask: true
  89. })
  90. }
  91. if (this.data.isGradeShow) {
  92. return
  93. }
  94. this.setData({
  95. isGradeShow: true,
  96. temporaryGrade: this.data.userInfo.grade
  97. })
  98. },
  99. // 修改年级
  100. async changeGrade(e) {
  101. const grade = this.data.temporaryGrade
  102. if (!grade) {
  103. return wx.showToast({
  104. title: '请选择年级',
  105. icon: 'none',
  106. duration: 2000
  107. })
  108. }
  109. let res = await setUserInfo({
  110. grade
  111. }, 'put')
  112. this.setUser(res)
  113. this.closeGrade()
  114. setTimeout(() => {
  115. this.triggerEvent('reload')
  116. }, 300)
  117. },
  118. agree(e) {
  119. console.log("用户同意隐私授权, 接下来可以调用隐私协议中声明的隐私接口")
  120. },
  121. }
  122. })