// pages/register/register.js import WxValidate from '../../src/WxValidate.js' var utils = require("../../utils/util.js"); var app = getApp(); var inpTelNum = ""; //记录输入的手机号码 Page({ /** * 页面的初始数据 */ data: { hasTime: false, //是否倒计时状态 showTopTips: false, //是否显示顶部提示 isAgree: 0, //是否同意隐私 radio: [ { name: '先生/男士/帅哥', value: '1', checked: true, }, { name: '小姐/女士/美女', value: '2', }, ], }, radioChange: function (e) { var radio = this.data.radio; for (var i in radio) { radio[i].checked = radio[i].value == e.detail.value; } this.setData({ // sex: e.detail.value, radio: radio }) }, countDownTap: function () { this.countDown(); }, // 倒计时 countDown: function () { var that = this; // 两位数补全 function toTwo(num) { return num > 9 ? num : "0" + num; }; // 如果还在倒计时中 点击无效 if (this.data.hasTime) { return false; } // 先检查手机号是否正确 if (!/^(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(inpTelNum)) { utils.showTopTips(this, "请输入正确的手机号") return false } wx.request({ url: utils.getUrl( '/shopping/verificationcodes.do?m=b'), header: { "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID') }, data: { telephone: inpTelNum }, success: res => { if(!utils.requestError(res)){ return false; } }, fail: function (errmsg) { utils.requestFail(errmsg,'/shopping/verificationcodes.do'); } }) // 倒计时,默认60S var num = 60; var timer = setInterval(function () { num--; that.setData({ countDown: toTwo(num), hasTime: true }) if (num <= 0) { clearInterval(timer); that.setData({ hasTime: false, }) } }, 1000) }, // 手机号输入 inputTel: function (e) { inpTelNum = e.detail.value; }, // 初始化验证信息 initValidate() { this.WxValidate = new WxValidate( // 验证通过时 // 值的命名需要与form表单内的各个name名相同,value值就是name内的值 { nickName: { required: true, //必填 nickName: true, //自定义验证条件 }, telNum: { required: true, tel: true, }, codeNum: { required: true, codeNum: true, } }, // 验证不通过时 { nickName: { required: '请输入姓名或昵称' }, telNum: { required: '请输入手机号', tel: '请输入正确的手机号', }, codeNum: { required: '请输入验证码' } }) // 自定义验证方法 this.WxValidate.addMethod('nickName', (value, param) => { // 不能为空 输入的值不能带有空格 // console.log(this.WxValidate.optional(value)) return this.WxValidate.optional(value) || !(/\s+/.test(value)) }, '请输入正确的姓名或昵称') }, // 同意按钮 bindAgreeChange: function (e) { // console.log(e) this.setData({ isAgree: e.detail.value.length }); }, // 保存提交 submitForm: function (e) { // this.tips(e.detail.value); console.log('form发生了submit事件,携带数据为:', e.detail.value) console.log(this.data.isAgree) if (!this.WxValidate.checkForm(e)) { var error = this.WxValidate.errorList[0] console.log(error) utils.showTopTips(this,error.msg) return false } wx.request({ url: utils.getUrl('/shopping/account.do?m=new®=1'), header: { "Cookie": "JSESSIONID=" + wx.getStorageSync('sessionID') }, data: { cltname: e.detail.value.nickName, gender: e.detail.value.sex, telephone: e.detail.value.telNum, code: e.detail.value.codeNum, address: e.detail.value.address, agree: this.data.isAgree }, success: res => { if(!utils.requestError(res)){ return false; } console.log(res) if (res.data.status && res.data.status == 'success') { console.log(res.data.status) wx.showModal({ content: '注册成功', showCancel: false, success: function (res) { if (res.confirm) { wx.navigateBack() } } }) } }, fail: function (errmsg) { utils.requestFail(errmsg,'/shopping/account.do'); } }) }, goLogin() { wx.redirectTo({ url: '../login/login', }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.initValidate() }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ // onShareAppMessage: function () { // } })