index.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. Component({
  12. // 自动绑定
  13. behaviors: [storeBindingsBehavior],
  14. storeBindings: {
  15. store,
  16. fields: {
  17. userInfo: 'userInfo'
  18. },
  19. actions: {
  20. setUser: 'setUser'
  21. }
  22. },
  23. properties: {
  24. title: {
  25. type: String,
  26. value: '朗读小咖秀',
  27. }
  28. },
  29. data: {
  30. navBarHeight: app.globalData.navBarHeight,
  31. menuRight: app.globalData.menuRight,
  32. menuTop: app.globalData.menuTop,
  33. menuHeight: app.globalData.menuHeight,
  34. isGradeShow: false,
  35. temporaryGrade: null
  36. },
  37. pageLifetimes: {
  38. show: function () {
  39. if (!this.data.userInfo.grade) {
  40. let timer = setInterval(() => {
  41. if (this.data.userInfo.uid && this.data.userInfo.grade) {
  42. clearInterval(timer)
  43. } else {
  44. this.showGrade()
  45. clearInterval(timer)
  46. }
  47. }, 500)
  48. }
  49. },
  50. },
  51. lifetimes: {
  52. attached() {
  53. // if (!this.data.userInfo.grade) {
  54. // let timer = setInterval(() => {
  55. // console.log(this.data.userInfo.uid, this.data.userInfo.grade);
  56. // if (this.data.userInfo.uid && this.data.userInfo.grade) {
  57. // clearInterval(timer)
  58. // } else {
  59. // this.showGrade()
  60. // clearInterval(timer)
  61. // }
  62. // }, 500)
  63. // }
  64. }
  65. },
  66. methods: {
  67. closeGrade() {
  68. if (!this.data.userInfo.grade) {
  69. return
  70. }
  71. this.setData({
  72. isGradeShow: false,
  73. })
  74. },
  75. // 选择年级
  76. selectGrade({
  77. target
  78. }) {
  79. let code = target.dataset.code
  80. if (!code) {
  81. return
  82. }
  83. this.setData({
  84. temporaryGrade: code
  85. })
  86. },
  87. showGrade() {
  88. this.setData({
  89. isGradeShow: true,
  90. temporaryGrade: this.data.userInfo.grade
  91. })
  92. },
  93. // 修改年级
  94. async changeGrade(e) {
  95. const grade = this.data.temporaryGrade
  96. if (!grade) {
  97. return wx.showToast({
  98. title: '请选择年级',
  99. icon: 'none',
  100. duration: 2000
  101. })
  102. }
  103. this.setData({
  104. isGradeShow: false,
  105. })
  106. let res = await setUserInfo({
  107. grade
  108. }, 'put')
  109. this.setUser(res)
  110. setTimeout(() => {
  111. this.triggerEvent('reload')
  112. }, 300)
  113. },
  114. }
  115. })