zhongping-miniprogram/miniprogram/pages/login/login.ts

226 lines
4.6 KiB
TypeScript

// pages/login/login.ts
// @ts-ignore
import RSA from 'wxmp-rsa'
import { sendSms, userInfo, } from '../../apis/login'
import { bindPhone, bindPhoneList } from "../../apis/consumer";
const app = getApp<IAppOption>()
Page({
/**
* 页面的初始数据
*/
data: {
// username: '13408085719',
phone: '',
code: '',
agree: false,
publicKey: 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDP1WOya5Ut9LdR6sRdaDzFjXNH3UIv5m8sJAXqow+BOOGbSsDWTZgTdbGv7JzRo5YZVMC0wg+UwNH7zD3UVKJqtaHPUavh4+j/UIzD615X0gFDbVUCBusxCVNj68L4iXP/dqRZfqD6s72f4YPwhHsHvHLdQkGYBL9ls3/Ds9rXEQIDAQAB',
sending: false,
sendNum: 60,
sendTimer: 0,
isChecked: false
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
console.log('optionsLogin', options, options.source);
if (options.source) {
this.setData({
source: options.source
})
}
wx.removeStorageSync('QSource')
},
send() {
console.log('send');
if (!this.data.isChecked) {
wx.showToast({
icon: 'none',
title: '请先阅读并勾选隐私政策和用户协议'
})
return
}
if (this.data.sending) return
// const rsa = new RSA()
// rsa.setPublicKey(this.data.publicKey)
const { phone } = this.data
if (!phone) {
wx.showToast({
icon: 'none',
title: '请输入手机号!'
})
return
}
if (!/^1[3|4|5|7|8][0-9]{9}$/.test(phone)) {
wx.showToast({
icon: 'none',
title: '手机号格式错误!'
})
return
}
sendSms({
phone,
}).then(() => {
wx.showToast({
icon: 'none',
title: '验证码发送成功'
})
const timer = setInterval(() => {
const { sendNum, sendTimer } = this.data
this.setData({
sendNum: sendNum - 1
})
if (sendNum <= 0) {
clearInterval(sendTimer)
this.setData({
sending: false,
sendNum: 60,
sendTimer: 0
})
}
}, 1000)
this.setData({
sendTimer: timer,
sending: true
})
})
},
defaultLogin() {
const { phone, code } = this.data
if (!phone) {
wx.showToast({
icon: 'none',
title: '手机号不能为空'
})
return
}
if (!code) {
wx.showToast({
icon: 'none',
title: '验证码不能为空'
})
return
}
if (!this.data.isChecked) {
wx.showToast({
icon: 'none',
title: '请先阅读并勾选隐私政策和用户协议'
})
return
}
bindPhone({ phone, code }).then(res => {
if (res.c === 200) {
bindPhoneList().then(resp => {
wx.setStorageSync('accountList', resp.d)
// this.setData({
// userInfo: resp.d.find(item => item.phone === this.data.userInfo.phone)
// })
wx.setStorageSync('userInfo', resp.d.find(item => item.phone === this.data.phone))
// wx.redirectTo({
// url: `/pages/my/my?phone=${phone}`
// })
let from = wx.getStorageSync('from')
if (from.page === 'comment') {
wx.removeStorageSync('from')
wx.redirectTo({
url: `/pages/searchResult/searchResult?phone=${from.phone}`
})
} else {
wx.redirectTo({
url: `/pages/my/my`
})
}
})
}
})
},
bindAgreeChange(e: WechatMiniprogram.CustomEvent) {
const { value } = e.detail
this.setData({
agree: value
})
},
jumpPrivacy() {
wx.navigateTo({
url: "/pages/privacy/privacy"
})
},
jumpAgreement() {
wx.navigateTo({
url: "/pages/agreement/agreement"
})
},
handleChecked() {
console.log('handleChecked');
this.setData({
isChecked: !this.data.isChecked
})
},
goPrivacy() {
wx.navigateTo({
url: '/pages/privacy/privacy'
})
},
goService() {
wx.navigateTo({
url: '/pages/service/service'
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})