姨妈管家小程序代码

firstSetting.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //index.js
  2. //获取应用实例
  3. const app = getApp()
  4. Page({
  5. data: {
  6. start_date: '',
  7. end_date: '',
  8. 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],
  9. menstrualArr: [3, 4, 5, 6, 7, 8],
  10. periodIndex: 8,
  11. index: 3,
  12. hiddenPicker: false,
  13. switchFlag: false,
  14. hasUserInfo: true
  15. },
  16. onLoad: function (e) {
  17. let that = this, today = '', choose_start = 0;
  18. const userInfo = wx.getStorageSync('userInfo') || null;
  19. if (userInfo) {
  20. that.setData({hasUserInfo: true})
  21. }else{
  22. that.setData({hasUserInfo: false})
  23. }
  24. if (e.source) {
  25. that.setData({source: e.source});
  26. }
  27. today = app.getFormat(new Date(), 'yyyy/MM/dd');
  28. choose_start = app.getFormat(new Date((app.getTimestamp(app.getFormat(new Date(), 'yyyy/MM/dd')) - 44 * app.globalData.TIME_TURN) *1000), 'yyyy/MM/dd');
  29. that.getToday();
  30. that.setData({today: app.toWechatFormat(today), choose_start: app.toWechatFormat(choose_start)})
  31. },
  32. getToday () {
  33. let that = this, date = new Date();
  34. let today = app.getFormat(date, 'yyyy/MM/dd');
  35. that.setData({start_date: app.toWechatFormat(today), end_date: app.toWechatFormat(today)})
  36. },
  37. isEnd () {
  38. const that = this;
  39. let switchFlag = that.data.switchFlag;
  40. console.log(switchFlag);
  41. this.setData({switchFlag: !switchFlag})
  42. },
  43. bindStartDateChange: function (e) {
  44. this.setData({
  45. start_date: e.detail.value
  46. })
  47. },
  48. bindEndDateChange: function (e) {
  49. this.setData({
  50. end_date: e.detail.value
  51. })
  52. },
  53. bindPeriodChange (e) {
  54. this.setData({
  55. periodIndex: e.detail.value
  56. })
  57. },
  58. bindMenstrualChange (e) {
  59. this.setData({
  60. index: e.detail.value
  61. })
  62. },
  63. finishSetting () {
  64. const that = this, hasEnd = this.data.switchFlag;
  65. let menstruate_time_start = app.getTimestamp(app.toIOSFormat(that.data.start_date)),
  66. menstruate_time_end = null,
  67. menstruate_period = that.data.periodArr[that.data.periodIndex],
  68. menstruate_days = that.data.menstrualArr[that.data.index],
  69. range_days = (app.getTimestamp(app.getFormat(new Date(), 'yyyy/MM/dd')) - menstruate_time_start) / app.globalData.TIME_TURN + 1,
  70. period_cal = 0;
  71. menstruate_time_end = hasEnd ? app.getTimestamp(app.toIOSFormat(that.data.end_date)) : null;
  72. if (menstruate_time_end) {
  73. period_cal = (menstruate_time_end - menstruate_time_start) / app.globalData.TIME_TURN;
  74. console.log(period_cal);
  75. }
  76. if (hasEnd && menstruate_time_start >= menstruate_time_end) {
  77. app.confirmModal('结束时间应该大于开始时间哦', false)
  78. }else if(period_cal &&( period_cal < 3 || period_cal > 8) ){
  79. app.confirmModal('经期时间应该在3-8天之内哦', false)
  80. }else if(!hasEnd && range_days >8 ){
  81. app.confirmModal('开始时间距离今天超过8天要设置经期结束时间哦, 请打开按钮设置姨妈结束时间', false);
  82. }else if(menstruate_period < range_days){
  83. app.confirmModal('您的月经周期最少应为' + range_days +'天~', false);
  84. }else{
  85. that.ajaxSettting(menstruate_time_start, menstruate_time_end, menstruate_period, menstruate_days);
  86. }
  87. },
  88. ajaxSettting (menstruate_time_start, menstruate_time_end, menstruate_period, menstruate_days, flag) {
  89. const info = {
  90. 'start_time': menstruate_time_start,
  91. 'end_time': menstruate_time_end,
  92. 'last_start_time': null,
  93. 'next_start_time': null,
  94. 'menstruate_period': menstruate_period,
  95. 'menstruate_days': menstruate_days
  96. },
  97. that = this;
  98. // wx.setStorageSync('info', JSON.stringify(info));
  99. wx.setStorageSync('latest_info', JSON.stringify(info));
  100. wx.request({
  101. url: app.globalData.HOST + '/api/mini_user/setUserSetting',
  102. method: 'POST',
  103. header: {
  104. "token": app.globalData.token
  105. },
  106. data: {
  107. info: info,
  108. flag: 1
  109. },
  110. success: function(res) {
  111. if (res.data.errno == 0) {
  112. app.getMonthPeriod(new Date().getFullYear())
  113. }else if(res.data.errno == 401){
  114. app.login(that)
  115. }
  116. }
  117. });
  118. // console.log(wx.getStorageSync('latest_info'));
  119. app.confirmModal('恭喜你设置完成啦', false, app.getToIndex)
  120. },
  121. formSubmit(e) {
  122. let that = this, formId = e.detail.formId;
  123. app.getFromid(formId);
  124. },
  125. getUserInfo: function(e) {
  126. const that = this, userInfo = {};
  127. userInfo.encryptedData = e.detail.encryptedData;
  128. userInfo.iv = e.detail.iv;
  129. userInfo.avatar_url = e.detail.userInfo.avatarUrl;
  130. userInfo.nick_name = e.detail.userInfo.nickName;
  131. app.globalData.userInfo = userInfo;
  132. wx.setStorageSync('userInfo', userInfo);
  133. app.login(that, ()=>{
  134. that.setData({hasUserInfo: true});
  135. });
  136. }
  137. })