index.js 4.3 KB

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