index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. // numA: () => store.numA, // 绑定字段第一种方式
  19. // numB: (store) => store.numB, // 绑定字段第二种方式
  20. // sum: 'sum' // 绑定字段第三种方式
  21. },
  22. actions: {
  23. setUser: 'setUser'
  24. }
  25. },
  26. properties: {
  27. title: {
  28. type: String,
  29. value: '朗读小咖秀',
  30. }
  31. },
  32. data: {
  33. navBarHeight: app.globalData.navBarHeight,
  34. menuRight: app.globalData.menuRight,
  35. menuTop: app.globalData.menuTop,
  36. menuHeight: app.globalData.menuHeight,
  37. isGradeShow: false,
  38. temporaryGrade: null
  39. },
  40. attached: function () {
  41. // console.log(wx.getStorage(user));
  42. },
  43. methods: {
  44. // 选择年级
  45. selectGrade({
  46. target
  47. }) {
  48. let code = target.dataset.code
  49. if (!code) {
  50. return
  51. }
  52. this.setData({
  53. temporaryGrade: code
  54. })
  55. },
  56. showGrade() {
  57. this.setData({
  58. isGradeShow: true,
  59. temporaryGrade: this.data.userInfo.grade
  60. })
  61. },
  62. // 修改年级
  63. async changeGrade(e) {
  64. const grade = this.data.temporaryGrade
  65. if (!grade) {
  66. return wx.showToast({
  67. title: '请选择年级',
  68. icon: 'none',
  69. duration: 2000
  70. })
  71. }
  72. this.setData({
  73. isGradeShow: false,
  74. })
  75. let res = await setUserInfo({
  76. grade
  77. }, 'put')
  78. this.setUser(res)
  79. },
  80. }
  81. })