import { createStoreBindings } from 'mobx-miniprogram-bindings' import { store } from '~/store/index' import { setUserInfo } from '~/api/user' let storeBindings Page({ data: { gradeIndex: 0, gradeArray: [{ value: 'PRESCHOOL', key: '学前班' }, { value: 'PRIMARY_FIRST_GRADE', key: '一年级' }, { value: 'PRIMARY_SECOND_GRADE', key: '二年级' }, { value: 'PRIMARY_THREE_GRADE', key: '三年级' }, { value: 'PRIMARY_SENIOR_GRADE', key: '四年级' }, ], }, onLoad(options) { // 手工绑定 this.storeBindings = createStoreBindings(this, { store, fields: { userInfo: 'userInfo' }, actions: { setUser: 'setUser' } }) // 立刻更新 this.storeBindings.updateStoreBindings() let { grade } = this.data.userInfo let gradeIndex = this.data.gradeArray.findIndex(item => { return item.value == grade }) this.setData({ gradeIndex }) }, // 调用清理函数 onUnload() { this.storeBindings.destroyStoreBindings() }, changeAvatar(e) { const { avatarUrl } = e.detail wx.uploadFile({ url: 'https://reader-api.ai160.com/file/upload', filePath: avatarUrl, name: '头像', header: { uid: wx.getStorageSync('uid') }, success: (res) => { const newAvatar = JSON.parse(res.data).data; const str = 'userInfo.avatar' this.setData({ [str]: newAvatar }) this.setUserInfo({ avatar: newAvatar }) } }) }, saveNickName(e) { let nickName = e.detail.value; if (nickName == this.data.userInfo.nickName) { return } this.setUserInfo({ nickName }) }, selectProfession() { wx.showActionSheet({ itemList: ['学生', '家长', '老师'], success: (res) => { /* if (res.tapIndex == this.data.userInfo.gender) { return } this.setUserInfo({ gender: res.tapIndex }) */ }, }) }, selectGender() { wx.showActionSheet({ itemList: ['女', '男'], success: (res) => { if (res.tapIndex == this.data.userInfo.gender) { return } this.setUserInfo({ gender: res.tapIndex }) }, }) }, bindDateChange(e) { this.setUserInfo({ birthday: e.detail.value }) }, bindGradeChange(e) { let grade = this.data.gradeArray[e.detail.value].value this.setUserInfo({ grade }) }, saveSchool(e) { let schoolName = e.detail.value; if (schoolName == this.data.userInfo.schoolName) { return } this.setUserInfo({ schoolName }) }, async setUserInfo(data) { wx.showLoading({ title: '提交中', }) let res = await setUserInfo(data, 'put').finally(() => { wx.hideLoading() }) this.setUser(res) }, })