var util = require("../../utils/util")
|
var token1 = getApp().globalData.token;
|
var req_url = getApp().globalData.base_url;
|
import Notify from '../../miniprogram_npm/@vant/weapp/notify/notify'
|
const app = getApp()
|
Page({
|
data: {
|
locak:false,
|
logoUrl: ['/pages/statis/img/login.png'],
|
phoneUrl: ['/pages/statis/img/user.png'],
|
codeUrl: ['/pages/statis/img/password.png'],
|
buttonColor: false,
|
yzms: '',
|
yzmcoon: '发送验证码',
|
checked: true
|
},
|
username: '',
|
password: '',
|
phone: '',
|
usernameBindInput(event) {
|
this.username = event.detail
|
this.inputIsEmpty()
|
},
|
passwordBindInput(event) {
|
this.password = event.detail
|
this.inputIsEmpty()
|
},
|
tap(event) {
|
|
this.setData({
|
locak:false
|
})
|
},
|
hqyzm(event) {
|
if (this.data.checked) {
|
var phone = this.phone
|
if (phone == "" || phone == null) {
|
return Notify({
|
message: '请先输入手机号后在获取验证码',
|
top: app.globalData.CustomBar,
|
type: 'warning'
|
})
|
// layer.msg('请先输入手机号后在获取验证码')
|
} else {
|
var that = this
|
var data = "phone=" + phone
|
wx.request({
|
url: req_url + "/findPhoneUser",
|
data: {
|
phone: that.phone,
|
},
|
verify: false,
|
loading: true,
|
title: '请稍后...',
|
success(data) {
|
if (data.data == "" || data.data.length == 0) {
|
return Notify({
|
message: '当前手机号不存在,请重新输入后在获取',
|
top: app.globalData.CustomBar,
|
type: 'warning'
|
})
|
} else {
|
wx.setStorageSync("username", data.data.name);
|
that.username = data.data.name;
|
//发送验证码
|
var yzm = that.generateRandomCode();
|
var yzmpd = "";
|
var data = "phone=" + phone + "&randomNumber=" + yzm;
|
wx.request({
|
url: req_url + "/sms",
|
data: {
|
phone: that.phone,
|
randomNumber: yzm,
|
},
|
verify: false,
|
loading: true,
|
title: '请稍后...',
|
success(data) {
|
console.log(data);
|
console.log(data.data);
|
that.setData({
|
yzms: data.data
|
})
|
},
|
fail: (error) => {
|
console.error("QwQ请求失败:", error);
|
wx.showToast({
|
title: '请求失败',
|
icon: 'none',
|
duration: 2000
|
});
|
}
|
});
|
that.startCountdown(60)
|
}
|
}
|
})
|
}
|
}
|
},
|
startCountdown: function (seconds) {
|
var count = seconds;
|
this.setData({
|
checked: false,
|
yzmcoon: count + ' 秒后重新获取'
|
})
|
var that = this
|
var timer = setInterval(function () {
|
count--;
|
if (count <= 0) {
|
clearInterval(timer);
|
that.setData({
|
checked: true,
|
yzmcoon: ' 获取验证码'
|
})
|
} else {
|
that.setData({
|
checked: false,
|
yzmcoon: count + ' 秒后重新获取'
|
})
|
}
|
}, 1000);
|
},
|
generateRandomCode: function () {
|
var code = "";
|
var possible = "0123456789"; // 可选的字符集合
|
for (var i = 0; i < 4; i++) {
|
// 从字符集合中随机选择一个字符,并添加到验证码中
|
code += possible.charAt(Math.floor(Math.random() * possible.length));
|
}
|
return code;
|
},
|
inputIsEmpty() {
|
let buttonColor = false
|
if (this.username && this.password)
|
buttonColor = true
|
if (buttonColor !== this.data.buttonColor)
|
this.setData({
|
'buttonColor': buttonColor
|
})
|
},
|
login(event) {
|
Notify.clear()
|
if (!this.username)
|
return Notify({
|
message: '用户名不能为空',
|
top: app.globalData.CustomBar,
|
type: 'warning'
|
})
|
if (!this.password)
|
return Notify({
|
message: '验证码不能为空',
|
top: app.globalData.CustomBar,
|
type: 'warning'
|
})
|
// if (this.data.yzms != this.password)
|
// return Notify({
|
// message: '验证码有误',
|
// top: app.globalData.CustomBar,
|
// type: 'warning'
|
// })
|
let _this = this
|
wx.login({
|
success: (res) => {
|
wx.request({
|
url: req_url + "/wxlogin",
|
|
header: {
|
'content-type': 'application/json;charset=UTF-8'
|
},
|
method: 'POST',
|
data: {
|
username: this.username,
|
password: this.password,
|
code: res.code
|
},
|
success: (res) => {
|
console.log(res);
|
if (res.data.code == 0) {
|
// _this.token1 = res.data.data
|
wx.setStorageSync("username", res.data.data.name);
|
wx.setStorageSync("phone", res.data.data.phone);
|
wx.setStorageSync("token", res.data.data.token);
|
wx.setStorageSync("password", res.data.data.password);
|
wx.setStorage({
|
key: 'token',
|
data: res.data.data.token
|
})
|
wx.redirectTo({
|
url: `/pages/index/index?message=${'登陆成功'}`
|
})
|
|
} else {
|
wx.showToast({
|
title: res.data.msg,
|
icon: 'none',
|
duration: 2000
|
});
|
}
|
|
},
|
fail: (error) => {
|
console.error("QwQ请求失败:", error);
|
wx.showToast({
|
title: '请求失败',
|
icon: 'none',
|
duration: 2000
|
});
|
}
|
})
|
}
|
})
|
},
|
onChange(event) { //复选框onchange事件
|
this.setData({
|
checked: event.detail,
|
});
|
},
|
onLoad(options) {
|
console.log(options);
|
this.setData({
|
locak:options
|
})
|
// debugger
|
if (wx.getStorageSync('user')) {
|
setTimeout(item => {
|
if (this.checked) {
|
this.username = wx.getStorageSync('username');
|
this.password = wx.getStorageSync('password');
|
this.login();
|
}
|
}, 500)
|
}
|
}
|
})
|