//index.js //获取应用实例 const app = getApp() Page({ data: { start_date: '', end_date: '', periodArr: [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45], menstrualArr: [3, 4, 5, 6, 7, 8], periodIndex: 8, index: 3, hiddenPicker: false, switchFlag: false, hasUserInfo: true }, onLoad: function (e) { let that = this, today = '', choose_start = 0; const userInfo = wx.getStorageSync('userInfo') || null; if (userInfo) { that.setData({hasUserInfo: true}) }else{ that.setData({hasUserInfo: false}) } if (e.source) { that.setData({source: e.source}); } today = app.getFormat(new Date(), 'yyyy/MM/dd'); choose_start = app.getFormat(new Date((app.getTimestamp(app.getFormat(new Date(), 'yyyy/MM/dd')) - 44 * app.globalData.TIME_TURN) *1000), 'yyyy/MM/dd'); that.getToday(); that.setData({today: app.toWechatFormat(today), choose_start: app.toWechatFormat(choose_start)}) }, getToday () { let that = this, date = new Date(); let today = app.getFormat(date, 'yyyy/MM/dd'); that.setData({start_date: app.toWechatFormat(today), end_date: app.toWechatFormat(today)}) }, isEnd () { const that = this; let switchFlag = that.data.switchFlag; console.log(switchFlag); this.setData({switchFlag: !switchFlag}) }, bindStartDateChange: function (e) { this.setData({ start_date: e.detail.value }) }, bindEndDateChange: function (e) { this.setData({ end_date: e.detail.value }) }, bindPeriodChange (e) { this.setData({ periodIndex: e.detail.value }) }, bindMenstrualChange (e) { this.setData({ index: e.detail.value }) }, finishSetting () { const that = this, hasEnd = this.data.switchFlag; let menstruate_time_start = app.getTimestamp(app.toIOSFormat(that.data.start_date)), menstruate_time_end = null, menstruate_period = that.data.periodArr[that.data.periodIndex], menstruate_days = that.data.menstrualArr[that.data.index], range_days = (app.getTimestamp(app.getFormat(new Date(), 'yyyy/MM/dd')) - menstruate_time_start) / app.globalData.TIME_TURN + 1, period_cal = 0; menstruate_time_end = hasEnd ? app.getTimestamp(app.toIOSFormat(that.data.end_date)) : null; if (menstruate_time_end) { period_cal = (menstruate_time_end - menstruate_time_start) / app.globalData.TIME_TURN; console.log(period_cal); } if (hasEnd && menstruate_time_start >= menstruate_time_end) { app.confirmModal('结束时间应该大于开始时间哦', false) }else if(period_cal &&( period_cal < 3 || period_cal > 8) ){ app.confirmModal('经期时间应该在3-8天之内哦', false) }else if(!hasEnd && range_days >8 ){ app.confirmModal('开始时间距离今天超过8天要设置经期结束时间哦, 请打开按钮设置姨妈结束时间', false); }else if(menstruate_period < range_days){ app.confirmModal('您的月经周期最少应为' + range_days +'天~', false); }else{ that.ajaxSettting(menstruate_time_start, menstruate_time_end, menstruate_period, menstruate_days); } }, ajaxSettting (menstruate_time_start, menstruate_time_end, menstruate_period, menstruate_days, flag) { const info = { 'start_time': menstruate_time_start, 'end_time': menstruate_time_end, 'last_start_time': null, 'next_start_time': null, 'menstruate_period': menstruate_period, 'menstruate_days': menstruate_days }, that = this; // wx.setStorageSync('info', JSON.stringify(info)); wx.setStorageSync('latest_info', JSON.stringify(info)); wx.request({ url: app.globalData.HOST + '/api/mini_user/setUserSetting', method: 'POST', header: { "token": app.globalData.token }, data: { info: info, flag: 1 }, success: function(res) { if (res.data.errno == 0) { app.getMonthPeriod(new Date().getFullYear()) }else if(res.data.errno == 401){ app.login(that) } } }); // console.log(wx.getStorageSync('latest_info')); app.confirmModal('恭喜你设置完成啦', false, app.getToIndex) }, formSubmit(e) { let that = this, formId = e.detail.formId; app.getFromid(formId); }, getUserInfo: function(e) { const that = this, userInfo = {}; userInfo.encryptedData = e.detail.encryptedData; userInfo.iv = e.detail.iv; userInfo.avatar_url = e.detail.userInfo.avatarUrl; userInfo.nick_name = e.detail.userInfo.nickName; app.globalData.userInfo = userInfo; wx.setStorageSync('userInfo', userInfo); app.login(that, ()=>{ that.setData({hasUserInfo: true}); }); } })