qingcheng-xiaochengxu/pages/logain/logain.js
2025-05-16 09:58:51 +08:00

344 lines
7.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {url} from '../../request'
Page({
data: {
show: true,
morenshow: false,
currentTab: 'code',
countdown: 60, // 设置倒计时时长(秒)
timer: null, // 存储计时器
},
startCountdown() {
this.setData({
isCountingDown: true,
countdown: 60
});
this.data.timer = setInterval(() => {
let count = this.data.countdown;
if (count <= 1) {
clearInterval(this.data.timer);
this.setData({
isCountingDown: false,
countdown: 60
});
} else {
this.setData({
countdown: count - 1
});
}
}, 1000);
},
onUnload() {
// 页面卸载时清除定时器
if (this.data.timer) {
clearInterval(this.data.timer);
}
},
// 初始加载
onShow(){
this.setData({
show: false,
morenshow: true,
currentTab: 'password'
})
},
// 账号密码登录
showchange() {
this.setData({
show: true,
morenshow: false,
currentTab: 'code'
})
},
// 注册界面
gozucepage(){
wx.navigateTo({
url: '/pages/zucepage/zucepage',
})
},
yanzhengphone(e){
this.setData({
yanzhengphone: e.detail.value
})
console.log(e.detail.value);
},
yanzhengcode(e){
this.setData({
yanzhengcode: e.detail.value
})
console.log(e.detail.value);
},
// 账号登录账号
bindKeyInput(e){
this.setData({
inputValue: e.detail.value
})
console.log(e.detail.value);
},
// 账号登录密码
bindKeyInputpassword(e){
this.setData({
inputValuepassword: e.detail.value
})
console.log(e.detail.value);
},
// 验证码登录
showchangeback() {
this.setData({
show: false,
morenshow: true,
currentTab: 'password'
})
},
// 忘记密码
wangji() {
wx.navigateTo({
url: '/pages/wangjimima/wangjimima',
})
},
// 账号密码登录提交
submit(){
const that = this
const phone = this.data.inputValue;
const password = this.data.inputValuepassword;
console.log(password,'isdjidasjiodaso');
// 手机号验证6~11位数字
if (!/^\d{6,11}$/.test(phone)) {
wx.showToast({
title: '请输入6到11位的账号',
icon: 'none'
});
return;
}
// 密码验证6~10位字符
if (!/^[a-zA-Z0-9]{6,10}$/.test(password)) {
wx.showToast({
title: '请输入6到10位密码',
icon: 'none'
});
return;
}
// 勾选校验
if (this.data.isAgree!=true) {
wx.showToast({
title: '请先勾选协议',
icon: 'none'
});
return;
}
console.log("sakldhasjdhja");
wx.request({
url: url + '/userInfo/mini/pwd/login',
method: 'POST',
data: {
phoneNumber: this.data.inputValue,
userPassword: this.data.inputValuepassword
},
header: {
'content-type': 'application/json',
},
success(res) {
console.log('登录成功', res);
if(res.data.code==1){
console.log(res.data.data,'zhehsidata');
wx.showToast({
title: '登录成功',
icon: 'success',
duration: 2000
});
try {
wx.setStorageSync('logmessage', {
Authorization: res.data.data
});
console.log("信息存储成功");
} catch (e) {
console.error("存储失败", e);
}
that.setmessage(() => {
wx.switchTab({
url: '/pages/jiedan/jiedan',
});
});
}else{
wx.showToast({
title: res.data.message||res.data.error,
icon: 'error',
duration: 2000
});
}
},
fail(err) {
console.error('请求失败', err);
}
});
},
// 勾选
onAgreeChange(e) {
this.setData({
isAgree: true
});
console.log("gaibianler",this.data.isAgree);
},
// 获取验证码
getcode(){
const phone = this.data.yanzhengphone;
if (!/^\d{11}$/.test(phone)) {
wx.showToast({
title: '请输入11位的手机号',
icon: 'none'
});
return;
}
wx.request({
url: url + '/userInfo/code',
method: 'POST',
data: {
templateString: this.data.yanzhengphone
},
header: {
'content-type': 'application/json',
},
success(res) {
console.log('发送成功', res);
if(res.data.code==1){
wx.showToast({
title: '发送成功',
icon: 'success',
duration: 2000
});
}else{
wx.showToast({
title: res.data.message,
icon: 'error',
duration: 2000
});
}
},
fail(err) {
console.error('请求失败', err);
}
});
this.startCountdown();
},
// 验证码登录提交
submityanzhengma(){
const that = this;
const code = this.data.yanzhengcode
if (!/^\d{6}$/.test(code)) {
wx.showToast({
title: '请输入6位的有效验证码',
icon: 'none'
});
return;
}
if (this.data.isAgree!=true) {
wx.showToast({
title: '请先勾选协议',
icon: 'none'
});
return;
}
console.log("sakldhasjdhja");
wx.request({
url: url + '/userInfo/mini/vcd/login',
method: 'POST',
data: {
phoneNumber: this.data.yanzhengphone,
verificationCode: this.data.yanzhengcode
},
header: {
'content-type': 'application/json',
},
success(res) {
console.log('登录成功', res);
if(res.data.code==1){
wx.showToast({
title: '登录成功',
icon: 'success',
duration: 2000
});
try {
wx.setStorageSync('logmessage', {
Authorization: res.data.data
});
console.log("信息存储成功");
} catch (e) {
console.error("存储失败", e);
}
that.setmessage(() => {
wx.switchTab({
url: '/pages/jiedan/jiedan',
});
});
}else{
wx.showToast({
title: res.data.message||res.data.error,
icon: 'error',
duration: 2000
});
}
},
fail(err) {
console.error('请求失败', err);
}
});
},
setmessage(callback) {
console.log("diaoyongle ");
const that = this;
wx.getStorage({
key: "logmessage",
success(res) {
console.log(res.data, 'sajlkdlasjdkl');
const userinfo = res.data;
console.log(userinfo,'这是获取的');
wx.request({
url: url + '/userInfo/get/jwt',
method: 'GET',
data: {},
header: {
'content-type': 'application/json',
'Authorization': userinfo.Authorization
},
success(res) {
console.log('查询成功', res);
if (res.data.code==1) {
try {
wx.setStorageSync("usermessage", {
nickName: res.data.data.nickName,
phoneNumber: res.data.data.phoneNumber,
userRole: res.data.data.userRole,
invitationCode: res.data.data.invitationCode,
userAvatar: res.data.data.userAvatar
});
console.log("信息存储成功");
if (callback) callback();
} catch (e) {
console.error("信息存储失败", e);
}
} else {
wx.showToast({
title: res.data.message,
icon: 'error',
duration: 2000
});
}
},
fail(err) {
console.error('请求失败', err);
}
});
},
fail(err) {
console.error("获取 logmessage 失败", err);
}
});
}
})