index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // components/tail_tip/index.js
  2. import {
  3. getDate,
  4. getWaterDrop,
  5. formatTime
  6. } from '../../utils/index/index'
  7. const app = getApp()
  8. Component({
  9. /**
  10. * 组件的属性列表
  11. */
  12. properties: {},
  13. props: {
  14. onFinish: {
  15. type: Function,
  16. value: () => {}
  17. },
  18. showTitText: true, //是否显示“操作时间"
  19. countdown: 0, // 倒计时时间
  20. urlPath: '../../', //图片路径
  21. titleText: '', // 右侧展示页面小标题
  22. isHomePage: false, // 是否是首页
  23. },
  24. /**
  25. * 组件的初始数据
  26. */
  27. data: {
  28. newDate: '', //现在时间
  29. remainingTime: '', //倒计时剩余时间
  30. timer: null,
  31. timer2: null,
  32. count: 0,
  33. hasNetworkType: false,
  34. cusModalObj: {
  35. visible: false,
  36. showCloseBtn: true,
  37. daysDifference: 0
  38. },
  39. },
  40. didMount() {
  41. let showTitText = this.props.showTitText
  42. let remainingTime = showTitText ? '操作时间 ' + this.props.countdown + ' 秒' : this.props.countdown + ' 秒'
  43. this.setData({
  44. newDate: getDate(),
  45. remainingTime,
  46. })
  47. this.data.timer2 && clearInterval(this.data.timer2)
  48. let _this = this
  49. this.data.timer2 = setInterval(() => {
  50. // 获取当前时间
  51. const currentHour = (new Date()).getHours();
  52. if (currentHour == 24) {
  53. app.globalData.expirationDateTodayOpen = false;
  54. }
  55. _this.checkExpiryDate()
  56. this.setData({
  57. newDate: getDate()
  58. })
  59. this.getNetworkType()
  60. }, 1000);
  61. this.getNetworkType()
  62. this.startCountdown()
  63. },
  64. /**
  65. * 组件的方法列表
  66. */
  67. methods: {
  68. checkExpiryDate() {
  69. let expiryDate = app.globalData.snDisposition.expirationDate;
  70. // let expiryDate = '2025-01-01';
  71. // let expiryDate = '2024-12-26';
  72. if (expiryDate && expiryDate !== '') {
  73. let { cusModalObj } = this.data
  74. const now = new Date();
  75. // 计算当前日期
  76. const currentDate = new Date(formatTime(now,'YYYY-MM-DD 00:00:00'));
  77. // 计算到期日期
  78. const expiry = new Date(formatTime(expiryDate,'YYYY-MM-DD 00:00:00'));
  79. const timeDifference = expiry - currentDate;
  80. // 将毫秒转换为天数
  81. const daysDifference = Math.ceil(timeDifference / (1000 * 3600 * 24));
  82. console.log(`弹窗次数:${app.globalData.expirationDateTodayOpen}`, daysDifference);
  83. // 检查是否在30天内
  84. if (!app.globalData.expirationDateTodayOpen && !cusModalObj.visible && daysDifference <= 30 && daysDifference > 0) {
  85. console.log(`您的FKJ账户将在 ${daysDifference} 天后到期:${expiry.toLocaleDateString()}`);
  86. app.globalData.expirationDateTodayOpen = true;
  87. this.setData({
  88. 'cusModalObj.visible': true,
  89. 'cusModalObj.showCloseBtn': true,
  90. 'cusModalObj.daysDifference': daysDifference,
  91. })
  92. return;
  93. }
  94. // 如果当前日期超过到期日期
  95. if (!cusModalObj.visible && currentDate.getTime() >= expiry.getTime()) {
  96. console.log(`您的FKJ账户已到期。${expiry.toLocaleDateString()}`);
  97. this.setData({
  98. 'cusModalObj.visible': true,
  99. 'cusModalObj.showCloseBtn': false,
  100. })
  101. }
  102. }
  103. },
  104. onOk() {
  105. this.setData({
  106. 'cusModalObj.visible': false
  107. })
  108. },
  109. // 倒计时
  110. startCountdown() {
  111. let that = this
  112. let {
  113. count
  114. } = that.data
  115. count = that.props.countdown
  116. let showTitText = that.props.showTitText
  117. if (count && count > 0) {
  118. that.data.timer && clearInterval(that.data.timer)
  119. that.data.timer = setInterval(() => {
  120. count--
  121. let remainingTime = showTitText ? '操作时间 ' + count + ' 秒' : count + ' 秒'
  122. that.setData({
  123. remainingTime
  124. })
  125. if (count <= 0) {
  126. that.data.timer && clearInterval(that.data.timer)
  127. that.props.onFinish('finish')
  128. // 倒计时结束,清除定时器
  129. }
  130. }, 1000) // 每秒更新一次倒计时数字
  131. }
  132. },
  133. // 网络状态
  134. getNetworkType() {
  135. my.getNetworkType({
  136. success: (res) => {
  137. if (res.networkType == 'NOTREACHABLE') {
  138. this.setData({
  139. hasNetworkType: false,
  140. })
  141. my.showToast({
  142. content: '网络状态异常',
  143. duration: 2000
  144. });
  145. } else {
  146. this.setData({
  147. hasNetworkType: true,
  148. })
  149. }
  150. },
  151. fail: (error) => {
  152. console.log(error)
  153. this.setData({
  154. hasNetworkType: false,
  155. })
  156. },
  157. })
  158. },
  159. // 长按logo进入设置
  160. goto() {
  161. this.data.timer && clearInterval(this.data.timer)
  162. getWaterDrop()
  163. if (this.props.isHomePage) {
  164. this.clearIntervalAll()
  165. my.reLaunch({
  166. url: '/pages/settings/login/index'
  167. });
  168. }
  169. },
  170. clearIntervalAll() {
  171. this.data.timer && clearInterval(this.data.timer)
  172. this.data.timer2 && clearInterval(this.data.timer2)
  173. },
  174. onClose() {
  175. console.log('onclose')
  176. this.clearIntervalAll();
  177. my.reLaunch({
  178. url: '/pages/home/index'
  179. });
  180. }
  181. },
  182. didUnmount() {
  183. this.clearIntervalAll();
  184. },
  185. onUnload() {
  186. this.clearIntervalAll();
  187. },
  188. observers: {}
  189. })