| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- import { models, db } from '../../utils/cloudbase.js'
- import Dialog from '@vant/weapp/dialog/dialog';
- Page({
- data: {
- phone: '', // 用户输入的手机号码
- name: '',
- yuan: '',
- falog: false,
- },
- // 监听手机号码输入
- onInputPhone(e) {
- this.setData({
- phone: e.detail.value
- });
- },
- onInputName(e) {
- this.setData({
- name: e.detail.value
- });
- },
- onInputYuan(e) {
- this.setData({
- yuan: e.detail.value
- });
- },
- // 确定按钮点击事件
- async onConfirm() {
- const { phone } = this.data;
- if (!phone) {
- wx.showToast({
- title: '请输入手机号码',
- icon: 'none'
- });
- return;
- }
- if (!/^1[3-9]\d{9}$/.test(phone)) {
- wx.showToast({
- title: '输入的手机号码有误',
- icon: 'none'
- });
- return;
- }
- const childComponent = this.selectComponent('#loginCaptcha');
- const result = childComponent.validCaptcha()
- if (!result.flag) {
- wx.showToast({
- title: result.msg,
- icon: 'none'
- });
- return
- }
- const { data } = await models.wx_teacher_user.list({
- filter: {
- where: {
- phone: this.data.phone
- }
- },
- pageSize: 100, // 分页大小,建议指定,如需设置为其它值,需要和 pageNumber 配合使用,两者同时指定才会生效
- pageNumber: 1, // 第几页
- getCount: true, // 开启用来获取总数
- // envType: pre 体验环境, prod 正式环境
- envType: "prod",
- });
- let users = data.records || [];
- // 如果查不到任何数据
- if (users.length === 0) {
- wx.showToast({
- title: '未查询该用户,请换其他手机号登录',
- icon: 'none'
- });
- return;
- }
- // ✅ 如果查到了,但全部都是 delete === 1,则视为无有效账号
- const allDeleted = users.every(user => user.delete === 1);
- if (allDeleted) {
- wx.showToast({
- title: '该手机号未注册,请换其他手机号登录',
- icon: 'none'
- });
- return;
- }
- // ✅ 只要有一个 delete === 0,就继续登录
- const validUser = users.find(user => user.delete === 0);
-
- if (validUser.bound === 0) {
- console.log(users[0].school_id, 'users._idusers._idusers._id');
- const { datass } = await models.wx_teacher_user.update({
- data: {
- bound: 1, // 是否绑定
- },
- filter: {
- where: {
- $and: [
- {
- _id: {
- $eq: users[0]._id, // 推荐传入_id数据标识进行操作
- },
- },
- ]
- }
- },
- envType: "prod",
- });
-
- const schoolRes = await models.wx_school.get({
- filter: {
- where: {
- _id: users[0].school_id
- }
- },
- envType: "prod",
- });
-
- console.log(schoolRes, 'schoolRes');
- const school = schoolRes.data; // 这里才是你要的园所信息
- this.setData({
- name: users[0].name,
- yuan: school.name,
- falog: true,
- })
- // onConfirm 成功时
- wx.setStorageSync('userInfo', validUser);
- // Dialog.confirm({
- // title: '用户信息',
- // message: `姓名:${validUser.name}\n园所:${school.name || '未知园所'}`,
- // showCancelButton: false,
- // }).then(() => {
- // wx.setStorageSync('userInfo', {
- // ...validUser,
- // bound: 1
- // });
- // wx.switchTab({
- // url: '/pages/index/index'
- // });
- // });
-
- // setTimeout(() => {
- // wx.navigateTo({
- // url: `/pages/login/login?phone=${encodeURIComponent(this.data.phone)}`
- // });
- // }, 1500); // 等提示展示完再跳转
- } else {
- // 登录成功,存储用户信息
- wx.setStorageSync('userInfo', validUser);
-
- wx.showToast({
- title: '登录成功',
- icon: 'success'
- });
-
- wx.switchTab({
- url: '/pages/index/index'
- });
- }
- },
- gotuhome() {
- const validUser = wx.getStorageSync('userInfo');
- if (!validUser) {
- wx.showToast({
- title: '用户信息丢失,请重新登录',
- icon: 'none'
- });
- return;
- }
- wx.showToast({
- title: '登录成功',
- icon: 'success'
- });
- wx.switchTab({
- url: '/pages/index/index'
- });
- }
- })
|