文件
jw-beauty/miniprogram/pages/login/login.js
T

86 行
2.2 KiB
JavaScript

var app = getApp()
var http = require('../../utils/request')
Page({
data: {
statusBarHeight: 44,
loading: false,
userProfile: null
},
onLoad: function () {
var app = getApp()
this.setData({ statusBarHeight: app.globalData.statusBarHeight })
},
onLogin: function () {
var self = this
self.setData({ loading: true })
self.getWechatProfile().then(function (profile) {
self.setData({ userProfile: profile })
return app.doLogin()
}).then(function () {
return self.saveProfile()
}).then(function () {
self.setData({ loading: false })
wx.switchTab({ url: '/pages/index/index' })
}).catch(function (err) {
self.setData({ loading: false })
wx.showToast({ title: err.message || '登录失败', icon: 'none' })
})
},
getWechatProfile: function () {
return new Promise(function (resolve) {
if (!wx.getUserProfile) {
resolve(null)
return
}
wx.getUserProfile({
desc: '用于完善会员资料',
success: function (res) {
resolve(res.userInfo || null)
},
fail: function () {
resolve(null)
}
})
})
},
saveProfile: function () {
var profile = this.data.userProfile || {}
if (!profile.nickName && !profile.avatarUrl) return Promise.resolve()
return this.updateProfile({
nickname: profile.nickName || '',
avatar: profile.avatarUrl || '',
gender: profile.gender || 0
})
},
updateProfile: function (payload) {
return http.put('/api/v1/user/profile', payload).then(function () {
return app.loadProfile()
})
},
onGetPhoneNumber: function (e) {
var self = this
if (!e.detail || !e.detail.code) {
wx.showToast({ title: '已跳过手机号授权', icon: 'none' })
return
}
self.setData({ loading: true })
app.doLogin().then(function () {
return http.post('/api/v1/user/phone', { code: e.detail.code })
}).then(function () {
self.setData({ loading: false })
wx.switchTab({ url: '/pages/index/index' })
}).catch(function (err) {
self.setData({ loading: false })
wx.showToast({ title: err.message || '手机号授权失败', icon: 'none' })
})
}
})