index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import {
  2. createStoreBindings
  3. } from 'mobx-miniprogram-bindings'
  4. import {
  5. store
  6. } from '~/store/index'
  7. import {
  8. setUserInfo
  9. } from '~/api/user'
  10. let storeBindings
  11. Page({
  12. data: {
  13. gradeIndex: 0,
  14. gradeArray: [{
  15. value: 'PRESCHOOL',
  16. key: '学前班'
  17. }, {
  18. value: 'PRIMARY_FIRST_GRADE',
  19. key: '一年级'
  20. },
  21. {
  22. value: 'PRIMARY_SECOND_GRADE',
  23. key: '二年级'
  24. },
  25. {
  26. value: 'PRIMARY_THREE_GRADE',
  27. key: '三年级'
  28. },
  29. {
  30. value: 'PRIMARY_SENIOR_GRADE',
  31. key: '四年级'
  32. },
  33. ],
  34. },
  35. onLoad(options) {
  36. // 手工绑定
  37. this.storeBindings = createStoreBindings(this, {
  38. store,
  39. fields: {
  40. userInfo: 'userInfo'
  41. },
  42. actions: {
  43. setUser: 'setUser'
  44. }
  45. })
  46. // 立刻更新
  47. this.storeBindings.updateStoreBindings()
  48. let {
  49. grade
  50. } = this.data.userInfo
  51. let gradeIndex = this.data.gradeArray.findIndex(item => {
  52. return item.value == grade
  53. })
  54. this.setData({
  55. gradeIndex
  56. })
  57. },
  58. // 调用清理函数
  59. onUnload() {
  60. this.storeBindings.destroyStoreBindings()
  61. },
  62. changeAvatar(e) {
  63. const {
  64. avatarUrl
  65. } = e.detail
  66. wx.uploadFile({
  67. url: 'https://reader-api.ai160.com/file/upload',
  68. filePath: avatarUrl,
  69. name: '头像',
  70. header: {
  71. uid: wx.getStorageSync('uid')
  72. },
  73. success: (res) => {
  74. const newAvatar = JSON.parse(res.data).data;
  75. const str = 'userInfo.avatar'
  76. this.setData({
  77. [str]: newAvatar
  78. })
  79. this.setUserInfo({
  80. avatar: newAvatar
  81. })
  82. }
  83. })
  84. },
  85. saveNickName(e) {
  86. let nickName = e.detail.value;
  87. if (nickName == this.data.userInfo.nickName) {
  88. return
  89. }
  90. this.setUserInfo({
  91. nickName
  92. })
  93. },
  94. selectProfession() {
  95. wx.showActionSheet({
  96. itemList: ['学生', '家长', '老师'],
  97. success: (res) => {
  98. /* if (res.tapIndex == this.data.userInfo.gender) {
  99. return
  100. }
  101. this.setUserInfo({
  102. gender: res.tapIndex
  103. }) */
  104. },
  105. })
  106. },
  107. selectGender() {
  108. wx.showActionSheet({
  109. itemList: ['女', '男'],
  110. success: (res) => {
  111. if (res.tapIndex == this.data.userInfo.gender) {
  112. return
  113. }
  114. this.setUserInfo({
  115. gender: res.tapIndex
  116. })
  117. },
  118. })
  119. },
  120. bindDateChange(e) {
  121. this.setUserInfo({
  122. birthday: e.detail.value
  123. })
  124. },
  125. bindGradeChange(e) {
  126. let grade = this.data.gradeArray[e.detail.value].value
  127. this.setUserInfo({
  128. grade
  129. })
  130. },
  131. saveSchool(e) {
  132. let schoolName = e.detail.value;
  133. if (schoolName == this.data.userInfo.schoolName) {
  134. return
  135. }
  136. this.setUserInfo({
  137. schoolName
  138. })
  139. },
  140. async setUserInfo(data) {
  141. wx.showLoading({
  142. title: '提交中',
  143. })
  144. let res = await setUserInfo(data, 'put').finally(() => {
  145. wx.hideLoading()
  146. })
  147. this.setUser(res)
  148. },
  149. })