logins.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { models, db } from '../../utils/cloudbase.js'
  2. Page({
  3. data: {
  4. phone: '', // 用户输入的手机号码
  5. code: '' // 用户输入的验证码
  6. },
  7. // 监听手机号码输入
  8. onInputPhone(e) {
  9. this.setData({
  10. phone: e.detail.value
  11. });
  12. },
  13. // 监听验证码输入
  14. onInputCode(e) {
  15. this.setData({
  16. code: e.detail.value
  17. });
  18. },
  19. // 获取验证码
  20. getCode() {
  21. const { phone } = this.data;
  22. if (!phone || !/^1[3-9]\d{9}$/.test(phone)) {
  23. wx.showToast({
  24. title: '请输入有效的手机号码',
  25. icon: 'none'
  26. });
  27. return;
  28. }
  29. const code = this.generateCode(); // 本地生成验证码(用于发送)
  30. // 保存验证码到 data
  31. this.setData({ code });
  32. console.log(code, 'codecode');
  33. },
  34. generateCode() {
  35. return Math.floor(100000 + Math.random() * 900000).toString();
  36. },
  37. // 确定按钮点击事件
  38. async onConfirm() {
  39. const { phone, code } = this.data;
  40. if (!phone || !code) {
  41. wx.showToast({
  42. title: '请输入手机号码和验证码',
  43. icon: 'none'
  44. });
  45. return;
  46. }
  47. const { data } = await models.wx_teacher_user.get({
  48. filter: {
  49. where: {
  50. phone: this.data.phone
  51. }
  52. },
  53. // envType: pre 体验环境, prod 正式环境
  54. envType: "prod",
  55. });
  56. console.log('data',data);
  57. if (!data || Object.keys(data).length === 0) {
  58. wx.showToast({
  59. title: '该手机号未注册,请换其他手机号进行登录',
  60. icon: 'none'
  61. });
  62. return;
  63. }
  64. const user = data; // 取第一条用户数据
  65. if (user.bound === 0) {
  66. // 第一次登录去绑定
  67. wx.showToast({
  68. title: '首次登录,请先绑定信息',
  69. icon: 'none',
  70. duration: 1500
  71. });
  72. setTimeout(() => {
  73. wx.navigateTo({
  74. url: `/pages/login/login?phone=${encodeURIComponent(this.data.phone)}`
  75. });
  76. }, 1500); // 等提示展示完再跳转
  77. } else {
  78. // 登录成功,存储用户信息
  79. wx.setStorageSync('userInfo', user);
  80. wx.showToast({
  81. title: '登录成功',
  82. icon: 'success'
  83. });
  84. wx.switchTab({
  85. url: '/pages/index/index'
  86. });
  87. }
  88. }
  89. })