index.js 4.6 KB

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