App({ globalData: { // HOST: "http://182.92.118.1:8043", HOST: "https://ymgjapi.726p.com", img: "https://ymgjapi.726p.com/icon/maicon/share.jpg", TIME_TURN: 86400, noticeLists: [] }, onLaunch: function () { const app = this, userInfo = wx.getStorageSync('userInfo'), noticeLists = wx.getStorageSync('noticeLists'), token = wx.getStorageSync('token'); if (noticeLists && token) { app.globalData.token = token; app.globalData.noticeLists = noticeLists; }else{ wx.redirectTo({url: '/pages/firstSetting/firstSetting'}) } }, onUnload () { wx.clearStorage(); }, login: function (that, func) { let obj = {}, app = this, token = '', source = ''; if (that.data.source) { source = that.data.source } wx.login({ success: function(res) { if (res.code) { wx.request({ url: app.globalData.HOST + '/api/mini_program/loginAction', method: "POST", data: { code: res.code, iv: app.globalData.userInfo.iv, encryptedData: app.globalData.userInfo.encryptedData, source: source }, success: function (resLogin) { if (resLogin.data.errno == 0) { token = resLogin.data.rst.token; app.globalData.token = token; app.globalData.noticeLists = resLogin.data.rst.data; wx.setStorage({ key: "token", data: token }) wx.setStorage({ key: "noticeLists", data: resLogin.data.rst.data }) // wx.setStorageSync('token', token); // wx.setStorageSync('noticeLists', resLogin.data.rst.data); app.getMonthPeriod(new Date().getFullYear()); if (func) { func(); } } } }) } else { console.log('登录失败!' + res.errMsg) } } }); }, confirmModal (content, flag, func, funcc) { wx.showModal({ title: '小提示', content: content, showCancel: flag, success: function(res) { if (res.confirm) { if (func) { func(); } } else if (res.cancel) { } } }) }, // 公共函数 getTimestamp (str) { if (str) { return Date.parse(new Date(str)) / 1000; }else{ return Date.parse(new Date()) / 1000; } }, // 2018-02-19 => 2018/02/19 toIOSFormat (time) { return time.replace(/\-/g, "/"); }, toWechatFormat (time) { return time.replace(/\//g, "-"); }, getFormat (time, fmt) { var o = { "M+": time.getMonth() + 1, "d+": time.getDate(), "h+": time.getHours(), "m+": time.getMinutes(), "s+": time.getSeconds(), "q+": Math.floor((time.getMonth() + 3) / 3), "S": time.getMilliseconds() }; if (/(y+)/.test(fmt)){ fmt = fmt.replace(RegExp.$1, (time.getFullYear() + "").substr(4 - RegExp.$1.length)); } for (var k in o){ if (new RegExp("(" + k + ")").test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); } } return fmt; }, getToIndex() { wx.switchTab({ url: '/pages/index/index' }) }, getNextStatus (obj, date) { const app = this, today = app.getTimestamp(app.getFormat(new Date(), 'yyyy/MM/dd')); let latest_info = wx.getStorageSync('latest_info'), status = 0, days = 0, from_that = 0, rest_day = 0, delay_days = 0; if (latest_info) { latest_info = JSON.parse(latest_info); } var date = date || today; if (obj == undefined) { return 0; } let start_time = obj.start_time, end_time = obj.end_time || null, last_start_time = obj.last_start_time || null, next_start_time = obj.next_start_time || null, cycle = obj.menstruate_period, period = obj.menstruate_days; let is_period = 0, is_cycle = 0; // console.log(app.getFormat(new Date(start_time * 1000), 'yyyy/MM/dd'), app.getFormat(new Date(end_time * 1000), 'yyyy/MM/dd')); // 下次月经时间存在时, 本次实际周期 = 下次月经时间 - 本次月经时间 // 不存在时 取latest_info里面的menstruate_period if (next_start_time != null) { is_cycle = (next_start_time - start_time) / app.globalData.TIME_TURN; cycle = is_cycle; }else{ cycle = latest_info.menstruate_period; } //当月没记录 上月有记录(占据当月记录) 且历史记录 if (obj.prev && date > obj.start_time + (cycle -1 ) * app.globalData.TIME_TURN) { if (date < today) { // 上月有记录, 当前计算月份没记录(已过去) return 0; }else{ // 上月有记录, 当前计算月份没记录(未过去) cycle = latest_info.menstruate_period; period = latest_info.menstruate_days; } } // 计算日期到经期开始的天数 今天距离经期开始的天数 let date_to_st = (date - start_time) / app.globalData.TIME_TURN + 1, today_to_st = (today - start_time) / app.globalData.TIME_TURN + 1, predict_start_time = start_time + cycle * app.globalData.TIME_TURN; // console.log(latest_info.start_time); // 姨妈延迟 delay_days = (date - latest_info.start_time) / app.globalData.TIME_TURN - cycle + 1; // console.log(delay_days); if (date <= today && delay_days > 0) { // console.log(delay_days, '延迟'); status = 11; wx.setStorageSync("predict_start_time", today + app.globalData.TIME_TURN); if (date == today) { from_that =Math.ceil(delay_days); wx.setStorageSync('statusInfo', {'status': status, 'from_that': from_that, 'rest_day': 0}) } // console.log(app.getFormat(new Date(latest_info.start_time * 1000), 'yyyy/MM/dd'), app.getFormat(new Date(date * 1000), 'yyyy/MM/dd')); // console.log('test--延迟--' + app.getFormat(new Date((latest_info.start_time + cycle * app.globalData.TIME_TURN) * 1000), 'yyyy/MM/dd')); return status; } if (date < start_time) { // 计算日期小于开始日期, 则该计算日期属于上一个周期 if (last_start_time) { // 存在上一个周期的记录 cycle = (start_time - last_start_time) / app.globalData.TIME_TURN; days = Math.ceil((date - last_start_time) / app.globalData.TIME_TURN % cycle) + 1; if (days <= 0) { return 0; } console.log(days); // console.log(app.getFormat(new Date(last_start_time * 1000), 'yyyy/MM/dd')); }else{ // 没有上一个周期的记录 return status; } // console.log(app.getFormat(new Date(start_time *1000), 'yyyy/MM/dd')); }else{ days = Math.ceil((date - start_time) / app.globalData.TIME_TURN % cycle) + 1; // console.log(app.getFormat(new Date(date *1000), 'yyyy/MM/dd')); // console.log( cycle, days, period, app.getFormat(new Date(start_time *1000), 'yyyy/MM/dd')); // console.log((date - start_time) / app.globalData.TIME_TURN); } // 经期时间延长: 当前周期未点击结束时间且已过月经预测期(最长经期10天) // console.log(app.getFormat(new Date(date * 1000), 'yyyy/MM/dd'), date_to_st); if (!end_time && (date_to_st - period + 1) > 0 && date <= today) { if (date_to_st <= 8) { period = Math.floor(date_to_st); }else{ period = 8; // console.log(period); } } const ovulatory_date = start_time + (cycle -14) * app.globalData.TIME_TURN; const ovulatory_start_time = start_time + (cycle - 19) * app.globalData.TIME_TURN, ovulatory_end_time = start_time + (cycle - 10) * app.globalData.TIME_TURN, safety_pre_st = start_time + period * app.globalData.TIME_TURN, safety_pre_et = ovulatory_start_time - 1 * app.globalData.TIME_TURN, safety_next_st = ovulatory_end_time + 1 * app.globalData.TIME_TURN, safety_next_et = start_time + (cycle - 1) * app.globalData.TIME_TURN, ovulatory_start = cycle -14 - 5 + 1, ovulatory_end = cycle -14 + 4 +1; // console.log(app.getFormat(new Date(start_time * 1000), 'yyyy/MM/dd'), app.getFormat(new Date(end_time * 1000), 'yyyy/MM/dd')); // console.log(app.getFormat(new Date(start_time * 1000), 'yyyy/MM/dd'), app.getFormat(new Date(end_time * 1000), 'yyyy/MM/dd')); // console.log(app.getFormat(new Date(safety_pre_st * 1000), 'yyyy/MM/dd'), app.getFormat(new Date(safety_pre_et * 1000), 'yyyy/MM/dd')); // console.log(app.getFormat(new Date(ovulatory_start_time * 1000), 'yyyy/MM/dd'), app.getFormat(new Date(ovulatory_end_time * 1000), 'yyyy/MM/dd')); // console.log(app.getFormat(new Date(safety_next_st * 1000), 'yyyy/MM/dd'), app.getFormat(new Date(safety_next_et * 1000), 'yyyy/MM/dd')); // console.log(today); // console.log(ovulatory_start, ovulatory_end); // console.log(cycle, period); // if(date == ovulatory_date && end_time){ // //排卵日 // return 10; // } // console.log(days); // console.log(period); // console.log(days, period, app.getFormat(new Date(date * 1000), 'yyyy/MM/dd')); let statusInfo = null; if (days <= period) { // 月经期 if (date < today) { status = 1; }else if(date > today){ status = 2; }else{ status = 3; from_that =Math.floor(( today - start_time ) / app.globalData.TIME_TURN) + 1; rest_day = period - from_that; // console.log(from_that, rest_day); statusInfo = {'status': status, 'from_that': from_that, 'rest_day': rest_day} if (today == end_time) { statusInfo['flag'] = 1; } wx.setStorageSync('statusInfo', statusInfo) } }else if(ovulatory_start <= days && days <= ovulatory_end){ // 排卵期 if (date < today) { status = 4; }else if(date > today){ status = 5; }else{ status = 6; from_that = Math.floor((date - ovulatory_start_time) / app.globalData.TIME_TURN) + 1; rest_day = 10 - from_that; // console.log(from_that, rest_day); statusInfo = {'status': status, 'from_that': from_that, 'rest_day': rest_day} wx.setStorageSync('statusInfo', statusInfo) } }else{ // 安全期 if (date < today) { status = 7; }else if(date > today){ status = 8; }else{ status = 9; if (today > safety_pre_et) { // 第二段安全期 from_that = ( today - safety_next_st ) / app.globalData.TIME_TURN + 1; rest_day = (safety_next_et - today) / app.globalData.TIME_TURN; }else{ // 第一段安全期 from_that = ( today - safety_pre_st ) / app.globalData.TIME_TURN + 1; rest_day = (safety_pre_et - today) / app.globalData.TIME_TURN; } statusInfo = {'status': status, 'from_that': from_that, 'rest_day': rest_day} wx.setStorageSync('statusInfo', statusInfo) } } return status }, // 获取某年某月内的月经周期 getMonthPeriod(year) { const app = this; var year = year + ''; // console.log(app.globalData.token); wx.request({ url: app.globalData.HOST + '/api/mini_menstruate_record/getCalendar', method: 'POST', header: { "token": app.globalData.token }, data: { year: year }, success: function(res) { if (res.data.errno == 0) { let yearData = res.data.rst.data if (yearData.length>0) { wx.setStorageSync(year, JSON.stringify(yearData)) }else{ wx.setStorageSync(year, '') } } } }); }, getFromid(formId) { let app = this, tmp = Date.parse(new Date()); wx.request({ url: app.globalData.HOST + '/api/mini_user/getFromidAction', method: "POST", header: { "token": app.globalData.token }, data: {from_id: formId, tmp: tmp}, success: function (res) { }, complete: function () { } }); } })