index.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. value: 'PRIMARY_FIVE_GRADE',
  34. key: '五年级'
  35. }, {
  36. value: 'PRIMARY_SIX_GRADE',
  37. key: '六年级'
  38. },
  39. ],
  40. },
  41. onLoad(options) {
  42. // 手工绑定
  43. this.storeBindings = createStoreBindings(this, {
  44. store,
  45. fields: {
  46. userInfo: 'userInfo'
  47. },
  48. actions: {
  49. setUser: 'setUser'
  50. }
  51. })
  52. // 立刻更新
  53. this.storeBindings.updateStoreBindings()
  54. let {
  55. grade
  56. } = this.data.userInfo
  57. let gradeIndex = this.data.gradeArray.findIndex(item => {
  58. return item.value == grade
  59. })
  60. this.setData({
  61. gradeIndex
  62. })
  63. },
  64. // 调用清理函数
  65. onUnload() {
  66. this.storeBindings.destroyStoreBindings()
  67. },
  68. changeAvatar(e) {
  69. const {
  70. avatarUrl
  71. } = e.detail
  72. wx.uploadFile({
  73. url: 'https://reader-api.ai160.com/file/upload',
  74. filePath: avatarUrl,
  75. name: '头像',
  76. header: {
  77. uid: wx.getStorageSync('uid')
  78. },
  79. success: (res) => {
  80. const newAvatar = JSON.parse(res.data).data;
  81. const str = 'userInfo.avatar'
  82. this.setData({
  83. [str]: newAvatar
  84. })
  85. this.setUserInfo({
  86. avatar: newAvatar
  87. })
  88. }
  89. })
  90. },
  91. saveNickName(e) {
  92. let nickName = e.detail.value;
  93. if (nickName == this.data.userInfo.nickName) {
  94. return
  95. }
  96. this.setUserInfo({
  97. nickName
  98. })
  99. },
  100. selectProfession() {
  101. wx.showActionSheet({
  102. itemList: ['学生', '家长', '老师'],
  103. success: (res) => {
  104. /* if (res.tapIndex == this.data.userInfo.gender) {
  105. return
  106. }
  107. this.setUserInfo({
  108. gender: res.tapIndex
  109. }) */
  110. },
  111. })
  112. },
  113. selectGender() {
  114. wx.showActionSheet({
  115. itemList: ['女', '男'],
  116. success: (res) => {
  117. if (res.tapIndex == this.data.userInfo.gender) {
  118. return
  119. }
  120. this.setUserInfo({
  121. gender: res.tapIndex
  122. })
  123. },
  124. })
  125. },
  126. bindDateChange(e) {
  127. this.setUserInfo({
  128. birthday: e.detail.value
  129. })
  130. },
  131. bindGradeChange(e) {
  132. let grade = this.data.gradeArray[e.detail.value].value
  133. this.setUserInfo({
  134. grade
  135. })
  136. },
  137. saveSchool(e) {
  138. let schoolName = e.detail.value;
  139. if (schoolName == this.data.userInfo.schoolName) {
  140. return
  141. }
  142. this.setUserInfo({
  143. schoolName
  144. })
  145. },
  146. async setUserInfo(data) {
  147. wx.showLoading({
  148. title: '提交中',
  149. })
  150. let res = await setUserInfo(data, 'put').finally(() => {
  151. wx.hideLoading()
  152. })
  153. this.setUser(res)
  154. },
  155. })