register.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const utils_api = require("../../utils/api.js");
  4. const _sfc_main = {
  5. data() {
  6. return {
  7. formData: {
  8. username: "",
  9. nickname: "",
  10. password: "",
  11. confirmPassword: "",
  12. phone: "",
  13. email: ""
  14. },
  15. showPassword: false,
  16. showConfirmPassword: false,
  17. isLoading: false
  18. };
  19. },
  20. computed: {
  21. canRegister() {
  22. return this.formData.username.trim().length > 0 && this.formData.password.length >= 6 && this.formData.password === this.formData.confirmPassword && !this.isLoading;
  23. }
  24. },
  25. methods: {
  26. togglePassword() {
  27. this.showPassword = !this.showPassword;
  28. },
  29. toggleConfirmPassword() {
  30. this.showConfirmPassword = !this.showConfirmPassword;
  31. },
  32. validateForm() {
  33. if (!this.formData.username || this.formData.username.trim().length === 0) {
  34. common_vendor.index.showToast({
  35. title: "请输入用户名",
  36. icon: "none"
  37. });
  38. return false;
  39. }
  40. if (!this.formData.password || this.formData.password.length < 6) {
  41. common_vendor.index.showToast({
  42. title: "密码长度不能少于6位",
  43. icon: "none"
  44. });
  45. return false;
  46. }
  47. if (this.formData.password.length > 20) {
  48. common_vendor.index.showToast({
  49. title: "密码长度不能超过20位",
  50. icon: "none"
  51. });
  52. return false;
  53. }
  54. if (this.formData.password !== this.formData.confirmPassword) {
  55. common_vendor.index.showToast({
  56. title: "两次输入的密码不一致",
  57. icon: "none"
  58. });
  59. return false;
  60. }
  61. if (this.formData.phone && this.formData.phone.trim().length > 0) {
  62. const phoneReg = /^1[3-9]\d{9}$/;
  63. if (!phoneReg.test(this.formData.phone.trim())) {
  64. common_vendor.index.showToast({
  65. title: "请输入正确的手机号",
  66. icon: "none"
  67. });
  68. return false;
  69. }
  70. }
  71. if (this.formData.email && this.formData.email.trim().length > 0) {
  72. const emailReg = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  73. if (!emailReg.test(this.formData.email.trim())) {
  74. common_vendor.index.showToast({
  75. title: "请输入正确的邮箱地址",
  76. icon: "none"
  77. });
  78. return false;
  79. }
  80. }
  81. return true;
  82. },
  83. handleRegister() {
  84. if (!this.validateForm()) {
  85. return;
  86. }
  87. this.isLoading = true;
  88. common_vendor.index.showLoading({
  89. title: "注册中...",
  90. mask: true
  91. });
  92. const registerData = {
  93. username: this.formData.username.trim(),
  94. password: this.formData.password,
  95. nickname: this.formData.nickname.trim() || this.formData.username.trim(),
  96. phone: this.formData.phone.trim() || null,
  97. email: this.formData.email.trim() || null
  98. };
  99. utils_api.register(registerData).then((res) => {
  100. common_vendor.index.hideLoading();
  101. this.isLoading = false;
  102. if (res.code === 200 && res.data) {
  103. common_vendor.index.showToast({
  104. title: "注册成功",
  105. icon: "success"
  106. });
  107. setTimeout(() => {
  108. common_vendor.index.redirectTo({
  109. url: `/pages/login/login?username=${encodeURIComponent(registerData.username)}`
  110. });
  111. }, 1500);
  112. } else {
  113. common_vendor.index.showToast({
  114. title: res.message || "注册失败",
  115. icon: "none",
  116. duration: 2e3
  117. });
  118. }
  119. }).catch((err) => {
  120. common_vendor.index.hideLoading();
  121. this.isLoading = false;
  122. common_vendor.index.__f__("error", "at pages/register/register.vue:261", "注册失败:", err);
  123. common_vendor.index.showToast({
  124. title: err.message || "注册失败,请检查网络连接",
  125. icon: "none",
  126. duration: 2e3
  127. });
  128. });
  129. },
  130. goToLogin() {
  131. common_vendor.index.navigateBack();
  132. }
  133. }
  134. };
  135. function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
  136. return {
  137. a: $data.formData.username,
  138. b: common_vendor.o(($event) => $data.formData.username = $event.detail.value),
  139. c: $data.formData.nickname,
  140. d: common_vendor.o(($event) => $data.formData.nickname = $event.detail.value),
  141. e: $data.showPassword ? "text" : "password",
  142. f: $data.formData.password,
  143. g: common_vendor.o(($event) => $data.formData.password = $event.detail.value),
  144. h: common_vendor.t($data.showPassword ? "隐藏" : "显示"),
  145. i: common_vendor.o((...args) => $options.togglePassword && $options.togglePassword(...args)),
  146. j: $data.showConfirmPassword ? "text" : "password",
  147. k: $data.formData.confirmPassword,
  148. l: common_vendor.o(($event) => $data.formData.confirmPassword = $event.detail.value),
  149. m: common_vendor.t($data.showConfirmPassword ? "隐藏" : "显示"),
  150. n: common_vendor.o((...args) => $options.toggleConfirmPassword && $options.toggleConfirmPassword(...args)),
  151. o: $data.formData.phone,
  152. p: common_vendor.o(($event) => $data.formData.phone = $event.detail.value),
  153. q: $data.formData.email,
  154. r: common_vendor.o(($event) => $data.formData.email = $event.detail.value),
  155. s: common_vendor.t($data.isLoading ? "注册中..." : "注册"),
  156. t: common_vendor.o((...args) => $options.handleRegister && $options.handleRegister(...args)),
  157. v: !$options.canRegister,
  158. w: common_vendor.o((...args) => $options.goToLogin && $options.goToLogin(...args))
  159. };
  160. }
  161. const MiniProgramPage = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["render", _sfc_render], ["__scopeId", "data-v-bac4a35d"]]);
  162. wx.createPage(MiniProgramPage);
  163. //# sourceMappingURL=../../../.sourcemap/mp-weixin/pages/register/register.js.map