当前位置:   article > 正文

微信小程序登录注册页面代码_微信小程序登录页面代码

微信小程序登录页面代码

以下是一个简单的微信小程序注册登录页面的代码示例:

  1. <!-- index.wxml -->
  2. <view class="container">
  3. <form bindsubmit="onSubmit">
  4. <view class="form-group">
  5. <text>用户名:</text>
  6. <input type="text" name="username" placeholder="请输入用户名" />
  7. </view>
  8. <view class="form-group">
  9. <text>密码:</text>
  10. <input type="password" name="password" placeholder="请输入密码" />
  11. </view>
  12. <view class="form-group">
  13. <text>确认密码:</text>
  14. <input type="password" name="confirmPassword" placeholder="请再次输入密码" />
  15. </view>
  16. <button formType="submit">注册</button>
  17. </form>
  18. <view class="login">
  19. <text>已有账号?</text>
  20. <text class="login-btn" bindtap="goToLogin">登录</text>
  21. </view>
  22. </view>
  23. <!-- index.js -->
  24. Page({
  25. onSubmit: function (e) {
  26. const { username, password, confirmPassword } = e.detail.value;
  27. if (!username || !password || !confirmPassword) {
  28. wx.showToast({
  29. title: "请填写完整信息",
  30. icon: "none",
  31. });
  32. return;
  33. }
  34. if (password !== confirmPassword) {
  35. wx.showToast({
  36. title: "两次密码不一致",
  37. icon: "none",
  38. });
  39. return;
  40. }
  41. // 发送注册请求,注册成功后跳转到登录页
  42. wx.request({
  43. url: "注册接口地址",
  44. method: "POST",
  45. data: {
  46. username,
  47. password,
  48. },
  49. success: (res) => {
  50. wx.showToast({
  51. title: "注册成功",
  52. icon: "success",
  53. });
  54. wx.navigateTo({
  55. url: "/pages/login/login",
  56. });
  57. },
  58. fail: (err) => {
  59. wx.showToast({
  60. title: "注册失败",
  61. icon: "none",
  62. });
  63. },
  64. });
  65. },
  66. goToLogin: function () {
  67. wx.navigateTo({
  68. url: "/pages/login/login",
  69. });
  70. },
  71. });
  72. // login.js
  73. Page({
  74. onSubmit: function (e) {
  75. const { username, password } = e.detail.value;
  76. if (!username || !password) {
  77. wx.showToast({
  78. title: "请填写完整信息",
  79. icon: "none",
  80. });
  81. return;
  82. }
  83. // 发送登录请求,登录成功后跳转到首页
  84. wx.request({
  85. url: "登录接口地址",
  86. method: "POST",
  87. data: {
  88. username,
  89. password,
  90. },
  91. success: (res) => {
  92. wx.showToast({
  93. title: "登录成功",
  94. icon: "success",
  95. });
  96. wx.switchTab({
  97. url: "/pages/index/index",
  98. });
  99. },
  100. fail: (err) => {
  101. wx.showToast({
  102. title: "登录失败",
  103. icon: "none",
  104. });
  105. },
  106. });
  107. },
  108. });

上面代码中,index.wxml 文件定义了注册页面的视图,包括用户名、密码、确认密码的输入框以及提交按钮和一个跳转到登录页面的链接。index.js 文件定义了注册页面的逻辑,当用户点击提交按钮时会发送注册请求,并根据请求的返回结果进行提示和页面跳转。login.js 文件类似地定义了登录页面的逻辑。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/466683
推荐阅读
相关标签
  

闽ICP备14008679号