1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- Page({
- data: {
- name: '', // 用户名
- password: '', // 密码
- errorMessage: '', // 错误提示
- },
- // 输入用户名
- inputName: function (e) {
- this.setData({
- name: e.detail.value
- });
- },
- // 输入密码
- inputPassword: function (e) {
- this.setData({
- password: e.detail.value
- });
- },
- // 注册逻辑
- register: function () {
- const { name, password } = this.data;
- // 检查用户名和密码是否为空
- if (!name || !password) {
- wx.showToast({
- title: '用户名和密码不能为空',
- icon: 'none',
- duration: 2000
- });
- return;
- }
- wx.showLoading({
- title: '注册中...'
- });
- setTimeout(() => {
- // 发送请求到后端,进行用户注册
- wx.request({
- url: 'https://127.0.0.1:5000/register', // 后端注册接口
- method: 'POST',
- data: {
- name: name, // 修改为 name
- password: password,
- },
- success: (res) => {
- wx.hideLoading();
- if (res.data.success) {
- wx.showToast({
- title: '注册成功',
- icon: 'success',
- duration: 2000
- });
- // 清空输入框
- this.setData({
- name: '',
- password: '',
- errorMessage: ''
- });
- // 跳转到登录页面
- wx.switchTab({
- url: '/pages/Home/Home'
- });
- } else {
- wx.showToast({
- title: res.data.message || '注册失败',
- icon: 'none',
- duration: 2000
- });
- }
- },
- fail: (err) => {
- wx.hideLoading();
- wx.showToast({
- title: '网络错误,请重试',
- icon: 'none',
- duration: 2000
- });
- console.error('注册失败:', err);
- }
- });
- }, 1500);
- },
- });
|