前端样式
.wxml文件
<view class="body">
<form bindsubmit="formSubmit">
<view class="password">
<view class="one">手机号码</view>
<view class="two">{
{
mobile}}</view>
<view class="send" bindtap="sendMsg" wx:if="{
{send}}">发送</view>
<view class="count_down" wx:else>{
{
second+'s'}}</view>
</view>
<view class="password">
<view class="one">短信验证</view>
<input type="number" name='code' class="two" placeholder="短信验证码" maxlength="6" value="" />
</view>
<view class="password">
<view class="one">新密码</view>
<input type="number" name="newPassword" class="two" password="{
{true}}" placeholder="必须是6位数字" maxlength="6" value="{
{password.newPassword}}" />
</view>
<view class="password">
<view class="one">确认密码</view>
<input type="number" name="cfmPassword" class="two" password="{
{true}}" placeholder="必须是6位数字" maxlength="6" value="{
{password.cfmPassword}}" />
</view>
<button class="submit" disabled="{
{!send}}" form-type="submit">确定</button>
</form>
</view>
.js文件
const urlList = require('../../../utils/api.js');
const hash = require('../../../utils/md5.js');
const app = getApp();
Page({
/**
* 页面的初始数据
*/
data: {
mobile:'',
send:true,//是否发送,还是显示倒计时。true:发送
second:60,
url:'',
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (e) {
var that = this;
if (e.path){
that.setData({
url: e.path
})
}
that.setData({
mobile: app.globalData.userInfoData.mobile
})
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
//发送验证码
sendMsg:function(){
var that = this;
wx.request({
url: urlList.sendCode,
method: "GET",
header: {
sign: app.globalData.authKey,
},
success: function (res) {
if (res.data.code == 1) {
wx.showToast({
title: res.data.msg,
icon: 'success',
duration: 2000
});
}else{
wx.showToast({
title: res.data.msg,
image: '/images/error.png',
duration: 1000
});
wx.navigateBack({
url: '/' + that.data.url,
})
return false;
}
}
});
that.setData({
send: false,
})
that.timer();
},
formSubmit:function(e){
var that = this;
var code = e.detail.value.code;
var newPassword = e.detail.value.newPassword;
var cfmPassword = e.detail.value.cfmPassword;
var patten = /^[0-9]{6}$/;
if (newPassword != cfmPassword) {
wx.showToast({
title: '密码不一致!',
image: '/images/error.png',
duration: 1000
});
return false;
}
var patten = /^[0-9]{6}$/;
if (!patten.test(newPassword)) {
wx.showToast({
title: '必须6位数字',
image: '/images/error.png',
duration: 1000
});
return;
}
if (!patten.test(code)) {
wx.showToast({
title: '验证码错误',
image: '/images/error.png',
duration: 1000
});
return;
}
var param = {
code:code,
payPassword: hash.hexMD5(newPassword),
}
wx.request({
url: urlList.findPwd,
data: param,
method: "POST",
header: {
sign: app.globalData.authKey,
},
success: function (res) {
if (res.data.code == 1) {
wx.showToast({
title: res.data.msg,
icon: 'success',
duration: 2000
});
setTimeout(function () {
wx.navigateBack({
url: '/' + that.data.url,
})
}, 1000);
} else {
wx.showToast({
title: res.data.msg,
image: '/images/error.png',
duration: 1000
});
return false;
}
}
});
},
//倒计时
timer: function () {
let promise = new Promise((resolve, reject) => {
let setTimer = setInterval(
() => {
this.setData({
second: this.data.second - 1
})
if (this.data.second <= 0) {
this.setData({
second: 60,
send: true
})
resolve(setTimer)
}
}
, 1000)
})
promise.then((setTimer) => {
clearInterval(setTimer)
})
},
})
涉及到的md5.js文件(我已经压缩过了,直接复制粘贴就行)
function hex_md5(a