var Api = require('../../../utils/common.js'); var re = require('../../../utils/request.js'); function Detail(nickname, realname, phone, idcard, gender) { this.nickname = nickname; //昵称 this.realname = realname; // this.phone = phone; this.idcard = idcard; this.gender = gender || 1; } function Info() { this.details = [new Detail()]; } Page({ /** * 页面的初始数据 */ data: { Agreement:'http://api.tuleduo.cn/privacy_agreement.html', genderdata: '', joiner: [{ clubId: '', activityId: '', cost: 0, nickname: '', realname: '', phone: '', idcard: '', isTeam: 0, gender: '1' }], price: '', index: 1, activityId: '', clubId: '', isHava: false, info: {}, checked: false }, history: function () { wx.navigateTo({ url: '../history/history' }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { var that = this; //console.log(options); that.setData({ clubId: options.clubId, activityId: options.acId, price: options.price }) that.init(); }, xieyi(e){ this.setData({ checked: e.detail.value.length > 0 }); }, init: function () { let that = this; this.setData({ info: new Info(), }); }, seexcieyi:function(e){ var webview= e.target.dataset.webview wx.navigateTo({ url: '/pages/webview/webview?webview='+webview, }) }, deteleItem : function(e){ var that = this; let info = this.data.info; //detele Detail() console.log(new Detail()); wx:if(info.details.length>1){ info.details.pop(new Detail()); this.setData({ info: info, }); if(info.details.length==1){ } } console.log(info.details) }, addItem: function (e) { var that = this; let info = this.data.info; // new Detail() console.log(new Detail()); info.details.push(new Detail()); this.setData({ info: info, }); console.log(info.details) }, removeItem: function (e) { var that = this; let info = this.data.info; info.details.pop(); this.setData({ info: info, }); }, // 通过id属性来判断操作的是哪个Detail类 nickInput: function (e) { let index = parseInt(e.currentTarget.id.replace("nick-", "")); let nick = e.detail.value; let info = this.data.info; info.details[index].nickname = nick; this.setData({ info: info }); }, telInput: function (e) { let index = parseInt(e.currentTarget.id.replace("tel-", "")); let tel = e.detail.value; let info = this.data.info; info.details[index].phone = tel; this.setData({ info: info }); }, nameInput: function (e) { let index = parseInt(e.currentTarget.id.replace("name-", "")); let name = e.detail.value; let info = this.data.info; info.details[index].realname = name; this.setData({ info: info }); }, genderChange: function (e) { let index = parseInt(e.currentTarget.id.replace("gender-", "")); let gender = e.detail.value; let info = this.data.info; info.details[index].gender = gender; this.setData({ info: info }); }, IdInput: function (e) { let index = parseInt(e.currentTarget.id.replace("id-", "")); let id = e.detail.value; let info = this.data.info; info.details[index].idcard = id; this.setData({ info: info }); }, //shangchuan toOrder: function () { console.log(this.genderdata); var that = this; // if(this.data.info.details) console.log(this.data.info.details); let isfl=false, phoneCardErr = false; if(this.data.info.details.length==0){ wx.showToast({ title: '至少添加一个用户', icon:'error' }) return } try { this.data.info.details.forEach((item, index) => { if(!/^1[3-9][0-9]{9}$/.test(item.phone)){ let err = `第${index + 1}个用户手机号填写有误`; wx.showToast({ title: err, icon:'none' }); phoneCardErr=true throw Error(err); return; } if(!/^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(item.idcard)){ let err = `第${index + 1}个用户身份证号码填写有误`; wx.showToast({ title: err, icon:'none' }); phoneCardErr=true throw Error(err); return; } if(item.gender==undefined||item.idcard==undefined||item.nickname==undefined||item.phone==undefined||item.realname==undefined||item.gender==''||item.nickname==''||item.realname==''){ isfl=true } }); } catch (error) {} if(phoneCardErr) return; if(isfl){ wx.showToast({ title: '请确保表单全部填写完成', icon:'none' }) return; } if(!this.data.checked){ wx.showToast({ title: '请勾选协议', icon:'none' }); return; } let postData = that.data.info.details; // 本地存储用于 if (!wx.getStorageSync('stroe')) { let stroe = postData wx.setStorageSync('stroe', stroe) } else { let stroe = wx.getStorageSync('stroe') console.log(stroe); stroe.splice(0,0,...postData) let obj = {} stroe = stroe.reduce((arr, item) => { obj[item.idcard] ? "" : obj[item.idcard] = true && arr.push(item) return arr }, []) if(stroe.length==11){ stroe.splice(stroe.length-1,1) } wx.setStorageSync('stroe', stroe) } // return for (let i = 0; i < postData.length; i++) { postData[i].clubId = that.data.clubId; postData[i].activityId = that.data.activityId; postData[i].cost = that.data.price; } postData = JSON.stringify(postData); console.log(postData); let lastData = { req: postData }; var header = { token: wx.getStorageSync('token'), "Content-Type": "application/x-www-form-urlencoded" }; if (!that.data.checked) { wx.showToast({ title: '请阅读并同意协议', icon: 'none', duration: 1000 }) } else { re.request(Api.toJoinactive(), lastData, header).then((res) => { console.log(res,'res'); wx.navigateTo({ url: '../order/order?id=' + res.data.msg + '' }) }) } }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function () { }, /** * 生命周期函数--监听页面显示 */ onShow: function () { // 判断本地是否有item 有的话就往里面加入 if (wx.getStorageSync('item')) { let items = wx.getStorageSync('item') let info = this.data.info console.log(info.details); let bl = info.details.some((item, index) => { return item.idcard == items.idcard }) console.log(bl); if (!bl) { info.details.push(items) this.setData({ info, }) wx.removeStorageSync('item') } else { wx.removeStorageSync('item') } } // let item=wx.getStorageSync('item') }, /** * 生命周期函数--监听页面隐藏 */ onHide: function () { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function () { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })