b.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. Page({
  2. data: {
  3. name: '', // 用户名
  4. password: '', // 密码
  5. errorMessage: '', // 错误提示
  6. },
  7. // 输入用户名
  8. inputName: function (e) {
  9. this.setData({
  10. name: e.detail.value
  11. });
  12. },
  13. // 输入密码
  14. inputPassword: function (e) {
  15. this.setData({
  16. password: e.detail.value
  17. });
  18. },
  19. // 注册逻辑
  20. register: function () {
  21. const { name, password } = this.data;
  22. // 检查用户名和密码是否为空
  23. if (!name || !password) {
  24. wx.showToast({
  25. title: '用户名和密码不能为空',
  26. icon: 'none',
  27. duration: 2000
  28. });
  29. return;
  30. }
  31. wx.showLoading({
  32. title: '注册中...'
  33. });
  34. setTimeout(() => {
  35. // 发送请求到后端,进行用户注册
  36. wx.request({
  37. url: 'https://127.0.0.1:5000/register', // 后端注册接口
  38. method: 'POST',
  39. data: {
  40. name: name, // 修改为 name
  41. password: password,
  42. },
  43. success: (res) => {
  44. wx.hideLoading();
  45. if (res.data.success) {
  46. wx.showToast({
  47. title: '注册成功',
  48. icon: 'success',
  49. duration: 2000
  50. });
  51. // 清空输入框
  52. this.setData({
  53. name: '',
  54. password: '',
  55. errorMessage: ''
  56. });
  57. // 跳转到登录页面
  58. wx.switchTab({
  59. url: '/pages/Home/Home'
  60. });
  61. } else {
  62. wx.showToast({
  63. title: res.data.message || '注册失败',
  64. icon: 'none',
  65. duration: 2000
  66. });
  67. }
  68. },
  69. fail: (err) => {
  70. wx.hideLoading();
  71. wx.showToast({
  72. title: '网络错误,请重试',
  73. icon: 'none',
  74. duration: 2000
  75. });
  76. console.error('注册失败:', err);
  77. }
  78. });
  79. }, 1500);
  80. },
  81. });