import { createStoreBindings } from 'mobx-miniprogram-bindings' import { store } from '~/store/index' import { setUserInfo, exchangePhone, getMyInfo, bindPhone, updateGender } from '~/api/user' import event from '~/mixins/event' let storeBindings Page({ behaviors: [event], 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: '四年级' }, { value: 'PRIMARY_FIVE_GRADE', key: '五年级' }, { value: 'PRIMARY_SIX_GRADE', key: '六年级' }, ], genderList: [{ value: 0, key: '女' }, { value: 1, 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 } else if (!nickName) { return this.setData({ ['userInfo.nickName']: this.data.userInfo.nickName }) } this.setUserInfo({ nickName }) }, async selectGender(e) { wx.showLoading({ title: '提交中', }) await setUserInfo({ gender: e.detail.value }, 'put').finally(() => { wx.hideLoading() }) let res = await updateGender({ gender: e.detail.value }) this.setUser(res) }, 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) }, async getPhoneNumber({ detail }) { let mobile = await exchangePhone({ code: detail.code }) await bindPhone({ mobile }) let userInfo = await getMyInfo() this.setUser(userInfo.user) wx.showToast({ title: '绑定成功!', icon: "none", duration: 4000 }) }, })