文件
jw-beauty/miniprogram/pages/register/register.js
T
Guoguo 4466c53c7b feat(i18n): 剩余15页全量接入中英(登录/注册/扫码/绑定/佩戴/扫描/治疗/订阅/帮助等)
- 3 个并行 agent 各管 5 页,互不重叠命名空间
- 全 App 18 页均 i18n.bind,22 命名空间/400 键中英对齐
- 动态文案(时长/天数/价格/区域)JS 内 i18n.t 计算;区域标签复用 common.regions
2026-07-19 06:00:04 -07:00

148 行
4.1 KiB
JavaScript

var app = getApp()
var http = require('../../utils/request')
var config = require('../../config/env')
var i18n = require('../../i18n/index')
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 i18n.t('register.nicknamePrefix') + digits
}
Page({
data: {
statusBarHeight: 44,
loading: false,
avatarUrl: DEFAULT_AVATAR,
nickname: '',
phone: ''
},
onShow: function () {
i18n.bind(this)
},
onLoad: function () {
this.setData({
statusBarHeight: app.globalData.statusBarHeight,
nickname: randomNickname()
})
},
onPickAvatar: function () {
var self = this
wx.chooseMedia({
count: 1,
mediaType: ['image'],
sourceType: ['album', 'camera'],
success: function (res) {
if (res.tempFiles && res.tempFiles[0]) {
self.setData({ avatarUrl: res.tempFiles[0].tempFilePath })
}
}
})
},
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: i18n.t('register.phoneRequired'), 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: i18n.t('register.authorized')
})
wx.showToast({ title: i18n.t('register.phoneAuthSuccess'), icon: 'success' })
}).catch(function (err) {
self.setData({ loading: false })
wx.showToast({ title: err.message || i18n.t('register.phoneAuthFailed'), icon: 'none' })
})
},
uploadAvatar: function (tempPath) {
return new Promise(function (resolve, reject) {
var token = wx.getStorageSync('token')
wx.uploadFile({
url: config.API_BASE + '/api/v1/user/avatar',
filePath: tempPath,
name: 'file',
header: { Authorization: 'Bearer ' + token },
success: function (res) {
try {
var data = JSON.parse(res.data)
if (data.code === 0 && data.data && data.data.avatar) {
resolve(data.data.avatar)
} else {
reject(new Error(data.message || i18n.t('register.uploadFailed')))
}
} catch (e) {
reject(new Error(i18n.t('register.uploadFailed')))
}
},
fail: function () { reject(new Error(i18n.t('register.uploadFailed'))) }
})
})
},
onSubmit: function () {
var self = this
var nickname = (self.data.nickname || '').trim()
var avatarUrl = self.data.avatarUrl
if (!self.data.phone) {
wx.showToast({ title: i18n.t('register.phoneFirst'), icon: 'none' })
return
}
if (!nickname) {
wx.showToast({ title: i18n.t('register.nicknameRequired'), icon: 'none' })
return
}
self.setData({ loading: true })
var avatarPromise = (avatarUrl && avatarUrl !== DEFAULT_AVATAR)
? self.uploadAvatar(avatarUrl)
: Promise.resolve('')
avatarPromise.then(function (permanentUrl) {
return http.put('/api/v1/user/profile', {
nickname: nickname,
avatar: permanentUrl || ''
})
}).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 || i18n.t('common.saveFailed'), icon: 'none' })
})
},
onAgreement: function () {
wx.showModal({ title: i18n.t('register.agreementTitle'), content: i18n.t('register.agreementContent'), showCancel: false })
}
})