index.js 1.9 KB

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