姨妈管家小程序代码

setting.js 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //获取应用实例
  2. const app = getApp()
  3. Page({
  4. data: {
  5. start_date: '',
  6. 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],
  7. menstrualArr: [3, 4, 5, 6, 7, 8],
  8. periodIndex: 8,
  9. index: 3,
  10. hiddenPicker: false,
  11. defaultPeriod: '',
  12. defaultCycle: ''
  13. },
  14. onLoad: function (e) {
  15. let that = this, today = '', choose_start = 0;
  16. let defaultPeriod = '', defaultCycle = '', latest_info = wx.getStorageSync('latest_info');
  17. today = app.getFormat(new Date(), 'yyyy/MM/dd');
  18. if (latest_info) {
  19. latest_info = JSON.parse(latest_info);
  20. defaultPeriod = latest_info.menstruate_days
  21. defaultCycle = latest_info.menstruate_period
  22. }
  23. choose_start = app.getFormat(new Date((app.getTimestamp(app.getFormat(new Date(), 'yyyy/MM/dd')) - 44 * app.globalData.TIME_TURN) *1000), 'yyyy/MM/dd');
  24. that.getToday();
  25. if (!app.globalData.token) {
  26. app.login(that)
  27. }
  28. that.setData({today: app.toWechatFormat(today), choose_start: app.toWechatFormat(choose_start), defaultPeriod: defaultPeriod, defaultCycle: defaultCycle})
  29. },
  30. getToday () {
  31. let that = this, date = new Date();
  32. let today = app.getFormat(date, 'yyyy/MM/dd');
  33. that.setData({start_date: app.toWechatFormat(today)})
  34. },
  35. bindPeriodChange (e) {
  36. this.setData({
  37. defaultCycle: '',
  38. periodIndex: e.detail.value
  39. })
  40. },
  41. bindMenstrualChange (e) {
  42. this.setData({
  43. defaultPeriod: '',
  44. index: e.detail.value
  45. })
  46. },
  47. finishSetting () {
  48. const that = this;
  49. let latest_info = wx.getStorageSync('latest_info');
  50. let menstruate_period =that.data.defaultCycle || that.data.periodArr[that.data.periodIndex],
  51. menstruate_days =that.data.defaultPeriod || that.data.menstrualArr[that.data.index];
  52. if (latest_info) {
  53. latest_info = JSON.parse(latest_info);
  54. latest_info.menstruate_period = menstruate_period;
  55. latest_info.menstruate_days = menstruate_days;
  56. wx.setStorageSync('latest_info', JSON.stringify(latest_info));
  57. wx.request({
  58. url: app.globalData.HOST + '/api/mini_user/setUserSetting',
  59. method: 'POST',
  60. header: {
  61. "token": app.globalData.token
  62. },
  63. data: {
  64. info: {
  65. menstruate_period: menstruate_period,
  66. menstruate_days: menstruate_days
  67. },
  68. flag: 0
  69. },
  70. success: function(res) {
  71. if (res.data.errno == 0) {
  72. app.getMonthPeriod(new Date().getFullYear())
  73. }else if(res.data.errno == 401){
  74. app.login(that)
  75. }
  76. }
  77. });
  78. app.confirmModal('恭喜你设置完成啦', false, app.getToIndex)
  79. }else{
  80. wx.redirectTo({url: '/pages/firstSetting/firstSetting'})
  81. }
  82. },
  83. formSubmit(e) {
  84. let that = this, formId = e.detail.formId;
  85. app.getFromid(formId);
  86. }
  87. })