姨妈管家小程序代码

app.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. App({
  2. globalData: {
  3. // HOST: "http://182.92.118.1:8043",
  4. HOST: "https://ymgjapi.726p.com",
  5. img: "https://ymgjapi.726p.com/icon/maicon/share.jpg",
  6. TIME_TURN: 86400,
  7. noticeLists: []
  8. },
  9. onLaunch: function () {
  10. const app = this,
  11. userInfo = wx.getStorageSync('userInfo'),
  12. noticeLists = wx.getStorageSync('noticeLists'),
  13. token = wx.getStorageSync('token');
  14. if (noticeLists && token) {
  15. app.globalData.token = token;
  16. app.globalData.noticeLists = noticeLists;
  17. }else{
  18. wx.redirectTo({url: '/pages/firstSetting/firstSetting'})
  19. }
  20. },
  21. onUnload () {
  22. wx.clearStorage();
  23. },
  24. login: function (that, func) {
  25. let obj = {}, app = this, token = '', source = '';
  26. if (that.data.source) {
  27. source = that.data.source
  28. }
  29. wx.login({
  30. success: function(res) {
  31. if (res.code) {
  32. wx.request({
  33. url: app.globalData.HOST + '/api/mini_program/loginAction',
  34. method: "POST",
  35. data: {
  36. code: res.code,
  37. iv: app.globalData.userInfo.iv,
  38. encryptedData: app.globalData.userInfo.encryptedData,
  39. source: source
  40. },
  41. success: function (resLogin) {
  42. if (resLogin.data.errno == 0) {
  43. token = resLogin.data.rst.token;
  44. app.globalData.token = token;
  45. app.globalData.noticeLists = resLogin.data.rst.data;
  46. wx.setStorage({
  47. key: "token",
  48. data: token
  49. })
  50. wx.setStorage({
  51. key: "noticeLists",
  52. data: resLogin.data.rst.data
  53. })
  54. // wx.setStorageSync('token', token);
  55. // wx.setStorageSync('noticeLists', resLogin.data.rst.data);
  56. app.getMonthPeriod(new Date().getFullYear());
  57. if (func) {
  58. func();
  59. }
  60. }
  61. }
  62. })
  63. } else {
  64. console.log('登录失败!' + res.errMsg)
  65. }
  66. }
  67. });
  68. },
  69. confirmModal (content, flag, func, funcc) {
  70. wx.showModal({
  71. title: '小提示',
  72. content: content,
  73. showCancel: flag,
  74. success: function(res) {
  75. if (res.confirm) {
  76. if (func) {
  77. func();
  78. }
  79. } else if (res.cancel) {
  80. }
  81. }
  82. })
  83. },
  84. // 公共函数
  85. getTimestamp (str) {
  86. if (str) {
  87. return Date.parse(new Date(str)) / 1000;
  88. }else{
  89. return Date.parse(new Date()) / 1000;
  90. }
  91. },
  92. // 2018-02-19 => 2018/02/19
  93. toIOSFormat (time) {
  94. return time.replace(/\-/g, "/");
  95. },
  96. toWechatFormat (time) {
  97. return time.replace(/\//g, "-");
  98. },
  99. getFormat (time, fmt) {
  100. var o = {
  101. "M+": time.getMonth() + 1,
  102. "d+": time.getDate(),
  103. "h+": time.getHours(),
  104. "m+": time.getMinutes(),
  105. "s+": time.getSeconds(),
  106. "q+": Math.floor((time.getMonth() + 3) / 3),
  107. "S": time.getMilliseconds()
  108. };
  109. if (/(y+)/.test(fmt)){
  110. fmt = fmt.replace(RegExp.$1, (time.getFullYear() + "").substr(4 - RegExp.$1.length));
  111. }
  112. for (var k in o){
  113. if (new RegExp("(" + k + ")").test(fmt)) {
  114. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  115. }
  116. }
  117. return fmt;
  118. },
  119. getToIndex() {
  120. wx.switchTab({
  121. url: '/pages/index/index'
  122. })
  123. },
  124. getNextStatus (obj, date) {
  125. const app = this,
  126. today = app.getTimestamp(app.getFormat(new Date(), 'yyyy/MM/dd'));
  127. let latest_info = wx.getStorageSync('latest_info'),
  128. status = 0,
  129. days = 0,
  130. from_that = 0,
  131. rest_day = 0,
  132. delay_days = 0;
  133. if (latest_info) {
  134. latest_info = JSON.parse(latest_info);
  135. }
  136. var date = date || today;
  137. if (obj == undefined) {
  138. return 0;
  139. }
  140. let start_time = obj.start_time,
  141. end_time = obj.end_time || null,
  142. last_start_time = obj.last_start_time || null,
  143. next_start_time = obj.next_start_time || null,
  144. cycle = obj.menstruate_period,
  145. period = obj.menstruate_days;
  146. let is_period = 0, is_cycle = 0;
  147. // console.log(app.getFormat(new Date(start_time * 1000), 'yyyy/MM/dd'), app.getFormat(new Date(end_time * 1000), 'yyyy/MM/dd'));
  148. // 下次月经时间存在时, 本次实际周期 = 下次月经时间 - 本次月经时间
  149. // 不存在时 取latest_info里面的menstruate_period
  150. if (next_start_time != null) {
  151. is_cycle = (next_start_time - start_time) / app.globalData.TIME_TURN;
  152. cycle = is_cycle;
  153. }else{
  154. cycle = latest_info.menstruate_period;
  155. }
  156. //当月没记录 上月有记录(占据当月记录) 且历史记录
  157. if (obj.prev && date > obj.start_time + (cycle -1 ) * app.globalData.TIME_TURN) {
  158. if (date < today) {
  159. // 上月有记录, 当前计算月份没记录(已过去)
  160. return 0;
  161. }else{
  162. // 上月有记录, 当前计算月份没记录(未过去)
  163. cycle = latest_info.menstruate_period;
  164. period = latest_info.menstruate_days;
  165. }
  166. }
  167. // 计算日期到经期开始的天数 今天距离经期开始的天数
  168. let date_to_st = (date - start_time) / app.globalData.TIME_TURN + 1,
  169. today_to_st = (today - start_time) / app.globalData.TIME_TURN + 1,
  170. predict_start_time = start_time + cycle * app.globalData.TIME_TURN;
  171. // console.log(latest_info.start_time);
  172. // 姨妈延迟
  173. delay_days = (date - latest_info.start_time) / app.globalData.TIME_TURN - cycle + 1;
  174. // console.log(delay_days);
  175. if (date <= today && delay_days > 0) {
  176. // console.log(delay_days, '延迟');
  177. status = 11;
  178. wx.setStorageSync("predict_start_time", today + app.globalData.TIME_TURN);
  179. if (date == today) {
  180. from_that =Math.ceil(delay_days);
  181. wx.setStorageSync('statusInfo', {'status': status, 'from_that': from_that, 'rest_day': 0})
  182. }
  183. // console.log(app.getFormat(new Date(latest_info.start_time * 1000), 'yyyy/MM/dd'), app.getFormat(new Date(date * 1000), 'yyyy/MM/dd'));
  184. // console.log('test--延迟--' + app.getFormat(new Date((latest_info.start_time + cycle * app.globalData.TIME_TURN) * 1000), 'yyyy/MM/dd'));
  185. return status;
  186. }
  187. if (date < start_time) {
  188. // 计算日期小于开始日期, 则该计算日期属于上一个周期
  189. if (last_start_time) {
  190. // 存在上一个周期的记录
  191. cycle = (start_time - last_start_time) / app.globalData.TIME_TURN;
  192. days = Math.ceil((date - last_start_time) / app.globalData.TIME_TURN % cycle) + 1;
  193. if (days <= 0) {
  194. return 0;
  195. }
  196. console.log(days);
  197. // console.log(app.getFormat(new Date(last_start_time * 1000), 'yyyy/MM/dd'));
  198. }else{
  199. // 没有上一个周期的记录
  200. return status;
  201. }
  202. // console.log(app.getFormat(new Date(start_time *1000), 'yyyy/MM/dd'));
  203. }else{
  204. days = Math.ceil((date - start_time) / app.globalData.TIME_TURN % cycle) + 1;
  205. // console.log(app.getFormat(new Date(date *1000), 'yyyy/MM/dd'));
  206. // console.log( cycle, days, period, app.getFormat(new Date(start_time *1000), 'yyyy/MM/dd'));
  207. // console.log((date - start_time) / app.globalData.TIME_TURN);
  208. }
  209. // 经期时间延长: 当前周期未点击结束时间且已过月经预测期(最长经期10天)
  210. // console.log(app.getFormat(new Date(date * 1000), 'yyyy/MM/dd'), date_to_st);
  211. if (!end_time && (date_to_st - period + 1) > 0 && date <= today) {
  212. if (date_to_st <= 8) {
  213. period = Math.floor(date_to_st);
  214. }else{
  215. period = 8;
  216. // console.log(period);
  217. }
  218. }
  219. const ovulatory_date = start_time + (cycle -14) * app.globalData.TIME_TURN;
  220. const ovulatory_start_time = start_time + (cycle - 19) * app.globalData.TIME_TURN,
  221. ovulatory_end_time = start_time + (cycle - 10) * app.globalData.TIME_TURN,
  222. safety_pre_st = start_time + period * app.globalData.TIME_TURN,
  223. safety_pre_et = ovulatory_start_time - 1 * app.globalData.TIME_TURN,
  224. safety_next_st = ovulatory_end_time + 1 * app.globalData.TIME_TURN,
  225. safety_next_et = start_time + (cycle - 1) * app.globalData.TIME_TURN,
  226. ovulatory_start = cycle -14 - 5 + 1,
  227. ovulatory_end = cycle -14 + 4 +1;
  228. // console.log(app.getFormat(new Date(start_time * 1000), 'yyyy/MM/dd'), app.getFormat(new Date(end_time * 1000), 'yyyy/MM/dd'));
  229. // console.log(app.getFormat(new Date(start_time * 1000), 'yyyy/MM/dd'), app.getFormat(new Date(end_time * 1000), 'yyyy/MM/dd'));
  230. // console.log(app.getFormat(new Date(safety_pre_st * 1000), 'yyyy/MM/dd'), app.getFormat(new Date(safety_pre_et * 1000), 'yyyy/MM/dd'));
  231. // console.log(app.getFormat(new Date(ovulatory_start_time * 1000), 'yyyy/MM/dd'), app.getFormat(new Date(ovulatory_end_time * 1000), 'yyyy/MM/dd'));
  232. // console.log(app.getFormat(new Date(safety_next_st * 1000), 'yyyy/MM/dd'), app.getFormat(new Date(safety_next_et * 1000), 'yyyy/MM/dd'));
  233. // console.log(today);
  234. // console.log(ovulatory_start, ovulatory_end);
  235. // console.log(cycle, period);
  236. // if(date == ovulatory_date && end_time){
  237. // //排卵日
  238. // return 10;
  239. // }
  240. // console.log(days);
  241. // console.log(period);
  242. // console.log(days, period, app.getFormat(new Date(date * 1000), 'yyyy/MM/dd'));
  243. let statusInfo = null;
  244. if (days <= period) {
  245. // 月经期
  246. if (date < today) {
  247. status = 1;
  248. }else if(date > today){
  249. status = 2;
  250. }else{
  251. status = 3;
  252. from_that =Math.floor(( today - start_time ) / app.globalData.TIME_TURN) + 1;
  253. rest_day = period - from_that;
  254. // console.log(from_that, rest_day);
  255. statusInfo = {'status': status, 'from_that': from_that, 'rest_day': rest_day}
  256. if (today == end_time) {
  257. statusInfo['flag'] = 1;
  258. }
  259. wx.setStorageSync('statusInfo', statusInfo)
  260. }
  261. }else if(ovulatory_start <= days && days <= ovulatory_end){
  262. // 排卵期
  263. if (date < today) {
  264. status = 4;
  265. }else if(date > today){
  266. status = 5;
  267. }else{
  268. status = 6;
  269. from_that = Math.floor((date - ovulatory_start_time) / app.globalData.TIME_TURN) + 1;
  270. rest_day = 10 - from_that;
  271. // console.log(from_that, rest_day);
  272. statusInfo = {'status': status, 'from_that': from_that, 'rest_day': rest_day}
  273. wx.setStorageSync('statusInfo', statusInfo)
  274. }
  275. }else{
  276. // 安全期
  277. if (date < today) {
  278. status = 7;
  279. }else if(date > today){
  280. status = 8;
  281. }else{
  282. status = 9;
  283. if (today > safety_pre_et) {
  284. // 第二段安全期
  285. from_that = ( today - safety_next_st ) / app.globalData.TIME_TURN + 1;
  286. rest_day = (safety_next_et - today) / app.globalData.TIME_TURN;
  287. }else{
  288. // 第一段安全期
  289. from_that = ( today - safety_pre_st ) / app.globalData.TIME_TURN + 1;
  290. rest_day = (safety_pre_et - today) / app.globalData.TIME_TURN;
  291. }
  292. statusInfo = {'status': status, 'from_that': from_that, 'rest_day': rest_day}
  293. wx.setStorageSync('statusInfo', statusInfo)
  294. }
  295. }
  296. return status
  297. },
  298. // 获取某年某月内的月经周期
  299. getMonthPeriod(year) {
  300. const app = this;
  301. var year = year + '';
  302. // console.log(app.globalData.token);
  303. wx.request({
  304. url: app.globalData.HOST + '/api/mini_menstruate_record/getCalendar',
  305. method: 'POST',
  306. header: {
  307. "token": app.globalData.token
  308. },
  309. data: {
  310. year: year
  311. },
  312. success: function(res) {
  313. if (res.data.errno == 0) {
  314. let yearData = res.data.rst.data
  315. if (yearData.length>0) {
  316. wx.setStorageSync(year, JSON.stringify(yearData))
  317. }else{
  318. wx.setStorageSync(year, '')
  319. }
  320. }
  321. }
  322. });
  323. },
  324. getFromid(formId) {
  325. let app = this, tmp = Date.parse(new Date());
  326. wx.request({
  327. url: app.globalData.HOST + '/api/mini_user/getFromidAction',
  328. method: "POST",
  329. header: {
  330. "token": app.globalData.token
  331. },
  332. data: {from_id: formId, tmp: tmp},
  333. success: function (res) {
  334. },
  335. complete: function () {
  336. }
  337. });
  338. }
  339. })