// pages/center/center.js var app = getApp() Page({ /** * 页面的初始数据 */ data: { userAvatar: '', token: '', userInfo: {}, usercenterUrl: app.usercenterUrl, sourcePath: '/route/file/downloadfile/true/', randomName: '', showPhone: true, phone: '', code: '', gotCode: false, restTime: 120, timer: '' }, // 选择头像来源 changeAvatar: function () { var self = this wx.showActionSheet({ itemList: ['从相册中选择','拍照'], success: function (res) { if (!res.cancel) { if (res.tapIndex == 0) { self.chooseAvatar('album') } else { self.chooseAvatar('camera') } } } }) }, // 选择图片 chooseAvatar: function (type) { var self = this wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: [type], success: function (res) { var avatar = res.tempFilePaths[0]; app.restAjax.file(app.restAjax.path('{usercenterUrl}/app/file/uploadimage', [app.usercenterUrl]), avatar, 'image', { headers: { token: self.data.token } }, function (code, data) { var id = JSON.parse(data).data self.uploadAvatar(id) }, function (code, data) { console.log(data) }) self.setData({ userAvatar: avatar }) } }) }, // 上传 uploadAvatar: function (avatarId) { var self = this app.restAjax.put(app.restAjax.path('{usercenterUrl}/app/user/updateuseravatar', [app.usercenterUrl]), { avatar: avatarId }, { headers: { token: self.data.token } }, function (code, data) { wx.setStorageSync('token', data.data); self.getUserInfo() }, function (code, data) { console.log(data) }) }, // 获取用户信息 getUserInfo: function () { var self = this app.restAjax.get(app.restAjax.path('{usercenterUrl}/app/user/getappuser', [app.usercenterUrl]), {}, { headers: { token: self.data.token } }, function (code, data) { self.setData({ userInfo: data }) }, function (code, data) { console.log(data) }) }, // 获取token getToken: function () { var self = this wx.getStorage({ key: 'token', success: function(res) { self.setData({ token: res.data }) self.getUserInfo() }, }) }, /** * 跳转到我的志愿团队页面 */ toMyTeam: function() { wx.navigateTo({ url: '../myVolunteerTeam/myVolunteerTeam', }) }, /** * 跳转到我的志愿活动页面 */ toMyVolunteerActivity: function() { wx.navigateTo({ url: '../myVolunteerActivity/myVolunteerActivity', }) }, // 判断用户是否为随机名称 isRandomName: function () { var self = this wx.getStorage({ key: 'isRandomUsername', success: function (res) { if (res.data == 1) { wx.hideTabBar() self.setData({ randomName: true }) } } }) }, // 取消绑定手机 cancelPhone: function () { this.setData({ showPhone: false }) wx.showTabBar() }, // 获取输入的手机号 phoneNum: function (res) { this.setData({ phone: res.detail.value }) console.log(this.data.phone) }, // 校验手机号 testPhone: function () { var self = this if (self.data.phone) { if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(self.data.phone)) { wx.showToast({ title: '请输入正确的手机号', icon: 'none', duration: 1500 }) } else { self.getCode() } } else { wx.showToast({ title: '手机号不能为空', icon: 'none', duration: 1500 }) } }, // 获取验证码 getCode: function () { var self = this app.restAjax.get(app.restAjax.path('{usercenterUrl}/api/sms/getverificationcode/' + self.data.phone, [app.usercenterUrl]), {}, null, function (code, data) { if (code == 200) { console.log(data) self.setData({ gotCode: true }) self.data.timer = setInterval(function () { var cur = self.data.restTime - 1 console.log(cur) if (cur == 0) { clearInterval(self.data.timer) self.setData({ restTime: 120, gotCode: false }) } self.setData({ restTime: cur }) }, 1000) } }, function (code, data) { console.log(data) wx.showToast({ title: data.msg, icon: 'none', duration: 1500 }) } ) }, // 提交绑定手机 submitPhone: function () { var self = this app.restAjax.put(app.restAjax.path('{usercenterUrl}/app/user/updateminiappdefaultusername', [app.usercenterUrl]), { phone: self.data.phone, verificationCode: self.data.code }, { headers: { token: self.data.token } }, function (code, data) { console.log(data) wx.setStorageSync('token', data.data); wx.setStorageSync('isRandomUsername', 0) clearInterval(self.data.timer) self.setData({ showPhone: false, restTime: 120 }) self.getUserInfo() }, function (code, data) { console.log(data) }) }, // 验证码 inputCode: function (e) { this.setData({ code: e.detail.value }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.isRandomName() this.getToken() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })