111 行
3.1 KiB
JavaScript
111 行
3.1 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()
|
|
})
|
|
},
|
|
|
|
uploadAvatar: function (filePath) {
|
|
var ext = 'jpg'
|
|
var match = String(filePath).match(/\.([a-zA-Z0-9]+)$/)
|
|
if (match && match[1]) ext = match[1].toLowerCase()
|
|
var contentType = ext === 'png' ? 'image/png' : 'image/jpeg'
|
|
return http.post('/api/v1/user/avatar/upload-url', {
|
|
ext: ext,
|
|
content_type: contentType
|
|
}).then(function (data) {
|
|
return new Promise(function (resolve, reject) {
|
|
wx.uploadFile({
|
|
url: data.upload_url,
|
|
filePath: filePath,
|
|
name: 'file',
|
|
header: { 'Content-Type': contentType },
|
|
success: function (res) {
|
|
if (res.statusCode >= 200 && res.statusCode < 300) resolve(data.public_url)
|
|
else reject({ message: '头像上传失败' })
|
|
},
|
|
fail: function () { reject({ message: '头像上传失败' }) }
|
|
})
|
|
})
|
|
})
|
|
},
|
|
|
|
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' })
|
|
})
|
|
}
|
|
})
|