feat: add registration page for new miniprogram users
- New register page with chooseAvatar, nickname input (random default), and required phone number authorization via getPhoneNumber - Login simplified: removed deprecated wx.getUserProfile, checks is_new_user flag to redirect new users to registration - Server login response now includes is_new_user field
这个提交包含在:
@@ -0,0 +1,102 @@
|
||||
var app = getApp()
|
||||
var http = require('../../utils/request')
|
||||
|
||||
var DEFAULT_AVATAR = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI9t2NHfLvvMiaiaCJQn3KP2P0vAHp1QBP0piavhGfhKCARlGd6z1EbR58ibJXKCp5WVlNnojwA/640'
|
||||
|
||||
function randomNickname() {
|
||||
var digits = ''
|
||||
for (var i = 0; i < 4; i++) {
|
||||
digits += Math.floor(Math.random() * 10)
|
||||
}
|
||||
return '用户' + digits
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
statusBarHeight: 44,
|
||||
loading: false,
|
||||
avatarUrl: DEFAULT_AVATAR,
|
||||
nickname: '',
|
||||
phone: ''
|
||||
},
|
||||
|
||||
onLoad: function () {
|
||||
this.setData({
|
||||
statusBarHeight: app.globalData.statusBarHeight,
|
||||
nickname: randomNickname()
|
||||
})
|
||||
},
|
||||
|
||||
onChooseAvatar: function (e) {
|
||||
if (e.detail && e.detail.avatarUrl) {
|
||||
this.setData({ avatarUrl: e.detail.avatarUrl })
|
||||
}
|
||||
},
|
||||
|
||||
onNicknameInput: function (e) {
|
||||
this.setData({ nickname: e.detail.value || '' })
|
||||
},
|
||||
|
||||
onNicknameBlur: function (e) {
|
||||
var val = (e.detail.value || '').trim()
|
||||
if (!val) {
|
||||
this.setData({ nickname: randomNickname() })
|
||||
} else {
|
||||
this.setData({ nickname: val })
|
||||
}
|
||||
},
|
||||
|
||||
onGetPhoneNumber: function (e) {
|
||||
var self = this
|
||||
if (!e.detail || !e.detail.code) {
|
||||
wx.showToast({ title: '需要授权手机号才能完成注册', icon: 'none' })
|
||||
return
|
||||
}
|
||||
self.setData({ loading: true })
|
||||
http.post('/api/v1/user/phone', { code: e.detail.code }).then(function (data) {
|
||||
self.setData({
|
||||
loading: false,
|
||||
phone: data.phone || data.pure_phone_number || '已授权'
|
||||
})
|
||||
wx.showToast({ title: '手机号授权成功', icon: 'success' })
|
||||
}).catch(function (err) {
|
||||
self.setData({ loading: false })
|
||||
wx.showToast({ title: err.message || '手机号授权失败', icon: 'none' })
|
||||
})
|
||||
},
|
||||
|
||||
onSubmit: function () {
|
||||
var self = this
|
||||
var nickname = (self.data.nickname || '').trim()
|
||||
var avatarUrl = self.data.avatarUrl
|
||||
|
||||
if (!self.data.phone) {
|
||||
wx.showToast({ title: '请先授权手机号', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
if (!nickname) {
|
||||
wx.showToast({ title: '请输入昵称', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
self.setData({ loading: true })
|
||||
|
||||
http.put('/api/v1/user/profile', {
|
||||
nickname: nickname,
|
||||
avatar: avatarUrl !== DEFAULT_AVATAR ? avatarUrl : ''
|
||||
}).then(function () {
|
||||
return app.loadProfile()
|
||||
}).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' })
|
||||
})
|
||||
},
|
||||
|
||||
onAgreement: function () {
|
||||
wx.showModal({ title: '提示', content: '用户协议和隐私政策内容建设中', showCancel: false })
|
||||
}
|
||||
})
|
||||
在新工单中引用
屏蔽一个用户