index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. },
  30. data: {
  31. navBarHeight: app.globalData.navBarHeight,
  32. menuRight: app.globalData.menuRight,
  33. menuTop: app.globalData.menuTop,
  34. menuHeight: app.globalData.menuHeight,
  35. isGradeShow: false,
  36. temporaryGrade: null
  37. },
  38. pageLifetimes: {
  39. show() {
  40. if (!this.data.userInfo.grade) {
  41. timer = setInterval(() => {
  42. if (this.data.userInfo.uid && !this.data.userInfo.grade) {
  43. this.showGrade()
  44. } else if (this.data.userInfo.uid && this.data.userInfo.grade) {
  45. clearInterval(timer)
  46. }
  47. }, 500)
  48. }
  49. },
  50. hide() {
  51. clearInterval(timer)
  52. }
  53. },
  54. methods: {
  55. closeGrade() {
  56. if (!this.data.userInfo.grade) {
  57. return
  58. }
  59. this.setData({
  60. isGradeShow: false,
  61. })
  62. },
  63. // 选择年级
  64. selectGrade({
  65. target
  66. }) {
  67. let code = target.dataset.code
  68. if (!code) {
  69. return
  70. }
  71. this.setData({
  72. temporaryGrade: code
  73. })
  74. },
  75. showGrade() {
  76. if (this.data.isGradeShow) {
  77. return
  78. }
  79. this.setData({
  80. isGradeShow: true,
  81. temporaryGrade: this.data.userInfo.grade
  82. })
  83. },
  84. // 修改年级
  85. async changeGrade(e) {
  86. const grade = this.data.temporaryGrade
  87. if (!grade) {
  88. return wx.showToast({
  89. title: '请选择年级',
  90. icon: 'none',
  91. duration: 2000
  92. })
  93. }
  94. this.setData({
  95. isGradeShow: false,
  96. })
  97. let res = await setUserInfo({
  98. grade
  99. }, 'put')
  100. this.setUser(res)
  101. setTimeout(() => {
  102. this.triggerEvent('reload')
  103. }, 300)
  104. },
  105. }
  106. })